ffead.server.doc
Object.h
1 /*
2  Copyright 2009-2012, Sumeet Chhetri
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 /*
17  * Object.h
18  *
19  * Created on: Dec 27, 2009
20  * Author: sumeet
21  */
22 
23 #ifndef OBJECT_H_
24 #define OBJECT_H_
25 #include <stdexcept>
26 #include "iostream"
27 /*Fix for Windows Cygwin*///#include <execinfo.h>
28 #include <dlfcn.h>
29 #include <cxxabi.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include "string"
33 #include <sstream>
34 #include <typeinfo>
35 #include "Logger.h"
36 using namespace std;
37 
38 class Object {
39 private:
40  Logger logger;
41  static string demangle(const char *mangled)
42  {
43  int status;
44  char *demangled;
45  using namespace abi;
46  demangled = __cxa_demangle(mangled, NULL, 0, &status);
47  string s(demangled);
48  delete demangled;
49  return s;
50 
51  }
52  string typeName;
53  void* pointer;
54  string serailizedXML;
55 public:
56  Object()
57  {
58  logger = Logger::getLogger("Object");
59  }
60  template <typename T> static string getClassName(T t)
61  {
62  const char *mangled = typeid(t).name();
63  string tn = demangle(mangled);
64  if(tn[tn.length()-1]=='*')
65  tn = tn.substr(0,tn.length()-1);
66  return tn;
67  }
68  template <typename T> void operator<<(T &t)
69  {
70  if(getClassName(t)=="Object")
71  {
72  Object *to = (Object *)&t;
73  this->typeName = to->typeName;
74  this->pointer = to->pointer;
75  }
76  else
77  {
78  this->typeName = getClassName(t);
79  this->pointer = &t;
80  }
81  //logger << " " << this->pointer << " " << flush;
82  //logger << this->typeName << "\n" << flush;
83  }
84  template <typename T> void operator<<(T *t)
85  {
86  if(getClassName(t)=="Object")
87  {
88  Object *to = (Object *)t;
89  this->typeName = to->typeName;
90  this->pointer = to->pointer;
91  }
92  else
93  {
94  this->typeName = getClassName(t);
95  this->pointer = t;
96  }
97  //logger << " " << this->pointer << " " << flush;
98  //logger << this->typeName << "\n" << flush;
99  }
100  ~Object() {
101  // TODO Auto-generated destructor stub
102  }
103  bool isInstanceOf(string className)
104  {
105  if(this->typeName==className)
106  return true;
107  else
108  return false;
109  }
110  template <typename T> bool isSimilarObject(T t)
111  {
112  string cn = getClassName(&t);
113  //logger << cn << flush;
114  if(isInstanceOf(cn))
115  return true;
116  else
117  return false;
118  }
119  template <typename T> bool isSimilarObject(T *t)
120  {
121  string cn = getClassName(t);
122  //logger << cn << flush;
123  if(isInstanceOf(cn))
124  return true;
125  else
126  return false;
127  }
128  string getTypeName()
129  {
130  return this->typeName;
131  }
132  void setTypeName(string type)
133  {
134  this->typeName = type;
135  }
136  template <typename T> T* getPointer()
137  {
138  T t;
139  string cn = getClassName(&t);
140  //logger << cn << flush;
141  if(isInstanceOf(cn))
142  return (T*)this->pointer;
143  else
144  return NULL;
145  }
146  template <typename T> T getValue()
147  {
148  T t;
149  string cn = getClassName(&t);
150  if(isInstanceOf(cn))
151  return *(T*)this->pointer;
152  else
153  return t;
154  }
155  void* getVoidPointer()
156  {
157  return this->pointer;
158  }
159  template <class T>
160  static bool instanceOf(T instance,string className)
161  {
162  int status;
163  const char *mangled = typeid(instance).name();
164  using namespace abi;
165  mangled = __cxa_demangle(mangled, NULL, 0, &status);
166  string cls(mangled);
167  delete mangled;
168  if(cls==className || cls==(className+"*")) return true;
169  else return false;
170  }
171 };
172 
173 #endif /* OBJECT_H_ */