libqi-api  2.4.3.28
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
typedispatcher.hxx
Go to the documentation of this file.
1 #pragma once
2 /*
3 ** Copyright (C) 2013 Aldebaran Robotics
4 ** See COPYING for the license
5 */
6 
7 #include <qi/type/typeobject.hpp>
8 #include <qi/anyobject.hpp>
9 
10 namespace qi {
11 
12  template<typename TypeDispatcher>
13  TypeDispatcher& typeDispatch(const TypeDispatcher &vv, AnyReference value)
14  {
15  if (!value.type())
16  throw std::runtime_error("NULL type");
17  TypeDispatcher& v = const_cast<TypeDispatcher&>(vv);
18  switch(value.kind())
19  {
20  case TypeKind_Void:
21  v.visitVoid();
22  break;
23  case TypeKind_Unknown:
24  v.visitUnknown(value);
25  break;
26  case TypeKind_Int:
27  {
28  IntTypeInterface* tint = static_cast<IntTypeInterface*>(value.type());
29 
30  v.visitInt(value.toInt(), tint->isSigned(), tint->size());
31  break;
32  }
33  case TypeKind_Float:
34  {
35  FloatTypeInterface* tfloat = static_cast<FloatTypeInterface*>(value.type());
36  v.visitFloat(value.toDouble(), tfloat->size());
37  break;
38  }
39  case TypeKind_String:
40  {
41  StringTypeInterface* tstring = static_cast<StringTypeInterface*>(value.type());
42  StringTypeInterface::ManagedRawString content = tstring->get(value.rawValue());
43  v.visitString(content.first.first, content.first.second);
44  if (content.second)
45  content.second(content.first);
46  break;
47  }
48  case TypeKind_List:
49  {
50  v.visitList(value.begin(), value.end());
51  break;
52  }
53  case TypeKind_Map:
54  {
55  v.visitMap(value.begin(), value.end());
56  break;
57  }
58  case TypeKind_Object:
59  {
60  v.visitObject(GenericObject(static_cast<ObjectTypeInterface*>(value.type()), value.rawValue()));
61  break;
62  }
63  case TypeKind_Pointer:
64  {
65  AnyReference pointee = *value;
66  PointerTypeInterface* type = static_cast<PointerTypeInterface*>(value.type());
68  && pointee.kind() == TypeKind_Object)
69  { // shared_ptr<Foo> p with Foo object type.
70  // Create our own shared_ptr, that holds p and delete it on destruction
71  qiLogDebug("qitype.typedispatcher") << "Detected object shared ptr";
72  AnyReference shared_ptr = value.clone();
73  AnyObject ao(new GenericObject(static_cast<ObjectTypeInterface*>(pointee.type()), pointee.rawValue()),
74  boost::bind(&AnyObject::deleteCustomDeleter, _1, (boost::function<void(Empty*)>)boost::bind(&AnyReference::destroy, shared_ptr)));
75  v.visitAnyObject(ao);
76  }
77  else
78  v.visitPointer(pointee);
79  break;
80  }
81  case TypeKind_Tuple:
82  {
83  StructTypeInterface* ttuple = static_cast<StructTypeInterface*>(value.type());
84  AnyReferenceVector tuple = ttuple->values(value.rawValue());
85  v.visitTuple(ttuple->className(), tuple, ttuple->elementsName());
86  break;
87  }
88  case TypeKind_Dynamic:
89  {
90  if (value.type()->info() == typeOf<AnyObject>()->info())
91  {
92  AnyObject* o = value.ptr<AnyObject>(false);
93  v.visitAnyObject(*o);
94  }
95  else
96  v.visitDynamic(value.content());
97  break;
98  }
99  case TypeKind_Raw:
100  {
101  v.visitRaw(value);
102  break;
103  }
104  case TypeKind_Iterator:
105  {
106  v.visitIterator(value);
107  break;
108  }
109  case TypeKind_VarArgs:
110  v.visitVarArgs(value.begin(), value.end());
111  break;
112  case TypeKind_Function:
113  case TypeKind_Signal:
114  case TypeKind_Property:
115  qiLogError("qitype.typedispatcher") << "Signal and Property not handled";
116 
117  }
118  return v;
119  }
120 
121 }
virtual std::string className()
Get the type name of the struct.
std::pair< RawString, Deleter > ManagedRawString
virtual unsigned int size()=0
Return the size in bytes.
T * ptr(bool check=true)
boost::function< RF > bind(const AF &fun,...)
virtual bool isSigned()=0
Return true if the integer is signed.
#define qiLogDebug(...)
Definition: log.hpp:69
TypeDispatcher & typeDispatch(const TypeDispatcher &vv, AnyReference value)
AnyReferenceVector values(void *storage)
Get all the fields of the structure.
void destroy()
Deletes storage.
#define qiLogError(...)
Log in error mode.
Definition: log.hpp:113
virtual unsigned int size()=0
Return the size in bytes.
AnyIterator begin() const
Return an iterator on the beginning of the container.
std::vector< AnyReference > AnyReferenceVector
TypeInterface * type() const
virtual ManagedRawString get(void *storage)=0
virtual const TypeInfo & info()=0
Get the TypeInfo corresponding to this type.
virtual std::vector< std::string > elementsName()
Get the names of the fields of the struct.
AnyReference content() const
AnyIterator end() const
Return an iterator on the end of the container.
AnyReference clone() const
virtual PointerKind pointerKind()=0
Return whether the pointer has raw or shared semantics.
static void deleteCustomDeleter(GenericObject *obj, boost::function< void(Empty *)> deleter)
Definition: object.hxx:282