Static Value-Flow Analysis
Loading...
Searching...
No Matches
Option< T > Class Template Reference

#include <CommandLine.h>

Inheritance diagram for Option< T >:
OptionBase

Public Member Functions

 Option (const std::string &name, const std::string &description, T init)
virtual bool canSet (void) const override
 Can this option be set?
virtual bool parseAndSetValue (const std::string s) override
 From a given string, set the value of this option.
virtual bool isBool (void) const override
void setValue (T v)
bool isSet (void) const
T operator() (void) const

Static Private Member Functions

static bool fromString (const std::string &s, bool &value)
static bool fromString (const std::string s, std::string &value)
static bool fromString (const std::string s, u32_t &value)

Private Attributes

bool isExplicitlySet
T value

Additional Inherited Members

Static Public Member Functions inherited from OptionBase
static std::vector< std::string > parseOptions (int argc, char *argv[], std::string description, std::string callFormat)
Protected Types inherited from OptionBase
typedef std::pair< std::string, std::string > PossibilityDescription
 Name/description pairs.
typedef std::vector< std::pair< std::string, std::string > > PossibilityDescriptions
template<typename T>
using OptionPossibility = std::tuple<T, std::string, std::string>
Protected Member Functions inherited from OptionBase
 OptionBase (std::string name, std::string description)
 OptionBase (std::string name, std::string description, PossibilityDescriptions possibilityDescriptions)
virtual bool isMultiple (void) const
 Whether this option is an OptionMultiple.
Static Protected Member Functions inherited from OptionBase
template<typename T>
static PossibilityDescriptions extractPossibilityDescriptions (const std::vector< OptionPossibility< T > > possibilities)
static std::map< std::string, OptionBase * > & getOptionsMap (void)
Protected Attributes inherited from OptionBase
std::string name
std::string description
PossibilityDescriptions possibilityDescriptions
 For when we have possibilities like in an OptionMap.

Detailed Description

template<typename T>
class Option< T >

General -name=value options. Retrieve value by Opt().

Definition at line 325 of file CommandLine.h.

Constructor & Destructor Documentation

◆ Option()

template<typename T>
Option< T >::Option ( const std::string & name,
const std::string & description,
T init )
inline

Definition at line 328 of file CommandLine.h.

330 {
331 assert(!name.empty() && "Option: empty option name given");
332 }
std::string name
std::string description
OptionBase(std::string name, std::string description)
Definition CommandLine.h:33
bool isExplicitlySet

Member Function Documentation

◆ canSet()

template<typename T>
virtual bool Option< T >::canSet ( void ) const
inlineoverridevirtual

Can this option be set?

Implements OptionBase.

Definition at line 334 of file CommandLine.h.

335 {
336 // Don't allow duplicates.
337 return !isExplicitlySet;
338 }

◆ fromString() [1/3]

template<typename T>
bool Option< T >::fromString ( const std::string & s,
bool & value )
inlinestaticprivate

Definition at line 368 of file CommandLine.h.

369 {
370 if (s == "true")
371 value = true;
372 else if (s == "false")
373 value = false;
374 else
375 return false;
376 return true;
377 }

◆ fromString() [2/3]

template<typename T>
bool Option< T >::fromString ( const std::string s,
std::string & value )
inlinestaticprivate

Definition at line 380 of file CommandLine.h.

381 {
382 value = s;
383 return true;
384 }

◆ fromString() [3/3]

template<typename T>
bool Option< T >::fromString ( const std::string s,
u32_t & value )
inlinestaticprivate

Definition at line 387 of file CommandLine.h.

388 {
389 // We won't allow anything except [0-9]+.
390 if (s.empty()) return false;
391 for (char c : s)
392 {
393 if (!(c >= '0' && c <= '9')) return false;
394 }
395
396 // Use strtoul because we're not using exceptions.
397 assert(sizeof(unsigned long) >= sizeof(u32_t));
398 const unsigned long sv = std::strtoul(s.c_str(), nullptr, 10);
399
400 // Out of range according to strtoul, or according to us compared to u32_t.
401 if (errno == ERANGE || sv > std::numeric_limits<u32_t>::max()) return false;
402 value = sv;
403 return true;
404 }

◆ isBool()

template<typename T>
virtual bool Option< T >::isBool ( void ) const
inlineoverridevirtual

Whether this option represents a boolean. Important as such arguments don't require a value.

Reimplemented from OptionBase.

Definition at line 346 of file CommandLine.h.

347 {
349 }

◆ isSet()

template<typename T>
bool Option< T >::isSet ( void ) const
inline

Definition at line 356 of file CommandLine.h.

357 {
358 return isExplicitlySet;
359 }

◆ operator()()

template<typename T>
T Option< T >::operator() ( void ) const
inline

Definition at line 361 of file CommandLine.h.

362 {
363 return value;
364 }

◆ parseAndSetValue()

template<typename T>
virtual bool Option< T >::parseAndSetValue ( const std::string value)
inlineoverridevirtual

From a given string, set the value of this option.

Implements OptionBase.

Definition at line 340 of file CommandLine.h.

341 {
343 return isExplicitlySet;
344 }
static bool fromString(const std::string &s, bool &value)

◆ setValue()

template<typename T>
void Option< T >::setValue ( T v)
inline

Definition at line 351 of file CommandLine.h.

352 {
353 value = v;
354 }

Member Data Documentation

◆ isExplicitlySet

template<typename T>
bool Option< T >::isExplicitlySet
private

Definition at line 407 of file CommandLine.h.

◆ value

template<typename T>
T Option< T >::value
private

Definition at line 408 of file CommandLine.h.


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