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