- Aldebaran documentation |
- C++ Libraries |
- index

libqi-api 2.8.7.4

- Main Page
- Related Pages
- Namespaces
- Classes
- Files
- Examples

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
optionaltypeinterface.hxx
Go to the documentation of this file.
1 /*
2 ** Copyright (C) 2017 SoftBank Robotics
3 ** See COPYING for the license
4 */
5
6 #ifndef QITYPE_DETAIL_OPTIONALTYPEINTERFACE_HXX
7 #define QITYPE_DETAIL_OPTIONALTYPEINTERFACE_HXX
8
9 #include <qi/type/typeinterface.hpp>
10 #include <qi/type/detail/typeimpl.hxx>
11 #include <boost/optional.hpp>
12
13 #if defined(__cpp_lib_optional) && defined(__has_include)
14 # if __has_include(<optional>)
15 # include <optional>
16 # define QI_HAS_STD_OPTIONAL 1
17 # else
18 # define QI_HAS_STD_OPTIONAL 0
19 # endif
20 #else
21 # define QI_HAS_STD_OPTIONAL 0
22 #endif
23
24 namespace qi
25 {
28 template<typename O>
29 class OptionalTypeInterfaceImpl: public OptionalTypeInterface
30 {
31 public:
32 using ValueType = typename O::value_type;
33
34 OptionalTypeInterfaceImpl();
35 TypeInterface* valueType() override;
36 bool hasValue(void* storage) override;
37 AnyReference value(void* storage) override;
38 void set(void** storage, void* valueStorage) override;
39 void reset(void** storage) override;
40
41 using TypeMethodsImpl = DefaultTypeImplMethods<O, TypeByPointerPOD<O>>;
42 _QI_BOUNCE_TYPE_METHODS(TypeMethodsImpl)
43 TypeInterface* _valueType;
44 };
45
46 template<typename O>
47 OptionalTypeInterfaceImpl<O>::OptionalTypeInterfaceImpl()
48 {
49 _valueType = typeOf<ValueType>();
50 }
51
52 template<typename O>
53 TypeInterface* OptionalTypeInterfaceImpl<O>::valueType()
54 {
55 return _valueType;
56 }
57
58 template<typename O>
59 bool OptionalTypeInterfaceImpl<O>::hasValue(void* storage)
60 {
61 auto& opt = *reinterpret_cast<O*>(ptrFromStorage(&storage));
62 return static_cast<bool>(opt);
63 }
64
65 template<typename O>
66 AnyReference OptionalTypeInterfaceImpl<O>::value(void* storage)
67 {
68 auto& opt = *reinterpret_cast<O*>(ptrFromStorage(&storage));
69 return opt ? AutoAnyReference(opt.value()) : AnyReference(typeOf<void>());
70 }
71
72 template<typename O>
73 void OptionalTypeInterfaceImpl<O>::set(void** storage, void* valueStorage)
74 {
75 auto& opt = *reinterpret_cast<O*>(ptrFromStorage(storage));
76 opt = *reinterpret_cast<ValueType*>(_valueType->ptrFromStorage(&valueStorage));
77 }
78
79 template<typename O>
80 void OptionalTypeInterfaceImpl<O>::reset(void** storage)
81 {
82 auto& opt = *reinterpret_cast<O*>(ptrFromStorage(storage));
83 opt = O{};
84 }
85
86 template <typename T>
87 class TypeImpl<boost::optional<T>> : public OptionalTypeInterfaceImpl<boost::optional<T>> {};
88 #if QI_HAS_STD_OPTIONAL
89 template<typename T>
90 class TypeImpl<std::optional<T>>: public OptionalTypeInterfaceImpl<std::optional<T>>{};
91 #endif
92
93 template<> class TypeImpl<boost::none_t>: public TypeImpl<void> {};
94 #if QI_HAS_STD_OPTIONAL
95 template<> class TypeImpl<std::nullopt_t>: public TypeImpl<void> {};
96 #endif
97 }
98
99 #endif // QITYPE_DETAIL_OPTIONALTYPEINTERFACE_HXX
qi::TypeInterface::ptrFromStorage
virtual void * ptrFromStorage(void **)=0
qi::TypeImpl
Definition: typeimpl.hxx:300
qi::OptionalTypeInterface
Definition: typeinterface.hpp:334
qi::AnyReference
Definition: anyreference.hpp:360
qi::OptionalTypeInterfaceImpl::value
AnyReference value(void *storage) override
Get the optional value or a default constructed AnyReference if no value is set.
Definition: optionaltypeinterface.hxx:66
qi::TypeInterface
Definition: typeinterface.hpp:129
qi::OptionalTypeInterfaceImpl::ValueType
typename O::value_type ValueType
Definition: optionaltypeinterface.hxx:32
qi::OptionalTypeInterfaceImpl::OptionalTypeInterfaceImpl
OptionalTypeInterfaceImpl()
Definition: optionaltypeinterface.hxx:47
_QI_BOUNCE_TYPE_METHODS
#define _QI_BOUNCE_TYPE_METHODS(Bounce)
Implement all methods of Type as bouncers to Bouncer.
Definition: typeimpl.hxx:273
qi::OptionalTypeInterfaceImpl::_valueType
TypeInterface * _valueType
Definition: optionaltypeinterface.hxx:43
qi::DefaultTypeImplMethods
Definition: typeimpl.hxx:228
typeimpl.hxx
qi::OptionalTypeInterfaceImpl::reset
void reset(void **storage) override
Resets the optional value, making the optional empty.
Definition: optionaltypeinterface.hxx:80
qi::OptionalTypeInterfaceImpl::valueType
TypeInterface * valueType() override
Get the type of the optional element.
Definition: optionaltypeinterface.hxx:53
qi::OptionalTypeInterfaceImpl::set
void set(void **storage, void *valueStorage) override
Set the optional value.
Definition: optionaltypeinterface.hxx:73
qi::OptionalTypeInterfaceImpl
Definition: optionaltypeinterface.hxx:29
qi::OptionalTypeInterfaceImpl::hasValue
bool hasValue(void *storage) override
Return true if the optional has a value, false if not.
Definition: optionaltypeinterface.hxx:59
qi::AutoAnyReference
Definition: anyreference.hpp:409
typeinterface.hpp