- 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.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_DETAIL_ANYREFERENCE_HPP_
8 #define _QI_TYPE_DETAIL_ANYREFERENCE_HPP_
9
10 #include <vector>
11 #include <map>
12 #include <stdexcept>
13 #include <cmath>
14 #include <qi/atomic.hpp>
15 #include <qi/types.hpp>
16 #include <qi/api.hpp>
17 #include <qi/type/fwd.hpp>
18 #include <qi/type/detail/typeinterface.hpp>
19 #include <ka/scoped.hpp>
20 #include <boost/type_traits/remove_const.hpp>
21 #include <boost/optional.hpp>
22
23 namespace qi {
24
25 class AnyReference;
26 using AnyReferenceVector = std::vector<AnyReference>;
27
28 namespace detail {
29
30 class UniqueAnyReference;
31
32
40 class QI_API AnyReferenceBase
41 {
42 protected:
45
47 AnyReferenceBase();
48
52 explicit AnyReferenceBase(TypeInterface* type);
53
59 AnyReferenceBase(TypeInterface* type, void* value)
60 : _type(type)
61 , _value(value)
62 {}
63
74 AnyReference _element(const AnyReference& key, bool throwOnFailure, bool autoInsert);
75
76 public:
79 UniqueAnyReference convert(TypeInterface* targetType) const;
80 UniqueAnyReference convert(ListTypeInterface* targetType) const;
81 UniqueAnyReference convert(StructTypeInterface* targetType) const;
82 UniqueAnyReference convert(MapTypeInterface* targetType) const;
83 UniqueAnyReference convert(IntTypeInterface* targetType) const;
84 UniqueAnyReference convert(FloatTypeInterface* targetType) const;
85 UniqueAnyReference convert(RawTypeInterface* targetType) const;
86 UniqueAnyReference convert(StringTypeInterface* targetType) const;
87 UniqueAnyReference convert(PointerTypeInterface* targetType) const;
88 UniqueAnyReference convert(DynamicTypeInterface* targetType) const;
89 UniqueAnyReference convert(OptionalTypeInterface* targetType) const;
90
96 template<typename T>
97 T* ptr(bool check = true);
98
100 bool isValid() const;
101
103 bool isValue() const;
104
106 AnyReference convertCopy(TypeInterface* targetType) const;
108
110
121 template <typename T>
122 static AnyReference from(const T& ref);
123
130 template <typename T>
131 static AnyReference fromPtr(const T* ptr);
132
133 AnyReference clone() const;
135 void destroy();
137
138
140
145 template<typename T>
147 T to() const;
148
150 template<typename T>
151 T to(const T&) const;
152
153 int64_t toInt() const;
154 uint64_t toUInt() const;
155 float toFloat() const;
156 double toDouble() const;
157 std::string toString() const;
158
159 template<typename T>
160 std::vector<T> toList() const;
161
162 template<typename K, typename V>
163 std::map<K, V> toMap() const;
164
165 AnyObject toObject() const;
166 template <typename T> boost::optional<T> toOptional() const;
167
175 AnyValue toTuple(bool homogeneous) const;
177
178 qi::Signature signature(bool resolveDynamic = false) const;
179 TypeKind kind() const;
180 AnyReference unwrap() const;
181
183
199 template<typename T>
200 T& as();
201
202 int64_t& asInt64() { return as<int64_t>();}
203 uint64_t& asUInt64() { return as<uint64_t>();}
204 int32_t& asInt32() { return as<int32_t>();}
205 uint32_t& asUInt32() { return as<uint32_t>();}
206 int16_t& asInt16() { return as<int16_t>();}
207 uint16_t& asUInt16() { return as<uint16_t>();}
208 int8_t& asInt8() { return as<int8_t>();}
209 uint8_t& asUInt8() { return as<uint8_t>();}
210 double& asDouble() { return as<double>();}
211 float& asFloat() { return as<float>();}
212 std::string& asString() { return as<std::string>();}
213
215 std::pair<char*, size_t> asRaw() const;
216
217 boost::optional<AnyReference> asOptional() const;
218
222 AnyReference content() const;
223
225
232 AnyReferenceVector asTupleValuePtr();
233 AnyReferenceVector asListValuePtr();
234 std::map<AnyReference, AnyReference> asMapValuePtr();
236
239 void update(const AutoAnyReference& b);
240
242 template<typename T>
243 void set(const T& val);
244
245 void set(int64_t v) { setInt(v); }
246 void set(int32_t v) { setInt(v); }
247 void set(uint64_t v) { setUInt(v); }
248 void set(uint32_t v) { setUInt(v); }
249 void set(float v) { setFloat(v); }
250 void set(double v) { setDouble(v); }
251 void set(const std::string& v) { setString(v); }
252
253 void setInt(int64_t v);
254 void setUInt(uint64_t v);
255 void setFloat(float v);
256 void setDouble(double v);
257 void setString(const std::string& v);
258 void setDynamic(const AnyReference &value);
259
262 void setOptional(const boost::optional<AnyReference>& opt);
263
264 QI_API_DEPRECATED_MSG(Use `setOptional(boost::optional<AnyReference>)` instead)
265 void setOptional(const AnyReference& opt);
266
269 void setRaw(const char *buffer, size_t size);
270
273 void setTuple(const AnyReferenceVector& values);
274
277 void resetOptional();
278
281
293 template<typename K>
294 AnyReference operator[](const K& key);
295 AnyReference operator[](const AnyReference& key);
296
298 template<typename E, typename K>
299 E& element(const K& key);
300
307 template<typename K>
308 AnyReference at(const K& key);
309 template<typename K>
310 AnyReference at(const K& key) const;
311 AnyReference at(const AnyReference& key);
312 AnyReference at(const AnyReference& key) const;
313
314 size_t size() const;
315
318 bool optionalHasValue() const;
319
320
321 //TODO: use AutoAnyReference
322 template<typename T>
323 void append(const T& element);
324 void append(const AnyReference& element);
325
326 template<typename K, typename V>
327 void insert(const K& key, const V& val);
328 void insert(const AnyReference& key, const AnyReference& val);
329
330
334 template<typename K>
335 AnyReference find(const K& key);
336
338 AnyIterator begin() const; //we lie on const but GV does not honor it yet
340 AnyIterator end() const;
341
343 AnyReference operator*() const;
345
347
348 TypeInterface* type() const { return _type; }
350 std::vector<TypeInterface*> membersType() const;
351 void* rawValue() const { return _value; }
352
353 protected:
354 TypeInterface* _type;
355 void* _value;
356 };
357
358 } // namespace detail
359
360 class QI_API AnyReference : public detail::AnyReferenceBase {
361 public:
362 AnyReference()
363 : AnyReferenceBase()
364 {}
365
366 AnyReference(const AnyReferenceBase& rhs)
367 : AnyReferenceBase(rhs)
368 {}
369
370 explicit AnyReference(TypeInterface* type)
371 : AnyReferenceBase(type)
372 {}
373
374 AnyReference(TypeInterface* type, void* value)
375 : AnyReferenceBase(type, value)
376 {}
377
378 private:
379 //block the dangerous automatic cast from AnyValue to AnyRef
380 //use AnyValue::asReference() to take a ref on the content of a value.
381 //use AnyValue::from(value) to take a ref on the value.
382 AnyReference(const AnyValue& val)
383 {
384 throw std::runtime_error("invalid internal operation.");
385 };
386 };
387
388 using AnyReferenceVector = std::vector<AnyReference>;
389 QI_API bool operator< (const AnyReference& a, const AnyReference& b);
390 QI_API bool operator==(const AnyReference& a, const AnyReference& b);
391 QI_API bool operator!=(const AnyReference& a, const AnyReference& b);
392
394 QI_API AnyReference makeGenericTuple(const AnyReferenceVector& values);
395
397 QI_API AnyReference makeGenericTuplePtr(
398 const std::vector<TypeInterface*>& types,
399 const std::vector<void*>& values);
400
409 class QI_API AutoAnyReference: public AnyReference
410 {
411 public:
412 AutoAnyReference () {}
413 AutoAnyReference(const AutoAnyReference& b)
414 : AnyReference()
415 {
416 _value = b._value;
417 _type = b._type;
418 }
419
420 AutoAnyReference(const AnyReference &self)
421 : AnyReference(self)
422 {}
423
424 AutoAnyReference(const detail::AnyReferenceBase &self)
425 : AnyReference(self)
426 {}
427
428 template<typename T,
429 // Disable this constructor if T inherits AnyReferenceBase to avoid it being greedy.
430 // It also handles cases where T inherits from either AutoAnyReference or AnyReference
431 // because both of these types inherit AnyReferenceBase.
432 typename = ka::EnableIfNotBaseOf<detail::AnyReferenceBase, T>>
433 AutoAnyReference(const T& ptr)
434 {
435 *static_cast<AnyReference*>(this) = AnyReference::from(ptr);
436 }
437 };
438
439 namespace detail
440 {
441
442 struct DeferOwnership {};
443
444 class UniqueAnyReference
445 {
446 public:
447 UniqueAnyReference() = default;
448
449 KA_GENERATE_FRIEND_REGULAR_OPS_2(UniqueAnyReference, ref, owned)
450
451 explicit UniqueAnyReference(AnyReference ref)
452 : ref{ ref }
453 , owned{ ref.isValid() }
454 {}
455
456 explicit UniqueAnyReference(AnyReference ref, DeferOwnership)
457 : ref{ ref }
458 , owned{ false }
459 {}
460
461 UniqueAnyReference(UniqueAnyReference&& o)
462 : ref{ ka::exchange(o.ref, AnyReference{}) }
463 , owned{ ka::exchange(o.owned, false) }
464 {
465 }
466
467 UniqueAnyReference& operator=(UniqueAnyReference&& o)
468 {
469 ref = ka::exchange(o.ref, AnyReference{});
470 owned = ka::exchange(o.owned, false);
471 return *this;
472 }
473
474 ~UniqueAnyReference()
475 {
476 destroyRef();
477 }
478
479 AnyReference& operator*()
480 {
481 return ref;
482 }
483
484 const AnyReference& operator*() const
485 {
486 return ref;
487 }
488
489 AnyReference* operator->()
490 {
491 return &ref;
492 }
493
494 const AnyReference* operator->() const
495 {
496 return &ref;
497 }
498
499 AnyReference release()
500 {
501 owned = false;
502 return ka::exchange(ref, AnyReference{});
503 }
504
505 void reset(AnyReference newRef = {})
506 {
507 destroyRef();
508 owned = newRef.isValid();
509 ref = newRef;
510 }
511
512 void reset(AnyReference newRef, DeferOwnership)
513 {
514 destroyRef();
515 owned = false;
516 ref = newRef;
517 }
518
519 bool ownsReference() const
520 {
521 return owned;
522 }
523
524 void takeOwnership()
525 {
526 owned = true;
527 }
528
529 private:
530 void destroyRef()
531 {
532 if (owned)
533 ref.destroy();
534 }
535
536 AnyReference ref;
537 bool owned = false;
538 };
539
540 } // namespace detail
541
542 } // namespace qi
543
544 #include <qi/type/detail/anyreference.hxx>
545 #include <qi/type/typeinterface.hpp>
546
547 /* Since AnyReference does not handle its memory, it cannot be used
548 * inside a AnyReference. use AnyValue instead.
549 */
550 QI_NO_TYPE(qi::AnyReference);
551 QI_NO_TYPE(qi::AutoAnyReference);
552
553 #endif
qi::detail::AnyReferenceBase::asUInt8
uint8_t & asUInt8()
Definition: anyreference.hpp:209
qi::AnyIterator
Definition: anyiterator.hpp:16
qi::AnyReference::AnyReference
AnyReference(TypeInterface *type)
Definition: anyreference.hpp:370
qi::AnyValue
Definition: anyvalue.hpp:19
qi::log::destroy
void destroy()
Stop and flush the logging system.
qi::StringTypeInterface
Definition: typeinterface.hpp:104
qi::int64_t
int64_t int64_t
Definition: types.hpp:61
qi::AutoAnyReference::AutoAnyReference
AutoAnyReference(const AutoAnyReference &b)
Definition: anyreference.hpp:413
qi::detail::UniqueAnyReference::operator*
AnyReference & operator*()
Definition: anyreference.hpp:479
qi::detail::UniqueAnyReference::ownsReference
bool ownsReference() const
Definition: anyreference.hpp:519
QI_API
#define QI_API
Definition: api.hpp:33
qi::detail::AnyReferenceBase::asString
std::string & asString()
Definition: anyreference.hpp:212
qi::detail::UniqueAnyReference::reset
void reset(AnyReference newRef, DeferOwnership)
Definition: anyreference.hpp:512
qi::uint32_t
uint32_t uint32_t
Definition: types.hpp:65
qi::detail::UniqueAnyReference::operator*
const AnyReference & operator*() const
Definition: anyreference.hpp:484
qi::detail::AnyReferenceBase::asFloat
float & asFloat()
Definition: anyreference.hpp:211
qi::detail::AnyReferenceBase::asInt64
int64_t & asInt64()
Definition: anyreference.hpp:202
qi::OptionalTypeInterface
Definition: typeinterface.hpp:334
qi::AnyReference
Definition: anyreference.hpp:360
qi::detail::UniqueAnyReference::operator->
const AnyReference * operator->() const
Definition: anyreference.hpp:494
qi::Signature
Definition: signature.hpp:66
qi::detail::AnyReferenceBase::set
void set(int32_t v)
Definition: anyreference.hpp:246
qi::detail::UniqueAnyReference::~UniqueAnyReference
~UniqueAnyReference()
Definition: anyreference.hpp:474
fwd.hpp
qi::detail::UniqueAnyReference::release
AnyReference release()
Definition: anyreference.hpp:499
qi::detail::AnyReferenceBase::set
void set(const std::string &v)
Definition: anyreference.hpp:251
qi::uint16_t
uint16_t uint16_t
Definition: types.hpp:64
api.hpp
dll import/export and compiler message
types.hpp
qi::detail::UniqueAnyReference::UniqueAnyReference
UniqueAnyReference(AnyReference ref, DeferOwnership)
Definition: anyreference.hpp:456
qi::detail::UniqueAnyReference::UniqueAnyReference
UniqueAnyReference(UniqueAnyReference &&o)
Definition: anyreference.hpp:461
anyreference.hxx
qi::detail::AnyReferenceBase::asDouble
double & asDouble()
Definition: anyreference.hpp:210
qi::detail::AnyReferenceBase::set
void set(int64_t v)
Definition: anyreference.hpp:245
qi::detail::AnyReferenceBase::asInt16
int16_t & asInt16()
Definition: anyreference.hpp:206
qi::detail::AnyReferenceBase::_value
void * _value
Definition: anyreference.hpp:355
qi::detail::UniqueAnyReference
Definition: anyreference.hpp:444
qi::MapTypeInterface
Definition: typeinterface.hpp:213
qi::AutoAnyReference::AutoAnyReference
AutoAnyReference(const AnyReference &self)
Definition: anyreference.hpp:420
qi::makeGenericTuplePtr
AnyReference makeGenericTuplePtr(const std::vector< TypeInterface * > &types, const std::vector< void * > &values)
qi::TypeInterface
Definition: typeinterface.hpp:129
qi::detail::AnyReferenceBase::asUInt16
uint16_t & asUInt16()
Definition: anyreference.hpp:207
qi::makeGenericTuple
AnyReference makeGenericTuple(const AnyReferenceVector &values)
qi::AutoAnyReference::AutoAnyReference
AutoAnyReference()
Definition: anyreference.hpp:412
qi::AutoAnyReference::AutoAnyReference
AutoAnyReference(const detail::AnyReferenceBase &self)
Definition: anyreference.hpp:424
qi::AnyReferenceVector
std::vector< AnyReference > AnyReferenceVector
Definition: anyreference.hpp:26
qi::detail::UniqueAnyReference::operator=
UniqueAnyReference & operator=(UniqueAnyReference &&o)
Definition: anyreference.hpp:467
qi::detail::UniqueAnyReference::takeOwnership
void takeOwnership()
Definition: anyreference.hpp:524
qi::operator==
bool operator==(const Signature &lhs, const Signature &rhs)
qi::detail::AnyReferenceBase::type
TypeInterface * type() const
Definition: anyreference.hpp:348
qi::detail::UniqueAnyReference::operator->
AnyReference * operator->()
Definition: anyreference.hpp:489
qi::detail::AnyReferenceBase::AnyReferenceBase
AnyReferenceBase(TypeInterface *type, void *value)
Definition: anyreference.hpp:59
qi::detail::DeferOwnership
Definition: anyreference.hpp:442
qi::detail::AnyReferenceBase::set
void set(float v)
Definition: anyreference.hpp:249
qi::int32_t
int32_t int32_t
Definition: types.hpp:60
qi::detail::AnyReferenceBase::rawValue
void * rawValue() const
Definition: anyreference.hpp:351
qi::FloatTypeInterface
Definition: typeinterface.hpp:92
qi::detail::AnyReferenceBase::asInt32
int32_t & asInt32()
Definition: anyreference.hpp:204
qi::AutoAnyReference::AutoAnyReference
AutoAnyReference(const T &ptr)
Definition: anyreference.hpp:433
qi::detail::UniqueAnyReference::UniqueAnyReference
UniqueAnyReference()=default
qi::detail::AnyReferenceBase
Definition: anyreference.hpp:40
qi::detail::UniqueAnyReference::reset
void reset(AnyReference newRef={})
Definition: anyreference.hpp:505
qi::uint8_t
uint8_t uint8_t
Definition: types.hpp:63
qi::detail::AnyReferenceBase::asUInt32
uint32_t & asUInt32()
Definition: anyreference.hpp:205
QI_NO_TYPE
QI_NO_TYPE(qi::AnyReference)
qi::Object< Empty >
qi::detail::AnyReferenceBase::isValid
bool isValid() const
Definition: anyreference.hxx:156
qi::RawTypeInterface
Definition: typeinterface.hpp:127
qi::detail::QI_API_DEPRECATED_MSG
struct QI_API_DEPRECATED_MSG(Use 'QI_TYPE_ENUM'instead) QI_TYPE_ENUM_REGISTER_
Definition: typeinterface.hpp:411
atomic.hpp
qi::IntTypeInterface
Definition: typeinterface.hpp:78
qi::PointerTypeInterface
Definition: typeinterface.hpp:137
qi::DynamicTypeInterface
Definition: typeinterface.hpp:310
qi::AutoAnyReference
Definition: anyreference.hpp:409
qi::TypeKind
TypeKind
Definition: fwd.hpp:54
qi::AnyReference::AnyReference
AnyReference(const AnyReferenceBase &rhs)
Definition: anyreference.hpp:366
typeinterface.hpp
qi::detail::AnyReferenceBase::_type
TypeInterface * _type
Definition: anyreference.hpp:354
qi::detail::AnyReferenceBase::asUInt64
uint64_t & asUInt64()
Definition: anyreference.hpp:203
qi::detail::AnyReferenceBase::from
static AnyReference from(const T &ref)
Definition: anyreference.hxx:64
qi::detail::AnyReferenceBase::set
void set(uint32_t v)
Definition: anyreference.hpp:248
typeinterface.hpp
qi::ListTypeInterface
Definition: typeinterface.hpp:188
qi::operator<
bool operator<(const AnyReference &a, const AnyReference &b)
qi::int8_t
int8_t int8_t
Definition: types.hpp:58
qi::AnyReference::AnyReference
AnyReference(TypeInterface *type, void *value)
Definition: anyreference.hpp:374
qi::operator!=
bool operator!=(const Signature &lhs, const Signature &rhs)
Definition: signature.hpp:157
qi::uint64_t
uint64_t uint64_t
Definition: types.hpp:66
qi::int16_t
int16_t int16_t
Definition: types.hpp:59
qi::StructTypeInterface
Definition: typeinterface.hpp:241
qi::detail::AnyReferenceBase::asInt8
int8_t & asInt8()
Definition: anyreference.hpp:208
qi::AnyReference::AnyReference
AnyReference()
Definition: anyreference.hpp:362
qi::detail::AnyReferenceBase::set
void set(double v)
Definition: anyreference.hpp:250
qi::detail::AnyReferenceBase::set
void set(uint64_t v)
Definition: anyreference.hpp:247