μHAL (v2.7.9)
Part of the IPbus software repository
XmlParser.hpp
Go to the documentation of this file.
1 /*
2 ---------------------------------------------------------------------------
3 
4  This file is part of uHAL.
5 
6  uHAL is a hardware access library and programming framework
7  originally developed for upgrades of the Level-1 trigger of the CMS
8  experiment at CERN.
9 
10  uHAL is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  uHAL is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with uHAL. If not, see <http://www.gnu.org/licenses/>.
22 
23 
24  Andrew Rose, Imperial College, London
25  email: awr01 <AT> imperial.ac.uk
26 
27  Marc Magrans de Abril, CERN
28  email: marc.magrans.de.abril <AT> cern.ch
29 
30 ---------------------------------------------------------------------------
31 */
32 
39 #ifndef _uhal_XmlParser_hpp_
40 #define _uhal_XmlParser_hpp_
41 
42 #include <string>
43 #include <deque>
44 #include <set>
45 
46 #include "pugixml.hpp"
47 #include "uhal/log/log.hpp"
48 #include "uhal/log/exception.hpp"
49 
50 namespace uhal
51 {
52 
53  namespace exception
54  {
56  UHAL_DEFINE_EXCEPTION_CLASS ( ContradictoryParserRule , "Exception class to handle the case where an attribute is both required and forbidden." )
58  UHAL_DEFINE_EXCEPTION_CLASS ( NoActionSpecified , "Exception class to handle the case where a callback is requested without it being specified." )
60  UHAL_DEFINE_EXCEPTION_CLASS ( TooManyAttributes , "Exception class to handle the case where the parser is asked to handle more than 64 attributes." )
62  UHAL_DEFINE_EXCEPTION_CLASS ( UnknownAttribute , "Exception class to handle the case where an unknown attribute is parsed." )
64  UHAL_DEFINE_EXCEPTION_CLASS ( AmbiguousParserRules , "Exception class to handle the case where two or more equally strict rules are passed." )
66  UHAL_DEFINE_EXCEPTION_CLASS ( NoRulesPassed , "Exception class to handle the case where no rules were parsed." )
67  }
68 
70  template < typename R >
71  class Parser;
72 
74  template < typename R >
76  {
77  public:
81  virtual ~BaseFunctionObject();
87  virtual R operator() ( const pugi::xml_node& aNode ) = 0;
88  };
89 
91  template < typename R , typename T >
93  {
94  public:
99  FunctionObject ( T& aT );
100 
106  R operator() ( const pugi::xml_node& aNode );
107 
108  private:
112  T mT;
113  };
114 
116  template < typename R , typename T >
118  {
119  public:
124  FunctionObject ( T* aT );
125 
131  R operator() ( const pugi::xml_node& aNode );
132 
133  private:
137  T* mT;
138  };
139 
140 
141 
143  template < typename R >
144  class Rule
145  {
147  friend class Parser< R >;
148  public:
150  Rule( );
151 
153  virtual ~Rule();
154 
160  Rule<R>& require ( const std::string& aStr );
161 
167  Rule<R>& forbid ( const std::string& aStr );
168 
174  Rule<R>& optional ( const std::string& aStr );
175 
180  std::string description() const;
181 
182  private:
183 
190  R operator() ( const pugi::xml_node& aNode );
191 
192  private:
194  std::set<std::string> mRequired;
196  std::set<std::string> mForbidden;
198  std::set<std::string> mOptional;
199 
201  uint32_t mRuleId;
203  uint64_t mRequiredHash;
205  uint64_t mForbiddenHash;
206 
209  };
210 
212  template < typename R >
213  class Parser
214  {
215  public:
217  Parser();
218 
220  ~Parser();
221 
227  template < typename T >
228  void addRule ( const Rule<R> & aRule , T aCallbackHandler );
229 
235  R operator() ( const pugi::xml_node& aNode );
236 
237  private:
239  uint64_t mNextHash;
241  boost::unordered_map< std::string , uint64_t > mHashes;
243  std::deque< Rule<R> > mRules;
245  uint32_t mRuleCounter;
246  };
247 
248 }
249 
251 
252 #endif
UHAL_DEFINE_EXCEPTION_CLASS
#define UHAL_DEFINE_EXCEPTION_CLASS(ClassName, ClassDescription)
Definition: exception.hpp:59
uhal::Parser::mRules
std::deque< Rule< R > > mRules
Container for storing rule objects.
Definition: XmlParser.hpp:243
uhal::Rule::mOptional
std::set< std::string > mOptional
The optional attributes for this rule.
Definition: XmlParser.hpp:198
uhal::Rule::mForbidden
std::set< std::string > mForbidden
The forbidden attributes for this rule.
Definition: XmlParser.hpp:196
uhal::FunctionObject
Class for wrapping bound functions and function objects as an object.
Definition: XmlParser.hpp:93
setupTemplate.description
description
Definition: setupTemplate.py:34
uhal::Parser::mHashes
boost::unordered_map< std::string, uint64_t > mHashes
Map of the tags to the one-hot encoded hash.
Definition: XmlParser.hpp:241
uhal::Rule::mRuleId
uint32_t mRuleId
The ID of the rule.
Definition: XmlParser.hpp:201
pugixml.hpp
uhal
Definition: HttpResponseGrammar.hpp:49
uhal::Rule::mFuncPtr
BaseFunctionObject< R > * mFuncPtr
An object wrapping the function pointer for the function to be called when the rule conditions are me...
Definition: XmlParser.hpp:208
uhal::Rule::mRequired
std::set< std::string > mRequired
The required attributes for this rule.
Definition: XmlParser.hpp:194
pugi::xml_node
Definition: pugixml.hpp:455
uhal::Rule
Rule for matching XML attributes.
Definition: XmlParser.hpp:145
uhal::Rule::mRequiredHash
uint64_t mRequiredHash
The hash for required attributes.
Definition: XmlParser.hpp:203
XmlParser.hxx
uhal::BaseFunctionObject
Abstract base class for wrapping function pointers, function objects and bound functions as objects.
Definition: XmlParser.hpp:76
log.hpp
uhal::FunctionObject::mT
T mT
The function object or bound function which will be called when the object is evaluated (bracket oper...
Definition: XmlParser.hpp:112
uhal::Rule::mForbiddenHash
uint64_t mForbiddenHash
The hash for forbidden attributes.
Definition: XmlParser.hpp:205
uhal::Parser
Forward declaration of the Parser so we can declare it friend.
Definition: XmlParser.hpp:71
exception.hpp
uhal::Parser::mNextHash
uint64_t mNextHash
One-hot encoded hash for rules.
Definition: XmlParser.hpp:239
uhal::Parser::mRuleCounter
uint32_t mRuleCounter
Member to track rule numbers for giving each rule a unique ID.
Definition: XmlParser.hpp:245