ffead.server.doc
Reflector.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 #ifndef REFLECTOR_H_
17 #define REFLECTOR_H_
18 #include "ClassInfo.h"
19 #include "string"
20 #include "Method.h"
21 #include "Field.h"
22 #include <stdio.h>
23 #include <sys/wait.h>
24 #include <stdexcept>
25 /*Fix for Windows Cygwin*///#include <execinfo.h>
26 #include <dlfcn.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <sstream>
30 #include <typeinfo>
31 #include "Constants.h"
32 
33 class Reflector
34 {
35  vector<string> objectT;
36  vector<void*> objects;
37  void cleanUp();
38 public:
39  Reflector();
40  virtual ~Reflector();
41  ClassInfo getClassInfo(string);
42  template <class T> T invokeMethod(void* instance,Method method,vals values)
43  {
44  T *obj;
45  string libName = Constants::INTER_LIB_FILE;
46  void *dlib = dlopen(libName.c_str(), RTLD_NOW);
47  if(dlib == NULL)
48  {
49  cerr << dlerror() << endl;
50  exit(-1);
51  }
52  string methodname = "invokeReflectionCIMethodFor"+method.getMethodName();
53  void *mkr = dlsym(dlib, methodname.c_str());
54  typedef void* (*RfPtr) (void*,vals);
55  RfPtr f = (RfPtr)mkr;
56  if(f!=NULL)
57  {
58  if(method.getReturnType()!="void")
59  obj = (T*)f(instance,values);
60  else
61  f(instance,values);
62  }
63  return *obj;
64  }
65  void destroy(void* instance,string classn)
66  {
67  string libName = Constants::INTER_LIB_FILE;
68  void *dlib = dlopen(libName.c_str(), RTLD_NOW);
69  if(dlib == NULL)
70  {
71  cerr << dlerror() << endl;
72  exit(-1);
73  }
74  string methodname = "invokeReflectionCIDtorFor"+classn;
75  void *mkr = dlsym(dlib, methodname.c_str());
76  typedef void (*RfPtr) (void*);
77  RfPtr f = (RfPtr)mkr;
78  if(f!=NULL)
79  {
80  f(instance);
81  }
82  }
83  void* invokeMethodGVP(void* instance,Method method,vals values)
84  {
85  void *obj = NULL;
86  string libName = Constants::INTER_LIB_FILE;
87  void *dlib = dlopen(libName.c_str(), RTLD_NOW);
88  if(dlib == NULL)
89  {
90  cerr << dlerror() << endl;
91  exit(-1);
92  }
93  string methodname = "invokeReflectionCIMethodFor"+method.getMethodName();
94  void *mkr = dlsym(dlib, methodname.c_str());
95  typedef void* (*RfPtr) (void*,vals);
96  RfPtr f = (RfPtr)mkr;
97  if(f!=NULL)
98  {
99  if(method.getReturnType()!="void")
100  obj = f(instance,values);
101  else
102  f(instance,values);
103  }
104  return obj;
105  }
106 
107  template <class T> T newInstance(Constructor ctor,vals values)
108  {
109  T *obj;
110  string libName = Constants::INTER_LIB_FILE;
111  void *dlib = dlopen(libName.c_str(), RTLD_NOW);
112  if(dlib == NULL)
113  {
114  cerr << dlerror() << endl;
115  exit(-1);
116  }
117  string methodname = "invokeReflectionCICtorFor"+ctor.getName();
118  void *mkr = dlsym(dlib, methodname.c_str());
119  typedef void* (*RfPtr) (vals);
120  RfPtr f = (RfPtr)mkr;
121  if(f!=NULL)
122  {
123  obj = (T*)f(values);
124  }
125  objectT.push_back(ctor.getName());
126  objects.push_back(obj);
127  return *obj;
128  }
129  template <class T> T newInstance(Constructor ctor)
130  {
131  vals values;
132  return newInstance<T>(ctor,values);
133  }
134 
135  void* newInstanceGVP(Constructor ctor,vals values)
136  {
137  void *obj = NULL;
138  string libName = Constants::INTER_LIB_FILE;
139  void *dlib = dlopen(libName.c_str(), RTLD_NOW);
140  if(dlib == NULL)
141  {
142  cerr << dlerror() << endl;
143  exit(-1);
144  }
145  string methodname = "invokeReflectionCICtorFor"+ctor.getName();
146  void *mkr = dlsym(dlib, methodname.c_str());
147  typedef void* (*RfPtr) (vals);
148  RfPtr f = (RfPtr)mkr;
149  if(f!=NULL)
150  {
151  obj = f(values);
152  }
153  objectT.push_back(ctor.getName());
154  objects.push_back(obj);
155  return obj;
156  }
157  void* newInstanceGVP(Constructor ctor)
158  {
159  vals values;
160  return newInstanceGVP(ctor,values);
161  }
162 
163  void* invokeMethodUnknownReturn(void* instance,Method method,vals values)
164  {
165  void* obj = NULL;
166  string libName = Constants::INTER_LIB_FILE;
167  void *dlib = dlopen(libName.c_str(), RTLD_NOW);
168  if(dlib == NULL)
169  {
170  cerr << dlerror() << endl;
171  exit(-1);
172  }
173  string methodname = "invokeReflectionCIMethodFor"+method.getMethodName();
174  void *mkr = dlsym(dlib, methodname.c_str());
175  typedef void* (*RfPtr) (void*,vals);
176  RfPtr f = (RfPtr)mkr;
177  if(f!=NULL)
178  {
179  if(method.getReturnType()!="void")
180  obj = f(instance,values);
181  else
182  f(instance,values);
183  }
184  return obj;
185  }
186 
187  template <class T> T getField(void* instance,Field field)
188  {
189  T t;
190  string libName = Constants::INTER_LIB_FILE;
191  void *dlib = dlopen(libName.c_str(), RTLD_NOW);
192  if(dlib == NULL)
193  {
194  cerr << dlerror() << endl;
195  exit(-1);
196  }
197  string fldname = "invokeReflectionCIFieldFor"+field.getFieldName();
198  void *mkr = dlsym(dlib, fldname.c_str());
199  typedef T (*RfPtr) (void*);
200  RfPtr f = (RfPtr)mkr;
201  if(f!=NULL)
202  {
203  t = f(instance);
204  }
205  return t;
206  }
207  void* execOperator(void* instance,string operato,vals values,string classn)
208  {
209  void *resul = NULL;
210  if(operato=="<")
211  {
212  string libName = Constants::INTER_LIB_FILE;
213  void *dlib = dlopen(libName.c_str(), RTLD_NOW);
214  if(dlib == NULL)
215  {
216  cerr << dlerror() << endl;
217  exit(-1);
218  }
219  string opname = "operator"+classn+"LT";
220  void *mkr = dlsym(dlib, opname.c_str());
221  typedef void* (*RfPtr) (void*,vals);
222  RfPtr f = (RfPtr)mkr;
223  if(f!=NULL)
224  {
225  resul = f(instance,values);
226  }
227  }
228  else if(operato==">")
229  {
230  string libName = Constants::INTER_LIB_FILE;
231  void *dlib = dlopen(libName.c_str(), RTLD_NOW);
232  if(dlib == NULL)
233  {
234  cerr << dlerror() << endl;
235  exit(-1);
236  }
237  string opname = "operator"+classn+"GT";
238  void *mkr = dlsym(dlib, opname.c_str());
239  typedef void* (*RfPtr) (void*,vals);
240  RfPtr f = (RfPtr)mkr;
241  if(f!=NULL)
242  {
243  resul = f(instance,values);
244  }
245  }
246  else if(operato=="==")
247  {
248  string libName = Constants::INTER_LIB_FILE;
249  void *dlib = dlopen(libName.c_str(), RTLD_NOW);
250  if(dlib == NULL)
251  {
252  cerr << dlerror() << endl;
253  exit(-1);
254  }
255  string opname = "operator"+classn+"EQ";
256  void *mkr = dlsym(dlib, opname.c_str());
257  typedef void* (*RfPtr) (void*,vals);
258  RfPtr f = (RfPtr)mkr;
259  if(f!=NULL)
260  {
261  resul = f(instance,values);
262  }
263  }
264  else if(operato=="!=")
265  {
266  string libName = Constants::INTER_LIB_FILE;
267  void *dlib = dlopen(libName.c_str(), RTLD_NOW);
268  if(dlib == NULL)
269  {
270  cerr << dlerror() << endl;
271  exit(-1);
272  }
273  string opname = "operator"+classn+"NE";
274  void *mkr = dlsym(dlib, opname.c_str());
275  typedef void* (*RfPtr) (void*,vals);
276  RfPtr f = (RfPtr)mkr;
277  if(f!=NULL)
278  {
279  resul = f(instance,values);
280  }
281  }
282  else if(operato=="<=")
283  {
284  string libName = Constants::INTER_LIB_FILE;
285  void *dlib = dlopen(libName.c_str(), RTLD_NOW);
286  if(dlib == NULL)
287  {
288  cerr << dlerror() << endl;
289  exit(-1);
290  }
291  string opname = "operator"+classn+"LE";
292  void *mkr = dlsym(dlib, opname.c_str());
293  typedef void* (*RfPtr) (void*,vals);
294  RfPtr f = (RfPtr)mkr;
295  if(f!=NULL)
296  {
297  resul = f(instance,values);
298  }
299  }
300  else if(operato==">=")
301  {
302  string libName = Constants::INTER_LIB_FILE;
303  void *dlib = dlopen(libName.c_str(), RTLD_NOW);
304  if(dlib == NULL)
305  {
306  cerr << dlerror() << endl;
307  exit(-1);
308  }
309  string opname = "operator"+classn+"GE";
310  void *mkr = dlsym(dlib, opname.c_str());
311  typedef void* (*RfPtr) (void*,vals);
312  RfPtr f = (RfPtr)mkr;
313  if(f!=NULL)
314  {
315  resul = f(instance,values);
316  }
317  }
318  else if(operato=="!")
319  {
320  string libName = Constants::INTER_LIB_FILE;
321  void *dlib = dlopen(libName.c_str(), RTLD_NOW);
322  if(dlib == NULL)
323  {
324  cerr << dlerror() << endl;
325  exit(-1);
326  }
327  string opname = "operator"+classn+"NT";
328  void *mkr = dlsym(dlib, opname.c_str());
329  typedef void* (*RfPtr) (void*,vals);
330  RfPtr f = (RfPtr)mkr;
331  if(f!=NULL)
332  {
333  resul = f(instance,values);
334  }
335  }
336  else if(operato=="<<")
337  {
338 
339  }
340  else if(operato==">>")
341  {
342 
343  }
344  else if(operato=="+")
345  {
346  string libName = Constants::INTER_LIB_FILE;
347  void *dlib = dlopen(libName.c_str(), RTLD_NOW);
348  if(dlib == NULL)
349  {
350  cerr << dlerror() << endl;
351  exit(-1);
352  }
353  string opname = "operator"+classn+"AD";
354  void *mkr = dlsym(dlib, opname.c_str());
355  typedef void* (*RfPtr) (void*,vals);
356  RfPtr f = (RfPtr)mkr;
357  if(f!=NULL)
358  {
359  resul = f(instance,values);
360  }
361  }
362  else if(operato=="-")
363  {
364  string libName = Constants::INTER_LIB_FILE;
365  void *dlib = dlopen(libName.c_str(), RTLD_NOW);
366  if(dlib == NULL)
367  {
368  cerr << dlerror() << endl;
369  exit(-1);
370  }
371  string opname = "operator"+classn+"SU";
372  void *mkr = dlsym(dlib, opname.c_str());
373  typedef void* (*RfPtr) (void*,vals);
374  RfPtr f = (RfPtr)mkr;
375  if(f!=NULL)
376  {
377  resul = f(instance,values);
378  }
379  }
380  else if(operato=="/")
381  {
382  string libName = Constants::INTER_LIB_FILE;
383  void *dlib = dlopen(libName.c_str(), RTLD_NOW);
384  if(dlib == NULL)
385  {
386  cerr << dlerror() << endl;
387  exit(-1);
388  }
389  string opname = "operator"+classn+"DI";
390  void *mkr = dlsym(dlib, opname.c_str());
391  typedef void* (*RfPtr) (void*,vals);
392  RfPtr f = (RfPtr)mkr;
393  if(f!=NULL)
394  {
395  resul = f(instance,values);
396  }
397  }
398  else if(operato=="*")
399  {
400  string libName = Constants::INTER_LIB_FILE;
401  void *dlib = dlopen(libName.c_str(), RTLD_NOW);
402  if(dlib == NULL)
403  {
404  cerr << dlerror() << endl;
405  exit(-1);
406  }
407  string opname = "operator"+classn+"MU";
408  void *mkr = dlsym(dlib, opname.c_str());
409  typedef void* (*RfPtr) (void*,vals);
410  RfPtr f = (RfPtr)mkr;
411  if(f!=NULL)
412  {
413  resul = f(instance,values);
414  }
415  }
416  else if(operato=="&&")
417  {
418 
419  }
420  else if(operato=="&")
421  {
422 
423  }
424  else if(operato=="||")
425  {
426 
427  }
428  else if(operato=="|")
429  {
430 
431  }
432  else if(operato=="[]")
433  {
434 
435  }
436  else if(operato=="()")
437  {
438 
439  }
440  return resul;
441  }
442 
443  void vectorPushBack(void* vec,void* instance,string classN)
444  {
445  string libName = Constants::INTER_LIB_FILE;
446  void *dlib = dlopen(libName.c_str(), RTLD_NOW);
447  if(dlib == NULL)
448  {
449  cerr << dlerror() << endl;
450  exit(-1);
451  }
452  string methodname = "invokeAdToVecFor"+classN;
453  void *mkr = dlsym(dlib, methodname.c_str());
454  typedef void* (*RfPtr) (void*,void*);
455  RfPtr f = (RfPtr)mkr;
456  if(f!=NULL)
457  {
458  f(vec,instance);
459  }
460  }
461  void* getNewVector(string classN)
462  {
463  void *obj = NULL;
464  string libName = Constants::INTER_LIB_FILE;
465  void *dlib = dlopen(libName.c_str(), RTLD_NOW);
466  if(dlib == NULL)
467  {
468  cerr << dlerror() << endl;
469  exit(-1);
470  }
471  string methodname = "invokeGetNewVecFor"+classN;
472  void *mkr = dlsym(dlib, methodname.c_str());
473  typedef void* (*RfPtr) ();
474  RfPtr f = (RfPtr)mkr;
475  if(f!=NULL)
476  {
477  obj = f();
478  }
479  return obj;
480  }
481  int getVectorSize(void* vec,string classN)
482  {
483  int obj = 0;
484  string libName = Constants::INTER_LIB_FILE;
485  void *dlib = dlopen(libName.c_str(), RTLD_NOW);
486  if(dlib == NULL)
487  {
488  cerr << dlerror() << endl;
489  exit(-1);
490  }
491  string methodname = "invokeGetVecSizeFor"+classN;
492  void *mkr = dlsym(dlib, methodname.c_str());
493  typedef int (*RfPtr) (void*);
494  RfPtr f = (RfPtr)mkr;
495  if(f!=NULL)
496  {
497  obj = f(vec);
498  }
499  return obj;
500  }
501  void* getVectorElement(void* vec,int pos,string classN)
502  {
503  void *obj = NULL;
504  string libName = Constants::INTER_LIB_FILE;
505  void *dlib = dlopen(libName.c_str(), RTLD_NOW);
506  if(dlib == NULL)
507  {
508  cerr << dlerror() << endl;
509  exit(-1);
510  }
511  string methodname = "invokeGetVecElementFor"+classN;
512  void *mkr = dlsym(dlib, methodname.c_str());
513  typedef void* (*RfPtr) (void*,int);
514  RfPtr f = (RfPtr)mkr;
515  if(f!=NULL)
516  {
517  obj = f(vec,pos);
518  }
519  return obj;
520  }
521 };
522 #endif