libqi-api  2.8.7.4
 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_ATTR_UNUSED \
71  = qi::registerType(qi::typeId<type>(), new typeimpl)
72 
73 
74  class ListTypeInterface;
75  class StructTypeInterface;
76 
77  // Interfaces for specialized types
79  {
80  public:
82  virtual int64_t get(void* value) = 0;
84  virtual unsigned int size() = 0;
86  virtual bool isSigned() = 0;
88  virtual void set(void** storage, int64_t value) = 0;
89  TypeKind kind() override { return TypeKind_Int;}
90  };
91 
93  {
94  public:
96  virtual double get(void* value) = 0;
98  virtual unsigned int size() = 0; // size in bytes
100  virtual void set(void** storage, double value) = 0;
101  TypeKind kind() override { return TypeKind_Float;}
102  };
103 
105  {
106  public:
107  using RawString = std::pair<char*, size_t>;
108  using Deleter = boost::function<void(const RawString&)>;
109  using ManagedRawString = std::pair<RawString, Deleter>;
110 
112  std::string getString(void* storage);
115  virtual ManagedRawString get(void* storage) = 0;
117  void set(void** storage, const std::string& value);
119  virtual void set(void** storage, const char* ptr, size_t sz) = 0;
120  TypeKind kind() override { return TypeKind_String; }
121 
122  };
123 
128  {
129  public:
131  virtual std::pair<char*, size_t> get(void* storage) = 0;
133  virtual void set(void** storage, const char* ptr, size_t sz) = 0;
134  TypeKind kind() override { return TypeKind_Raw; }
135  };
136 
138  {
139  public:
141  {
144  };
146  virtual PointerKind pointerKind() = 0;
148  virtual TypeInterface* pointedType() = 0;
150  virtual AnyReference dereference(void* storage) = 0;
152  virtual void set(void** storage, AnyReference pointer) = 0;
154  virtual void setPointee(void** storage, void* pointer) = 0;
155  TypeKind kind() override { return TypeKind_Pointer; }
156  };
157 
165  {
166  public:
175  virtual AnyReference dereference(void* storage) = 0;
177  virtual void next(void** storage) = 0;
179  virtual bool equals(void* s1, void* s2) = 0;
180  TypeKind kind() override { return TypeKind_Iterator; }
181  };
182 
189  {
190  public:
192  virtual TypeInterface* elementType() = 0;
194  virtual size_t size(void* storage) = 0;
196  virtual AnyIterator begin(void* storage) = 0;
199  virtual AnyIterator end(void* storage) = 0;
201  virtual void pushBack(void** storage, void* valueStorage) = 0;
203  virtual void* element(void* storage, int index);
204  TypeKind kind() override { return TypeKind_List;}
205  };
206 
214  {
215  public:
217  virtual TypeInterface* elementType() = 0;
219  virtual TypeInterface* keyType() = 0;
221  virtual size_t size(void* storage) = 0;
223  virtual AnyIterator begin(void* storage) = 0;
226  virtual AnyIterator end(void* storage) = 0;
228  virtual void insert(void** storage, void* keyStorage, void* valueStorage) = 0;
235  virtual AnyReference element(void** storage, void* keyStorage, bool autoInsert) = 0;
236  TypeKind kind() override { return TypeKind_Map; }
237  // Since our typesystem has no erased operator < or operator ==,
238  // MapTypeInterface does not provide a find()
239  };
240 
242  {
243  public:
245  AnyReferenceVector values(void* storage);
253  virtual std::vector<TypeInterface*> memberTypes() = 0;
255  virtual std::vector<void*> get(void* storage);
257  virtual void* get(void* storage, unsigned int index) = 0;
259  virtual void set(void** storage, const std::vector<void*>&);
261  virtual void set(void** storage, unsigned int index, void* valStorage) = 0;
262  TypeKind kind() override { return TypeKind_Tuple; }
264  virtual std::vector<std::string> elementsName() { return std::vector<std::string>();}
266  virtual std::string className() { return std::string(); }
267 
287  virtual bool convertFrom(std::map<std::string, ::qi::AnyValue>& fields,
289  const std::vector<std::tuple<std::string, TypeInterface*>>& missing,
290  const std::map<std::string, ::qi::AnyReference>& dropfields)
291  {
292  return false;
293  }
295  virtual bool convertTo(std::map<std::string, ::qi::AnyValue>& fields,
296  const std::vector<std::tuple<std::string, TypeInterface*>>& missing,
297  const std::map<std::string, ::qi::AnyReference>& dropfields)
298  {
299  return false;
300  }
301 
303  };
304 
311  {
312  public:
314  virtual AnyReference get(void* storage) = 0;
316  virtual void set(void** storage, AnyReference source) = 0;
317  TypeKind kind() override { return TypeKind_Dynamic; }
318  };
319 
324  {
325  public:
326  //virtual AnyReference get(void *storage) = 0;
327  //virtual TypeInterface* elementType() = 0;
328  TypeKind kind() override { return TypeKind_VarArgs; }
329  };
330 
335  {
336  public:
338  virtual TypeInterface* valueType() = 0;
340  virtual bool hasValue(void* storage) = 0;
342  virtual AnyReference value(void* storage) = 0;
344  virtual void set(void** storage, void* valueStorage) = 0;
346  virtual void reset(void** storage) = 0;
347  TypeKind kind() override { return TypeKind_Optional; }
348  };
349 
350 
354  QI_API TypeInterface* makeTypeOfKind(const qi::TypeKind& kind);
355 
357  QI_API TypeInterface* makeFloatType(int bytelen);
358 
360  QI_API TypeInterface* makeIntType(bool issigned, int bytelen);
361 
363  QI_API TypeInterface* makeVarArgsType(TypeInterface* elementType);
364 
366  QI_API TypeInterface* makeListType(TypeInterface* elementType);
367 
369  QI_API TypeInterface* makeMapType(TypeInterface* keyType, TypeInterface* ElementType);
370 
372  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>());
373 
375  QI_API TypeInterface* makeOptionalType(TypeInterface* valueType);
376 
377 
378 
382 #define QI_TEMPLATE_TYPE_DECLARE(n) \
383  namespace qi \
384  { \
385  template <typename T> \
386  class QITYPE_TEMPLATE_API TypeImpl<n<T> > : public TypeOfTemplateImpl<n, T> \
387  { \
388  }; \
389  }
390 
394 #define QI_TEMPLATE_TYPE_GET(typeInst, templateName) \
395  dynamic_cast< ::qi::TypeOfTemplate<templateName>*>(typeInst)
396 
400 #define QI_TYPE_ENUM(Enum) \
401  namespace qi \
402  { \
403  template <> \
404  class TypeImpl<Enum> : public IntTypeInterfaceImpl<int> \
405  { \
406  }; \
407  }
408 
409 namespace detail
410 {
411  struct QI_API_DEPRECATED_MSG(Use 'QI_TYPE_ENUM' instead) QI_TYPE_ENUM_REGISTER_ {};
412 }
413 
414 #define QI_TYPE_ENUM_REGISTER(Enum) \
415  namespace qi \
416  { \
417  template <> \
418  class TypeImpl<Enum> : public IntTypeInterfaceImpl<int> \
419  { \
420  static const detail::QI_TYPE_ENUM_REGISTER_ BLAH; \
421  }; \
422  }
423 
424 #define QI_TYPE_STRUCT_DECLARE(name) \
425  __QI_TYPE_STRUCT_DECLARE(name, )
426 
427 }
428 
429 
431 #include <qi/type/detail/type.hxx>
441 
444 
445 #ifdef _MSC_VER
446 # pragma warning( pop )
447 // restore the disabling of this warning
448 # pragma warning( disable: 4503 )
449 #endif
450 
451 #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
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)
TypeInterface * makeOptionalType(TypeInterface *valueType)
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:54
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)