ATLAS Offline Software
Loading...
Searching...
No Matches
DataStructure.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
5#ifndef TRIGCONFDATA_DATASTRUCTURE_H
6#define TRIGCONFDATA_DATASTRUCTURE_H
7
11
12#include <iostream>
13#include <vector>
14#include <memory>
15#include <type_traits>
16#include <optional>
17#include "boost/property_tree/ptree.hpp"
18
19namespace TrigConf {
20
38 public:
39
40 using ptree = boost::property_tree::ptree;
41
44
45 DataStructure(const DataStructure&) = default;
47
50
54 DataStructure(const ptree & data);
55 DataStructure(const std::string & name, const ptree & data);
57 DataStructure(const std::string & name, ptree && data);
58
60 virtual ~DataStructure() = default;
61
63 void setData(const ptree & data);
64 void setData(ptree && data);
65
67 void setName(const std::string& n);
68
70 virtual std::string className() const;
71
72 virtual const std::string & name() const final;
73
80 virtual void clear();
81
83 const ptree & data() const {
84 if( ! isInitialized() ) {
85 throw std::runtime_error("Trying to access data of uninitialized object of type " + className());
86 }
87 // Don't use ?: operator here: it confuses cppcheck.
88 if (ownsData()) {
89 return *m_dataSPtr.get();
90 }
91 return *m_dataPtr;
92 }
93
97 bool isValue() const;
98
105 std::string getValue() const; // this will be removed in the next MR (TrigSignatureMoniMT still depends on it)
106
107 template<class T>
108 T getValue() const {
109 return data().get_value<T>();
110 }
111
115 template<class T>
116 std::optional<T> getValue_optional() const {
117 auto v = data().get_value_optional<T>();
118 return v ? std::optional<T>(std::move(*v)) : std::nullopt;
119 }
120
125 bool hasAttribute(const std::string & key) const;
126
134 bool isNull(const std::string & key) const;
135
140 bool hasChild(const std::string & path) const;
141
145 std::string operator[](const std::string & key) const;
146
151 template<class T>
152 T getAttribute(const std::string & key, bool ignoreIfMissing = false, const T & def = T()) const {
153 const auto & obj = data().get_child_optional(key);
154 if( !obj ) {
155 if( ignoreIfMissing ) {
156 return def;
157 } else {
158 throw std::runtime_error(className() + "#" + name() + ": structure '" + key + "' does not exist" );
159 }
160 }
161 return obj.get().get_value<T>();
162 }
163
164 template<class T>
165 std::optional<T> getAttribute_optional(const std::string & key) const {
166 const auto & obj = data().get_child_optional(key);
167 if( ! obj ) {
168 return std::nullopt;
169 }
170 auto v = obj.get().get_value_optional<T>();
171 return v ? std::optional(std::move(*v)) : std::nullopt;
172 }
173
174 const std::string & getAttribute(const std::string & key, bool ignoreIfMissing = false, const std::string & def = "") const;
175
183 std::vector<DataStructure> getList(const std::string & pathToChild, bool ignoreIfMissing = false) const;
184
185 std::optional<std::vector<DataStructure> > getList_optional(const std::string & pathToChild) const;
186
200 DataStructure getObject(const std::string & pathToChild, bool ignoreIfMissing = false) const;
201
202 std::optional<TrigConf::DataStructure>
203 getObject_optional(const std::string & pathToChild) const;
204
205
210 std::vector<std::string> getKeys() const;
211
212
214 explicit operator bool() const { return m_initialized; }
215 bool isValid() const { return m_initialized; }
216 bool isInitialized() const { return m_initialized; }
217
219 bool empty() const { return data().empty(); }
220
221 /* Print this object including children
222 * @param os The output stream
223 */
224 void printRaw(std::ostream & os = std::cout) const;
225
226 /* Print this object including children
227 * @param os The output stream
228 */
229 virtual void print(std::ostream & os = std::cout) const;
230
237 static void printElement(const std::string& key,
238 const ptree & data,
239 uint level = 0,
240 std::ostream & os = std::cout);
241
242 bool ownsData() const {
243 return (bool)m_dataSPtr;
244 }
245
246 protected:
247
252 virtual void update() {};
253
254 bool m_initialized { false };
255
256 std::shared_ptr<ptree> m_dataSPtr { nullptr }; // used when owning the tree
257 const ptree * m_dataPtr { nullptr }; // used when not owning the tree
258
259 std::string m_name{""}; // most objects are named
260
261 };
262
263}
264
265#ifndef TRIGCONF_STANDALONE
266#ifndef XAOD_STANDALONE
267
268#include "AthenaKernel/CLASS_DEF.h"
269CLASS_DEF( TrigConf::DataStructure , 98904516 , 1 )
270
271#include "AthenaKernel/CondCont.h"
273
274#endif
275#endif
276
277
278#endif
#define CONDCONT_DEF(...)
Definition CondCont.h:1413
macros to associate a CLID to a type
#define CLASS_DEF(NAME, CID, VERSION)
associate a clid and a version to a type eg
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11
boost::property_tree::ptree ptree
unsigned int uint
Base class for Trigger configuration data and wrapper around underlying representation.
std::optional< TrigConf::DataStructure > getObject_optional(const std::string &pathToChild) const
void setName(const std::string &n)
Setting the configuration element name.
std::shared_ptr< ptree > m_dataSPtr
virtual const std::string & name() const final
std::vector< DataStructure > getList(const std::string &pathToChild, bool ignoreIfMissing=false) const
Access to array structure.
bool m_initialized
if initialized, the underlying ptree is has been assigned to (can be empty)
const ptree & data() const
Access to the underlying data, if needed.
bool hasAttribute(const std::string &key) const
Check for attribute.
std::optional< std::vector< DataStructure > > getList_optional(const std::string &pathToChild) const
DataStructure()
Default constructor, leading to an uninitialized configuration object.
void setData(const ptree &data)
Setting the configuration data.
virtual void clear()
Clearing the configuration data.
bool empty() const
Check if children exist.
DataStructure & operator=(const DataStructure &)=default
std::optional< T > getAttribute_optional(const std::string &key) const
std::vector< std::string > getKeys() const
Access to the keys of an DataStructure which presents a dictionary.
DataStructure(DataStructure &&)=default
virtual std::string className() const
A string that is the name of the class.
std::string getValue() const
Access to simple content.
T getAttribute(const std::string &key, bool ignoreIfMissing=false, const T &def=T()) const
Access to simple attribute.
bool isNull(const std::string &key) const
Check if an attribute is null.
void printRaw(std::ostream &os=std::cout) const
virtual void update()
Update the internal data after modification of the data object.
static void printElement(const std::string &key, const ptree &data, uint level=0, std::ostream &os=std::cout)
Static function to print a ptree object.
bool hasChild(const std::string &path) const
Check if child exists.
DataStructure getObject(const std::string &pathToChild, bool ignoreIfMissing=false) const
Access to configuration object.
std::string operator[](const std::string &key) const
Access to simple attribute.
virtual ~DataStructure()=default
Destructor.
boost::property_tree::ptree ptree
std::optional< T > getValue_optional() const
access to content of the note Will return false if the value could not be converted into T
DataStructure & operator=(DataStructure &&)=default
bool isValue() const
Check for attribute.
virtual void print(std::ostream &os=std::cout) const
DataStructure(const DataStructure &)=default
Forward iterator to traverse the main components of the trigger configuration.
Definition Config.h:22