Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
StringMapper Class Reference

A tool for expanding variables embedded in a std::string to literals. More...

#include <StringMapper.h>

Inheritance diagram for StringMapper:
Inheritance graph
[legend]
Collaboration diagram for StringMapper:
Collaboration graph
[legend]

Public Member Functions

 StringMapper (const std::string &format="", const std::string &validChars="[a-zA-Z0-9_]+", bool formatting=true)
 Default constructor. More...
 
 StringMapper (const RegExp &regexp, bool formatting=true)
 Initialize with the given RegExp // REMOVE!
 
 StringMapper (const StringMapper &mapper)
 Copy constructor copies the parsed string and the regExp.
 
StringMappersetValidChars (const std::string &chars)
 
StringMapperenableFormatting (bool formatting)
 Enable variable formatting, followed by pipe '|'. More...
 
StringMapperparse (const std::string &s, bool convertEscaped=false)
 Converts a std::string containing variables like in "Hello, ${NAME}!" to a list of StringLet's. More...
 
bool isLiteral () const
 Return true, if all the elements are literal.
 
std::ostream & toStream (std::ostream &ostr) const
 Output a concatenated chain of stringlets: literals as such and variables surrounded with "${" and "}". More...
 
template<class T >
std::ostream & toStream (std::ostream &ostr, const std::map< std::string, T > &variables, int replace=0, const VariableFormatter< T > &formatter=VariableFormatter< T >()) const
 Expands the variables in the last. More...
 
template<class T >
std::string toStr (const std::map< std::string, T > &m, int replaceChar=-1, const VariableFormatter< T > &formatter=VariableFormatter< T >()) const
 Expands the variables in the last parsed std::string to a std::string. More...
 
template<class T >
void expand (const std::map< std::string, T > &m, bool clear=false)
 Expands the variables in StringMapper, turning expanded variables to constants. More...
 
template<class T >
std::ostream & debug (std::ostream &ostr, const std::map< std::string, T > &m) const
 Dumps the list of StringLet's.
 

Static Public Member Functions

static std::string & convertEscaped (std::string &s)
 Interpret commond special chars tab '\t' and newline '
'.
 

Public Attributes

IosFormat iosFormat
 

Protected Member Functions

void updateRegExp ()
 
StringMapperparse (const std::string &s, RegExp &r)
 

Protected Attributes

std::string validChars
 
bool formatting
 
RegExp regExp
 

Detailed Description

A tool for expanding variables embedded in a std::string to literals.

The input variables are provided as a map, which is allowed to change dynamically. The input std::string, issued with parse(), contains the variables as "$key" or "${key}", a format familiar in Unix shell.

Constructor & Destructor Documentation

◆ StringMapper()

StringMapper ( const std::string &  format = "",
const std::string &  validChars = "[a-zA-Z0-9_]+",
bool  formatting = true 
)
inline

Default constructor.

Parameters
format- string containing variables like ${name}
validChars- allowed characters in variable names, ie inside braces.
formatting- support postprocessing of variable value (number rounding, string operations)

Member Function Documentation

◆ enableFormatting()

StringMapper& enableFormatting ( bool  formatting)
inline

Enable variable formatting, followed by pipe '|'.

The formatting depends on the implementation. In this base class, VariableFormatter<T> is applied.

◆ expand()

void expand ( const std::map< std::string, T > &  m,
bool  clear = false 
)
inline

Expands the variables in StringMapper, turning expanded variables to constants.

m - map containing variable values
clear - if true, replace undefined variables with empty std::strings.

◆ parse()

StringMapper & parse ( const std::string &  s,
bool  convertEscaped = false 
)

Converts a std::string containing variables like in "Hello, ${NAME}!" to a list of StringLet's.

The Stringlet list is internal.

Parameters
s- string containing variables like in "Hello, ${NAME}!"
convertEscaped- convert backslash+letter segments to actual chars (\t,
} first

◆ toStr()

std::string toStr ( const std::map< std::string, T > &  m,
int  replaceChar = -1,
const VariableFormatter< T > &  formatter = VariableFormatter<T>() 
) const
inline

Expands the variables in the last parsed std::string to a std::string.

m - map containing variable values
clear - if true, expand undefined variables as empty std::strings, else leave variable entry

◆ toStream() [1/2]

std::ostream& toStream ( std::ostream &  ostr) const
inline

Output a concatenated chain of stringlets: literals as such and variables surrounded with "${" and "}".

Prints the mapper in its current state, ie. some variables may have been expanded to literals.

Parameters
ostr- output stream

◆ toStream() [2/2]

std::ostream& toStream ( std::ostream &  ostr,
const std::map< std::string, T > &  variables,
int  replace = 0,
const VariableFormatter< T > &  formatter = VariableFormatter<T>() 
) const
inline

Expands the variables in the last.

ostr - output stream
m - map containing variable values
clear - if given, replace undefined variables with this char, or empty (if 0), else (-1) leave variable entry

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