CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FWItemValueGetter.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Core
4 // Class : FWItemValueGetter
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: Chris Jones
10 // Created: Sun Nov 30 16:15:43 EST 2008
11 // $Id: FWItemValueGetter.cc,v 1.5 2010/08/18 10:30:12 amraktad Exp $
12 //
13 
14 // system include files
15 #include <sstream>
16 #include "Reflex/Object.h"
17 #include "Reflex/Base.h"
18 #include <cstdio>
19 
20 // user include files
22 
23 //
24 // constants, enums and typedefs
25 //
26 
27 //
28 // static data member definitions
29 //
30 static
31 ROOT::Reflex::Member
32 recursiveFindMember(const std::string& iName,
33  const ROOT::Reflex::Type& iType)
34 {
35  using namespace ROOT::Reflex;
36 
37  Member temp = iType.MemberByName(iName);
38  if(temp) {return temp;}
39 
40  //try all base classes
41  for(Base_Iterator it = iType.Base_Begin(), itEnd = iType.Base_End();
42  it != itEnd;
43  ++it) {
44  temp = recursiveFindMember(iName,it->ToType());
45  if(temp) {break;}
46  }
47  return temp;
48 }
49 
50 namespace {
51  template <class T>
52  const std::string& valueToString(const std::string& iName,
53  const std::string& iUnit,
54  const Reflex::Object& iObject,
55  const Reflex::Member& iMember)
56  {
57  static std::string bala(128, 0);
58  T temp;
59  iMember.Invoke(iObject,temp);
60  snprintf(&bala[0], 127, "%s: %.1f %s", iName.c_str(), temp, iUnit.c_str());
61  return bala;
62  }
63 
64  typedef const std::string& (*FunctionType)(const std::string&, const std::string&,const Reflex::Object&, const Reflex::Member&);
65  typedef std::map<std::string, FunctionType> TypeToStringMap;
66 
67  template<typename T>
68  static void addToStringMap(TypeToStringMap& iMap) {
69  iMap[typeid(T).name()]=valueToString<T>;
70  }
71 
72  template <class T>
73  double valueToDouble(const Reflex::Object& iObj, const Reflex::Member& iMember) {
74  T temp;
75  iMember.Invoke(iObj,temp);
76  return temp;
77  }
78 
79  typedef double (*DoubleFunctionType)(const Reflex::Object&, const Reflex::Member&);
80  typedef std::map<std::string, DoubleFunctionType> TypeToDoubleMap;
81 
82  template<typename T>
83  static void addToDoubleMap(TypeToDoubleMap& iMap) {
84  iMap[typeid(T).name()]=valueToDouble<T>;
85  }
86 
87 }
88 
89 static
90 const std::string&
91 stringValueFor(const ROOT::Reflex::Object& iObj,
92  const ROOT::Reflex::Member& iMember,
93  const std::string& iUnit)
94 {
95  static std::string s_empty_string;
96  static TypeToStringMap s_map;
97  if (s_map.empty())
98  {
99  addToStringMap<float>(s_map);
100  addToStringMap<double>(s_map);
101  }
102  Reflex::Type returnType = iMember.TypeOf().ReturnType().FinalType();
103 
104  TypeToStringMap::iterator itFound =s_map.find(returnType.TypeInfo().name());
105  if (itFound == s_map.end())
106  {
107  //std::cout <<" could not print because type is "<<iObj.TypeOf().TypeInfo().name()<<std::endl;
108  return s_empty_string;
109  }
110 
111  return itFound->second(iMember.Name(),iUnit,iObj,iMember);
112 }
113 
114 static
115 double
116 doubleValueFor(const ROOT::Reflex::Object& iObj, const ROOT::Reflex::Member& iMember) {
117  static TypeToDoubleMap s_map;
118  if(s_map.empty() ) {
119  addToDoubleMap<float>(s_map);
120  addToDoubleMap<double>(s_map);
121  }
122 
123  const Reflex::Type returnType = iMember.TypeOf().ReturnType().FinalType();
124 
125  //std::cout << val.TypeOf().TypeInfo().name()<<std::endl;
126  TypeToDoubleMap::iterator itFound =s_map.find(returnType.TypeInfo().name());
127  if(itFound == s_map.end()) {
128  //std::cout <<" could not print because type is "<<iObj.TypeOf().TypeInfo().name()<<std::endl;
129  return -999.0;
130  }
131 
132  return itFound->second(iObj,iMember);
133 }
134 
135 
136 //
137 // constructors and destructor
138 //
140  const std::vector<std::pair<std::string, std::string> >& iFindValueFrom) :
141  m_type(iType)
142 {
143  using namespace ROOT::Reflex;
144  for(std::vector<std::pair<std::string,std::string> >::const_iterator it = iFindValueFrom.begin(), itEnd=iFindValueFrom.end();
145  it != itEnd;
146  ++it) {
147  //std::cout <<" trying function "<<*it<<std::endl;
148  Member temp = recursiveFindMember(it->first,iType);
149  if(temp) {
150  if(0==temp.FunctionParameterSize(true)) {
151  //std::cout <<" FOUND "<<temp.Name()<<std::endl;
152  //std::cout <<" in type "<<temp.DeclaringType().Name(SCOPED)<<std::endl;
154  m_unit = it->second;
155  break;
156  }
157  }
158  }
159 }
160 
161 // FWItemValueGetter::FWItemValueGetter(const FWItemValueGetter& rhs)
162 // {
163 // // do actual copying here;
164 // }
165 
166 //FWItemValueGetter::~FWItemValueGetter()
167 //{
168 //}
169 
170 //
171 // assignment operators
172 //
173 // const FWItemValueGetter& FWItemValueGetter::operator=(const FWItemValueGetter& rhs)
174 // {
175 // //An exception safe implementation is
176 // FWItemValueGetter temp(rhs);
177 // swap(rhs);
178 //
179 // return *this;
180 // }
181 
182 //
183 // member functions
184 //
185 
186 //
187 // const member functions
188 //
189 double
190 FWItemValueGetter::valueFor(const void* iObject) const
191 {
192  ROOT::Reflex::Object temp(m_type,
193  const_cast<void*>(iObject));
194  ROOT::Reflex::Object obj= temp.CastObject(m_memberFunction.DeclaringType());
196 }
197 
198 const std::string&
199 FWItemValueGetter::stringValueFor(const void* iObject) const
200 {
201  ROOT::Reflex::Object temp(m_type,
202  const_cast<void*>(iObject));
203  ROOT::Reflex::Object obj= temp.CastObject(m_memberFunction.DeclaringType());
204 
206 }
207 
208 bool
210 {
211  return bool(m_memberFunction);
212 }
213 
214 std::string
216 {
217  return m_memberFunction.Name();
218 }
219 
220 const std::string&
222 {
223  return m_unit;
224 }
225 
226 //
227 // static member functions
228 //
static ROOT::Reflex::Member recursiveFindMember(const std::string &iName, const ROOT::Reflex::Type &iType)
const std::string & unit() const
Type returnType(const Member &mem)
Definition: returnType.cc:9
std::string valueName() const
tuple obj
Example code starts here #.
Definition: VarParsing.py:655
const std::string & stringValueFor(const void *) const
ROOT::Reflex::Member m_memberFunction
FWItemValueGetter(const ROOT::Reflex::Type &, const std::vector< std::pair< std::string, std::string > > &iFindValueFrom)
double valueFor(const void *) const
static const std::string & stringValueFor(const ROOT::Reflex::Object &iObj, const ROOT::Reflex::Member &iMember, const std::string &iUnit)
static double doubleValueFor(const ROOT::Reflex::Object &iObj, const ROOT::Reflex::Member &iMember)
long double T
ROOT::Reflex::Type m_type