Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v36r7 (7f57a304)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
AttribStringParser.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef _ATTRIB_STRING_PARSER_H_
12 #define _ATTRIB_STRING_PARSER_H_
13 
14 #include "GaudiKernel/Kernel.h"
15 #include "GaudiKernel/System.h"
16 #include <iterator>
17 #ifdef __clang__
18 # pragma clang diagnostic push
19 // Hide warning message:
20 // boost/regex/v4/instances.hpp:128:17: warning: keyword is hidden by macro definition
21 # pragma clang diagnostic ignored "-Wkeyword-macro"
22 #endif
23 #include <boost/regex.hpp>
24 #ifdef __clang__
25 # pragma clang diagnostic pop
26 #endif
27 
28 namespace Gaudi {
29  namespace Utils {
41  public:
43  struct Attrib {
46  };
47 
49  // This class is essentially a wrapper around boost::sregex_iterator.
50  class Iterator : public std::iterator<std::input_iterator_tag, Attrib> {
51  public:
52  Iterator() = default;
53  Iterator( const boost::sregex_iterator& it, bool expand_vars ) : m_it( it ), m_expandVars( expand_vars ) {
54  // i_setAttrib();
55  }
56  Iterator( const Iterator& other ) : Iterator( other.m_it, other.m_expandVars ) {}
57  Iterator( Iterator&& other ) : m_it( std::move( other.m_it ) ), m_expandVars( other.m_expandVars ) {}
59  ++m_it;
60  return *this;
61  }
63  auto old = *this;
64  ++m_it;
65  return old;
66  }
67  reference operator*() {
68  i_setAttrib();
69  return m_attrib;
70  }
71  bool operator==( const Iterator& other ) { return m_it == other.m_it; }
72  bool operator!=( const Iterator& other ) { return m_it != other.m_it; }
73 
74  private:
76  boost::sregex_iterator m_it;
77  bool m_expandVars = false;
82  void i_setAttrib() {
83  static const boost::sregex_iterator endmark;
84  if ( m_it != endmark ) {
85  // if we have a match, we cache the values
86  m_attrib = Attrib{ ( *m_it )[1], ( *m_it )[2] };
87  if ( m_expandVars && m_attrib.value.find( "${" ) != std::string::npos ) {
88  static const boost::regex varexp{ "\\$\\{([^}]+)\\}" };
89  auto i = 1;
90  while ( i ) {
91  i = 0;
92  m_attrib.value = boost::regex_replace(
93  m_attrib.value, varexp,
94  [&i]( const boost::smatch& m ) -> std::string {
95  const std::string name = m[1];
96  const char* cname = name.c_str();
97  if ( System::isEnvSet( cname ) ) {
98  ++i;
99  return System::getEnv( cname );
100  }
101  return m[0]; // do not expand unknown vars
102  },
103  boost::match_default | boost::format_all );
104  }
105  }
106  } else {
107  // otherwise we clean the cache
108  m_attrib = Attrib();
109  }
110  }
111  };
112 
118  AttribStringParser( std::string data, bool expand_vars = true ) : m_data( data ), m_expandVars( expand_vars ) {}
119 
120  private:
123 
124  boost::sregex_iterator parse() const {
125  static const boost::regex exp{ "[[:space:]]*([^[:space:]]+)[[:space:]]*=[[:space:]]*'(.*?)'" };
126  return boost::sregex_iterator( begin( m_data ), end( m_data ), exp );
127  }
128  friend Iterator begin( const AttribStringParser& );
129  };
131  return AttribStringParser::Iterator( parser.parse(), parser.m_expandVars );
132  }
135  }
136  } // namespace Utils
137 } // namespace Gaudi
138 
139 #endif
Gaudi::Utils::AttribStringParser::Iterator::operator!=
bool operator!=(const Iterator &other)
Definition: AttribStringParser.h:72
std::string
STL class.
Gaudi::Utils::AttribStringParser::Iterator::Iterator
Iterator(Iterator &&other)
Definition: AttribStringParser.h:57
System.h
std::string::find
T find(T... args)
std::iterator
Gaudi::Utils::AttribStringParser::Iterator::operator++
Iterator operator++()
Definition: AttribStringParser.h:62
Gaudi::Utils::AttribStringParser::Iterator::Iterator
Iterator(const Iterator &other)
Definition: AttribStringParser.h:56
Gaudi::Utils::AttribStringParser::Iterator::operator==
bool operator==(const Iterator &other)
Definition: AttribStringParser.h:71
Gaudi::Utils::AttribStringParser::Iterator::m_it
boost::sregex_iterator m_it
Wrapped boost::sregex_iterator instance.
Definition: AttribStringParser.h:76
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:130
Gaudi::Utils::AttribStringParser::parse
boost::sregex_iterator parse() const
Definition: AttribStringParser.h:124
Gaudi::Units::m
constexpr double m
Definition: SystemOfUnits.h:108
Gaudi::Utils::AttribStringParser::Iterator::Iterator
Iterator(const boost::sregex_iterator &it, bool expand_vars)
Definition: AttribStringParser.h:53
Gaudi::Utils::AttribStringParser::Attrib
Simple class to wrap tag/value pairs.
Definition: AttribStringParser.h:43
Gaudi::Utils::end
AttribStringParser::Iterator end(const AttribStringParser &)
Definition: AttribStringParser.h:133
Gaudi::Utils::AttribStringParser::m_data
std::string m_data
Definition: AttribStringParser.h:121
Gaudi::Utils::AttribStringParser::Iterator::operator*
reference operator*()
Definition: AttribStringParser.h:67
Gaudi::Utils::AttribStringParser::Iterator
Iterator to loop over the tag/value pairs in the attribute string.
Definition: AttribStringParser.h:50
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
Gaudi::Utils::AttribStringParser::AttribStringParser
AttribStringParser(std::string data, bool expand_vars=true)
Initialize the parsing of an attribute string.
Definition: AttribStringParser.h:118
Gaudi::Utils::AttribStringParser::Attrib::value
std::string value
Definition: AttribStringParser.h:45
Gaudi::Utils::AttribStringParser::Iterator::m_attrib
Attrib m_attrib
Cached Attrib instance.
Definition: AttribStringParser.h:79
std
STL namespace.
Gaudi::Utils::AttribStringParser::Iterator::Iterator
Iterator()=default
Gaudi::Utils::AttribStringParser::Iterator::operator++
Iterator operator++(int)
Definition: AttribStringParser.h:58
Kernel.h
Gaudi::Utils::AttribStringParser::Iterator::m_expandVars
bool m_expandVars
Definition: AttribStringParser.h:77
Gaudi::Utils::AttribStringParser::m_expandVars
bool m_expandVars
Definition: AttribStringParser.h:122
gaudirun.parser
parser
Definition: gaudirun.py:149
Gaudi::Utils::AttribStringParser::Attrib::tag
std::string tag
Definition: AttribStringParser.h:44
Gaudi::Utils::AttribStringParser
Parse attribute strings allowing iteration over the various attributes.
Definition: AttribStringParser.h:40
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:82
Iterator
boost::spirit::classic::position_iterator2< ForwardIterator > Iterator
Definition: Iterator.h:28