ATLAS Offline Software
Loading...
Searching...
No Matches
CxxUtils/CxxUtils/ClassName.h
Go to the documentation of this file.
1// This file's extension implies that it's C, but it's really -*- C++ -*-.
2
3/*
4 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
5*/
12
13
14#ifndef CXXUTILS_CLASSNAME_H
15#define CXXUTILS_CLASSNAME_H
16
17
18#include <string>
19#include <vector>
20#include <map>
21#include <stdexcept>
22
23
24namespace CxxUtils {
25
26
101{
102public:
103
108 : public std::runtime_error
109 {
110 public:
111 ExcBadClassName (const std::string& name);
112 };
113
114
119 : public std::runtime_error
120 {
121 public:
122 ExcMissingVariable (const std::string& var);
123 };
124
125
129 class Rules
130 {
131 public:
135 size_t size() const;
136
137
148 void add (const ClassName& pattern, const ClassName& replacement);
149
150
161 void add (ClassName&& pattern, ClassName&& replacement);
162
163
175 bool applyTo (ClassName& cn) const;
176
177
186 std::string apply (const std::string& name) const;
187
188
189 private:
191 typedef std::pair<ClassName, ClassName> pat_repl_t;
192
194 typedef std::multimap<std::string, pat_repl_t> rulemap_t;
196 };
197
198
200 typedef std::map<std::string, ClassName> match_t;
201
202
208 ClassName ();
209
210
217 ClassName (const char* name);
218
219
226 ClassName (const std::string& name);
227
228
236 ClassName (const std::string& name, std::string::size_type& pos);
237
238
243 void swap (ClassName& other);
244
245
249 bool isConst() const;
250
251
255 void setConst();
256
257
263 const std::string& name() const;
264
265
271 std::string qualifiedName() const;
272
273
277 std::string fullName() const;
278
279
283 size_t ntargs() const;
284
285
290 const ClassName& targ (size_t i) const;
291
292
296 bool operator== (const ClassName& other) const;
297
298
302 bool operator!= (const ClassName& other) const;
303
304
315 bool match (const ClassName& pattern, match_t& matches) const;
316
317
329 void subst (const match_t& matches);
330
331
343 ClassName substCopy (const match_t& matches) const;
344
345
358 void applyRules (const Rules& rules);
359
360
374 static std::string applyRules (const std::string& name,
375 const Rules& rules);
376
377
378private:
387 void parse (const std::string& name, std::string::size_type& pos);
388
389
401 std::string parsePrimary (const std::string& name,
402 std::string::size_type& pos);
403
404
418 void parseNamespace (const std::string& name, std::string::size_type& pos);
419
420
433 void parseTemplateArgs (const std::string& name, std::string::size_type& pos);
434
435
444 void skipSpaces (const std::string& name, std::string::size_type& pos);
445
446
458 bool match1 (const ClassName& pattern,
459 bool topLevel,
460 match_t& matches) const;
461
462
473 bool applyRules1 (const Rules& rules);
474
475
478
483 std::vector<ClassName> m_namespace;
484
486 std::string m_name;
487
489 std::vector<ClassName> m_targs;
490};
491
492
493} // namespace CxxUtils
494
495
496#endif // not CXXUTILS_CLASSNAME_H
ExcBadClassName(const std::string &name)
Exception to signal a malformed class name.
ExcMissingVariable(const std::string &var)
Exception to signal a missing variable.
A set of transformation rules to use with ClassName.
std::string apply(const std::string &name) const
Apply transformations to a class name.
void add(const ClassName &pattern, const ClassName &replacement)
Add a new transformation rule.
bool applyTo(ClassName &cn) const
Apply the set of transformation rules to a class name object.
size_t size() const
Return the number of defined rules.
std::multimap< std::string, pat_repl_t > rulemap_t
Map from the root of a pattern to the pattern, replacement pair.
std::pair< ClassName, ClassName > pat_repl_t
A pattern and replacement.
bool operator==(const ClassName &other) const
Test two expressions for equality.
bool match1(const ClassName &pattern, bool topLevel, match_t &matches) const
Match this expression against a pattern.
bool isConst() const
Get the const flag for this expression.
std::vector< ClassName > m_namespace
The containing namespace.
void subst(const match_t &matches)
Substitute variables into this expression.
std::string parsePrimary(const std::string &name, std::string::size_type &pos)
Parse a primary part of the class name.
bool m_const
Is this expression const?
void skipSpaces(const std::string &name, std::string::size_type &pos)
Skip past spaces in a string.
std::string m_name
The primary name part of this expression.
std::string fullName() const
Return the full name of the expression.
bool operator!=(const ClassName &other) const
Test two expressions for inequality.
std::vector< ClassName > m_targs
The template arguments for this name.
bool applyRules1(const Rules &rules)
Apply a set of transformation rules to this object.
ClassName substCopy(const match_t &matches) const
Return a copy of this expression with variables substituted.
std::map< std::string, ClassName > match_t
Map used to hold variable assignments from matching.
ClassName()
Default constructor.
const std::string & name() const
Return the root name of the expression.
bool match(const ClassName &pattern, match_t &matches) const
Match this expression against a pattern.
const ClassName & targ(size_t i) const
Return one template argument.
std::string qualifiedName() const
Return the namespace-qualified name of the expression.
void setConst()
Set the const flag for this expression.
void applyRules(const Rules &rules)
Apply a set of transformation rules to this object.
void parseNamespace(const std::string &name, std::string::size_type &pos)
Parse a namespace qualification.
void swap(ClassName &other)
Swap this expression with another one.
void parse(const std::string &name, std::string::size_type &pos)
Parse a string into a ClassName.
void parseTemplateArgs(const std::string &name, std::string::size_type &pos)
Parse the template part of a name.
size_t ntargs() const
Return number of template arguments.