Jpp  19.1.0
the software that should make you happy
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Protected Attributes | Static Private Member Functions | Friends | List of all members
JROOT::JManager< JKey_t, JValue_t > Class Template Reference

Auxiliary class to manage set of compatible ROOT objects (e.g. histograms) using unique keys. More...

#include <JManager.hh>

Inheritance diagram for JROOT::JManager< JKey_t, JValue_t >:
JLANG::JPointer< JClass_t > std::map< JKey_t, JPointer< JValue_t > > JLANG::JAbstractPointer< JClass_t > JLANG::JEquals< JAbstractPointer< JClass_t > >

Public Types

typedef JPointer< JValue_t > ptr_type
 
typedef std::map< JKey_t, ptr_typemap_type
 

Public Member Functions

 JManager ()
 Default constructor. More...
 
 JManager (JValue_t *master, char wildcard='%', JFormat_t format=JFormat_t())
 Constructor. More...
 
 JManager (JValue_t *master, const std::string &name, char wildcard='%', std::ios::fmtflags format=std::ios::fmtflags())
 Constructor. More...
 
 JManager (const JManager &manager)
 Copy constructor. More...
 
void clear ()
 Clear client objects. More...
 
JValue_t * operator[] (JKey_t key)
 Get pointer to object for given key. More...
 
void Read (TDirectory &in)
 Read objects from file. More...
 
void Write (TDirectory &out, const bool wm=false)
 Write objects to file. More...
 
void Write (const char *file_name, const bool wm=false)
 Write objects to file. More...
 
virtual JClass_t * get () const override
 Get pointer. More...
 
virtual void set (JClass_t *p) override
 Set pointer. More...
 
template<class T >
void set (const JPointer< T > &pointer)
 Set pointer. More...
 
virtual void reset () override
 Reset pointer. More...
 
template<class T >
void reset (const JPointer< T > &pointer)
 Reset pointer. More...
 
void reset (JClass_t *p)
 Reset pointer. More...
 
JClass_t *const & getReference () const
 Get rereference to internal pointer. More...
 
JClass_t *& getReference ()
 Get rereference to internal pointer. More...
 
virtual bool equals (const JAbstractPointer &object) const
 Equals. More...
 
bool is_valid () const
 Check validity of pointer. More...
 
JClass_t * operator-> () const
 Smart pointer operator. More...
 
 operator JClass_t * () const
 Type conversion operator. More...
 

Static Public Member Functions

static JManager Read (TDirectory &in, const char *const master, const char wildcard)
 Read objects from file into manager. More...
 

Public Attributes

char wc
 
JFormat_t fmt
 

Protected Attributes

JClass_t * __p
 pointer to object More...
 

Static Private Member Functions

static void detach (...)
 Fall back for detaching object pointed to from ROOT I/O. More...
 
static void detach (TH1 *p)
 Implementation for detaching of object pointed to from ROOT I/O. More...
 

Friends

TFile & operator>> (TFile &file, JManager &object)
 Read manager from file. More...
 
TFile & operator<< (TFile &file, JManager &object)
 Write manager to file. More...
 

Detailed Description

template<class JKey_t, class JValue_t>
class JROOT::JManager< JKey_t, JValue_t >

Auxiliary class to manage set of compatible ROOT objects (e.g. histograms) using unique keys.


For each use of the map operator [], it is checked whether the corresponding client object exists.
If the corresponding client object does not yet exists, a clone is made of the master object (i.e. the 'client').
The name of the newly created object is derived from the name of the master object by replacing the wild card character with the value of the key according the optional format specifier.

Definition at line 44 of file JManager.hh.

Member Typedef Documentation

◆ ptr_type

template<class JKey_t , class JValue_t >
typedef JPointer<JValue_t> JROOT::JManager< JKey_t, JValue_t >::ptr_type

Definition at line 50 of file JManager.hh.

◆ map_type

template<class JKey_t , class JValue_t >
typedef std::map<JKey_t, ptr_type> JROOT::JManager< JKey_t, JValue_t >::map_type

Definition at line 51 of file JManager.hh.

