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