Belle II Software  release-05-02-19
Gearbox.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010-2011 Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Martin Ritter *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 
13 #include <libxml/xpath.h>
14 
15 #include <framework/gearbox/Interface.h>
16 #include <framework/gearbox/InputHandler.h>
17 
18 #include <map>
19 #include <vector>
20 #include <string>
21 
22 namespace Belle2 {
27  namespace gearbox {
28  void* openXmlUri(const char*);
29  }
30  template <class KEY, class VALUE> class MRUCache;
31 
36  class Gearbox: public gearbox::Interface {
37  public:
39  enum { c_DefaultCacheSize = 200 };
40 
42  struct PathValue {
43  PathValue(): numNodes(0), value(""), unit("") {}
45  int numNodes;
47  std::string value;
49  std::string unit;
50  };
51 
53  struct PathOverride {
55  std::string path {""};
57  std::string value {""};
59  std::string unit {""};
62  bool multiple {false};
63  };
64 
66  ~Gearbox();
67 
69  static Gearbox& getInstance();
70 
77  void setBackends(const std::vector<std::string>& backends);
78 
80  void clearBackends();
81 
83  void addOverride(const PathOverride& poverride)
84  {
85  m_overrides.push_back(poverride);
86  }
87 
89  void clearOverrides()
90  {
91  m_overrides.clear();
92  }
93 
99  void open(const std::string& name = "Belle2.xml", size_t cacheSize = c_DefaultCacheSize);
100 
102  void close();
103 
108  bool isOpen() const { return m_xmlDocument != 0; }
109 
114  virtual int getNumberNodes(const std::string& path = "") const override
115  {
116  return getPathValue(path).numNodes;
117  }
118 
125  virtual std::string getString(const std::string& path = "") const noexcept(false) override
126  {
127  PathValue p = getPathValue(path);
128  if (p.numNodes == 0) throw gearbox::PathEmptyError() << path;
129  return p.value;
130  }
131 
140  std::string getString(const std::string& path, const std::string& defaultValue) const
141  {
142  return gearbox::Interface::getString(path, defaultValue);
143  }
144 
156  virtual std::pair<std::string, std::string> getStringWithUnit(const std::string& path = "") const noexcept(false) override
157  {
158  PathValue p = getPathValue(path);
159  if (!p.numNodes) throw gearbox::PathEmptyError() << path;
160  return make_pair(p.value, p.unit);
161  }
162 
172  virtual const TObject* getTObject(const std::string& path) const noexcept(false) override;
173 
179  GearDir getDetectorComponent(const std::string& component);
180 
189  static void registerInputHandler(const std::string& prefix, gearbox::InputHandler::Factory* factory)
190  {
191  getInstance().m_registeredHandlers[prefix] = factory;
192  }
193 
194  private:
196  Gearbox();
198  Gearbox(const Gearbox&) = delete;
200  Gearbox& operator=(const Gearbox&) = delete;
201 
203  gearbox::InputContext* openXmlUri(const std::string& uri) const;
204 
206  void overridePathValue(const PathOverride& poverride);
208  PathValue getPathValue(const std::string& path) const;
209 
211  xmlDocPtr m_xmlDocument;
213  xmlXPathContextPtr m_xpathContext;
215  mutable MRUCache<std::string, PathValue>* m_parameterCache;
217  mutable std::map<std::string, TObject*> m_ownedObjects;
218 
220  std::vector<gearbox::InputHandler*> m_handlers;
222  std::map<std::string, gearbox::InputHandler::Factory*> m_registeredHandlers;
223 
225  std::vector<PathOverride> m_overrides;
226 
234  friend void* gearbox::openXmlUri(const char*);
235  };
236 
238  template<class T> struct InputHandlerFactory {
240  explicit InputHandlerFactory(const std::string& prefix)
241  {
243  }
245  static gearbox::InputHandler* factory(const std::string& uri)
246  {
247  return new T(uri);
248  }
249  };
250 
258 #define B2_GEARBOX_REGISTER_INPUTHANDLER(classname,prefix)\
259  InputHandlerFactory<classname> Gearbox_InputHandlerFactory_##classname(prefix)
260 
261 }
Belle2::Gearbox::getDetectorComponent
GearDir getDetectorComponent(const std::string &component)
Return GearDir representing a given DetectorComponent.
Definition: Gearbox.cc:307
Belle2::Gearbox::m_handlers
std::vector< gearbox::InputHandler * > m_handlers
List of input handlers which will be used to find resources.
Definition: Gearbox.h:228
Belle2::Gearbox::getStringWithUnit
virtual std::pair< std::string, std::string > getStringWithUnit(const std::string &path="") const noexcept(false) override
Get the parameter path as string and also return the unit it was defined with.
Definition: Gearbox.h:164
Belle2::Gearbox::PathOverride::value
std::string value
New value.
Definition: Gearbox.h:65
Belle2::Gearbox::PathValue::numNodes
int numNodes
number of nodes corresponding to the path
Definition: Gearbox.h:53
Belle2::Gearbox::clearOverrides
void clearOverrides()
Clear all existing overrides.
Definition: Gearbox.h:97
Belle2::InputHandlerFactory::InputHandlerFactory
InputHandlerFactory(const std::string &prefix)
constructor, used by B2_GEARBOX_REGISTER_INPUTHANDLER macro.
Definition: Gearbox.h:248
Belle2::Gearbox::getTObject
virtual const TObject * getTObject(const std::string &path) const noexcept(false) override
Get the parameter path as a TObject.
Definition: Gearbox.cc:288
Belle2::Gearbox::getInstance
static Gearbox & getInstance()
Return reference to the Gearbox instance.
Definition: Gearbox.cc:74
Belle2::Gearbox::clearBackends
void clearBackends()
Clear list of backends.
Definition: Gearbox.cc:120
Belle2::Gearbox::m_overrides
std::vector< PathOverride > m_overrides
the existing overrides
Definition: Gearbox.h:233
Belle2::Gearbox::getNumberNodes
virtual int getNumberNodes(const std::string &path="") const override
Return the number of nodes a given path will expand to.
Definition: Gearbox.h:122
Belle2::Gearbox::m_registeredHandlers
std::map< std::string, gearbox::InputHandler::Factory * > m_registeredHandlers
Map of registered InputHandlers.
Definition: Gearbox.h:230
Belle2::gearbox::Interface::getString
virtual std::string getString(const std::string &path="") const noexcept(false)=0
Get the parameter path as a string.
Belle2::gearbox::InputHandler::Factory
InputHandler * Factory(const std::string &uri)
Factory function which takes a backend uri and returns an InputHandler instance.
Definition: InputHandler.h:63
Belle2::Gearbox::PathOverride::multiple
bool multiple
if true, override all nodes when more than one node matches the XPath expression, bail otherwise
Definition: Gearbox.h:70
Belle2::Gearbox::PathValue::unit
std::string unit
unit attribute of the first node if present, otherwise ""
Definition: Gearbox.h:57
Belle2::Gearbox::openXmlUri
friend void * gearbox::openXmlUri(const char *)
friend to internal c-like function to interface libxml2 callback
Belle2::Gearbox::PathOverride
Struct to override a path in the XML file with a custom value.
Definition: Gearbox.h:61
Belle2::Gearbox::close
void close()
Free internal structures of previously parsed tree and clear cache.
Definition: Gearbox.cc:162
Belle2::Gearbox::operator=
Gearbox & operator=(const Gearbox &)=delete
Also no assignment operator.
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::Gearbox
Singleton class responsible for loading detector parameters from an XML file.
Definition: Gearbox.h:44
Belle2::Gearbox::getString
virtual std::string getString(const std::string &path="") const noexcept(false) override
Get the parameter path as a string.
Definition: Gearbox.h:133
Belle2::Gearbox::addOverride
void addOverride(const PathOverride &poverride)
Add an override for a given XPath expression.
Definition: Gearbox.h:91
Belle2::Gearbox::m_xmlDocument
xmlDocPtr m_xmlDocument
Pointer to the libxml Document structure.
Definition: Gearbox.h:219
Belle2::Gearbox::m_ownedObjects
std::map< std::string, TObject * > m_ownedObjects
Map of queried objects (path -> TObject*).
Definition: Gearbox.h:225
Belle2::Gearbox::PathValue::value
std::string value
value of the first node if present, otherwise ""
Definition: Gearbox.h:55
Belle2::InputHandlerFactory
Helper class to easily register new input handlers.
Definition: Gearbox.h:246
Belle2::Gearbox::PathOverride::path
std::string path
XPath expression of the path to override.
Definition: Gearbox.h:63
Belle2::Gearbox::m_xpathContext
xmlXPathContextPtr m_xpathContext
Pointer to the libxml XPath context.
Definition: Gearbox.h:221
Belle2::Gearbox::Gearbox
Gearbox()
Singleton: private constructor.
Definition: Gearbox.cc:60
Belle2::Gearbox::m_parameterCache
MRUCache< std::string, PathValue > * m_parameterCache
Cache for already queried paths.
Definition: Gearbox.h:223
Belle2::Gearbox::~Gearbox
~Gearbox()
Free structures on destruction.
Definition: Gearbox.cc:67
Belle2::Gearbox::getPathValue
PathValue getPathValue(const std::string &path) const
Return the (cached) value of a given path.
Definition: Gearbox.cc:240
Belle2::Gearbox::registerInputHandler
static void registerInputHandler(const std::string &prefix, gearbox::InputHandler::Factory *factory)
Register a new input handler.
Definition: Gearbox.h:197
Belle2::Gearbox::open
void open(const std::string &name="Belle2.xml", size_t cacheSize=c_DefaultCacheSize)
Open connection to backend and parse tree.
Definition: Gearbox.cc:126
Belle2::Gearbox::openXmlUri
gearbox::InputContext * openXmlUri(const std::string &uri) const
Function to be called when libxml requests a new input uri to be opened.
Definition: Gearbox.cc:80
Belle2::Gearbox::overridePathValue
void overridePathValue(const PathOverride &poverride)
Change the value of a given path expression.
Definition: Gearbox.cc:177
Belle2::Gearbox::PathOverride::unit
std::string unit
new Unit
Definition: Gearbox.h:67
Belle2::InputHandlerFactory::factory
static gearbox::InputHandler * factory(const std::string &uri)
create a new InputHandler of type T for given URI.
Definition: Gearbox.h:253
Belle2::Gearbox::setBackends
void setBackends(const std::vector< std::string > &backends)
Select the backends to use to find resources.
Definition: Gearbox.cc:92
Belle2::Gearbox::isOpen
bool isOpen() const
Return the state of the Gearbox.
Definition: Gearbox.h:116