Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
34template <class AParamType>
35class TParameter : public TObject {
36
37public:
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
47private:
49 AParamType fVal;
50
53
54public:
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') {
88 } else if (mergemode == 'l') {
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
119template <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
154template <>
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.
191template <> void TParameter<Long64_t>::Streamer(TBuffer &R__b);
193
194#endif
#define c(i)
Definition RSha256.hxx:101
const Bool_t kFALSE
Definition RtypesCore.h:101
unsigned long ULong_t
Definition RtypesCore.h:55
bool Bool_t
Definition RtypesCore.h:63
const Bool_t kTRUE
Definition RtypesCore.h:100
const char Option_t
Definition RtypesCore.h:66
#define ClassDef(name, id)
Definition Rtypes.h:325
#define BIT(n)
Definition Rtypes.h:85
@ kMultiply
Definition TAttMarker.h:49
char name[80]
Definition TGX11.cxx:110
Buffer base class used for serializing objects.
Definition TBuffer.h:43
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:80
Collection abstract base class.
Definition TCollection.h:65
Mother of all ROOT objects.
Definition TObject.h:41
virtual const char * GetName() const
Returns name of object.
Definition TObject.cxx:429
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:201
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:766
void ResetBit(UInt_t f)
Definition TObject.h:200
Named parameter, streamable and storable.
Definition TParameter.h:35
virtual ~TParameter()
Definition TParameter.h:60
virtual Bool_t IsSortable() const
Definition TParameter.h:93
TParameter(const char *name, const AParamType &val, char mergemode)
Definition TParameter.h:58
virtual Int_t Merge(TCollection *in)
Definition TParameter.h:120
Bool_t IsConst() const
Definition TParameter.h:68
virtual void Print(Option_t *) const
This method must be overridden when a class wants to print itself.
Definition TParameter.h:108
AParamType fVal
Definition TParameter.h:49
void SetMergeMode(char mergemode='+')
Definition TParameter.h:78
TString fName
Definition TParameter.h:48
virtual Int_t Compare(const TObject *obj) const
Compare abstract method.
Definition TParameter.h:94
void SetVal(const AParamType &val)
Definition TParameter.h:69
const AParamType & GetVal() const
Definition TParameter.h:67
const char * GetName() const
Returns name of object.
Definition TParameter.h:66
virtual void ls(Option_t *) const
The ls function lists the contents of a class on stdout.
Definition TParameter.h:102
void Reset()
Definition TParameter.h:51
TParameter(const char *name, const AParamType &val)
Definition TParameter.h:56
virtual ULong_t Hash() const
Return hash value for this object.
Definition TParameter.h:92
static void IndentLevel()
Functions used by ls() to indent an object hierarchy.
Definition TROOT.cxx:2819
Basic string class.
Definition TString.h:136
int CompareTo(const char *cs, ECaseCompare cmp=kExact) const
Compare a string to char *cs2.
Definition TString.cxx:442
UInt_t Hash(ECaseCompare cmp=kExact) const
Return hash value.
Definition TString.cxx:662
const Int_t n
Definition legend1.C:16
void CallRecursiveRemoveIfNeeded(TObject &obj)
call RecursiveRemove for obj if gROOT is valid and obj.TestBit(kMustCleanup) is true.
Definition TROOT.h:393