The Gaudi Framework  master (37c0b60a)
AttribStringParser.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 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 {
51  public:
53  using value_type = Attrib;
55  using pointer = value_type*;
57 
58  Iterator() = default;
59  Iterator( const boost::sregex_iterator& it, bool expand_vars ) : m_it( it ), m_expandVars( expand_vars ) {
60  // i_setAttrib();
61  }
62  Iterator( const Iterator& other ) : Iterator( other.m_it, other.m_expandVars ) {}
63  Iterator( Iterator&& other ) : m_it( std::move( other.m_it ) ), m_expandVars( other.m_expandVars ) {}
65  ++m_it;
66  return *this;
67  }
69  auto old = *this;
70  ++m_it;
71  return old;
72  }
74  i_setAttrib();
75  return m_attrib;
76  }
77  bool operator==( const Iterator& other ) { return m_it == other.m_it; }
78  bool operator!=( const Iterator& other ) { return m_it != other.m_it; }
79 
80  private:
82  boost::sregex_iterator m_it;
83  bool m_expandVars = false;
88  void i_setAttrib() {
89  static const boost::sregex_iterator endmark;
90  if ( m_it != endmark ) {
91  // if we have a match, we cache the values
92  m_attrib = Attrib{ ( *m_it )[1], ( *m_it )[2] };
93  if ( m_expandVars && m_attrib.value.find( "${" ) != std::string::npos ) {
94  static const boost::regex varexp{ "\\$\\{([^}]+)\\}" };
95  auto i = 1;
96  while ( i ) {
97  i = 0;
98  m_attrib.value = boost::regex_replace(
99  m_attrib.value, varexp,
100  [&i]( const boost::smatch& m ) -> std::string {
101  const std::string name = m[1];
102  const char* cname = name.c_str();
103  if ( System::isEnvSet( cname ) ) {
104  ++i;
105  return System::getEnv( cname );
106  }
107  return m[0]; // do not expand unknown vars
108  },
109  boost::match_default | boost::format_all );
110  }
111  }
112  } else {
113  // otherwise we clean the cache
114  m_attrib = Attrib();
115  }
116  }
117  };
118 
124  AttribStringParser( std::string data, bool expand_vars = true ) : m_data( data ), m_expandVars( expand_vars ) {}
125 
126  private:
129 
130  boost::sregex_iterator parse() const {
131  static const boost::regex exp{ "[[:space:]]*([^[:space:]]+)[[:space:]]*=[[:space:]]*'(.*?)'" };
132  return boost::sregex_iterator( begin( m_data ), end( m_data ), exp );
133  }
134  friend Iterator begin( const AttribStringParser& );
135  };
137  return AttribStringParser::Iterator( parser.parse(), parser.m_expandVars );
138  }
141  }
142  } // namespace Utils
143 } // namespace Gaudi
144 
145 #endif
Gaudi::Utils::AttribStringParser::Iterator::operator!=
bool operator!=(const Iterator &other)
Definition: AttribStringParser.h:78
std::string
STL class.
Gaudi::Utils::AttribStringParser::Iterator::Iterator
Iterator(Iterator &&other)
Definition: AttribStringParser.h:63
System.h
std::string::find
T find(T... args)
Gaudi::Utils::AttribStringParser::Iterator::operator++
Iterator operator++()
Definition: AttribStringParser.h:68
std::input_iterator_tag
Gaudi::Utils::AttribStringParser::Iterator::Iterator
Iterator(const Iterator &other)
Definition: AttribStringParser.h:62
Gaudi::Utils::AttribStringParser::Iterator::operator==
bool operator==(const Iterator &other)
Definition: AttribStringParser.h:77
Gaudi::Utils::AttribStringParser::Iterator::m_it
boost::sregex_iterator m_it
Wrapped boost::sregex_iterator instance.
Definition: AttribStringParser.h:82
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:136
Gaudi::Utils::AttribStringParser::parse
boost::sregex_iterator parse() const
Definition: AttribStringParser.h:130
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:59
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:139
Gaudi::Utils::AttribStringParser::m_data
std::string m_data
Definition: AttribStringParser.h:127
Gaudi::Utils::AttribStringParser::Iterator::operator*
reference operator*()
Definition: AttribStringParser.h:73
Gaudi::Utils::AttribStringParser::Iterator
Iterator to loop over the tag/value pairs in the attribute string.
Definition: AttribStringParser.h:50
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
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:124
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:85
std
STL namespace.
Gaudi::Utils::AttribStringParser::Iterator::Iterator
Iterator()=default
Gaudi::Utils::AttribStringParser::Iterator::operator++
Iterator operator++(int)
Definition: AttribStringParser.h:64
Kernel.h
Gaudi::Utils::AttribStringParser::Iterator::m_expandVars
bool m_expandVars
Definition: AttribStringParser.h:83
Gaudi::Utils::AttribStringParser::m_expandVars
bool m_expandVars
Definition: AttribStringParser.h:128
std::ptrdiff_t
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:88
Iterator
boost::spirit::classic::position_iterator2< ForwardIterator > Iterator
Definition: Iterator.h:28