Constructor & Destructor Documentation

◆ JManager() [1/4]

template<class JKey_t , class JValue_t >
JROOT::JManager< JKey_t, JValue_t >::JManager ( )
inline

Default constructor.

Definition at line 56 of file JManager.hh.

57  {}

◆ JManager() [2/4]

template<class JKey_t , class JValue_t >
JROOT::JManager< JKey_t, JValue_t >::JManager ( JValue_t *  master,
char  wildcard = '%',
JFormat_t  format = JFormat_t() 
)
inline

Constructor.

Note that the manager owns the master object pointed to.
For saving the objects to file, use method JManager::Write (all objects are detached from the current ROOT directory).

Parameters
mastermaster object
wildcardwild card character
formatformat specifier for replacement of wildcard character by value of key

Definition at line 71 of file JManager.hh.

73  :
74  ptr_type(master),
75  map_type(),
76  wc (wildcard),
77  fmt(format)
78  {
79  using namespace std;
80  using namespace JPP;
81 
82  if (!this->is_valid()) {
83  THROW(JException, "No valid master");
84  }
85 
86  if (string(this->get()->GetName()).find(wc) == string::npos) {
87  THROW(JException, "Invalid wildcard <" << this->get()->GetName() << "> '" << wc << "'");
88  }
89 
90  detach(this->get());
91 
92  resetObject(this->get(), true);
93  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
bool is_valid() const
Check validity of pointer.
General exception.
Definition: JException.hh:24
virtual JClass_t * get() const override
Get pointer.
Definition: JPointer.hh:64
JPointer< JValue_t > ptr_type
Definition: JManager.hh:50
std::map< JKey_t, ptr_type > map_type
Definition: JManager.hh:51
JFormat_t fmt
Definition: JManager.hh:363
static void detach(...)
Fall back for detaching object pointed to from ROOT I/O.
Definition: JManager.hh:369
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
void resetObject(JManager< JKey_t, JValue_t > *object, const bool reset=false)
Reset JManager object.
Definition: JManager.hh:394
Definition: JSTDTypes.hh:14

◆ JManager() [3/4]

template<class JKey_t , class JValue_t >
JROOT::JManager< JKey_t, JValue_t >::JManager ( JValue_t *  master,
const std::string &  name,
char  wildcard = '%',
std::ios::fmtflags  format = std::ios::fmtflags() 
)
inline

Constructor.

Note that the manager owns the master object pointed to.
For saving the objects to file, use method JManager::Write (all objects are detached from the current ROOT directory).

Parameters
mastermaster object
namename of master
wildcardwild card character
formatformat specifier for replacement of wildcard character by value of key

Definition at line 108 of file JManager.hh.

111  :
112  ptr_type(master),
113  map_type(),
114  wc (wildcard),
115  fmt(format)
116  {
117  using namespace std;
118  using namespace JPP;
119 
120  if (!this->is_valid()) {
121  THROW(JException, "No valid master");
122  }
123 
124  this->get()->SetName(name.c_str());
125 
126  if (string(this->get()->GetName()).find(wc) == string::npos) {
127  THROW(JException, "Invalid wildcard <" << this->get()->GetName() << "> '" << wc << "'");
128  }
129 
130  detach(this->get());
131 
132  resetObject(this->get(), true);
133  }

◆ JManager() [4/4]

template<class JKey_t , class JValue_t >
JROOT::JManager< JKey_t, JValue_t >::JManager ( const JManager< JKey_t, JValue_t > &  manager)
inline

Copy constructor.

Note that the master object of the given manager is cloned and the wildcard copied.
The client objects are not copied.

Parameters
managermanager

Definition at line 144 of file JManager.hh.

144  :
145  ptr_type(NULL),
146  map_type(),
147  wc (manager.wc),
148  fmt(manager.fmt)
149  {
150  using namespace JPP;
151 
152  if (manager.is_valid()) {
153 
154  this->set((JValue_t*) manager.get()->Clone());
155 
156  detach(this->get());
157 
158  resetObject(this->get(), true);
159  }
160  }
virtual void set(JClass_t *p) override
Set pointer.
Definition: JPointer.hh:75

Member Function Documentation

◆ clear()

template<class JKey_t , class JValue_t >
void JROOT::JManager< JKey_t, JValue_t >::clear ( )
inline

Clear client objects.

Definition at line 166 of file JManager.hh.

167  {
168  for (typename map_type::iterator i = this->begin(); i != this->end(); ++i) {
169  i->second.reset();
170  }
171 
172  this->clear();
173  }
void clear()
Clear client objects.
Definition: JManager.hh:166

◆ operator[]()

template<class JKey_t , class JValue_t >
JValue_t* JROOT::JManager< JKey_t, JValue_t >::operator[] ( JKey_t  key)
inline

Get pointer to object for given key.

Parameters
keykey
Returns
pointer to client object

Definition at line 182 of file JManager.hh.

183  {
184  using namespace std;
185  using namespace JPP;
186 
187  typename map_type::iterator i = this->find(key);
188 
189  if (i == this->end()) {
190 
191  string buffer = this->get()->GetName();
192  string::size_type ipos = buffer.find(wc);
193 
194  ostringstream os;
195 
196  fmt.put(os);
197 
198  os << key;
199 
200  buffer.replace(ipos, 1, os.str());
201 
202  JValue_t* p = (JValue_t*) this->get()->Clone(buffer.c_str());
203 
204  detach(p);
205 
206  resetObject(p, true);
207 
208  this->insert(make_pair(key,p));
209 
210  return p;
211 
212  } else {
213 
214  return i->second;
215  }
216  }
void put(std::ostream &out) const
Put format specificaton to given output stream.
Definition: JManip.hh:599

◆ Read() [1/2]

template<class JKey_t , class JValue_t >
static JManager JROOT::JManager< JKey_t, JValue_t >::Read ( TDirectory &  in,
const char *const  master,
const char  wildcard 
)
inlinestatic

Read objects from file into manager.

Parameters
ininput file or directory
mastermaster name
wildcardwild card character
Returns
manager

Definition at line 227 of file JManager.hh.

228  {
229  using namespace std;
230  using namespace JPP;
231 
232  JManager manager;
233 
234  TString buffer = master;
235 
236  const Ssiz_t pos = buffer.Index(wildcard);
237 
238  const TString a = buffer(0, pos);
239  const TString b = buffer(pos+1, buffer.Length());
240 
241  buffer.ReplaceAll("[", "\\[");
242  buffer.ReplaceAll("]", "\\]");
243 
244  buffer.ReplaceAll(wildcard, ".*");
245 
246  const TRegexp regexp(buffer);
247 
248  TIter iter(in.GetListOfKeys());
249 
250  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
251 
252  const TString tag(key->GetName());
253 
254  // option match
255 
256  if (tag.Index(regexp) != -1) {
257 
258  JValue_t* p = dynamic_cast<JValue_t*>(key->ReadObj());
259 
260  if (p != NULL) {
261 
262  detach(p);
263 
264  string buffer = p->GetName();
265 
266  if (a.Length() != 0) { buffer.replace(buffer.find(a), a.Length(), ""); }
267  if (b.Length() != 0) { buffer.replace(buffer.find(b), b.Length(), ""); }
268 
269  JKey_t key;
270 
271  istringstream(buffer) >> key;
272 
273  manager.insert(make_pair(key, p));
274  }
275  }
276  }
277 
278  return manager;
279  }
Auxiliary class to manage set of compatible ROOT objects (e.g. histograms) using unique keys.
Definition: JManager.hh:47
const double a
Definition: JQuadrature.cc:42

