libqi-api  2.8.7.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pointertypeinterface.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 #ifndef _QITYPE_DETAIL_TYPEPOINTER_HXX_
8 #define _QITYPE_DETAIL_TYPEPOINTER_HXX_
9 
10 #include <boost/shared_ptr.hpp>
11 
12 namespace qi
13 {
14  template<typename T> class PointerTypeInterfaceImpl: public PointerTypeInterface
15  {
16  public:
18  {
19  // Caching the result here is dangerous if T uses runtime factory.
20  return typeOf<T>();
21  }
22  PointerKind pointerKind() override { return Raw; }
23  AnyReference dereference(void* storage) override
24  {
25  // We are in DirectAccess mode, so storage is a T*.
26  void* value = pointedType()->initializeStorage(storage);
27  return AnyReference(pointedType(), value);
28  }
29 
30  void set(void** storage, AnyReference pointer) override
31  {
32  AnyReference obj = *pointer;
33  *storage = obj.rawValue();
34  }
35 
36  void setPointee(void** storage, void* pointer) override
37  {
38  *storage = pointer;
39  }
40 
43  };
44 
45  template<typename T> class TypeImpl<T*>: public PointerTypeInterfaceImpl<T>{};
46 
47  template<typename T> class TypeSharedPointerImpl: public PointerTypeInterface
48  {
49  public:
51  {
52  return typeOf<typename T::element_type>();
53  }
54  PointerKind pointerKind() override { return Shared; }
55  AnyReference dereference(void* storage) override
56  {
57  T* ptr = (T*)ptrFromStorage(&storage);
58  void *value = pointedType()->initializeStorage(ptr->get());
59  return AnyReference(pointedType(), value);
60  }
61  void set(void** storage, AnyReference pointer) override
62  {
63  T* ptr = (T*)ptrFromStorage(storage);
64  T* otherPtr = (T*)pointer.rawValue();
65  *ptr = *otherPtr;
66  }
67  void setPointee(void** storage, void* pointer) override
68  {
69  // we can't do that as it means that we would take ownership of pointer
70  throw std::runtime_error("cannot convert to shared_ptr");
71  }
74  };
75 
76  template<typename T> class TypeImpl<boost::shared_ptr<T> >: public TypeSharedPointerImpl<boost::shared_ptr<T> >{};
77 }
78 
79 #endif // _QITYPE_DETAIL_TYPEPOINTER_HXX_
virtual void * ptrFromStorage(void **)=0
void set(void **storage, AnyReference pointer) override
Set new pointee value. pointer must be a pointer to type pointedType()
_QI_BOUNCE_TYPE_METHODS(TypeMethodsImpl)
TypeInterface * pointedType() override
Get the type of the pointed element.
AnyReference dereference(void *storage) override
Get the pointed element (must not be destroyed)
virtual void * initializeStorage(void *ptr=nullptr)=0
void setPointee(void **storage, void *pointer) override
Set new pointee value. pointer must be a pointer to type pointedType()
void setPointee(void **storage, void *pointer) override
Set new pointee value. pointer must be a pointer to type pointedType()
AnyReference dereference(void *storage) override
Get the pointed element (must not be destroyed)
PointerKind pointerKind() override
Return whether the pointer has raw or shared semantics.
void set(void **storage, AnyReference pointer) override
Set new pointee value. pointer must be a pointer to type pointedType()
PointerKind pointerKind() override
Return whether the pointer has raw or shared semantics.
TypeInterface * pointedType() override
Get the type of the pointed element.