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:
40// clang++ <v20 (-Wshadow) complains about shadowing TAttMarker.h global enum EMarkerStyle. Let's silence warning:
41#if defined(__clang__) && __clang_major__ < 20
42#pragma clang diagnostic push
43#pragma clang diagnostic ignored "-Wshadow"
44#endif
45 kMultiply = BIT(16), // Use multiplication
46#if defined(__clang__) && __clang_major__ < 20
47#pragma clang diagnostic pop
48#endif
49 kMax = BIT(17), // Take max value
50 kMin = BIT(18), // Take min value
51 kFirst = BIT(19), // Take the first value
52 kLast = BIT(20), // Take the last value
53 kIsConst = BIT(21) // Set if all values are equal
54 };
55
56private:
58 AParamType fVal;
59
62
63public:
65 TParameter(const char *name, const AParamType &val)
66 : fName(name), fVal(val) { Reset(); SetBit(kIsConst);}
67 TParameter(const char *name, const AParamType &val, char mergemode)
69 virtual ~TParameter()
70 {
71 // Required since we overload TObject::Hash.
73 }
74
75 const char *GetName() const override { return fName; }
76 const AParamType &GetVal() const { return fVal; }
77 Bool_t IsConst() const { return (TestBit(kIsConst) ? kTRUE : kFALSE); }
78 void SetVal(const AParamType &val) { fVal = val; }
79
80 // Merging modes:
81 // '+' addition ('OR' for booleans) [default]
82 // '*' multiplication ('AND' for booleans)
83 // 'M' maximum ('OR' for booleans)
84 // 'm' minimum ('AND' for booleans)
85 // 'f' first value
86 // 'l' last value
87 void SetMergeMode(char mergemode = '+') {
88 Reset();
89 if (mergemode == '*') {
91 } else if (mergemode == 'M') {
92 SetBit(kMax);
93 } else if (mergemode == 'm') {
94 SetBit(kMin);
95 } else if (mergemode == 'f') {
97 } else if (mergemode == 'l') {
99 }
100 }
101 ULong_t Hash() const override { return fName.Hash(); }
102 Bool_t IsSortable() const override { return kTRUE; }
103 Int_t Compare(const TObject *obj) const override
104 {
105 // Compare two TParameter objects. Returns 0 when equal, -1 when this is
106 // smaller and +1 when bigger (like strcmp).
107
108 if (this == obj) return 0;
109 return fName.CompareTo(obj->GetName());
110 }
111
112 void ls(Option_t *) const override
113 {
114 // Print this parameter content
116 std::cout << "OBJ: " << IsA()->GetName() << "\t" << fName << " = " << fVal << std::endl;
117 }
118
119 void Print(Option_t *) const override
120 {
121 // Print this parameter content
123 std::cout << IsA()->GetName() << "\t" << fName << " = " << fVal << std::endl;
124 }
125
127
128 ClassDefOverride(TParameter,2) //Named templated parameter type
129};
130
131template <class AParamType>
133 // Merge objects in the list.
134 // Returns the number of objects that were in the list.
135 TIter nxo(in);
136 Int_t n = 0;
137 while (TObject *o = nxo()) {
138 TParameter<AParamType> *c = dynamic_cast<TParameter<AParamType> *>(o);
139 if (c) {
140 // Check if constant
141 if (fVal != c->GetVal()) ResetBit(kIsConst);
142 if (TestBit(kMultiply)) {
143 // Multiply
144 fVal *= c->GetVal();
145 } else if (TestBit(kMax)) {
146 // Take max
147 if (c->GetVal() > fVal) fVal = c->GetVal();
148 } else if (TestBit(kMin)) {
149 // Take min
150 if (c->GetVal() < fVal) fVal = c->GetVal();
151 } else if (TestBit(kLast)) {
152 // Take the last
153 fVal = c->GetVal();
154 } else if (!TestBit(kFirst)) {
155 // Add, if not asked to take the first
156 fVal += c->GetVal();
157 }
158 n++;
159 }
160 }
161
162 return n;
163}
164
165// Specialization of Merge for Bool_t
166template <>
168{
169 // Merge bool objects in the list.
170 // Returns the number of objects that were in the list.
171 TIter nxo(in);
172 Int_t n = 0;
173 while (TObject *o = nxo()) {
174 TParameter<Bool_t> *c = dynamic_cast<TParameter<Bool_t> *>(o);
175 if (c) {
176 // Check if constant
177 if (fVal != (Bool_t) c->GetVal()) ResetBit(kIsConst);
178 if (TestBit(TParameter::kMultiply) || TestBit(kMin)) {
179 // And
180 fVal &= (Bool_t) c->GetVal();
181 } else if (TestBit(kLast)) {
182 // Take the last
183 fVal = (Bool_t) c->GetVal();
184 } else if (!TestBit(kFirst) || TestBit(kMax)) {
185 // Or
186 fVal |= (Bool_t) c->GetVal();
187 }
188 n++;
189 }
190 }
191
192 return n;
193}
194
195// FIXME: Remove once we implement https://sft.its.cern.ch/jira/browse/ROOT-6284
196// When building with -fmodules, it instantiates all pending instantiations,
197// instead of delaying them until the end of the translation unit.
198// We 'got away with' probably because the use and the definition of the
199// explicit specialization do not occur in the same TU.
200//
201// In case we are building with -fmodules, we need to forward declare the
202// specialization in order to compile the dictionary G__Core.cxx.
205
206#endif
#define c(i)
Definition RSha256.hxx:101
bool Bool_t
Boolean (0=false, 1=true) (bool)
Definition RtypesCore.h:77
unsigned long ULong_t
Unsigned long integer 4 bytes (unsigned long). Size depends on architecture.
Definition RtypesCore.h:69
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
#define BIT(n)
Definition Rtypes.h:91
#define ClassDefOverride(name, id)
Definition Rtypes.h:348
@ kMultiply
Definition TAttMarker.h:56
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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:84
Collection abstract base class.
Definition TCollection.h:65
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
Mother of all ROOT objects.
Definition TObject.h:42
virtual const char * GetName() const
Returns name of object.
Definition TObject.cxx:458
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:204
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:882
void ResetBit(UInt_t f)
Definition TObject.h:203
Named parameter, streamable and storable.
Definition TParameter.h:35
static TClass * Class()
virtual ~TParameter()
Definition TParameter.h:69
void Streamer(TBuffer &) override
Stream an object of class TObject.
ULong_t Hash() const override
Return hash value for this object.
Definition TParameter.h:101
TParameter(const char *name, const AParamType &val, char mergemode)
Definition TParameter.h:67
const char * GetName() const override
Returns name of object.
Definition TParameter.h:75
virtual Int_t Merge(TCollection *in)
Definition TParameter.h:132
TClass * IsA() const override
Definition TParameter.h:128
void ls(Option_t *) const override
The ls function lists the contents of a class on stdout.
Definition TParameter.h:112
Bool_t IsConst() const
Definition TParameter.h:77
void Print(Option_t *) const override
This method must be overridden when a class wants to print itself.
Definition TParameter.h:119
AParamType fVal
Definition TParameter.h:58
void SetMergeMode(char mergemode='+')
Definition TParameter.h:87
TString fName
Definition TParameter.h:57
void SetVal(const AParamType &val)
Definition TParameter.h:78
const AParamType & GetVal() const
Definition TParameter.h:76
Int_t Compare(const TObject *obj) const override
Compare abstract method.
Definition TParameter.h:103
void Reset()
Definition TParameter.h:60
TParameter(const char *name, const AParamType &val)
Definition TParameter.h:65
Bool_t IsSortable() const override
Definition TParameter.h:102
static void IndentLevel()
Functions used by ls() to indent an object hierarchy.
Definition TROOT.cxx:2902
Basic string class.
Definition TString.h:138
int CompareTo(const char *cs, ECaseCompare cmp=kExact) const
Compare a string to char *cs2.
Definition TString.cxx:464
UInt_t Hash(ECaseCompare cmp=kExact) const
Return hash value.
Definition TString.cxx:684
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:403