Belle II Software development
Gearbox.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#pragma once
10
11#include <libxml/xpath.h>
12
13#include <framework/gearbox/Interface.h>
14#include <framework/gearbox/InputHandler.h>
15
16#include <map>
17#include <vector>
18#include <string>
19
20namespace Belle2 {
25 namespace gearbox {
26 void* openXmlUri(const char*);
27 }
28 template <class KEY, class VALUE> class MRUCache;
29
35 public:
37 enum { c_DefaultCacheSize = 200 };
38
40 struct PathValue {
41 PathValue(): numNodes(0), value(""), unit("") {}
45 std::string value;
47 std::string unit;
48 };
49
51 struct PathOverride {
53 std::string path {""};
55 std::string value {""};
57 std::string unit {""};
60 bool multiple {false};
61 };
62
64 ~Gearbox();
65
67 static Gearbox& getInstance();
68
75 void setBackends(const std::vector<std::string>& backends);
76
78 void clearBackends();
79
81 void addOverride(const PathOverride& poverride)
82 {
83 m_overrides.push_back(poverride);
84 }
85
88 {
89 m_overrides.clear();
90 }
91
97 void open(const std::string& name = "Belle2.xml", size_t cacheSize = c_DefaultCacheSize);
98
100 void close();
101
106 bool isOpen() const { return m_xmlDocument != 0; }
107
112 virtual int getNumberNodes(const std::string& path = "") const override
113 {
114 return getPathValue(path).numNodes;
115 }
116
123 virtual std::string getString(const std::string& path = "") const noexcept(false) override
124 {
125 PathValue p = getPathValue(path);
126 if (p.numNodes == 0) throw gearbox::PathEmptyError() << path;
127 return p.value;
128 }
129
138 std::string getString(const std::string& path, const std::string& defaultValue) const
139 {
140 return gearbox::Interface::getString(path, defaultValue);
141 }
142
154 virtual std::pair<std::string, std::string> getStringWithUnit(const std::string& path = "") const noexcept(false) override
155 {
156 PathValue p = getPathValue(path);
157 if (!p.numNodes) throw gearbox::PathEmptyError() << path;
158 return make_pair(p.value, p.unit);
159 }
160
170 virtual const TObject* getTObject(const std::string& path) const noexcept(false) override;
171
177 GearDir getDetectorComponent(const std::string& component);
178
187 static void registerInputHandler(const std::string& prefix, gearbox::InputHandler::Factory* factory)
188 {
189 getInstance().m_registeredHandlers[prefix] = factory;
190 }
191
192 private:
194 Gearbox();
196 Gearbox(const Gearbox&) = delete;
198 Gearbox& operator=(const Gearbox&) = delete;
199
201 gearbox::InputContext* openXmlUri(const std::string& uri) const;
202
204 void overridePathValue(const PathOverride& poverride);
206 PathValue getPathValue(const std::string& path) const;
207
209 xmlDocPtr m_xmlDocument;
211 xmlXPathContextPtr m_xpathContext;
215 mutable std::map<std::string, TObject*> m_ownedObjects;
216
218 std::vector<gearbox::InputHandler*> m_handlers;
220 std::map<std::string, gearbox::InputHandler::Factory*> m_registeredHandlers;
221
223 std::vector<PathOverride> m_overrides;
224
232 friend void* gearbox::openXmlUri(const char*);
233 };
234
236 template<class T> struct InputHandlerFactory {
238 explicit InputHandlerFactory(const std::string& prefix)
239 {
241 }
243 static gearbox::InputHandler* factory(const std::string& uri)
244 {
245 return new T(uri);
246 }
247 };
248
256#define B2_GEARBOX_REGISTER_INPUTHANDLER(classname,prefix)\
257 InputHandlerFactory<classname> Gearbox_InputHandlerFactory_##classname(prefix)
259}
GearDir is the basic class used for accessing the parameter store.
Definition: GearDir.h:31
Singleton class responsible for loading detector parameters from an XML file.
Definition: Gearbox.h:34
Gearbox & operator=(const Gearbox &)=delete
Also no assignment operator.
bool isOpen() const
Return the state of the Gearbox.
Definition: Gearbox.h:106
static void registerInputHandler(const std::string &prefix, gearbox::InputHandler::Factory *factory)
Register a new input handler.
Definition: Gearbox.h:187
void clearOverrides()
Clear all existing overrides.
Definition: Gearbox.h:87
std::map< std::string, TObject * > m_ownedObjects
Map of queried objects (path -> TObject*).
Definition: Gearbox.h:215
std::map< std::string, gearbox::InputHandler::Factory * > m_registeredHandlers
Map of registered InputHandlers.
Definition: Gearbox.h:220
std::string getString(const std::string &path, const std::string &defaultValue) const
Get the parameter path as a string.
Definition: Gearbox.h:138
Gearbox(const Gearbox &)=delete
Singleton: private copy constructor.
xmlDocPtr m_xmlDocument
Pointer to the libxml Document structure.
Definition: Gearbox.h:209
friend void * gearbox::openXmlUri(const char *)
friend to internal c-like function to interface libxml2 callback
MRUCache< std::string, PathValue > * m_parameterCache
Cache for already queried paths.
Definition: Gearbox.h:213
void addOverride(const PathOverride &poverride)
Add an override for a given XPath expression.
Definition: Gearbox.h:81
virtual int getNumberNodes(const std::string &path="") const override
Return the number of nodes a given path will expand to.
Definition: Gearbox.h:112
virtual std::string getString(const std::string &path="") const noexcept(false) override
Get the parameter path as a string.
Definition: Gearbox.h:123
std::vector< PathOverride > m_overrides
the existing overrides
Definition: Gearbox.h:223
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:154
std::vector< gearbox::InputHandler * > m_handlers
List of input handlers which will be used to find resources.
Definition: Gearbox.h:218
xmlXPathContextPtr m_xpathContext
Pointer to the libxml XPath context.
Definition: Gearbox.h:211
Class implementing a generic Most Recently Used cache.
Definition: MRUCache.h:48
Class representing a resource context for gearbox.
Definition: InputHandler.h:25
Class to provide an InputContext for a given XML resource name.
Definition: InputHandler.h:47
InputHandler * Factory(const std::string &uri)
Factory function which takes a backend uri and returns an InputHandler instance.
Definition: InputHandler.h:53
Exception to be thrown in case of an empty result.
Definition: Interface.h:35
virtual std::string getString(const std::string &path="") const noexcept(false)=0
Get the parameter path as a string.
static Gearbox & getInstance()
Return reference to the Gearbox instance.
Definition: Gearbox.cc:81
virtual const TObject * getTObject(const std::string &path) const noexcept(false) override
Get the parameter path as a TObject.
Definition: Gearbox.cc:295
~Gearbox()
Free structures on destruction.
Definition: Gearbox.cc:74
Gearbox()
Singleton: private constructor.
Definition: Gearbox.cc:67
void overridePathValue(const PathOverride &poverride)
Change the value of a given path expression.
Definition: Gearbox.cc:184
PathValue getPathValue(const std::string &path) const
Return the (cached) value of a given path.
Definition: Gearbox.cc:247
void clearBackends()
Clear list of backends.
Definition: Gearbox.cc:127
void close()
Free internal structures of previously parsed tree and clear cache.
Definition: Gearbox.cc:169
void setBackends(const std::vector< std::string > &backends)
Select the backends to use to find resources.
Definition: Gearbox.cc:99
void open(const std::string &name="Belle2.xml", size_t cacheSize=c_DefaultCacheSize)
Open connection to backend and parse tree.
Definition: Gearbox.cc:133
GearDir getDetectorComponent(const std::string &component)
Return GearDir representing a given DetectorComponent.
Definition: Gearbox.cc:314
Abstract base class for different kinds of events.
Struct to override a path in the XML file with a custom value.
Definition: Gearbox.h:51
std::string unit
new Unit
Definition: Gearbox.h:57
bool multiple
if true, override all nodes when more than one node matches the XPath expression, bail otherwise
Definition: Gearbox.h:60
std::string path
XPath expression of the path to override.
Definition: Gearbox.h:53
std::string value
New value.
Definition: Gearbox.h:55
Struct for caching results from the xml file.
Definition: Gearbox.h:40
std::string unit
unit attribute of the first node if present, otherwise ""
Definition: Gearbox.h:47
std::string value
value of the first node if present, otherwise ""
Definition: Gearbox.h:45
int numNodes
number of nodes corresponding to the path
Definition: Gearbox.h:43
Helper class to easily register new input handlers.
Definition: Gearbox.h:236
InputHandlerFactory(const std::string &prefix)
constructor, used by B2_GEARBOX_REGISTER_INPUTHANDLER macro.
Definition: Gearbox.h:238
static gearbox::InputHandler * factory(const std::string &uri)
create a new InputHandler of type T for given URI.
Definition: Gearbox.h:243