Logo ROOT   6.10/09
Reference Guide
TParameter.h
Go to the documentation of this file.
1 // @(#)root/base:$Id$
2 // Author: Maarten Ballintijn 21/06/2004
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TParameter
13 #define ROOT_TParameter
14 
15 
16 //////////////////////////////////////////////////////////////////////////
17 // //
18 // TParameter<AParamType> //
19 // //
20 // Named parameter, streamable and storable. //
21 // //
22 //////////////////////////////////////////////////////////////////////////
23 
24 #include "Riostream.h"
25 
26 #include "TClass.h"
27 
28 #include "TObject.h"
29 
30 #include "TCollection.h"
31 
32 #include "TString.h"
33 
34 #include "TROOT.h"
35 
36 template <class AParamType>
37 class TParameter : public TObject {
38 
39 public:
40  // Defines options / status while merging:
41  enum EStatusBits { kMultiply = BIT(16), // Use multiplication
42  kMax = BIT(17), // Take max value
43  kMin = BIT(18), // Take min value
44  kFirst = BIT(19), // Take the first value
45  kLast = BIT(20), // Take the last value
46  kIsConst = BIT(21) // Set if all values are equal
47  };
48 
49 private:
51  AParamType fVal;
52 
55 
56 public:
57  TParameter(): fVal() { Reset(); SetBit(kIsConst); }
58  TParameter(const char *name, const AParamType &val)
59  : fName(name), fVal(val) { Reset(); SetBit(kIsConst);}
60  TParameter(const char *name, const AParamType &val, char mergemode)
61  : fName(name), fVal(val) { SetMergeMode(mergemode); SetBit(kIsConst);}
62  virtual ~TParameter() { }
63 
64  const char *GetName() const { return fName; }
65  const AParamType &GetVal() const { return fVal; }
66  Bool_t IsConst() const { return (TestBit(kIsConst) ? kTRUE : kFALSE); }
67  void SetVal(const AParamType &val) { fVal = val; }
68 
69  // Merging modes:
70  // '+' addition ('OR' for booleans) [default]
71  // '*' multiplication ('AND' for booleans)
72  // 'M' maximum ('OR' for booleans)
73  // 'm' minimum ('AND' for booleans)
74  // 'f' first value
75  // 'l' last value
76  void SetMergeMode(char mergemode = '+') {
77  Reset();
78  if (mergemode == '*') {
80  } else if (mergemode == 'M') {
81  SetBit(kMax);
82  } else if (mergemode == 'm') {
83  SetBit(kMin);
84  } else if (mergemode == 'f') {
85  SetBit(kFirst);
86  } else if (mergemode == 'l') {
87  SetBit(kLast);
88  }
89  }
90  virtual ULong_t Hash() const { return fName.Hash(); }
91  virtual Bool_t IsSortable() const { return kTRUE; }
92  virtual Int_t Compare(const TObject *obj) const {
93  // Compare two TParameter objects. Returns 0 when equal, -1 when this is
94  // smaller and +1 when bigger (like strcmp).
95 
96  if (this == obj) return 0;
97  return fName.CompareTo(obj->GetName());
98  }
99 
100  virtual void ls(Option_t *) const {
101  // Print this parameter content
103  std::cout << "OBJ: " << IsA()->GetName() << "\t" << fName << " = " << fVal << std::endl;
104  }
105 
106  virtual void Print(Option_t *) const {
107  // Print this parameter content
109  std::cout << IsA()->GetName() << "\t" << fName << " = " << fVal << std::endl;
110  }
111 
112  virtual Int_t Merge(TCollection *in);
113 
114  ClassDef(TParameter,2) //Named templated parameter type
115 };
116 
117 template <class AParamType>
119  // Merge objects in the list.
120  // Returns the number of objects that were in the list.
121  TIter nxo(in);
122  Int_t n = 0;
123  while (TObject *o = nxo()) {
124  TParameter<AParamType> *c = dynamic_cast<TParameter<AParamType> *>(o);
125  if (c) {
126  // Check if constant
127  if (fVal != c->GetVal()) ResetBit(kIsConst);
128  if (TestBit(kMultiply)) {
129  // Multiply
130  fVal *= c->GetVal();
131  } else if (TestBit(kMax)) {
132  // Take max
133  if (c->GetVal() > fVal) fVal = c->GetVal();
134  } else if (TestBit(kMin)) {
135  // Take min
136  if (c->GetVal() < fVal) fVal = c->GetVal();
137  } else if (TestBit(kLast)) {
138  // Take the last
139  fVal = c->GetVal();
140  } else if (!TestBit(kFirst)) {
141  // Add, if not asked to take the first
142  fVal += c->GetVal();
143  }
144  n++;
145  }
146  }
147 
148  return n;
149 }
150 
151 // Specialization of Merge for Bool_t
152 template <>
154 {
155  // Merge bool objects in the list.
156  // Returns the number of objects that were in the list.
157  TIter nxo(in);
158  Int_t n = 0;
159  while (TObject *o = nxo()) {
160  TParameter<Bool_t> *c = dynamic_cast<TParameter<Bool_t> *>(o);
161  if (c) {
162  // Check if constant
163  if (fVal != (Bool_t) c->GetVal()) ResetBit(kIsConst);
165  // And
166  fVal &= (Bool_t) c->GetVal();
167  } else if (TestBit(kLast)) {
168  // Take the last
169  fVal = (Bool_t) c->GetVal();
170  } else if (!TestBit(kFirst) || TestBit(kMax)) {
171  // Or
172  fVal |= (Bool_t) c->GetVal();
173  }
174  n++;
175  }
176  }
177 
178  return n;
179 }
180 
181 // FIXME: Remove once we implement https://sft.its.cern.ch/jira/browse/ROOT-6284
182 // When building with -fmodules, it instantiates all pending instantiations,
183 // instead of delaying them until the end of the translation unit.
184 // We 'got away with' probably because the use and the definition of the
185 // explicit specialization do not occur in the same TU.
186 //
187 // In case we are building with -fmodules, we need to forward declare the
188 // specialization in order to compile the dictionary G__Core.cxx.
189 template <> void TParameter<Long64_t>::Streamer(TBuffer &R__b);
190 template<> TClass *TParameter<Long64_t>::Class();
191 
192 #endif
const char Option_t
Definition: RtypesCore.h:62
Bool_t TestBit(UInt_t f) const
Definition: TObject.h:159
#define BIT(n)
Definition: Rtypes.h:75
Buffer base class used for serializing objects.
Definition: TBuffer.h:40
const char * GetName() const
Returns name of object.
Definition: TParameter.h:64
TParameter(const char *name, const AParamType &val, char mergemode)
Definition: TParameter.h:60
void SetVal(const AParamType &val)
Definition: TParameter.h:67
Basic string class.
Definition: TString.h:129
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
void SetMergeMode(char mergemode='+')
Definition: TParameter.h:76
virtual Int_t Merge(TCollection *in)
Definition: TParameter.h:118
Bool_t IsConst() const
Definition: TParameter.h:66
UInt_t Hash(ECaseCompare cmp=kExact) const
Return hash value.
Definition: TString.cxx:616
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:687
#define ClassDef(name, id)
Definition: Rtypes.h:297
virtual void ls(Option_t *) const
The ls function lists the contents of a class on stdout.
Definition: TParameter.h:100
void Class()
Definition: Class.C:29
TParameter(const char *name, const AParamType &val)
Definition: TParameter.h:58
virtual ULong_t Hash() const
Return hash value for this object.
Definition: TParameter.h:90
Named parameter, streamable and storable.
Definition: TParameter.h:37
virtual ~TParameter()
Definition: TParameter.h:62
Collection abstract base class.
Definition: TCollection.h:42
static void IndentLevel()
Functions used by ls() to indent an object hierarchy.
Definition: TROOT.cxx:2632
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:71
virtual Bool_t IsSortable() const
Definition: TParameter.h:91
const Bool_t kFALSE
Definition: RtypesCore.h:92
EStatusBits
Definition: TObject.h:57
unsigned long ULong_t
Definition: RtypesCore.h:51
TString fName
Definition: TParameter.h:50
int CompareTo(const char *cs, ECaseCompare cmp=kExact) const
Compare a string to char *cs2.
Definition: TString.cxx:396
Mother of all ROOT objects.
Definition: TObject.h:37
void Reset()
Definition: TParameter.h:53
const AParamType & GetVal() const
Definition: TParameter.h:65
void ResetBit(UInt_t f)
Definition: TObject.h:158
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:364
const Bool_t kTRUE
Definition: RtypesCore.h:91
const Int_t n
Definition: legend1.C:16
virtual void Print(Option_t *) const
This method must be overridden when a class wants to print itself.
Definition: TParameter.h:106
virtual Int_t Compare(const TObject *obj) const
Compare abstract method.
Definition: TParameter.h:92
AParamType fVal
Definition: TParameter.h:51