◆ Read() [2/2]

template<class JKey_t , class JValue_t >
void JROOT::JManager< JKey_t, JValue_t >::Read ( TDirectory &  in)
inline

Read objects from file.

Parameters
ininput file or directory

Definition at line 287 of file JManager.hh.

288  {
289  if (this->is_valid()) {
290 
291  JManager buffer = Read(in, this->get()->GetName(), this->wc);
292 
293  this->swap(buffer);
294  }
295  }
static JManager Read(TDirectory &in, const char *const master, const char wildcard)
Read objects from file into manager.
Definition: JManager.hh:227

◆ Write() [1/2]

template<class JKey_t , class JValue_t >
void JROOT::JManager< JKey_t, JValue_t >::Write ( TDirectory &  out,
const bool  wm = false 
)
inline

Write objects to file.

Parameters
outoutput file or directory
wmwrite master

Definition at line 304 of file JManager.hh.

305  {
306  for (typename map_type::iterator i = this->begin(); i != this->end(); ++i) {
307  out.WriteTObject(i->second);
308  }
309 
310  if (wm) {
311  out.WriteTObject(this->get());
312  }
313  }

◆ Write() [2/2]

template<class JKey_t , class JValue_t >
void JROOT::JManager< JKey_t, JValue_t >::Write ( const char *  file_name,
const bool  wm = false 
)
inline

