| The Gaudi Framework v32r1 (f65d50dc)

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 ) : m_it( it ), m_expandVars( expand_vars ) {
44 // i_setAttrib();
45 }
46 Iterator( const Iterator& other ) : Iterator( other.m_it, other.m_expandVars ) {}
47 Iterator( Iterator&& other ) : m_it( std::move( other.m_it ) ), m_expandVars( other.m_expandVars ) {}
48 Iterator operator++( int ) {
49 ++m_it;
50 return *this;
51 }
52 Iterator operator++() {
53 auto old = *this;
54 ++m_it;
55 return old;
56 }
57 reference operator*() {
58 i_setAttrib();
59 return m_attrib;
60 }
61 bool operator==( const Iterator& other ) { return m_it == other.m_it; }
62 bool operator!=( const Iterator& other ) { return m_it != other.m_it; }
63
64 private:
66 boost::sregex_iterator m_it;
67 bool m_expandVars = false;
69 Attrib m_attrib;
72 void i_setAttrib() {
73 static const boost::sregex_iterator endmark;
74 if ( m_it != endmark ) {
75 // if we have a match, we cache the values
76 m_attrib = Attrib{( *m_it )[1], ( *m_it )[2]};
77 if ( m_expandVars && m_attrib.value.find( "${" ) != std::string::npos ) {
78 static const boost::regex varexp{"\\$\\{([^}]+)\\}"};
79 auto i = 1;
80 while ( i ) {
81 i = 0;
82 m_attrib.value = boost::regex_replace(
83 m_attrib.value, varexp,
84 [&i]( const boost::smatch& m ) -> std::string {
85 const std::string name = m[1];
86 const char* cname = name.c_str();
87 if ( System::isEnvSet( cname ) ) {
88 ++i;
89 return System::getEnv( cname );
90 }
91 return m[0]; // do not expand unknown vars
92 },
93 boost::match_default | boost::format_all );
94 }
95 }
96 } else {
97 // otherwise we clean the cache
98 m_attrib = Attrib();
99 }
100 }
101 };
102
108 AttribStringParser( std::string data, bool expand_vars = true ) : m_data( data ), m_expandVars( expand_vars ) {}
109
110 private:
111 std::string m_data;
112 bool m_expandVars;
113
114 boost::sregex_iterator parse() const {
115 static const boost::regex exp{"[[:space:]]*([^[:space:]]+)[[:space:]]*=[[:space:]]*'(.*?)'"};
116 return boost::sregex_iterator( begin( m_data ), end( m_data ), exp );
117 }
118 friend Iterator begin( const AttribStringParser& );
119 };
120 inline AttribStringParser::Iterator begin( const AttribStringParser& parser ) {
121 return AttribStringParser::Iterator( parser.parse(), parser.m_expandVars );
122 }
123 inline AttribStringParser::Iterator end( const AttribStringParser& /*parser*/ ) {
124 return AttribStringParser::Iterator();
125 }
126 } // namespace Utils
127 } // namespace Gaudi
128
129 #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 *
reference operator *()
Definition: AttribStringParser.h:57
Gaudi::Utils::AttribStringParser::Iterator::Iterator
Iterator()=default
Gaudi::Utils::AttribStringParser::Iterator::operator++
Iterator operator++(int)
Definition: AttribStringParser.h:48
Gaudi::Utils::end
AttribStringParser::Iterator end(const AttribStringParser &)
Definition: AttribStringParser.h:123
std::iterator
gaudirun.parser
parser
Definition: gaudirun.py:132
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:47
std::string
STL class.
Gaudi::Utils::AttribStringParser::Iterator::operator++
Iterator operator++()
Definition: AttribStringParser.h:52
Gaudi::Utils::AttribStringParser::Iterator::operator!=
bool operator!=(const Iterator &other)
Definition: AttribStringParser.h:62
Gaudi::Utils::AttribStringParser::Iterator::Iterator
Iterator(const Iterator &other)
Definition: AttribStringParser.h:46
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:92
Gaudi::Utils::AttribStringParser::m_expandVars
bool m_expandVars
Definition: AttribStringParser.h:112
Gaudi::Utils::AttribStringParser::Iterator::m_it
boost::sregex_iterator m_it
Wrapped boost::sregex_iterator instance.
Definition: AttribStringParser.h:66
Gaudi::Utils::AttribStringParser::Iterator::m_expandVars
bool m_expandVars
Definition: AttribStringParser.h:67
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)
Gaudi::Utils::AttribStringParser::parse
boost::sregex_iterator parse() const
Definition: AttribStringParser.h:114
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:69
Gaudi::Utils::AttribStringParser::AttribStringParser
AttribStringParser(std::string data, bool expand_vars=true)
Initialize the parsing of an attribute string.
Definition: AttribStringParser.h:108
Gaudi::Utils::AttribStringParser::m_data
std::string m_data
Definition: AttribStringParser.h:111
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:120
Gaudi::Utils::AttribStringParser::Attrib::tag
std::string tag
Definition: AttribStringParser.h:34
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
Gaudi::Utils::AttribStringParser::Iterator::operator==
bool operator==(const Iterator &other)
Definition: AttribStringParser.h:61
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:72