| The Gaudi Framework v30r0 (c919700c)

- 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 {
20 namespace Utils
21 {
32 class AttribStringParser
33 {
34 public:
36 struct Attrib {
37 std::string tag;
38 std::string value;
39 };
40
42 // This class is essentially a wrapper around boost::sregex_iterator.
43 class Iterator : public std::iterator<std::input_iterator_tag, Attrib>
44 {
45 public:
46 Iterator() = default;
47 Iterator( const boost::sregex_iterator& it, bool expand_vars ) : m_it( it ), m_expandVars( expand_vars )
48 {
49 // i_setAttrib();
50 }
51 Iterator( const Iterator& other ) : Iterator( other.m_it, other.m_expandVars ) {}
52 Iterator( Iterator&& other ) : m_it( std::move( other.m_it ) ), m_expandVars( other.m_expandVars ) {}
53 Iterator operator++( int )
54 {
55 ++m_it;
56 return *this;
57 }
58 Iterator operator++()
59 {
60 auto old = *this;
61 ++m_it;
62 return old;
63 }
64 reference operator*()
65 {
66 i_setAttrib();
67 return m_attrib;
68 }
69 bool operator==( const Iterator& other ) { return m_it == other.m_it; }
70 bool operator!=( const Iterator& other ) { return m_it != other.m_it; }
71
72 private:
74 boost::sregex_iterator m_it;
75 bool m_expandVars = false;
77 Attrib m_attrib;
80 void i_setAttrib()
81 {
82 static const boost::sregex_iterator endmark;
83 if ( m_it != endmark ) {
84 // if we have a match, we cache the values
85 m_attrib = Attrib{( *m_it )[1], ( *m_it )[2]};
86 if ( m_expandVars && m_attrib.value.find( "${" ) != std::string::npos ) {
87 static const boost::regex varexp{"\\$\\{([^}]+)\\}"};
88 auto i = 1;
89 while ( i ) {
90 i = 0;
91 m_attrib.value = boost::regex_replace( m_attrib.value, varexp,
92 [&i]( const boost::smatch& m ) -> std::string {
93 const std::string name = m[1];
94 const char* cname = name.c_str();
95 if ( System::isEnvSet( cname ) ) {
96 ++i;
97 return System::getEnv( cname );
98 }
99 return m[0]; // do not expand unknown vars
100 },
101 boost::match_default | boost::format_all );
102 }
103 }
104 } else {
105 // otherwise we clean the cache
106 m_attrib = Attrib();
107 }
108 }
109 };
110
116 AttribStringParser( std::string data, bool expand_vars = true ) : m_data( data ), m_expandVars( expand_vars ) {}
117
118 private:
119 std::string m_data;
120 bool m_expandVars;
121
122 boost::sregex_iterator parse() const
123 {
124 static const boost::regex exp{"[[:space:]]*([^[:space:]]+)[[:space:]]*=[[:space:]]*'(.*?)'"};
125 return boost::sregex_iterator( begin( m_data ), end( m_data ), exp );
126 }
127 friend Iterator begin( const AttribStringParser& );
128 };
129 inline AttribStringParser::Iterator begin( const AttribStringParser& parser )
130 {
131 return AttribStringParser::Iterator( parser.parse(), parser.m_expandVars );
132 }
133 inline AttribStringParser::Iterator end( const AttribStringParser& /*parser*/ )
134 {
135 return AttribStringParser::Iterator();
136 }
137 }
138 }
139
140 #endif
Gaudi::Utils::AttribStringParser
Parse attribute strings allowing iteration over the various attributes.
Definition: AttribStringParser.h:32
System.h
Kernel.h
Gaudi::Utils::AttribStringParser::Iterator::operator++
Iterator operator++(int)
Definition: AttribStringParser.h:53
Gaudi::Utils::end
AttribStringParser::Iterator end(const AttribStringParser &)
Definition: AttribStringParser.h:133
std::iterator
gaudirun.parser
parser
Definition: gaudirun.py:130
Gaudi::Utils::AttribStringParser::begin
friend Iterator begin(const AttribStringParser &)
Definition: AttribStringParser.h:129
std
STL namespace.
Gaudi::Utils::AttribStringParser::Iterator
Iterator to loop over the tag/value pairs in the attribute string.
Definition: AttribStringParser.h:43
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:58
Gaudi::Utils::AttribStringParser::Iterator::operator!=
bool operator!=(const Iterator &other)
Definition: AttribStringParser.h:70
Gaudi::Utils::AttribStringParser::Iterator::Iterator
Iterator(const Iterator &other)
Definition: AttribStringParser.h:51
Gaudi::Utils::AttribStringParser::Iterator::Iterator
Iterator(const boost::sregex_iterator &it, bool expand_vars)
Definition: AttribStringParser.h:47
Gaudi::Units::m
constexpr double m
Definition: SystemOfUnits.h:94
Gaudi::Utils::AttribStringParser::m_expandVars
bool m_expandVars
Definition: AttribStringParser.h:120
Gaudi::Utils::AttribStringParser::Iterator::m_it
boost::sregex_iterator m_it
Wrapped boost::sregex_iterator instance.
Definition: AttribStringParser.h:74
Gaudi::Utils::AttribStringParser::Attrib::value
std::string value
Definition: AttribStringParser.h:38
Gaudi::Utils::AttribStringParser::Attrib
Simple class to wrap tag/value pairs.
Definition: AttribStringParser.h:36
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:77
Gaudi::Utils::AttribStringParser::AttribStringParser
AttribStringParser(std::string data, bool expand_vars=true)
Initialize the parsing of an attribute string.
Definition: AttribStringParser.h:116
Gaudi::Utils::AttribStringParser::Iterator::operator*
reference operator*()
Definition: AttribStringParser.h:64
Gaudi::Utils::AttribStringParser::m_data
std::string m_data
Definition: AttribStringParser.h:119
Gaudi::Utils::AttribStringParser::parse
boost::sregex_iterator parse() const
Definition: AttribStringParser.h:122
Gaudi::Utils::AttribStringParser::Attrib::tag
std::string tag
Definition: AttribStringParser.h:37
Gaudi
Helper functions to set/get the application return code.
Definition: __init__.py:1
Objects::reference
TO * reference(FROM *from)
Definition: KeyedObject.cpp:21
Gaudi::Utils::AttribStringParser::Iterator::operator==
bool operator==(const Iterator &other)
Definition: AttribStringParser.h:69
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:80