Write objects to file.

Parameters
file_namefile name
wmwrite master

Definition at line 322 of file JManager.hh.

323  {
324  TFile out(file_name, "RECREATE");
325 
326  this->Write(out, wm) ;
327 
328  out.Write();
329  out.Close();
330  }
void Write(TDirectory &out, const bool wm=false)
Write objects to file.
Definition: JManager.hh:304

◆ detach() [1/2]

template<class JKey_t , class JValue_t >
static void JROOT::JManager< JKey_t, JValue_t >::detach (   ...)
inlinestaticprivate

Fall back for detaching object pointed to from ROOT I/O.

Definition at line 369 of file JManager.hh.

370  {}

◆ detach() [2/2]

template<class JKey_t , class JValue_t >
static void JROOT::JManager< JKey_t, JValue_t >::detach ( TH1 *  p)
inlinestaticprivate

Implementation for detaching of object pointed to from ROOT I/O.

Parameters
ppointer to object

Definition at line 378 of file JManager.hh.

379  {
380  if (p != NULL) {
381  p->SetDirectory(0);
382  }
383  }

◆ get()

template<class JClass_t >
virtual JClass_t* JLANG::JPointer< JClass_t >::get ( ) const
inlineoverridevirtualinherited

Get pointer.

Returns
pointer to object

Implements JLANG::JAbstractPointer< JClass_t >.

Reimplemented in JLANG::JMultiPointer< JTypeList< JHead_t, JTail_t > >, JLANG::JMultiPointer< JClass_t >, JLANG::JMultiPointer< JTail_t >, JLANG::JMultiPointer< JHead_t >, and JLANG::JMultiPointer< T >.

Definition at line 64 of file JPointer.hh.

65  {
66  return this->__p;
67  }
JClass_t * __p
pointer to object
Definition: JPointer.hh:136

◆ set() [1/2]

template<class JClass_t >
virtual void JLANG::JPointer< JClass_t >::set ( JClass_t *  p)
inlineoverridevirtualinherited

Set pointer.

Parameters
ppointer to object

Implements JLANG::JAbstractPointer< JClass_t >.

Reimplemented in JLANG::JSharedPointer< JClass_t, JMemory_t >.

Definition at line 75 of file JPointer.hh.

76  {
77  this->__p = p;
78  }

◆ set() [2/2]

template<class JClass_t >
template<class T >
void JLANG::JPointer< JClass_t >::set ( const JPointer< T > &  pointer)
inlineinherited

Set pointer.

Parameters
pointerpointer to object

Definition at line 96 of file JPointer.hh.

97  {
98  this->set(pointer.get());
99  }

◆ reset() [1/3]

template<class JClass_t >
virtual void JLANG::JPointer< JClass_t >::reset ( )
inlineoverridevirtualinherited

◆ reset() [2/3]

template<class JClass_t >
template<class T >
void JLANG::JPointer< JClass_t >::reset ( const JPointer< T > &  pointer)
inlineinherited

Reset pointer.

Parameters
pointerpointer to object

Definition at line 108 of file JPointer.hh.

109  {
110  this->reset(pointer.get());
111  }
virtual void reset() override
Reset pointer.
Definition: JPointer.hh:84

◆ reset() [3/3]

