00001
00002
00003
00004
00005
00006
00007
00008 #ifndef _JObject_
00009 #define _JObject_
00010
00011 #include <sstream>
00012 #include <cassert>
00013 #include <map>
00014 #include <vector>
00015 #include <string>
00016 #include <typeinfo>
00017 using std::pair;
00018 using std::map;
00019 using std::vector;
00020 using std::string;
00021 using std::stringstream;
00022
00023
00024 #ifdef __CINT__
00025 #include "cint.h"
00026 #endif
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #define JOBJECT_PUBLIC(T) \
00047 virtual const char* className(void) const {return static_className();} \
00048 static const char* static_className(void) {return #T;}
00049
00050
00051 namespace jana{
00052
00053 class JObject{
00054
00055 public:
00056 JOBJECT_PUBLIC(JObject);
00057
00058 typedef unsigned long oid_t;
00059
00060 JObject(){id = (oid_t)this; append_types=false;}
00061 JObject( oid_t aId ) : id( aId ) { append_types=false;}
00062
00063 virtual ~JObject(){}
00064
00065
00066 template<typename T> bool IsA(const T *t) const {return dynamic_cast<const T*>(this)!=0L;}
00067
00068
00069 template<typename T> bool IsAT(const T *t) const {return dynamic_cast<const T*>(this)!=0L;}
00070
00071
00072 inline void AddAssociatedObject(const JObject *obj);
00073 inline void RemoveAssociatedObject(const JObject *obj);
00074 template<typename T> void Get(vector<const T*> &ptrs, string classname="") const ;
00075 template<typename T> void GetT(vector<const T*> &ptrs) const ;
00076
00077
00078 virtual void toStrings(vector<pair<string,string> > &items)const;
00079 template<typename T> void AddString(vector<pair<string,string> > &items, const char *name, const char *format, const T &val) const;
00080
00081 bool GetAppendTypes(void) const {return append_types;}
00082 void SetAppendTypes(bool append_types){this->append_types=append_types;}
00083
00084 oid_t id;
00085
00086 private:
00087
00088 bool append_types;
00089 map<const JObject*, string> associated;
00090
00091 };
00092
00093 #ifndef __CINT__
00094
00095
00096
00097
00098 void JObject::AddAssociatedObject(const JObject *obj)
00099 {
00100
00101
00102 assert(obj!=NULL);
00103
00104 associated[obj] = obj->className();
00105 }
00106
00107
00108
00109
00110 void JObject::RemoveAssociatedObject(const JObject *obj)
00111 {
00112
00113
00114
00115 map<const JObject*, string>::iterator iter = associated.find(obj);
00116
00117 if(iter!=associated.end()){
00118 associated.erase(iter);
00119 }
00120 }
00121
00122
00123
00124
00125 template<typename T>
00126 void JObject::Get(vector<const T*> &ptrs, string classname) const
00127 {
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 ptrs.clear();
00138
00139 if(classname=="")classname=T::static_className();
00140
00141 map<const JObject*, string>::const_iterator iter = associated.begin();
00142 for(; iter!=associated.end(); iter++){
00143 if(iter->second == classname){
00144 const T *ptr = dynamic_cast<const T*>(iter->first);
00145 ptrs.push_back(ptr);
00146 }
00147 }
00148 }
00149
00150
00151
00152
00153 template<typename T>
00154 void JObject::GetT(vector<const T*> &ptrs) const
00155 {
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 ptrs.clear();
00171
00172 string classname=T::static_className();
00173
00174 map<const JObject*, string>::const_iterator iter = associated.begin();
00175 for(; iter!=associated.end(); iter++){
00176 const T *ptr = dynamic_cast<const T*>(iter->first);
00177 if(ptr != NULL)ptrs.push_back(ptr);
00178 }
00179 }
00180
00181
00182
00183
00184 inline void JObject::toStrings(vector<pair<string,string> > &items) const
00185 {
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200 AddString(items, "JObject", "0x%08x", (unsigned long)this);
00201 }
00202
00203
00204
00205
00206 template<typename T>
00207 void JObject::AddString(vector<pair<string,string> > &items, const char *name, const char *format, const T &val) const
00208 {
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 char str[256];
00235 sprintf(str, format, val);
00236
00237 stringstream ss;
00238 ss<<name;
00239 if(append_types){
00240 if(typeid(T)==typeid(int)){
00241 ss<<":int:"<<val;
00242 }else if(typeid(T)==typeid(unsigned int)){
00243 ss<<":uint:"<<val;
00244 }else if(typeid(T)==typeid(long)){
00245 ss<<":long:"<<val;
00246 }else if(typeid(T)==typeid(unsigned long)){
00247 ss<<":ulong:"<<val;
00248 }else if(typeid(T)==typeid(float)){
00249 ss<<":float:"<<val;
00250 }else if(typeid(T)==typeid(double)){
00251 ss<<":double:"<<val;
00252 }else if(typeid(T)==typeid(string)){
00253 ss<<":string:"<<val;
00254 }else if(typeid(T)==typeid(const char*)){
00255 ss<<":string:"<<val;
00256 }else if(typeid(T)==typeid(char*)){
00257 ss<<":string:"<<val;
00258 }else{
00259 ss<<":unknown:"<<str;
00260 }
00261 }
00262
00263 pair<string, string> item;
00264 item.first = ss.str();
00265 item.second = string(str);
00266 items.push_back(item);
00267 }
00268
00269 #endif // __CINT__
00270
00271
00272 }
00273
00274 #endif // _JObject_
00275