ccuty  1.0
 All Classes Namespaces Files Functions Enumerations Macros Pages
Public Member Functions | List of all members
ccuty::stringset Class Reference

Set of strings, allows converting strings to numbers. More...

Inheritance diagram for ccuty::stringset:
ccuty::istringset

Public Member Functions

 stringset ()
 No argument constructor.
 
 stringset (const char *str)
 Constructor: str is a C string containing tokens separated by commas. More...
 
 stringset (const std::string &str)
 Constructor: str is a C++ string containing tokens separated by commas. More...
 
 stringset (std::initializer_list< const char * > tokens)
 Constructor: tokens is a sequence of tokens (each token is C string). More...
 
 stringset (std::initializer_list< elem > tokens)
 Constructor: tokens is a sequence of {token, ID} couples. More...
 
template<typename T >
 stringset (const T &tokens)
 Constructor: tokens is a container (vector, list, map) of tokens or {token, ID} couples. More...
 
bool empty () const
 Tests whether the stringset is empty.
 
size_t size () const
 Returns the size of the stringset.
 
const string_set & elements () const
 Returns the internal set of elements (each containing a token and its IDs).
 
int operator[] (const std::string &token) const
 Returns the ID of token or -1 if not found.
 
int starts_with (const std::string &str) const
 Returns the ID of the token which starts with str. More...
 
void clear ()
 Clears the stringset.
 
int insert (const std::string &token)
 Inserts token in the stringset. More...
 
int insert (const char *token)
 Inserts token in the stringset. More...
 
int insert (const elem &e)
 Inserts a {token, ID} couple in the stringset. More...
 
template<typename T >
void append (const T &cont)
 Inserts the elements of a container (vector, list, map) of tokens or {token, ID} couples. More...
 

Detailed Description

Set of strings, allows converting strings to numbers.

Manages a set of strings (here named tokens). Tokens are associated with a unique numeric ID, which is >=0 and can be used in switch statements for comparing strings. Provides functions to insert tokens, to test if a token belongs to the set and to retrieve the ID of a token.

Example:

int getRGB(const std::string& color) {
enum {red, blue, green};
static const stringset colors("red, blue, green");
switch (colors[color]) {
case red: return 0xf00;
case blue: return 0x0f0;
case green: return 0x00f;
default: return 0;
}
}

Tokens must be in the same order in the enum and in the string thar is given as an argument to the stringset constr. If the labels have specific values, proceed as follows:

enum {red=1, blue=10, green=100};
static const stringset colors({"red",red}, {"blue",blue}, {"green",green});
switch (colors[color]) {
// etc.
}
See Also
Macro STRING_SWITCH.
Class istringset if tokens need to be retreived from their numeric ID.
cutty::strid() function which provides another way to compare strings in switch statements.

Constructor & Destructor Documentation

ccuty::stringset::stringset ( const char *  str)
inlineexplicit

Constructor: str is a C string containing tokens separated by commas.

Each token is associated with an ID which corresponds to its position in the list (starting from 0). Duplicate tokens are ignored.

ccuty::stringset::stringset ( const std::string &  str)
inlineexplicit

Constructor: str is a C++ string containing tokens separated by commas.

Each token is associated with an ID which corresponds to its position in the list (starting from 0). Duplicate tokens are ignored.

ccuty::stringset::stringset ( std::initializer_list< const char * >  tokens)
inline

Constructor: tokens is a sequence of tokens (each token is C string).

Each token is associated with an ID which corresponds to its position in the list (starting from 0). Duplicate tokens are ignored.

ccuty::stringset::stringset ( std::initializer_list< elem >  tokens)
inline

Constructor: tokens is a sequence of {token, ID} couples.

IDs must be >= 0. Multiple couples can use the same ID but not the same token (duplicate tokens are ignored).

template<typename T >
ccuty::stringset::stringset ( const T &  tokens)
inlineexplicit

Constructor: tokens is a container (vector, list, map) of tokens or {token, ID} couples.

Duplicate tokens are ignored. IDs must be >= 0.

Member Function Documentation

int ccuty::stringset::starts_with ( const std::string &  str) const
inline

Returns the ID of the token which starts with str.

Returns -1 if not found and -2 if several tokens match.

int ccuty::stringset::insert ( const std::string &  token)
inline

Inserts token in the stringset.

Duplicate tokens are ignored. Return the ID of token.

int ccuty::stringset::insert ( const char *  token)
inline

Inserts token in the stringset.

Duplicate tokens are ignored. Return the ID of token.

int ccuty::stringset::insert ( const elem &  e)
inline

Inserts a {token, ID} couple in the stringset.

Duplicate tokens are ignored. Return the ID of token.

template<typename T >
void ccuty::stringset::append ( const T &  cont)
inline

Inserts the elements of a container (vector, list, map) of tokens or {token, ID} couples.

Duplicate tokens are ignored. IDs must be >= 0.


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