#include <CmdLineParser.h>
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()) |
T | Get (const std::string &s) const |
bool | Get (const std::string &s, T &value) const |
template<class C> | |
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 Parser & | m_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 |
typedef std::map<std::string, void*>::const_iterator CmdLine::OptionsType< T >::CI |
Just for maintaining the code clean.
CmdLine::OptionsType< T >::OptionsType | ( | const class Parser & | parser | ) | [inline] |
[in] | parser | Reference to the Parser object that owns this OptionsType object. |
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.
Option<T>& CmdLine::OptionsType< T >::Add | ( | const std::string & | name, | |
const std::string & | alias = "" , |
|||
T | def = T() , |
|||
T | min = T() , |
|||
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)
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.
[in] | s | The option's label |
Referenced by CmdLine::OptionsType< long double >::Get().
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.
[in] | s | The option's label/name |
[out] | value | The option's value |
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.
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.
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.
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().
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().
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().
bool CmdLine::OptionsType< T >::CheckDuplicate | ( | const Option< T > * | p, | |
const std::string | opt | |||
) | const [inline, protected] |
Check if an option was declared before.
p | Pointer to option to be checked | |
opt | Option's name |
true
if one should accept the option; false
otherwise.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().
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().
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().