AGXDynamics  2.31.1.0
agxIO::ArgumentParser Class Reference

A small argument parser class. More...

#include <ArgumentParser.h>

+ Inheritance diagram for agxIO::ArgumentParser:

Classes

class  Argument
 Help class for handling arguments. More...
 

Public Types

typedef agx::Vector< Argument > ArgumentVector
 

Public Member Functions

 ArgumentParser ()
 Constructs an argument parser from the program command line input. More...
 
 ArgumentParser (int argc, char **argv)
 Constructs an argument parser from the program command line input. More...
 
 ArgumentParser (const agx::StringVector &arguments)
 Constructs an argument parser from the program command line input given as an array of strings. More...
 
void addArgument (const char *argumentName)
 Adds an argument manually. More...
 
template<typename T >
T * as ()
 Subclass casting. More...
 
template<typename T >
const T * as () const
 
template<typename T >
T * asSafe ()
 Safe subclass casting, return nullptr if template type does not match. More...
 
template<typename T >
const T * asSafe () const
 
template<typename T >
T defaultRead (const std::string &key, const T &defaultValue, size_t startPosition=0, bool findAlsoHandled=true)
 Reads an argument into a variable. More...
 
int find (const std::string &key, size_t startPosition=0, bool findAlsoHandled=true)
 Finds the position of an argument. More...
 
Argument * getArgument (size_t index)
 Gets an argument at a position in the argument container. More...
 
char ** getArgv ()
 Gets the original array of c-strings. More...
 
size_t getNumArguments () const
 Returns the number of arguments. More...
 
int getReferenceCount () const
 
template<typename T >
bool is () const
 Subclass test. More...
 
bool read (const std::string &key, size_t startPosition=0, bool findAlsoHandled=true)
 Looks for an argument, and returns true if it finds it. More...
 
template<typename T >
bool read1 (const std::string &key, T &value, size_t startPosition=0, bool findAlsoHandled=true)
 Looks for an argument, and reads it into a variable if found. More...
 
template<typename T1 , typename T2 >
bool read2 (const std::string &key, T1 &value1, T2 &value2, size_t startPosition=0, bool findAlsoHandled=true)
 Reads two argument into variables. More...
 
template<typename T1 , typename T2 , typename T3 >
bool read3 (const std::string &key, T1 &value1, T2 &value2, T3 &value3, size_t startPosition=0, bool findAlsoHandled=true)
 Reads three argument into variables. More...
 
void reference (void *ptr=nullptr) const
 Explicitly increment the reference count by one, indicating that this object has another pointer which is referencing it. More...
 
bool reportUnreadArguments (std::ostream &out=std::cout)
 Writes all arguments which have not been handled to a stream. More...
 
void unreference (void *ptr=nullptr) const
 Decrement the reference count by one, indicating that a pointer to this object is referencing it. More...
 
void unreference_nodelete () const
 Decrement the reference count by one, indicating that a pointer to this object is referencing it. More...
 

Static Public Member Functions

template<typename T >
static bool ValidateCast (const Referenced *object)
 

Protected Member Functions

virtual ~ArgumentParser ()
 
void allocateObserverVector () const
 
void deleteUsingDeleteHandler () const
 

Static Protected Member Functions

static DeleteHandler * getDeleteHandler ()
 
static void setDeleteHandler (DeleteHandler *handler)
 Internal: Set a DeleteHandler to which deletion of all referenced counted objects will be delegated to. More...
 

Protected Attributes

Mutex m_mutex
 
ObserverContainer m_observers
 
AtomicValue m_refCount
 

Detailed Description

A small argument parser class.

Copies over the content of all c strings to std::strings.

There is no differentiation between keys and values, which means that if the arguments are "a b", then "a" could be key and "b" value, and/or both could be keys (without values).

Definition at line 38 of file ArgumentParser.h.

Member Typedef Documentation

◆ ArgumentVector

Constructor & Destructor Documentation

◆ ArgumentParser() [1/3]

agxIO::ArgumentParser::ArgumentParser ( )

Constructs an argument parser from the program command line input.

◆ ArgumentParser() [2/3]

agxIO::ArgumentParser::ArgumentParser ( int  argc,
char **  argv 
)

Constructs an argument parser from the program command line input.

