libqi-api  2.4.3.28
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
typeinterface.hpp
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 #ifndef _QI_TYPE_TYPEINTERFACE_HPP_
8 #define _QI_TYPE_TYPEINTERFACE_HPP_
9 
10 #include <typeinfo>
11 #include <string>
12 
13 #include <boost/preprocessor.hpp>
14 #include <boost/function.hpp>
15 #include <boost/type_traits/is_function.hpp>
16 #include <boost/mpl/if.hpp>
17 
18 #include <qi/log.hpp>
19 #include <qi/api.hpp>
20 #include <qi/type/fwd.hpp>
21 #include <qi/signature.hpp>
23 
24 #ifdef _MSC_VER
25 # pragma warning( push )
26 # pragma warning( disable: 4251 )
27  // C4503 decorated name length exceeded, name was truncated
28  // The only workaround is to make structs to hide the template complexity
29  // We don't want to have to do that
30 # pragma warning( disable: 4503 )
31 #endif
32 
33 /* A lot of class are found in this headers... to kill circular dependencies.
34  Futhermore we need that all "default template" types are registered (included)
35  when type.hpp is used. (for typeOf to works reliably)
36 */
37 
38 namespace qi{
39 
42  #define QI_TYPE_NOT_CONSTRUCTIBLE(T) \
43  namespace qi { namespace detail { \
44  template<> struct TypeManager<T>: public TypeManagerNotConstructible<T> {};}}
45 
48  #define QI_NO_TYPE(T) namespace qi {template<> class TypeImpl<T>: public detail::ForbiddenInTypeSystem {};}
49 
52  #define QI_TYPE_INTERFACE(T) \
53  namespace qi { namespace detail { \
54  template<> struct TypeManager<T>: public TypeManagerDefaultInterface<T> {};}}
55 
58  #define QI_TYPE_CONCRETE(T) \
59  namespace qi { namespace detail { \
60  template<> struct TypeManager<T>: public TypeManagerDefaultStruct<T> {}; }}
61 
64  #define QI_TYPE_REGISTER(t) \
65  QI_TYPE_REGISTER_CUSTOM(t, qi::TypeImpl<t>)
66 
69  #define QI_TYPE_REGISTER_CUSTOM(type, typeimpl) \
70  static bool BOOST_PP_CAT(__qi_registration, __LINE__) = qi::registerType(typeid(type), new typeimpl)
71 
72 
73  class ListTypeInterface;
74  class StructTypeInterface;
75 
76  // Interfaces for specialized types
78  {
79  public:
81  virtual int64_t get(void* value) = 0;
83  virtual unsigned int size() = 0;
85  virtual bool isSigned() = 0;
87  virtual void set(void** storage, int64_t value) = 0;
88  virtual TypeKind kind() { return TypeKind_Int;}
89  };
90 
92  {
93  public:
95  virtual double get(void* value) = 0;
97  virtual unsigned int size() = 0; // size in bytes
99  virtual void set(void** storage, double value) = 0;
100  virtual TypeKind kind() { return TypeKind_Float;}
101  };
102 
104  {
105  public:
106  typedef std::pair<char*, size_t> RawString;
107  typedef boost::function<void(const RawString&)> Deleter;
108  typedef std::pair<RawString, Deleter> ManagedRawString;
109 
111  std::string getString(void* storage);
114  virtual ManagedRawString get(void* storage) = 0;
116  void set(void** storage, const std::string& value);
118  virtual void set(void** storage, const char* ptr, size_t sz) = 0;
119  virtual TypeKind kind() { return TypeKind_String; }
120 
121  };
122 
127  {
128  public:
130  virtual std::pair<char*, size_t> get(void* storage) = 0;
132  virtual void set(void** storage, const char* ptr, size_t sz) = 0;
133  virtual TypeKind kind() { return TypeKind_Raw; }
134  };
135 
137  {
138  public:
140  {
143  };
145  virtual PointerKind pointerKind() = 0;
147  virtual TypeInterface* pointedType() = 0;
149  virtual AnyReference dereference(void* storage) = 0;
151  virtual void setPointee(void** storage, void* pointer) = 0;
152  virtual TypeKind kind() { return TypeKind_Pointer; }
153  };
154 
162  {
163  public:
172  virtual AnyReference dereference(void* storage) = 0;
174  virtual void next(void** storage) = 0;
176  virtual bool equals(void* s1, void* s2) = 0;
177  virtual TypeKind kind() { return TypeKind_Iterator; }
178  };
179 
186  {
187  public:
189  virtual TypeInterface* elementType() = 0;
191  virtual size_t size(void* storage) = 0;
193  virtual AnyIterator begin(void* storage) = 0;
196  virtual AnyIterator end(void* storage) = 0;
198  virtual void pushBack(void** storage, void* valueStorage) = 0;
200  virtual void* element(void* storage, int index);
201  virtual TypeKind kind() { return TypeKind_List;}
202  };
203 
211  {
212  public:
214  virtual TypeInterface* elementType() = 0;
216  virtual TypeInterface* keyType() = 0;
218  virtual size_t size(void* storage) = 0;
220  virtual AnyIterator begin(void* storage) = 0;
223  virtual AnyIterator end(void* storage) = 0;
225  virtual void insert(void** storage, void* keyStorage, void* valueStorage) = 0;
232  virtual AnyReference element(void** storage, void* keyStorage, bool autoInsert) = 0;
233  virtual TypeKind kind() { return TypeKind_Map; }
234  // Since our typesystem has no erased operator < or operator ==,
235  // MapTypeInterface does not provide a find()
236  };
237 
239  {
240  public:
242  AnyReferenceVector values(void* storage);
250  virtual std::vector<TypeInterface*> memberTypes() = 0;
252  virtual std::vector<void*> get(void* storage);
254  virtual void* get(void* storage, unsigned int index) = 0;
256  virtual void set(void** storage, const std::vector<void*>&);
258  virtual void set(void** storage, unsigned int index, void* valStorage) = 0;
259  virtual TypeKind kind() { return TypeKind_Tuple; }
261  virtual std::vector<std::string> elementsName() { return std::vector<std::string>();}
263  virtual std::string className() { return std::string(); }
264 
284  virtual bool canDropFields(void* storage, const std::vector<std::string>& fieldNames) { return false;}
287  virtual bool fillMissingFields(std::map<std::string, AnyValue>& fields, const std::vector<std::string>& missing) { return false;}
288 
290  };
291 
298  {
299  public:
301  virtual AnyReference get(void* storage) = 0;
303  virtual void set(void** storage, AnyReference source) = 0;
304  virtual TypeKind kind() { return TypeKind_Dynamic; }
305  };
306 
311  {
312  public:
313  //virtual AnyReference get(void *storage) = 0;
314  //virtual TypeInterface* elementType() = 0;
315  virtual TypeKind kind() { return TypeKind_VarArgs; }
316  };
317 
321  QI_API TypeInterface* makeTypeOfKind(const qi::TypeKind& kind);
322 
324  QI_API TypeInterface* makeFloatType(int bytelen);
325 
327  QI_API TypeInterface* makeIntType(bool issigned, int bytelen);
328 
330  QI_API TypeInterface* makeVarArgsType(TypeInterface* elementType);
331 
333  QI_API TypeInterface* makeListType(TypeInterface* elementType);
334 
336  QI_API TypeInterface* makeMapType(TypeInterface* keyType, TypeInterface* ElementType);
337 
339  QI_API TypeInterface* makeTupleType(const std::vector<TypeInterface*>& memberTypes, const std::string &name = std::string(), const std::vector<std::string>& elementNames = std::vector<std::string>());
340 
341 
342 
346 #define QI_TEMPLATE_TYPE_DECLARE(n) \
347  namespace qi \
348  { \
349  template <typename T> \
350  class QITYPE_TEMPLATE_API TypeImpl<n<T> > : public TypeOfTemplateImpl<n, T> \
351  { \
352  }; \
353  }
354 
358 #define QI_TEMPLATE_TYPE_GET(typeInst, templateName) \
359  dynamic_cast< ::qi::TypeOfTemplate<templateName>*>(typeInst)
360 
361 
365  #define QI_TYPE_ENUM_REGISTER(Enum) \
366  namespace qi { \
367  template<> class TypeImpl<Enum>: public IntTypeInterfaceImpl<int> {}; \
368  }
369 
370 #define QI_TYPE_STRUCT_DECLARE(name) \
371  __QI_TYPE_STRUCT_DECLARE(name, )
372 
373 }
374 
375 
377 #include <qi/type/detail/type.hxx>
386 
389 
390 #ifdef _MSC_VER
391 # pragma warning( pop )
392 // restore the disabling of this warning
393 # pragma warning( disable: 4503 )
394 #endif
395 
396 #endif // _QITYPE_TYPEINTERFACE_HPP_
virtual std::string className()
Get the type name of the struct.
TypeInterface * makeTupleType(const std::vector< TypeInterface * > &memberTypes, const std::string &name=std::string(), const std::vector< std::string > &elementNames=std::vector< std::string >())
#define QI_API
Definition: api.hpp:34
std::pair< RawString, Deleter > ManagedRawString
void pushBack(T &container, E *element)
TypeInterface * makeFloatType(int bytelen)
virtual TypeKind kind()
virtual TypeKind kind()
dll import/export and compiler message
virtual TypeKind kind()
TypeInterface * makeTypeOfKind(const qi::TypeKind &kind)
virtual TypeKind kind()
boost::function< void(const RawString &)> Deleter
virtual TypeKind kind()
virtual TypeKind kind()
TypeInterface * makeMapType(TypeInterface *keyType, TypeInterface *ElementType)
std::vector< AnyReference > AnyReferenceVector
std::pair< char *, size_t > RawString
virtual bool fillMissingFields(std::map< std::string, AnyValue > &fields, const std::vector< std::string > &missing)
Fill missing fields caused by conversion from a different struct. Return whether fill succeeded...
TypeInterface * makeVarArgsType(TypeInterface *elementType)
virtual std::vector< std::string > elementsName()
Get the names of the fields of the struct.
TypeInterface * makeIntType(bool issigned, int bytelen)
TypeInterface * makeListType(TypeInterface *elementType)
TypeKind
Definition: fwd.hpp:53
virtual TypeKind kind()
Convenient log macro.
virtual TypeKind kind()
virtual TypeKind kind()
virtual TypeKind kind()
virtual TypeKind kind()
#define QI_NO_TYPE(T)
int64_t int64_t
Cross-platform signed integer of length 64 bits (8 bytes).
Definition: types.hpp:76