The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
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
27namespace 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;
53 using difference_type = std::ptrdiff_t;
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 }
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
boost::spirit::classic::position_iterator2< ForwardIterator > Iterator
Definition Iterator.h:18
Iterator to loop over the tag/value pairs in the attribute string.
Iterator(const boost::sregex_iterator &it, bool expand_vars)
void i_setAttrib()
Helper method used to update the cached Attrib instance when dereferencing the iterator.
boost::sregex_iterator m_it
Wrapped boost::sregex_iterator instance.
Parse attribute strings allowing iteration over the various attributes.
boost::sregex_iterator parse() const
AttribStringParser(std::string data, bool expand_vars=true)
Initialize the parsing of an attribute string.
friend Iterator begin(const AttribStringParser &)
AttribStringParser::Iterator begin(const AttribStringParser &parser)
AttribStringParser::Iterator end(const AttribStringParser &)
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition __init__.py:1
STL namespace.
Simple class to wrap tag/value pairs.