Each white-space separated string is treated as a separate argument, so that the number of arguments will correspond to argc. The content of argv is copied and stored internally.

◆ ArgumentParser() [3/3]

agxIO::ArgumentParser::ArgumentParser ( const agx::StringVector &  arguments)

Constructs an argument parser from the program command line input given as an array of strings.

◆ ~ArgumentParser()

virtual agxIO::ArgumentParser::~ArgumentParser ( )
protectedvirtual

Member Function Documentation

◆ addArgument()

void agxIO::ArgumentParser::addArgument ( const char *  argumentName)

Adds an argument manually.

Not recommended unless you know what you're doing.

Parameters
argumentNameName of the argument to be added.

◆ allocateObserverVector()

void agx::Referenced::allocateObserverVector ( ) const
protectedinherited

◆ as() [1/2]

template<typename T >
T* agx::Referenced::as ( )
inherited

Subclass casting.

Template type must match actual type, dynamic_cast assertion will fail otherwise.

Referenced by agx::InternalData::get(), agxVehicle::Track::getNode(), agx::InternalData::getOrCreate(), and agx::InternalData::hasData().

◆ as() [2/2]

template<typename T >
const T* agx::Referenced::as ( ) const
inherited

◆ asSafe() [1/2]

template<typename T >
T* agx::Referenced::asSafe ( )
inherited

Safe subclass casting, return nullptr if template type does not match.

Referenced by agxUtil::CollectBodiesAndWiresVisitor::visit().

◆ asSafe() [2/2]

template<typename T >
const T* agx::Referenced::asSafe ( ) const
inherited

◆ defaultRead()

template<typename T >
T agxIO::ArgumentParser::defaultRead ( const std::string &  key,
const T &  defaultValue,
size_t  startPosition = 0,
bool  findAlsoHandled = true 
)

Reads an argument into a variable.

Note
Will set the argument to 'handled'.
Parameters
keyKey argument
defaultValueValue to be returned if read is not successful
startPositionPosition in argument array where to start searching from.
findAlsoHandledShould also arguments been found which have been treated as 'handled'?
Return values
Thevalue of the value argument, if the read was successful, otherwise the defaultValue.

Definition at line 276 of file ArgumentParser.h.

References AGXCORE_EXPORT.

◆ deleteUsingDeleteHandler()

void agx::Referenced::deleteUsingDeleteHandler ( ) const
protectedinherited

◆ find()

int agxIO::ArgumentParser::find ( const std::string &  key,
size_t  startPosition = 0,
bool  findAlsoHandled = true 
)

Finds the position of an argument.

Returns -1 if not found.

Note
Will not set the argument to 'handled'.
Parameters
keyArgument name
startPositionPosition in argument array where to start searching from.
findAlsoHandledShould also arguments been found which have been treated as 'handled'?
Return values
positionof argument if found, -1 if not found

Referenced by agxUnit::haveUnittestingArguments().

◆ getArgument()

Argument* agxIO::ArgumentParser::getArgument ( size_t  index)

Gets an argument at a position in the argument container.

◆ getArgv()

char** agxIO::ArgumentParser::getArgv ( )

Gets the original array of c-strings.

The content has been copied to an internal data structure.

◆ getDeleteHandler()

static DeleteHandler* agx::Referenced::getDeleteHandler ( )
staticprotectedinherited
Returns
a DeleteHandler.

◆ getNumArguments()

size_t agxIO::ArgumentParser::getNumArguments ( ) const

Returns the number of arguments.

◆ getReferenceCount()

int agx::Referenced::getReferenceCount ( ) const
inherited
Returns
the number pointers currently referencing this object.

◆ is()

template<typename T >
bool agx::Referenced::is ( ) const
inherited

◆ read()

bool agxIO::ArgumentParser::read ( const std::string &  key,
size_t  startPosition = 0,
bool  findAlsoHandled = true 
)

Looks for an argument, and returns true if it finds it.

Note
Will set the argument to 'handled'.
Parameters
keyArgument name
startPositionPosition in argument array where to start searching from.
findAlsoHandledShould also arguments been found which have been treated as 'handled'?
Return values
Returnsif the argument was found.

◆ read1()