template<class JClass_t >
void JLANG::JAbstractPointer< JClass_t >::reset ( JClass_t *  p)
inlineinherited

Reset pointer.

Parameters
ppointer to object

Definition at line 94 of file JAbstractPointer.hh.

95  {
96  if (this->get() != p) {
97 
98  this->reset();
99 
100  if (p != NULL) {
101  this->set(p);
102  }
103  }
104  }
virtual void set(JClass_t *p)=0
Set pointer.
virtual void reset()=0
Reset pointer.
virtual JClass_t * get() const =0
Get pointer.

◆ getReference() [1/2]

template<class JClass_t >
JClass_t* const& JLANG::JPointer< JClass_t >::getReference ( ) const
inlineinherited

Get rereference to internal pointer.

Returns
reference to internal pointer

Definition at line 119 of file JPointer.hh.

120  {
121  return __p;
122  }

◆ getReference() [2/2]

template<class JClass_t >
JClass_t* & JLANG::JPointer< JClass_t >::getReference ( )
inlineinherited

Get rereference to internal pointer.

Returns
reference to internal pointer

Definition at line 130 of file JPointer.hh.

131  {
132  return __p;
133  }

◆ equals()

template<class JClass_t >
virtual bool JLANG::JAbstractPointer< JClass_t >::equals ( const JAbstractPointer< JClass_t > &  object) const
inlinevirtualinherited

Equals.

The equality is evaluated by comparison of the internal pointers.

Parameters
objectabstract pointer
Returns
true if equals; else false

Definition at line 50 of file JAbstractPointer.hh.

51  {
52  return this->get() == object.get();
53  }

◆ is_valid()

template<class JClass_t >
bool JLANG::JAbstractPointer< JClass_t >::is_valid ( ) const
inlineinherited

Check validity of pointer.

Returns
true if pointer not null; else false

Definition at line 83 of file JAbstractPointer.hh.

84  {
85  return this->get() != NULL;
86  }

◆ operator->()

template<class JClass_t >
JClass_t* JLANG::JAbstractPointer< JClass_t >::operator-> ( ) const
inlineinherited

Smart pointer operator.

Returns
pointer to object

Definition at line 112 of file JAbstractPointer.hh.

113  {
114  if (!is_valid())
115  THROW(JNullPointerException, "JAbstractPointer::operator->()");
116  else
117  return this->get();
118  }
Exception for null pointer operation.
Definition: JException.hh:234

◆ operator JClass_t *()

template<class JClass_t >
JLANG::JAbstractPointer< JClass_t >::operator JClass_t * ( ) const
inlineinherited

Type conversion operator.

Returns
pointer to object

Definition at line 126 of file JAbstractPointer.hh.

127  {
128  return this->get();
129  }

Friends And Related Function Documentation

◆ operator>>

template<class JKey_t , class JValue_t >
TFile& operator>> ( TFile &  file,
JManager< JKey_t, JValue_t > &  object 
)
friend

Read manager from file.

Parameters
filefile
objectmanager
Returns
file

Definition at line 340 of file JManager.hh.

341  {
342  object.Read(file);
343 
344  return file;
345  }

◆ operator<<

template<class JKey_t , class JValue_t >
TFile& operator<< ( TFile &  file,
JManager< JKey_t, JValue_t > &  object 
)
friend

Write manager to file.

Parameters
filefile
objectmanager
Returns
file

Definition at line 355 of file JManager.hh.

356  {
357  object.Write(file);
358 
359  return file;
360  }

Member Data Documentation

◆ wc

template<class JKey_t , class JValue_t >
char JROOT::JManager< JKey_t, JValue_t >::wc

Definition at line 362 of file JManager.hh.

◆ fmt

template<class JKey_t , class JValue_t >
JFormat_t JROOT::JManager< JKey_t, JValue_t >::fmt

Definition at line 363 of file JManager.hh.

◆ __p

template<class JClass_t >
JClass_t* JLANG::JPointer< JClass_t >::__p
protectedinherited

pointer to object

Definition at line 136 of file JPointer.hh.


The documentation for this class was generated from the following file: