libqi-api  release-2.5.3-2016-11-18
 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  TypeKind kind() override { 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  TypeKind kind() override { return TypeKind_Float;}
101  };
102 
104  {
105  public:
106  using RawString = std::pair<char*, size_t>;
107  using Deleter = boost::function<void(const RawString&)>;
108  using ManagedRawString = std::pair<RawString, Deleter>;
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  TypeKind kind() override { 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  TypeKind kind() override { 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 set(void** storage, AnyReference pointer) = 0;
153  virtual void setPointee(void** storage, void* pointer) = 0;
154  TypeKind kind() override { return TypeKind_Pointer; }
155  };
156 
164  {
165  public:
174  virtual AnyReference dereference(void* storage) = 0;
176  virtual void next(void** storage) = 0;
178  virtual bool equals(void* s1, void* s2) = 0;
179  TypeKind kind() override { return TypeKind_Iterator; }
180  };
181 
188  {
189  public:
191  virtual TypeInterface* elementType() = 0;
193  virtual size_t size(void* storage) = 0;
195  virtual AnyIterator begin(void* storage) = 0;
198  virtual AnyIterator end(void* storage) = 0;
200  virtual void pushBack(void** storage, void* valueStorage) = 0;
202  virtual void* element(void* storage, int index);
203  TypeKind kind() override { return TypeKind_List;}
204  };
205 
213  {
214  public:
216  virtual TypeInterface* elementType() = 0;
218  virtual TypeInterface* keyType() = 0;
220  virtual size_t size(void* storage) = 0;
222  virtual AnyIterator begin(void* storage) = 0;
225  virtual AnyIterator end(void* storage) = 0;
227  virtual void insert(void** storage, void* keyStorage, void* valueStorage) = 0;
234  virtual AnyReference element(void** storage, void* keyStorage, bool autoInsert) = 0;
235  TypeKind kind() override { return TypeKind_Map; }
236  // Since our typesystem has no erased operator < or operator ==,
237  // MapTypeInterface does not provide a find()
238  };
239 
241  {
242  public:
244  AnyReferenceVector values(void* storage);
252  virtual std::vector<TypeInterface*> memberTypes() = 0;
254  virtual std::vector<void*> get(void* storage);
256  virtual void* get(void* storage, unsigned int index) = 0;
258  virtual void set(void** storage, const std::vector<void*>&);
260  virtual void set(void** storage, unsigned int index, void* valStorage) = 0;
261  TypeKind kind() override { return TypeKind_Tuple; }
263  virtual std::vector<std::string> elementsName() { return std::vector<std::string>();}
265  virtual std::string className() { return std::string(); }
266 
286  virtual bool convertFrom(std::map<std::string, ::qi::AnyValue>& fields,
288  const std::vector<std::tuple<std::string, TypeInterface*>>& missing,
289  const std::map<std::string, ::qi::AnyReference>& dropfields)
290  {
291  return false;
292  }
294  virtual bool convertTo(std::map<std::string, ::qi::AnyValue>& fields,
295  const std::vector<std::tuple<std::string, TypeInterface*>>& missing,
296  const std::map<std::string, ::qi::AnyReference>& dropfields)
297  {
298  return false;
299  }
300 
302  };
303 
310  {
311  public:
313  virtual AnyReference get(void* storage) = 0;
315  virtual void set(void** storage, AnyReference source) = 0;
316  TypeKind kind() override { return TypeKind_Dynamic; }
317  };
318 
323  {
324  public:
325  //virtual AnyReference get(void *storage) = 0;
326  //virtual TypeInterface* elementType() = 0;
327  TypeKind kind() override { return TypeKind_VarArgs; }
328  };
329 
333  QI_API TypeInterface* makeTypeOfKind(const qi::TypeKind& kind);
334 
336  QI_API TypeInterface* makeFloatType(int bytelen);
337 
339  QI_API TypeInterface* makeIntType(bool issigned, int bytelen);
340 
342  QI_API TypeInterface* makeVarArgsType(TypeInterface* elementType);
343 
345  QI_API TypeInterface* makeListType(TypeInterface* elementType);
346 
348  QI_API TypeInterface* makeMapType(TypeInterface* keyType, TypeInterface* ElementType);
349 
351  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>());
352 
353 
354 
358 #define QI_TEMPLATE_TYPE_DECLARE(n) \
359  namespace qi \
360  { \
361  template <typename T> \
362  class QITYPE_TEMPLATE_API TypeImpl<n<T> > : public TypeOfTemplateImpl<n, T> \
363  { \
364  }; \
365  }
366 
370 #define QI_TEMPLATE_TYPE_GET(typeInst, templateName) \
371  dynamic_cast< ::qi::TypeOfTemplate<templateName>*>(typeInst)
372 
376 #define QI_TYPE_ENUM(Enum) \
377  namespace qi \
378  { \
379  template <> \
380  class TypeImpl<Enum> : public IntTypeInterfaceImpl<int> \
381  { \
382  }; \
383  }
384 
385 namespace detail
386 {
387  struct QI_API_DEPRECATED_MSG(Use 'QI_TYPE_ENUM' instead) QI_TYPE_ENUM_REGISTER_ {};
388 }
389 
390 #define QI_TYPE_ENUM_REGISTER(Enum) \
391  namespace qi \
392  { \
393  template <> \
394  class TypeImpl<Enum> : public IntTypeInterfaceImpl<int> \
395  { \
396  static const detail::QI_TYPE_ENUM_REGISTER_ BLAH; \
397  }; \
398  }
399 
400 #define QI_TYPE_STRUCT_DECLARE(name) \
401  __QI_TYPE_STRUCT_DECLARE(name, )
402 
403 }
404 
405 
407 #include <qi/type/detail/type.hxx>
416 
419 
420 #ifdef _MSC_VER
421 # pragma warning( pop )
422 // restore the disabling of this warning
423 # pragma warning( disable: 4503 )
424 #endif
425 
426 #endif // _QITYPE_TYPEINTERFACE_HPP_
TypeKind kind() override
virtual std::string className()
Get the type name of the struct.
TypeKind kind() override
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_TYPE_ENUM(Enum)
int64_t int64_t
Definition: types.hpp:61
#define QI_API
Definition: api.hpp:33
void pushBack(T &container, E *element)
TypeInterface * makeFloatType(int bytelen)
boost::function< void(const RawString &)> Deleter
dll import/export and compiler message
TypeKind kind() override
TypeInterface * makeTypeOfKind(const qi::TypeKind &kind)
std::pair< RawString, Deleter > ManagedRawString
TypeKind kind() override
TypeKind kind() override
TypeInterface * makeMapType(TypeInterface *keyType, TypeInterface *ElementType)
std::vector< AnyReference > AnyReferenceVector
TypeKind kind() override
TypeKind kind() override
std::pair< char *, size_t > RawString
TypeInterface * makeVarArgsType(TypeInterface *elementType)
TypeKind kind() override
virtual std::vector< std::string > elementsName()
Get the names of the fields of the struct.
TypeInterface * makeIntType(bool issigned, int bytelen)
struct QI_API_DEPRECATED_MSG(Use 'QI_TYPE_ENUM'instead) QI_TYPE_ENUM_REGISTER_
TypeInterface * makeListType(TypeInterface *elementType)
TypeKind kind() override
TypeKind kind() override
TypeKind
Definition: fwd.hpp:53
Convenient log macro.
virtual bool convertTo(std::map< std::string,::qi::AnyValue > &fields, const std::vector< std::tuple< std::string, TypeInterface * >> &missing, const std::map< std::string,::qi::AnyReference > &dropfields)
Fill missing fields caused by conversion to a different struct. Return whether fill succeeded...
TypeKind kind() override
#define QI_NO_TYPE(T)