template<typename T >
bool agxIO::ArgumentParser::read1 ( const std::string &  key,
T &  value,
size_t  startPosition = 0,
bool  findAlsoHandled = true 
)

Looks for an argument, and reads it into a variable if found.

Note
Will set the argument to 'handled'.
Parameters
keyKey argument
valueValue argument, which has to come directly after the key argument in the argument list.
startPositionPosition in argument array where to start searching from.
findAlsoHandledShould also arguments been found which have been treated as 'handled'?
Return values
Werethe key and value argument found, and was the conversion successful?

Definition at line 222 of file ArgumentParser.h.

◆ read2()

template<typename T1 , typename T2 >
bool agxIO::ArgumentParser::read2 ( const std::string &  key,
T1 &  value1,
T2 &  value2,
size_t  startPosition = 0,
bool  findAlsoHandled = true 
)

Reads two argument into variables.

Note
Will set the argument to 'handled'.
Parameters
keyKey argument
value1Value argument, which has to come directly after the key argument in the argument list.
value2Value argument, which has to come directly after the key argument in the argument list.
startPositionPosition in argument array where to start searching from.
findAlsoHandledShould also arguments been found which have been treated as 'handled'?
Return values
Werethe key and value argument found, and was the conversion successful?

Definition at line 238 of file ArgumentParser.h.

◆ read3()

template<typename T1 , typename T2 , typename T3 >
bool agxIO::ArgumentParser::read3 ( const std::string &  key,
T1 &  value1,
T2 &  value2,
T3 &  value3,
size_t  startPosition = 0,
bool  findAlsoHandled = true 
)

Reads three argument into variables.

Note
Will set the argument to 'handled'.
Parameters
keyKey argument
value1Value argument, which has to come directly after the key argument in the argument list.
value2Value argument, which has to come directly after the key argument in the argument list.
value3Value argument, which has to come directly after the key argument in the argument list.
startPositionPosition in argument array where to start searching from.
findAlsoHandledShould also arguments been found which have been treated as 'handled'?
Return values
Werethe key and value argument found, and was the conversion successful?

Definition at line 256 of file ArgumentParser.h.

◆ reference()

void agx::Referenced::reference ( void *  ptr = nullptr) const
inherited

Explicitly increment the reference count by one, indicating that this object has another pointer which is referencing it.

This method should be used with care, always balanced with the same number of calls to unreference.

◆ reportUnreadArguments()

bool agxIO::ArgumentParser::reportUnreadArguments ( std::ostream &  out = std::cout)

Writes all arguments which have not been handled to a stream.

Parameters
outAn std::ostream that should be written to.
Returns
true if all arguments were read. All is ok.

◆ setDeleteHandler()

static void agx::Referenced::setDeleteHandler ( DeleteHandler *  handler)
staticprotectedinherited

Internal: Set a DeleteHandler to which deletion of all referenced counted objects will be delegated to.

◆ unreference()

void agx::Referenced::unreference ( void *  ptr = nullptr) const
inherited

Decrement the reference count by one, indicating that a pointer to this object is referencing it.

If the reference count goes to zero, it is assumed that this object is no longer referenced and is automatically deleted.

This method should be used with care, always balanced with the same number of calls to reference.

Referenced by agxData::EntityRef< ContactMaterialPtr >::~EntityRef().

◆ unreference_nodelete()

void agx::Referenced::unreference_nodelete ( ) const
inherited

Decrement the reference count by one, indicating that a pointer to this object is referencing it.

However, do not delete it, even if ref count goes to 0. Warning, unref_nodelete() should only be called if the user knows exactly who will be responsible for, one should prefer unref() over unref_nodelete() as the later can lead to memory leaks.

◆ ValidateCast()

template<typename T >
static bool agx::Referenced::ValidateCast ( const Referenced *  object)
staticinherited

Member Data Documentation

◆ m_mutex

Mutex agx::Referenced::m_mutex
mutableprotectedinherited

Definition at line 240 of file Referenced.h.

◆ m_observers

ObserverContainer agx::Referenced::m_observers
mutableprotectedinherited

Definition at line 239 of file Referenced.h.

◆ m_refCount

AtomicValue agx::Referenced::m_refCount
mutableprotectedinherited

Definition at line 241 of file Referenced.h.


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