Registry.h
Go to the documentation of this file.
1 //____________________________________________________________________________
2 /*!
3 
4 \class genie::Registry
5 
6 \brief A registry. Provides the container for algorithm configuration
7  parameters.
8 
9 \author Costas Andreopoulos <constantinos.andreopoulos \at cern.ch>
10  University of Liverpool & STFC Rutherford Appleton Laboratory
11 
12 \created May 04, 2004
13 
14 \cpright Copyright (c) 2003-2020, The GENIE Collaboration
15  For the full text of the license visit http://copyright.genie-mc.org
16 */
17 //____________________________________________________________________________
18 
19 #ifndef _REGISTRY_H_
20 #define _REGISTRY_H_
21 
22 #include <map>
23 #include <vector>
24 #include <string>
25 #include <iostream>
26 
29 
30 class TH1F;
31 class TH2F;
32 class TTree;
33 class TFolder;
34 
35 using std::map;
36 using std::vector;
37 using std::pair;
38 using std::string;
39 using std::ostream;
40 
41 namespace genie {
42 
43 // Type definitions
44 //
45 typedef map <RgKey, RegistryItemI *> RgIMap;
46 typedef pair<RgKey, RegistryItemI *> RgIMapPair;
47 typedef map <RgKey, RegistryItemI *>::size_type RgIMapSizeType;
50 typedef vector<RgKey> RgKeyList;
51 
52 // Templated utility methods to set/get registry items
53 //
54 class Registry;
55 template<class T>
56  void SetRegistryItem(Registry * r, RgKey key, T item);
57 template<class T>
59  Registry * r, RgKey key, T def, bool set_def=true);
60 
61 //
62 //
63 ostream & operator << (ostream & stream, const Registry & registry);
64 
65 class Registry {
66 
67 public:
68  // Ctor's & dtor
69  //
70  Registry();
71  Registry(string name, bool isReadOnly = true);
72  Registry(const Registry &);
73  virtual ~Registry();
74 
75  // Overloaded registry operators (<<, (), = , +=)
76  //
77  friend ostream & operator << (ostream & stream, const Registry & registry);
78 
79  Registry & operator = (const Registry & reg);
80  Registry & operator += (const Registry & reg);
81 
82  void operator () (RgKey key, int item);
83  void operator () (RgKey key, bool item);
84  void operator () (RgKey key, double item);
85  void operator () (RgKey key, const char * item);
86  void operator () (RgKey key, string item);
87 
88  // Registry & registry item locks
89  //
90  void Lock (void); ///< locks the registry
91  void UnLock (void); ///< unlocks the registry (doesn't unlock items)
92  bool IsLocked (void) const; ///< checks registry lock
93  void InhibitItemLocks (void); ///< override individual item locks
94  void RestoreItemLocks (void); ///< restore individual item locks
95  bool ItemLocksAreActive (void) const; ///< check if item locks are active
96  void LockItem (RgKey key); ///< locks the registry item
97  void UnLockItem (RgKey key); ///< unlocks the registry item
98  bool ItemIsLocked (RgKey key) const; ///< check item lock
99  bool ItemIsLocal (RgKey key) const; ///< local or global?
100  void OverrideGlobalDef (RgKey key); ///< let item override global default (i.e. a 'local' item)
101  void LinkToGlobalDef (RgKey key); ///< link its value to a global default (i.e. a 'global' item)
102 
103  // Methods to set/retrieve Registry values
104  //
105  void Set (RgIMapPair entry);
106  void Set (RgKey key, RgBool item);
107  void Set (RgKey key, RgInt item);
108  void Set (RgKey key, RgDbl item);
109  void Set (RgKey key, RgStr item);
110  void Set (RgKey key, RgAlg item);
111  void Set (RgKey key, RgCChAr item);
112  void Set (RgKey key, RgH1F item);
113  void Set (RgKey key, RgH2F item);
114  void Set (RgKey key, RgTree item);
115 
116  void Get (RgKey key, const RegistryItemI * & item) const;
117  void Get (RgKey key, RgBool & item) const;
118  void Get (RgKey key, RgInt & item) const;
119  void Get (RgKey key, RgDbl & item) const;
120  void Get (RgKey key, RgStr & item) const;
121  void Get (RgKey key, RgAlg & item) const;
122  void Get (RgKey key, RgH1F & item) const;
123  void Get (RgKey key, RgH2F & item) const;
124  void Get (RgKey key, RgTree & item) const;
125 
126  RgBool GetBool (RgKey key) const;
127  RgInt GetInt (RgKey key) const;
128  RgDbl GetDouble (RgKey key) const;
129  RgStr GetString (RgKey key) const;
130  RgAlg GetAlg (RgKey key) const;
131  RgH1F GetH1F (RgKey key) const;
132  RgH2F GetH2F (RgKey key) const;
133  RgTree GetTree (RgKey key) const;
134 
135  RgBool GetBoolDef (RgKey key, RgBool def_opt, bool set_def=true);
136  RgInt GetIntDef (RgKey key, RgInt def_opt, bool set_def=true);
137  RgDbl GetDoubleDef (RgKey key, RgDbl def_opt, bool set_def=true);
138  RgStr GetStringDef (RgKey key, RgStr def_opt, bool set_def=true);
139  RgAlg GetAlgDef (RgKey key, RgAlg def_opt, bool set_def=true);
140 
141  RgIMapConstIter SafeFind (RgKey key) const;
142 
143  int NEntries (void) const; ///< get number of items
144  bool Exists (RgKey key) const; ///< item with input key exists?
145  bool CanSetItem (RgKey key) const; ///< can I set the specifed item?
146  bool DeleteEntry (RgKey key); ///< delete the spcified item
147  void SetName (string name); ///< set the registry name
148  string Name (void) const; ///< get the registry name
149  void Print (ostream & stream) const; ///< print the registry to stream
150  void Copy (const Registry &); ///< copy the input registry
151  void Append (const Registry &, RgKey pfx=""); ///< append the input registry. Entries already in the registry are not updated
152  void Merge (const Registry &, RgKey pfx=""); ///< append the input registry. Entries already in the registry are updated
153  void Clear (bool force = false); ///< clear the registry
154  void Init (void); ///< initialize the registry
155 
156  RgType_t ItemType (RgKey key) const; ///< return item type
157  RgKeyList FindKeys (RgKey key_part) const; ///< create list with all keys containing 'key_part'
158 
159  // Access key->item map
160  //
161  const RgIMap & GetItemMap(void) const { return fRegistry; }
162 
163  // Convert to TFolder (this is the primary mechanism for saving the
164  // GENIE configuration in a ROOT file, along with its generated events)
165  //
166  void CopyToFolder (TFolder * folder) const;
167 
168  // Assert the existence or registry items
169  //
170  void AssertExistence (RgKey key0) const;
171  void AssertExistence (RgKey key0, RgKey key1) const;
172  void AssertExistence (RgKey key0, RgKey key1, RgKey key2) const;
173 
174 private:
175 
176  RegistryItemI * CloneRegistryItem( const RgKey & key ) const ; ///< Properly clone a registry Item according to its type
177 
178  // Registry's private data members
179  //
180  string fName; ///< registry's name
181  bool fIsReadOnly; ///< is read only?
182  bool fInhibitItemLocks; ///<
183  RgIMap fRegistry; ///< 'key' -> 'value' map
184 };
185 
186 } // genie namespace
187 
188 #endif // _REGISTRY_H_
static QCString name
Definition: declinfo.cpp:673
intermediate_table::iterator iterator
bool fIsReadOnly
is read only?
Definition: Registry.h:181
RgDbl GetDoubleDef(RgKey key, RgDbl def_opt, bool set_def=true)
Definition: Registry.cxx:535
const char * RgCChAr
QList< Entry > entry
bool IsLocked(void) const
checks registry lock
Definition: Registry.cxx:158
THE MAIN GENIE PROJECT NAMESPACE
Definition: AlgCmp.h:25
friend ostream & operator<<(ostream &stream, const Registry &registry)
Definition: Registry.cxx:77
std::string string
Definition: nybbler.cc:12
bool RgBool
int RgInt
struct vector vector
Registry & operator=(const Registry &reg)
Definition: Registry.cxx:136
T GetValueOrUseDefault(Registry *r, RgKey key, T def, bool set_def)
Definition: Registry.cxx:51
bool DeleteEntry(RgKey key)
delete the spcified item
Definition: Registry.cxx:569
void Append(const Registry &, RgKey pfx="")
append the input registry. Entries already in the registry are not updated
Definition: Registry.cxx:743
intermediate_table::const_iterator const_iterator
RgDbl GetDouble(RgKey key) const
Definition: Registry.cxx:474
bool ItemLocksAreActive(void) const
check if item locks are active
Definition: Registry.cxx:173
void InhibitItemLocks(void)
override individual item locks
Definition: Registry.cxx:163
void Print(ostream &stream) const
print the registry to stream
Definition: Registry.cxx:679
RgInt GetInt(RgKey key) const
Definition: Registry.cxx:467
void LockItem(RgKey key)
locks the registry item
Definition: Registry.cxx:234
void SetRegistryItem(Registry *r, RgKey key, T item)
Definition: Registry.cxx:39
Registry item pABC.
Definition: RegistryItemI.h:29
TH2F * RgH2F
string Name(void) const
get the registry name
Definition: Registry.cxx:597
void Copy(const Registry &)
copy the input registry
Definition: Registry.cxx:722
RgH1F GetH1F(RgKey key) const
Definition: Registry.cxx:495
enum genie::ERgType RgType_t
RgType_t ItemType(RgKey key) const
return item type
Definition: Registry.cxx:829
void Get(RgKey key, const RegistryItemI *&item) const
Definition: Registry.cxx:325
RgKeyList FindKeys(RgKey key_part) const
create list with all keys containing &#39;key_part&#39;
Definition: Registry.cxx:840
void Init(void)
initialize the registry
Definition: Registry.cxx:855
map< RgKey, RegistryItemI * >::const_iterator RgIMapConstIter
Definition: Registry.h:49
const RgIMap & GetItemMap(void) const
Definition: Registry.h:161
def key(type, name=None)
Definition: graph.py:13
bool ItemIsLocal(RgKey key) const
local or global?
Definition: Registry.cxx:178
RgIMap fRegistry
&#39;key&#39; -> &#39;value&#39; map
Definition: Registry.h:183
string RgStr
RgInt GetIntDef(RgKey key, RgInt def_opt, bool set_def=true)
Definition: Registry.cxx:530
void Lock(void)
locks the registry
Definition: Registry.cxx:148
void SetName(string name)
set the registry name
Definition: Registry.cxx:588
void AssertExistence(RgKey key0) const
Definition: Registry.cxx:602
RgIMapConstIter SafeFind(RgKey key) const
Definition: Registry.cxx:550
pair< RgKey, RegistryItemI * > RgIMapPair
Definition: Registry.h:46
bool fInhibitItemLocks
Definition: Registry.h:182
vector< RgKey > RgKeyList
Definition: Registry.h:50
map< RgKey, RegistryItemI * >::size_type RgIMapSizeType
Definition: Registry.h:47
void UnLock(void)
unlocks the registry (doesn&#39;t unlock items)
Definition: Registry.cxx:153
RgStr GetString(RgKey key) const
Definition: Registry.cxx:481
void RestoreItemLocks(void)
restore individual item locks
Definition: Registry.cxx:168
RgStr GetStringDef(RgKey key, RgStr def_opt, bool set_def=true)
Definition: Registry.cxx:540
void LinkToGlobalDef(RgKey key)
link its value to a global default (i.e. a &#39;global&#39; item)
Definition: Registry.cxx:206
string RgKey
ostream & operator<<(ostream &stream, const AlgConfigPool &config_pool)
A registry. Provides the container for algorithm configuration parameters.
Definition: Registry.h:65
TH1F * RgH1F
RgBool GetBool(RgKey key) const
Definition: Registry.cxx:460
map< RgKey, RegistryItemI * >::iterator RgIMapIter
Definition: Registry.h:48
RgTree GetTree(RgKey key) const
Definition: Registry.cxx:515
void OverrideGlobalDef(RgKey key)
let item override global default (i.e. a &#39;local&#39; item)
Definition: Registry.cxx:194
void UnLockItem(RgKey key)
unlocks the registry item
Definition: Registry.cxx:245
RgH2F GetH2F(RgKey key) const
Definition: Registry.cxx:505
virtual ~Registry()
Definition: Registry.cxx:105
bool Exists(RgKey key) const
item with input key exists?
Definition: Registry.cxx:563
string fName
registry&#39;s name
Definition: Registry.h:180
RgAlg GetAlgDef(RgKey key, RgAlg def_opt, bool set_def=true)
Definition: Registry.cxx:545
bool CanSetItem(RgKey key) const
can I set the specifed item?
Definition: Registry.cxx:256
void Clear(bool force=false)
clear the registry
Definition: Registry.cxx:864
void CopyToFolder(TFolder *folder) const
Definition: Registry.cxx:626
double RgDbl
void Set(RgIMapPair entry)
Definition: Registry.cxx:267
Registry & operator+=(const Registry &reg)
Definition: Registry.cxx:142
void Merge(const Registry &, RgKey pfx="")
append the input registry. Entries already in the registry are updated
Definition: Registry.cxx:787
RgBool GetBoolDef(RgKey key, RgBool def_opt, bool set_def=true)
Definition: Registry.cxx:525
bool ItemIsLocked(RgKey key) const
check item lock
Definition: Registry.cxx:218
RegistryItemI * CloneRegistryItem(const RgKey &key) const
Properly clone a registry Item according to its type.
Definition: Registry.cxx:890
RgAlg GetAlg(RgKey key) const
Definition: Registry.cxx:488
void operator()(RgKey key, int item)
Definition: Registry.cxx:110
map< RgKey, RegistryItemI * > RgIMap
Definition: Registry.h:45
TTree * RgTree
int NEntries(void) const
get number of items
Definition: Registry.cxx:582