The Gaudi Framework  master (d98a2936)
AttribStringParser.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 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 #pragma once
12 
13 #include <GaudiKernel/Kernel.h>
14 #include <GaudiKernel/System.h>
15 #include <iterator>
16 #ifdef __clang__
17 # pragma clang diagnostic push
18 // Hide warning message:
19 // boost/regex/v4/instances.hpp:128:17: warning: keyword is hidden by macro definition
20 # pragma clang diagnostic ignored "-Wkeyword-macro"
21 #endif
22 #include <boost/regex.hpp>
23 #ifdef __clang__
24 # pragma clang diagnostic pop
25 #endif
26 
27 namespace Gaudi {
28  namespace Utils {
40  public:
42  struct Attrib {
43  std::string tag;
44  std::string value;
45  };
46 
48  // This class is essentially a wrapper around boost::sregex_iterator.
49  class Iterator {
50  public:
51  using iterator_category = std::input_iterator_tag;
52  using value_type = Attrib;
53  using difference_type = std::ptrdiff_t;
54  using pointer = value_type*;
56 
57  Iterator() = default;
58  Iterator( const boost::sregex_iterator& it, bool expand_vars ) : m_it( it ), m_expandVars( expand_vars ) {
59  // i_setAttrib();
60  }
61  Iterator( const Iterator& other ) : Iterator( other.m_it, other.m_expandVars ) {}
62  Iterator( Iterator&& other ) : m_it( std::move( other.m_it ) ), m_expandVars( other.m_expandVars ) {}
64  ++m_it;
65  return *this;
66  }
68  auto old = *this;
69  ++m_it;
70  return old;
71  }
73  i_setAttrib();
74  return m_attrib;
75  }
76  bool operator==( const Iterator& other ) { return m_it == other.m_it; }
77  bool operator!=( const Iterator& other ) { return m_it != other.m_it; }
78 
79  private:
81  boost::sregex_iterator m_it;
82  bool m_expandVars = false;
87  void i_setAttrib() {
88  static const boost::sregex_iterator endmark;
89  if ( m_it != endmark ) {
90  // if we have a match, we cache the values
91  m_attrib = Attrib{ ( *m_it )[1], ( *m_it )[2] };
92  if ( m_expandVars && m_attrib.value.find( "${" ) != std::string::npos ) {
93  static const boost::regex varexp{ "\\$\\{([^}]+)\\}" };
94  auto i = 1;
95  while ( i ) {
96  i = 0;
97  m_attrib.value = boost::regex_replace(
98  m_attrib.value, varexp,
99  [&i]( const boost::smatch& m ) -> std::string {
100  const std::string name = m[1];
101  const char* cname = name.c_str();
102  if ( System::isEnvSet( cname ) ) {
103  ++i;
104  return System::getEnv( cname );
105  }
106  return m[0]; // do not expand unknown vars
107  },
108  boost::match_default | boost::format_all );
109  }
110  }
111  } else {
112  // otherwise we clean the cache
113  m_attrib = Attrib();
114  }
115  }
116  };
117 
123  AttribStringParser( std::string data, bool expand_vars = true ) : m_data( data ), m_expandVars( expand_vars ) {}
124 
125  private:
126  std::string m_data;
128 
129  boost::sregex_iterator parse() const {
130  static const boost::regex exp{ "[[:space:]]*([^[:space:]]+)[[:space:]]*=[[:space:]]*'(.*?)'" };
131  return boost::sregex_iterator( begin( m_data ), end( m_data ), exp );
132  }
133  friend Iterator begin( const AttribStringParser& );
134  };
136  return AttribStringParser::Iterator( parser.parse(), parser.m_expandVars );
137  }
140  }
141  } // namespace Utils
142 } // namespace Gaudi
Gaudi::Utils::AttribStringParser::Iterator::operator!=
bool operator!=(const Iterator &other)
Definition: AttribStringParser.h:77
Gaudi::Utils::AttribStringParser::Iterator::Iterator
Iterator(Iterator &&other)
Definition: AttribStringParser.h:62
GaudiPartProp.decorators.std
std
Definition: decorators.py:32
System.h
Gaudi::Utils::AttribStringParser::Iterator::operator++
Iterator operator++()
Definition: AttribStringParser.h:67
Gaudi::Utils::AttribStringParser::Iterator::Iterator
Iterator(const Iterator &other)
Definition: AttribStringParser.h:61
Gaudi::Utils::AttribStringParser::Iterator::operator==
bool operator==(const Iterator &other)
Definition: AttribStringParser.h:76
Gaudi::Utils::AttribStringParser::Iterator::m_it
boost::sregex_iterator m_it
Wrapped boost::sregex_iterator instance.
Definition: AttribStringParser.h:81
Gaudi::Utils::AttribStringParser::Iterator::difference_type
std::ptrdiff_t difference_type
Definition: AttribStringParser.h:53
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:135
Gaudi::Utils::AttribStringParser::parse
boost::sregex_iterator parse() const
Definition: AttribStringParser.h:129
Gaudi::Units::m
constexpr double m
Definition: SystemOfUnits.h:107
Gaudi::Utils::AttribStringParser::Iterator::iterator_category
std::input_iterator_tag iterator_category
Definition: AttribStringParser.h:51
Gaudi::Utils::AttribStringParser::Iterator::Iterator
Iterator(const boost::sregex_iterator &it, bool expand_vars)
Definition: AttribStringParser.h:58
Gaudi::Utils::AttribStringParser::Attrib
Simple class to wrap tag/value pairs.
Definition: AttribStringParser.h:42
Gaudi::Utils::end
AttribStringParser::Iterator end(const AttribStringParser &)
Definition: AttribStringParser.h:138
Gaudi::Utils::AttribStringParser::m_data
std::string m_data
Definition: AttribStringParser.h:126
Gaudi::Utils::AttribStringParser::Iterator::operator*
reference operator*()
Definition: AttribStringParser.h:72
Gaudi::Utils::AttribStringParser::Iterator
Iterator to loop over the tag/value pairs in the attribute string.
Definition: AttribStringParser.h:49
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:123
Gaudi::Utils::AttribStringParser::Attrib::value
std::string value
Definition: AttribStringParser.h:44
Gaudi::Utils::AttribStringParser::Iterator::m_attrib
Attrib m_attrib
Cached Attrib instance.
Definition: AttribStringParser.h:84
Gaudi::Utils::AttribStringParser::Iterator::Iterator
Iterator()=default
Gaudi::Utils::AttribStringParser::Iterator::operator++
Iterator operator++(int)
Definition: AttribStringParser.h:63
Kernel.h
Gaudi::Utils::AttribStringParser::Iterator::m_expandVars
bool m_expandVars
Definition: AttribStringParser.h:82
Gaudi::Utils::AttribStringParser::m_expandVars
bool m_expandVars
Definition: AttribStringParser.h:127
gaudirun.parser
parser
Definition: gaudirun.py:149
Gaudi::Utils::AttribStringParser::Attrib::tag
std::string tag
Definition: AttribStringParser.h:43
Gaudi::Utils::AttribStringParser
Parse attribute strings allowing iteration over the various attributes.
Definition: AttribStringParser.h:39
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:87
Iterator
boost::spirit::classic::position_iterator2< ForwardIterator > Iterator
Definition: Iterator.h:18