Static Value-Flow Analysis
Loading...
Searching...
No Matches
Public Member Functions | Static Private Member Functions | Private Attributes | List of all members
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)
 
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
 
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,
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
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 >
static bool Option< T >::fromString ( const std::string &  s,
bool &  value 
)
inlinestaticprivate

Definition at line 363 of file CommandLine.h.

364 {
365 if (s == "true")
366 value = true;
367 else if (s == "false")
368 value = false;
369 else
370 return false;
371 return true;
372 }

◆ fromString() [2/3]

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

Definition at line 375 of file CommandLine.h.

376 {
377 value = s;
378 return true;
379 }

◆ fromString() [3/3]

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

Definition at line 382 of file CommandLine.h.

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

◆ 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 {
348 return std::is_same<T, bool>::value;
349 }

◆ operator()()

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

Definition at line 356 of file CommandLine.h.

357 {
358 return value;
359 }

◆ 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 ( 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 402 of file CommandLine.h.

◆ value

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

Definition at line 403 of file CommandLine.h.


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