libqi-api  2.8.7.4
 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(TypeDispatcher &v, AnyReference value)
14  {
15  if (!value.type())
16  throw std::runtime_error("NULL type");
17  switch(value.kind())
18  {
19  case TypeKind_Void:
20  v.visitVoid();
21  break;
22  case TypeKind_Unknown:
23  v.visitUnknown(value);
24  break;
25  case TypeKind_Int:
26  {
27  IntTypeInterface* tint = static_cast<IntTypeInterface*>(value.type());
28 
29  v.visitInt(value.toInt(), tint->isSigned(), tint->size());
30  break;
31  }
32  case TypeKind_Float:
33  {
34  FloatTypeInterface* tfloat = static_cast<FloatTypeInterface*>(value.type());
35  v.visitFloat(value.toDouble(), tfloat->size());
36  break;
37  }
38  case TypeKind_String:
39  {
40  StringTypeInterface* tstring = static_cast<StringTypeInterface*>(value.type());
41  StringTypeInterface::ManagedRawString content = tstring->get(value.rawValue());
42  v.visitString(content.first.first, content.first.second);
43  if (content.second)
44  content.second(content.first);
45  break;
46  }
47  case TypeKind_List:
48  {
49  v.visitList(value.begin(), value.end());
50  break;
51  }
52  case TypeKind_Map:
53  {
54  v.visitMap(value.begin(), value.end());
55  break;
56  }
57  case TypeKind_Object:
58  {
59  v.visitObject(GenericObject(static_cast<ObjectTypeInterface*>(value.type()), value.rawValue()));
60  break;
61  }
62  case TypeKind_Pointer:
63  {
64  AnyReference pointee = *value;
65  PointerTypeInterface* type = static_cast<PointerTypeInterface*>(value.type());
67  && pointee.kind() == TypeKind_Object)
68  { // shared_ptr<Foo> p with Foo object type.
69  // Create our own shared_ptr, that holds p and delete it on destruction
70  qiLogDebug("qitype.typedispatcher") << "Detected object shared ptr";
71  AnyReference shared_ptr = value.clone();
72  AnyObject ao(new GenericObject(static_cast<ObjectTypeInterface*>(pointee.type()), pointee.rawValue()),
73  boost::bind(&AnyObject::deleteCustomDeleter, _1, (boost::function<void(Empty*)>)boost::bind(&AnyReference::destroy, shared_ptr)));
74  v.visitAnyObject(ao);
75  }
76  else
77  v.visitPointer(pointee);
78  break;
79  }
80  case TypeKind_Tuple:
81  {
82  StructTypeInterface* ttuple = static_cast<StructTypeInterface*>(value.type());
83  AnyReferenceVector tuple = ttuple->values(value.rawValue());
84  v.visitTuple(ttuple->className(), tuple, ttuple->elementsName());
85  break;
86  }
87  case TypeKind_Dynamic:
88  {
89  if (value.type()->info() == typeOf<AnyObject>()->info())
90  {
91  AnyObject* o = value.ptr<AnyObject>(false);
92  v.visitAnyObject(*o);
93  }
94  else
95  v.visitDynamic(value.content());
96  break;
97  }
98  case TypeKind_Raw:
99  {
100  v.visitRaw(value);
101  break;
102  }
103  case TypeKind_Iterator:
104  {
105  v.visitIterator(value);
106  break;
107  }
108  case TypeKind_VarArgs:
109  v.visitVarArgs(value.begin(), value.end());
110  break;
111  case TypeKind_Optional:
112  v.visitOptional(value);
113  break;
114 
115  case TypeKind_Function:
116  case TypeKind_Signal:
117  case TypeKind_Property:
118  qiLogError("qitype.typedispatcher") << "Signal and Property not handled";
119 
120  }
121  return v;
122  }
123 
124 }
virtual std::string className()
Get the type name of the struct.
virtual unsigned int size()=0
Return the size in bytes.
T * ptr(bool check=true)
virtual bool isSigned()=0
Return true if the integer is signed.
TypeDispatcher & typeDispatch(TypeDispatcher &v, AnyReference value)
#define qiLogDebug(...)
Definition: log.hpp:76
AnyReferenceVector values(void *storage)
Get all the fields of the structure.
std::pair< RawString, Deleter > ManagedRawString
void destroy()
Deletes storage.
#define qiLogError(...)
Log in error mode.
Definition: log.hpp:120
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.
std::enable_if< std::is_function< RF >::value, boost::function< RF > >::type bind(AF &&fun, Arg0 &&arg0, Args &&...args)
Definition: trackable.hxx:308
static void deleteCustomDeleter(GenericObject *obj, boost::function< void(Empty *)> deleter)
Definition: object.hxx:318