Recursively separate out template arguments in a C++ class name. More...
#include <ClassName.h>
Classes | |
class | ExcBadClassName |
Exception to signal a malformed class name. More... | |
class | ExcMissingVariable |
Exception to signal a missing variable. More... | |
class | Rules |
A set of transformation rules to use with ClassName . More... | |
Public Types | |
typedef std::map< std::string, ClassName > | match_t |
Map used to hold variable assignments from matching. | |
Public Member Functions | |
ClassName () | |
Default constructor. | |
ClassName (const char *name) | |
Parse a class name into component parts. | |
ClassName (const std::string &name) | |
Parse a class name into component parts. | |
ClassName (const std::string &name, std::string::size_type &pos) | |
Parse a class name into component parts. | |
void | swap (ClassName &other) |
Swap this expression with another one. | |
void | setConst () |
Set the const flag for this expression. | |
std::string | name () const |
Return the root name of the expression. | |
std::string | qualifiedName () const |
Return the namespace-qualified name of the expression. | |
std::string | fullName () const |
Return the full name of the expression. | |
bool | operator== (const ClassName &other) const |
Test two expressions for equality. | |
bool | operator!= (const ClassName &other) const |
Test two expressions for inequality. | |
bool | match (const ClassName &pattern, match_t &matches) const |
Match this expression against a pattern. | |
void | subst (const match_t &matches) |
Substitute variables into this expression. | |
ClassName | substCopy (const match_t &matches) const |
Return a copy of this expression with variables substituted. | |
void | applyRules (const Rules &rules) |
Apply a set of transformation rules to this object. | |
Static Public Member Functions | |
static std::string | applyRules (const std::string &name, const Rules &rules) |
Apply a set of transformation rules a class name. param The name of the class to transform. |
Recursively separate out template arguments in a C++ class name.
This class allows making some simple transformations of C++ class names. For example, given these rules:
ClassName::Rules rules; rules.add ("std::vector<$T, std::allocator<$T> >", "std::vector<$T>"); rules.add ("std::map<$K,$V, std::less<$K>, std::allocator<std::pair<const $K,$V> > >", "std::map<$K,$V>"); rules.add ("DataVector<$T, $B>", "DataVector<$T>"); rules.add ("std::__1", "std");
then `rules.apply` can make transformations like this:
std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > > -> std::vector<std::vector<int> > std::map<int, float, std::less<int>, std::allocator<std::pair<const int, float> > > -> std::map<int, float> DataVector<Foo, DataModel_detail::NoBase> -> DataVector<Foo>
In slightly more detail: this class analyzes C++ class names. A name like
A::B<int, double>
is broken down like this:
This is done recursively; both the namespace and template argument pieces can be further broken down like this. A name can also be marked as `const', but no other parsing of C-like declarators is done.
Parsed names can be matched against simple patterns like this:
A::B<$T>
and the variable `T` gets set to the corresponding piece of the type being matched. For example, given the above pattern,
If the pattern were `AB<const $t>="">`, then `AB<const int>` would match with `T` set to `int`, but `AB<int>` would not match.
However, the root name of a pattern may not be a variable; for example, you can't use `A$T<int>` as a pattern.
You can also substitute variables back into a pattern; for example,
CxxUtils::ClassName::ClassName | ( | ) |
Default constructor.
Needed for STL compatibility.
CxxUtils::ClassName::ClassName | ( | const char * | name | ) |
Parse a class name into component parts.
name | The name to parse. |
Raises a BadClassName
exception if the name isn't completely parsed.
name | The name to parse. |
Raises a ExcBadClassName
exception if the name isn't completely parsed.
CxxUtils::ClassName::ClassName | ( | const std::string & | name | ) |
Parse a class name into component parts.
name | The name to parse. |
Raises a BadClassName
exception if the name isn't completely parsed.
name | The name to parse. |
Raises a ExcBadClassName
exception if the name isn't completely parsed.
CxxUtils::ClassName::ClassName | ( | const std::string & | name, | |
std::string::size_type & | pos | |||
) |
Parse a class name into component parts.
name | String containing the name to parse. | |
pos | Position in the string at which parsing should start. |
pos
is updated to point to past the point where parsing stopped.
std::string CxxUtils::ClassName::applyRules | ( | const std::string & | name, | |
const Rules & | rules | |||
) | [static] |
void CxxUtils::ClassName::applyRules | ( | const Rules & | rules | ) |
Apply a set of transformation rules to this object.
rules | The set of rules to apply. |
Recursively walk this expression, trying to apply the transformation rules in rules
. If any matches are found, this expression is modified in-place and the walk is repeated. This function terminates when no further matches are found.
Warning: An infinite loop is possible if the replacement for a pattern can always be matched by another pattern.
Match this expression against a pattern.
pattern | The pattern to match. | |
[out] | matches | Dictionary of pattern substitutions. |
Return true if pattern
matches the current expression. pattern
may contain dummy variables of the form `$T`. On a successful return, the map matches
contains the variable assignments needed for the match.
std::string CxxUtils::ClassName::name | ( | ) | const |
Return the root name of the expression.
In `AB<C>`, the root name is `B`.
std::string CxxUtils::ClassName::qualifiedName | ( | ) | const |
Return the namespace-qualified name of the expression.
Return the root name of the expression.
In `AB<C>`, this is `AB`.
In `AB<C>`, the root name is `B`.
void CxxUtils::ClassName::subst | ( | const match_t & | matches | ) |
Substitute variables into this expression.
The | dictionary of variables to substitute. |
If this expression contains variables like `$T`, they are replaced with the corresponding values from matches
. If a variable is present in the expression but is not in matches
, ExcMissingVariable
is thrown.
The substitutions are made in-place.
Return a copy of this expression with variables substituted.
The | dictionary of variables to substitute. |
If this expression contains variables like `$T`, they are replaced with the corresponding values from matches
. If a variable is present in the expression but is not in matches
, ExcMissingVariable
is thrown.
The substitutions are made in a copy of the expression, which is returned.
void CxxUtils::ClassName::swap | ( | ClassName & | other | ) |
Swap this expression with another one.
other | The other expression with which to swap. |