- 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
anyreference.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 _QI_TYPE_DETAIL_ANYREFERENCE_HXX_
8 #define _QI_TYPE_DETAIL_ANYREFERENCE_HXX_
9
10 #include <qi/type/detail/anyreference.hpp>
11
12 namespace qi
13 {
14
15 namespace detail
16 {
17
18 inline AnyReference AnyReferenceBase::clone() const
19 {
20 AnyReference res;
21 res._type = _type;
22 res._value = _type ? res._type->clone(_value) : 0;
23 return res;
24 }
25
26 inline qi::Signature AnyReferenceBase::signature(bool resolveDynamic) const
27 {
28 if (!_type)
29 return qi::Signature();
30 else
31 return _type->signature(_value, resolveDynamic);
32 }
33
34 inline void AnyReferenceBase::destroy()
35 {
36 if (_type)
37 _type->destroy(_value);
38 _value = _type = 0;
39 }
40
41 inline AnyReferenceBase::AnyReferenceBase()
42 : _type(0)
43 , _value(0)
44 {
45 }
46
47 inline AnyReferenceBase::AnyReferenceBase(TypeInterface* type)
48 : _type(type)
49 , _value(type->initializeStorage())
50 {
51 }
52
53 template<typename T>
54 AnyReference AnyReferenceBase::fromPtr(const T* ptr)
55 {
56 static TypeInterface* t = 0;
57 if (!t)
58 t = typeOf<typename boost::remove_const<T>::type>();
59 void *value = t->initializeStorage(const_cast<void*>((const void*)ptr));
60 return AnyReference(t, value);
61 }
62
63 template<typename T>
64 AnyReference AnyReferenceBase::from(const T& value)
65 {
66 static TypeInterface* t = 0;
67 QI_ONCE( t = typeOf<typename boost::remove_const<T>::type>());
68 return AnyReference(t, t->initializeStorage(const_cast<void*>((const void*)&value)));
69 }
70
71 inline TypeKind AnyReferenceBase::kind() const
72 {
73 if (!_type)
74 throw std::runtime_error("Can't take the kind of an invalid value");
75 else
76 return _type->kind();
77 }
78
79 inline AnyReference AnyReferenceBase::unwrap() const
80 {
81 AnyReference res = *this;
82 while (res.isValid() && res.kind() == TypeKind_Dynamic)
83 {
84 res = res.content();
85 }
86 return res;
87 }
88
89 template<TypeKind T> struct TypeOfKind {};
90
91 #define TYPE_OF_KIND(k, t) template<> struct TypeOfKind<k> { using type = t;}
92 // Kind -> handler Type (IntTypeInterface, ListTypeInterface...) accessor
93 TYPE_OF_KIND(TypeKind_Int, IntTypeInterface);
94 TYPE_OF_KIND(TypeKind_Float, FloatTypeInterface);
95 TYPE_OF_KIND(TypeKind_String, StringTypeInterface);
96 #undef TYPE_OF_KIND
97
98 // Optimized AnyReferenceBase::as<T> for direct access to a subType getter
99 template<typename T, TypeKind k>
100 inline T valueAs(const AnyReferenceBase& v)
101 {
102 if (v.kind() == k)
103 return static_cast<T>(
104 static_cast<typename TypeOfKind<k>::type*>(v.type())
105 ->get(v.rawValue()));
106 // Fallback to default which will attempt a full conversion.
107 return v.to<T>();
108 }
109
110 template<typename T>
111 inline T* AnyReferenceBase::ptr(bool check)
112 {
113 if (!_type || (check && typeOf<T>()->info() != _type->info()))
114 return 0;
115 else
116 return (T*)_type->ptrFromStorage(&_value);
117 }
118
119 template<typename T>
120 inline T& AnyReferenceBase::as()
121 {
122 T* p = ptr<T>(true);
123 if (!p)
124 throw std::runtime_error("Type mismatch");
125 return *p;
126 }
127
128 template<typename T>
129 inline T AnyReferenceBase::to(const T&) const
130 {
131 return to<T>();
132 }
133
134 QI_NORETURN QI_API void throwConversionFailure(
135 TypeInterface* from, TypeInterface* to, const std::string& additionalMsg);
136
137 template<typename T>
138 inline T AnyReferenceBase::to() const
139 {
140 TypeInterface* targetType = typeOf<T>();
141 auto conv = convert(targetType);
142 if (!conv->_type)
143 {
144 throwConversionFailure(_type, targetType, ""); // no additional message
145 }
146 T result = *conv->ptr<T>(false);
147 return result;
148 }
149
150 template<>
151 inline void AnyReferenceBase::to<void>() const
152 {
153 return;
154 }
155
156 inline bool AnyReferenceBase::isValid() const
157 {
158 return _type != 0;
159 }
160
161 inline bool AnyReferenceBase::isValue() const
162 {
163 return _type != 0 && _type->info() != typeOf<void>()->info();
164 }
165
166 inline int64_t AnyReferenceBase::toInt() const
167 {
168 return detail::valueAs<int64_t, TypeKind_Int>(*this);
169 }
170
171 inline uint64_t AnyReferenceBase::toUInt() const
172 {
173 return detail::valueAs<uint64_t, TypeKind_Int>(*this);
174 }
175
176 inline float AnyReferenceBase::toFloat() const
177 {
178 return detail::valueAs<float, TypeKind_Float>(*this);
179 }
180
181 inline double AnyReferenceBase::toDouble() const
182 {
183 return detail::valueAs<double, TypeKind_Float>(*this);
184 }
185
186 inline std::string AnyReferenceBase::toString() const
187 {
188 return to<std::string>();
189 }
190
191 template<typename T>
192 inline std::vector<T> AnyReferenceBase::toList() const
193 {
194 return to<std::vector<T> >();
195 }
196
197 template<typename K, typename V>
198 inline std::map<K, V> AnyReferenceBase::toMap() const
199 {
200 return to<std::map<K, V> >();
201 }
202
203 template <typename T>
204 inline boost::optional<T> AnyReferenceBase::toOptional() const
205 {
206 return to<boost::optional<T>>();
207 }
208
209 inline AnyReferenceVector AnyReferenceBase::asListValuePtr()
210 {
211 return asTupleValuePtr();
212 }
213
214 template<typename T>
215 void AnyReferenceBase::set(const T& v)
216 {
217 update(AnyReferenceBase::from(v));
218 }
219
220 inline void AnyReferenceBase::setFloat(float v)
221 {
222 setDouble(static_cast<double>(v));
223 }
224
225 template<typename E, typename K>
226 E& AnyReferenceBase::element(const K& key)
227 {
228 return (*this)[key].template as<E>();
229 }
230
231 template<typename K>
232 AnyReference AnyReferenceBase::operator[](const K& key)
233 {
234 return operator[](AnyReferenceBase::from(key));
235 }
236
237 inline AnyReference AnyReferenceBase::operator[](const AnyReference& key)
238 {
239 return _element(key, true, true);
240 }
241
242 template<typename K>
243 AnyReference AnyReferenceBase::at(const K& key)
244 {
245 // note that this implementation is currently not very useful
246 // (it does the same thing as the const version) and could be removed.
247 // In the future, AnyReferenceConst should be implemented to
248 // make the distinction between the two, and in this case
249 // this version of the function will have a real meaning.
250 return at(AnyReferenceBase::from(key));
251 }
252
253 template<typename K>
254 AnyReference AnyReferenceBase::at(const K& key) const
255 {
256 return at(AnyReferenceBase::from(key));
257 }
258
259 inline AnyReference AnyReferenceBase::at(const AnyReference& key)
260 {
261 return _element(key, false, false);
262 }
263
264 inline AnyReference AnyReferenceBase::at(const AnyReference& key) const
265 {
266 return const_cast<AnyReferenceBase*>(this)->at(key);
267 }
268
269 template<typename T>
270 void AnyReferenceBase::append(const T& element)
271 {
272 append(AnyReference::from(element));
273 }
274
275 template<typename K, typename V>
276 void AnyReferenceBase::insert(const K& key, const V& val)
277 {
278 insert(AnyReference::from(key), AnyReference::from(val));
279 }
280
281 template<typename K>
282 AnyReference AnyReferenceBase::find(const K& key)
283 {
284 return _element(AnyReference::from(key), false, false);
285 }
286
287 } // namespace detail
288
289 inline bool operator != (const AnyReference& a, const AnyReference& b)
290 {
291 return !(a==b);
292 }
293
294 } // namespace qi
295
296 #endif
qi::TypeInterface::ptrFromStorage
virtual void * ptrFromStorage(void **)=0
qi::StringTypeInterface
Definition: typeinterface.hpp:104
qi::int64_t
int64_t int64_t
Definition: types.hpp:61
qi::detail::AnyReferenceBase::update
void update(const AutoAnyReference &b)
QI_API
#define QI_API
Definition: api.hpp:33
qi::detail::AnyReferenceBase::ptr
T * ptr(bool check=true)
Definition: anyreference.hxx:111
QI_NORETURN
#define QI_NORETURN
Portable noreturn attribute, used to declare that a function does not return.
Definition: macro.hpp:68
qi::TypeKind_Dynamic
Definition: fwd.hpp:66
qi::detail::AnyReferenceBase::convert
UniqueAnyReference convert(TypeInterface *targetType) const
qi::detail::throwConversionFailure
QI_NORETURN void throwConversionFailure(TypeInterface *from, TypeInterface *to, const std::string &additionalMsg)
qi::detail::AnyReferenceBase::element
E & element(const K &key)
Call operator[](key).as<E>, element type must match E.
Definition: anyreference.hxx:226
qi::detail::AnyReferenceBase::asTupleValuePtr
AnyReferenceVector asTupleValuePtr()
qi::detail::AnyReferenceBase::find
AnyReference find(const K &key)
Definition: anyreference.hxx:282
qi::AnyReference
Definition: anyreference.hpp:360
qi::detail::AnyReferenceBase::toFloat
float toFloat() const
Definition: anyreference.hxx:176
qi::TypeInterface::kind
virtual TypeKind kind()
Definition: type.hxx:99
qi::Signature
Definition: signature.hpp:66
qi::detail::AnyReferenceBase::setFloat
void setFloat(float v)
Definition: anyreference.hxx:220
qi::detail::AnyReferenceBase::toUInt
uint64_t toUInt() const
Definition: anyreference.hxx:171
qi::detail::AnyReferenceBase::AnyReferenceBase
AnyReferenceBase()
Constructs an invalid reference, pointing to nothing.
Definition: anyreference.hxx:41
qi::TypeInterface::signature
qi::Signature signature(void *storage=nullptr, bool resolveDynamic=false)
qi::TypeInterface::initializeStorage
virtual void * initializeStorage(void *ptr=nullptr)=0
qi::detail::AnyReferenceBase::_element
AnyReference _element(const AnyReference &key, bool throwOnFailure, bool autoInsert)
qi::detail::AnyReferenceBase::insert
void insert(const K &key, const V &val)
Definition: anyreference.hxx:276
qi::detail::AnyReferenceBase::_value
void * _value
Definition: anyreference.hpp:355
qi::detail::valueAs
T valueAs(const AnyReferenceBase &v)
Definition: anyreference.hxx:100
qi::detail::AnyReferenceBase::at
AnyReference at(const K &key)
Definition: anyreference.hxx:243
qi::detail::AnyReferenceBase::setDouble
void setDouble(double v)
qi::detail::AnyReferenceBase::destroy
void destroy()
Deletes storage.
Definition: anyreference.hxx:34
qi::TypeInterface
Definition: typeinterface.hpp:129
qi::detail::AnyReferenceBase::toInt
int64_t toInt() const
Definition: anyreference.hxx:166
anyreference.hpp
qi::detail::AnyReferenceBase::to
T to() const
Convert to anything or throw trying.
Definition: anyreference.hxx:138
qi::detail::AnyReferenceBase::signature
qi::Signature signature(bool resolveDynamic=false) const
Definition: anyreference.hxx:26
qi::AnyReferenceVector
std::vector< AnyReference > AnyReferenceVector
Definition: anyreference.hpp:26
qi::detail::AnyReferenceBase::toString
std::string toString() const
Definition: anyreference.hxx:186
qi::detail::AnyReferenceBase::type
TypeInterface * type() const
Definition: anyreference.hpp:348
qi::detail::AnyReferenceBase::asListValuePtr
AnyReferenceVector asListValuePtr()
Definition: anyreference.hxx:209
qi::TypeKind_Int
Definition: fwd.hpp:58
qi::detail::AnyReferenceBase::toMap
std::map< K, V > toMap() const
Definition: anyreference.hxx:198
qi::detail::AnyReferenceBase::rawValue
void * rawValue() const
Definition: anyreference.hpp:351
qi::TypeInterface::destroy
virtual void destroy(void *)=0
Free all resources of a storage.
qi::FloatTypeInterface
Definition: typeinterface.hpp:92
QI_ONCE
#define QI_ONCE(code)
Execute code once, parallel calls are blocked until code finishes.
Definition: atomic.hpp:420
qi::detail::AnyReferenceBase::kind
TypeKind kind() const
Definition: anyreference.hxx:71
qi::detail::AnyReferenceBase
Definition: anyreference.hpp:40
qi::TypeInterface::info
virtual const TypeInfo & info()=0
Get the TypeInfo corresponding to this type.
qi::TypeKind_Float
Definition: fwd.hpp:59
qi::detail::AnyReferenceBase::content
AnyReference content() const
qi::detail::AnyReferenceBase::isValid
bool isValid() const
Definition: anyreference.hxx:156
qi::detail::TypeOfKind
Definition: anyreference.hxx:89
qi::IntTypeInterface
Definition: typeinterface.hpp:78
qi::typeOf
TypeInterface * typeOf()
Definition: type.hxx:94
qi::detail::AnyReferenceBase::clone
AnyReference clone() const
Definition: anyreference.hxx:18
qi::TypeKind_String
Definition: fwd.hpp:60
qi::detail::AnyReferenceBase::as
T & as()
Definition: anyreference.hxx:120
qi::detail::AnyReferenceBase::set
void set(const T &val)
Update the value to val, which will be converted if required.
Definition: anyreference.hxx:215
qi::TypeKind
TypeKind
Definition: fwd.hpp:54
qi::detail::AnyReferenceBase::toList
std::vector< T > toList() const
Definition: anyreference.hxx:192
TYPE_OF_KIND
#define TYPE_OF_KIND(k, t)
Definition: anyreference.hxx:91
qi::detail::AnyReferenceBase::_type
TypeInterface * _type
Definition: anyreference.hpp:354
qi::detail::AnyReferenceBase::from
static AnyReference from(const T &ref)
Definition: anyreference.hxx:64
qi::detail::AnyReferenceBase::fromPtr
static AnyReference fromPtr(const T *ptr)
Definition: anyreference.hxx:54
qi::TypeInterface::clone
virtual void * clone(void *)=0
Allocate a storage and copy the value given as an argument.
qi::operator!=
bool operator!=(const Signature &lhs, const Signature &rhs)
Definition: signature.hpp:157
qi::detail::AnyReferenceBase::isValue
bool isValue() const
Definition: anyreference.hxx:161
qi::detail::AnyReferenceBase::operator[]
AnyReference operator[](const K &key)
Definition: anyreference.hxx:232
qi::detail::AnyReferenceBase::toOptional
boost::optional< T > toOptional() const
Definition: anyreference.hxx:204
qi::uint64_t
uint64_t uint64_t
Definition: types.hpp:66
qi::detail::AnyReferenceBase::append
void append(const T &element)
Definition: anyreference.hxx:270
qi::detail::AnyReferenceBase::toDouble
double toDouble() const
Definition: anyreference.hxx:181
qi::detail::AnyReferenceBase::unwrap
AnyReference unwrap() const
Definition: anyreference.hxx:79