| The Gaudi Framework v28r2p1 (f1a77ff4)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

AttribStringParser.h
Go to the documentation of this file.
1 #ifndef _ATTRIB_STRING_PARSER_H_
2 #define _ATTRIB_STRING_PARSER_H_
3
4 #include "GaudiKernel/Kernel.h"
5 #include "GaudiKernel/System.h"
6 #include <iterator>
7 #ifdef __clang__
8 #pragma clang diagnostic push
9 // Hide warning message:
10 // boost/regex/v4/instances.hpp:128:17: warning: keyword is hidden by macro definition
11 #pragma clang diagnostic ignored "-Wkeyword-macro"
12 #endif
13 #include <boost/regex.hpp>
14 #ifdef __clang__
15 #pragma clang diagnostic pop
16 #endif
17
18 namespace Gaudi {
19 namespace Utils {
30 class AttribStringParser {
31 public:
33 struct Attrib {
34 std::string tag;
35 std::string value;
36 };
37
39 // This class is essentially a wrapper around boost::sregex_iterator.
40 class Iterator: public std::iterator<std::input_iterator_tag, Attrib> {
41 public:
42 Iterator() = default;
43 Iterator(const boost::sregex_iterator& it, bool expand_vars):
44 m_it(it),
45 m_expandVars(expand_vars)
46 {
47 //i_setAttrib();
48 }
49 Iterator(const Iterator& other): Iterator(other.m_it, other.m_expandVars)
50 {
51 }
52 Iterator(Iterator&& other):
53 m_it(std::move(other.m_it)),
54 m_expandVars(other.m_expandVars)
55 {
56 }
57 Iterator operator++(int) {
58 ++m_it;
59 return *this;
60 }
61 Iterator operator++() {
62 auto old = *this;
63 ++m_it;
64 return old;
65 }
66 reference operator*() {
67 i_setAttrib();
68 return m_attrib;
69 }
70 bool operator==(const Iterator& other) {
71 return m_it == other.m_it;
72 }
73 bool operator!=(const Iterator& other) {
74 return m_it != other.m_it;
75 }
76 private:
78 boost::sregex_iterator m_it;
79 bool m_expandVars = false;
81 Attrib m_attrib;
84 void i_setAttrib() {
85 static const boost::sregex_iterator endmark;
86 if (m_it != endmark) {
87 // if we have a match, we cache the values
88 m_attrib = Attrib{(*m_it)[1], (*m_it)[2]};
89 if (m_expandVars && m_attrib.value.find("${") != std::string::npos) {
90 static const boost::regex varexp{"\\$\\{([^}]+)\\}"};
91 auto i = 1;
92 while (i) {
93 i = 0;
94 m_attrib.value = boost::regex_replace(m_attrib.value,
95 varexp, [&i] (const boost::smatch& m) -> std::string {
96 const std::string name = m[1];
97 const char* cname = name.c_str();
98 if (System::isEnvSet(cname)) {
99 ++i;
100 return System::getEnv(cname);
101 }
102 return m[0]; // do not expand unknown vars
103 },
104 boost::match_default | boost::format_all);
105 }
106 }
107 } else {
108 // otherwise we clean the cache
109 m_attrib = Attrib();
110 }
111 }
112 };
113
119 AttribStringParser(std::string data, bool expand_vars=true):
120 m_data(data),
121 m_expandVars(expand_vars) {
122 }
123
124 private:
125 std::string m_data;
126 bool m_expandVars;
127
128 boost::sregex_iterator parse() const {
129 static const boost::regex exp{"[[:space:]]*([^[:space:]]+)[[:space:]]*=[[:space:]]*'(.*?)'"};
130 return boost::sregex_iterator(begin(m_data), end(m_data), exp);
131 }
132 friend Iterator begin(const AttribStringParser&);
133 };
134 inline AttribStringParser::Iterator begin(const AttribStringParser& parser) {
135 return AttribStringParser::Iterator(parser.parse(), parser.m_expandVars);
136 }
137 inline AttribStringParser::Iterator end(const AttribStringParser& /*parser*/) {
138 return AttribStringParser::Iterator();
139 }
140 }
141 }
142
143 #endif
Gaudi::Utils::AttribStringParser
Parse attribute strings allowing iteration over the various attributes.
Definition: AttribStringParser.h:30
System.h
Kernel.h
Gaudi::Utils::AttribStringParser::Iterator::operator++
Iterator operator++(int)
Definition: AttribStringParser.h:57
Gaudi::Utils::end
AttribStringParser::Iterator end(const AttribStringParser &)
Definition: AttribStringParser.h:137
std::iterator
gaudirun.parser
parser
Definition: gaudirun.py:122
Gaudi::Utils::AttribStringParser::begin
friend Iterator begin(const AttribStringParser &)
Definition: AttribStringParser.h:134
std
STL namespace.
Gaudi::Utils::AttribStringParser::Iterator
Iterator to loop over the tag/value pairs in the attribute string.
Definition: AttribStringParser.h:40
Gaudi::Utils::AttribStringParser::Iterator::Iterator
Iterator(Iterator &&other)
Definition: AttribStringParser.h:52
std::string
STL class.
Gaudi::Utils::AttribStringParser::Iterator::operator++
Iterator operator++()
Definition: AttribStringParser.h:61
Gaudi::Utils::AttribStringParser::Iterator::operator!=
bool operator!=(const Iterator &other)
Definition: AttribStringParser.h:73
Gaudi::Utils::AttribStringParser::Iterator::Iterator
Iterator(const Iterator &other)
Definition: AttribStringParser.h:49
Gaudi::Utils::AttribStringParser::Iterator::Iterator
Iterator(const boost::sregex_iterator &it, bool expand_vars)
Definition: AttribStringParser.h:43
Gaudi::Units::m
constexpr double m
Definition: SystemOfUnits.h:93
Gaudi::Utils::AttribStringParser::m_expandVars
bool m_expandVars
Definition: AttribStringParser.h:126
Gaudi::Utils::AttribStringParser::Iterator::m_it
boost::sregex_iterator m_it
Wrapped boost::sregex_iterator instance.
Definition: AttribStringParser.h:78
Gaudi::Utils::AttribStringParser::Attrib::value
std::string value
Definition: AttribStringParser.h:35
Gaudi::Utils::AttribStringParser::Attrib
Simple class to wrap tag/value pairs.
Definition: AttribStringParser.h:33
std::string::find
T find(T...args)
Iterator
boost::spirit::classic::position_iterator2< ForwardIterator > Iterator
Definition: Iterator.h:18
Gaudi::Utils::AttribStringParser::Iterator::m_attrib
Attrib m_attrib
Cached Attrib instance.
Definition: AttribStringParser.h:81
Gaudi::Utils::AttribStringParser::AttribStringParser
AttribStringParser(std::string data, bool expand_vars=true)
Initialize the parsing of an attribute string.
Definition: AttribStringParser.h:119
Gaudi::Utils::AttribStringParser::Iterator::operator*
reference operator*()
Definition: AttribStringParser.h:66
Gaudi::Utils::AttribStringParser::m_data
std::string m_data
Definition: AttribStringParser.h:125
Gaudi::Utils::AttribStringParser::parse
boost::sregex_iterator parse() const
Definition: AttribStringParser.h:128
Gaudi::Utils::AttribStringParser::Attrib::tag
std::string tag
Definition: AttribStringParser.h:34
Gaudi
Helper functions to set/get the application return code.
Definition: __init__.py:1
Objects::reference
TO * reference(FROM *from)
Definition: KeyedObject.cpp:20
Gaudi::Utils::AttribStringParser::Iterator::operator==
bool operator==(const Iterator &other)
Definition: AttribStringParser.h:70
Gaudi::Utils::AttribStringParser::Iterator::i_setAttrib
void i_setAttrib()
Helper method used to update the cached Attrib instance when dereferencing the iterator.
Definition: AttribStringParser.h:84