CmdLine::OptionsType< T > Class Template Reference

#include <CmdLineParser.h>

Inheritance diagram for CmdLine::OptionsType< T >:

Inheritance graph
[legend]
Collaboration diagram for CmdLine::OptionsType< T >:

Collaboration graph
[legend]

List of all members.

Public Types

typedef std::map< std::string,
void * >::const_iterator 
CI

Public Member Functions

 OptionsType (const class Parser &parser)
Option< T > & Add (const std::string &name, const std::string &alias="", T def=T(), T min=T(), T max=T())
Get (const std::string &s) const
bool Get (const std::string &s, T &value) const
template<class C>
Get (const std::string &s) const
template<class C>
bool Get (const std::string &s, C &value) const
bool Found (const std::string &s) const

Public Attributes

class Parserm_parser

Protected Member Functions

virtual ~OptionsType ()
void CheckOption (const std::string &name, const std::string &alias) const
Option< T > * Cast (CI p) const
Option< T > * Find (const std::string &s) const
bool CheckDuplicate (const Option< T > *p, const std::string opt) const
 Check if an option was declared before.

Private Attributes

std::map< std::string, void * > m_opts


Detailed Description

template<class T>
class CmdLine::OptionsType< T >

Base class for the five types of options: Bool, Integer, Float, Char and String.

Member Typedef Documentation

template<class T>
typedef std::map<std::string, void*>::const_iterator CmdLine::OptionsType< T >::CI

Just for maintaining the code clean.


Constructor & Destructor Documentation

template<class T>
CmdLine::OptionsType< T >::OptionsType ( const class Parser parser  )  [inline]

Parameters:
[in] parser Reference to the Parser object that owns this OptionsType object.

template<class T>
virtual CmdLine::OptionsType< T >::~OptionsType (  )  [inline, protected, virtual]

Memory deallocation for Option<T> objects. First checks if the object has two shared references (when an alias is used), making sure that only one reference (pointer) will be deleted.


Member Function Documentation

template<class T>
Option<T>& CmdLine::OptionsType< T >::Add ( const std::string &  name,
const std::string &  alias = "",
def = T(),
min = T(),
max = T() 
) [inline]

Add an option into m_opts, checking for errors. Actually m_opts holds just the name/alias (sorted), which then link to an Option<T> object:

    ...
    [name_option_1]  ------> | Option<T> |
    ...                   /  |   object  | 
    [alias_option_1] ----/    (values of 
    ...                        option_1)
    

template<class T>
T CmdLine::OptionsType< T >::Get ( const std::string &  s  )  const [inline]

Return the value of option 's'. If 's' is not found in command-line (or not in range [min:max]) then default value is returned.

Parameters:
[in] s The option's label
Returns:
The option's value

Referenced by CmdLine::OptionsType< long double >::Get().

template<class T>
bool CmdLine::OptionsType< T >::Get ( const std::string &  s,
T &  value 
) const [inline]

Overloaded version of Get( const std::string& ) that puts the option argument value in the given parameter value and returns true if option s was provided on the command-line; false otherwise.

Parameters:
[in] s The option's label/name
[out] value The option's value
Returns:
true if matched; false otherwise

template<class T>
template<class C>
C CmdLine::OptionsType< T >::Get ( const std::string &  s  )  const [inline]

Simplified interface to perform casting. At times it is necessary to convert a INT (possibly "long int") into 'short int', for instance.

template<class T>
template<class C>
bool CmdLine::OptionsType< T >::Get ( const std::string &  s,
C &  value 
) const [inline]

Same as above but it returns both the option's value and if the option was found on the command-line.

template<class T>
bool CmdLine::OptionsType< T >::Found ( const std::string &  s  )  const [inline]

Returns true if the option specified as 's' was found in command-line parameters. Returns false otherwise.

Attention:
It isn't recommended to call Found() followed by Get() because doing so two calls to Find() are being performed. Instead, prefer to use bool Get( const std::string&, T& ), in which just a single call to Find() is performed.

template<class T>
void CmdLine::OptionsType< T >::CheckOption ( const std::string &  name,
const std::string &  alias 
) const [inline, protected]

Make sure that an option name/alias begins with minus sign.

Referenced by CmdLine::OptionsType< long double >::Add().

template<class T>
Option<T>* CmdLine::OptionsType< T >::Cast ( CI  p  )  const [inline, protected]

Performs a static_cast from a void pointer, returning the correct type of option.

Referenced by CmdLine::OptionsType< long double >::Find(), and CmdLine::OptionsType< long double >::~OptionsType().

template<class T>
Option<T>* CmdLine::OptionsType< T >::Find ( const std::string &  s  )  const [inline, protected]

Try to find an option 's' and returns its pointer. The binary search--provided by std::map--is used.

Referenced by CmdLine::OptionsType< long double >::Found(), CmdLine::OptionsType< long double >::Get(), CmdLine::OptionsFLOAT::Match(), and CmdLine::OptionsINT::Match().

template<class T>
bool CmdLine::OptionsType< T >::CheckDuplicate ( const Option< T > *  p,
const std::string  opt 
) const [inline, protected]

Check if an option was declared before.

Parameters:
p Pointer to option to be checked
opt Option's name
Returns:
true if one should accept the option; false otherwise.
If flag DUPLICATE is set then a fatal exception is thrown if the option was declared before. If flag FIRSTONLY is set then the function returns false indicating that this duplicate declaration is to be ignored.

Referenced by CmdLine::OptionsFLOAT::Match(), and CmdLine::OptionsINT::Match().


Member Data Documentation

template<class T>
class Parser& CmdLine::OptionsType< T >::m_parser

This reference provides access to Parser member objects/variables. Passing a reference to Parser is preferable to declaring its member objects as static.

Referenced by CmdLine::OptionsFLOAT::Add(), CmdLine::OptionsINT::Add(), and CmdLine::OptionsINT::Match().

template<class T>
std::map<std::string, void*> CmdLine::OptionsType< T >::m_opts [private]

Database of options of type T (Bool, Int, Float, Char or String). STL 'map' object provides fast search via 'binary search'.

Referenced by CmdLine::OptionsType< long double >::Add(), CmdLine::OptionsType< long double >::Find(), and CmdLine::OptionsType< long double >::~OptionsType().


The documentation for this class was generated from the following file: