libqi-api  release-2.5.3-2016-11-18
 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>
19 #include <boost/type_traits/remove_const.hpp>
20 
21 namespace qi {
22 
23 class AnyReference;
24 using AnyReferenceVector = std::vector<AnyReference>;
25 
26 namespace detail {
27 
36 {
37 protected:
39 
42 
46  explicit AnyReferenceBase(TypeInterface* type);
47 
53  AnyReferenceBase(TypeInterface* type, void* value)
54  : _type(type)
55  , _value(value)
56  {}
57 
68  AnyReference _element(const AnyReference& key, bool throwOnFailure, bool autoInsert);
69 
70 public:
72  std::pair<AnyReference, bool> convert(TypeInterface* targetType) const;
73  std::pair<AnyReference, bool> convert(ListTypeInterface* targetType) const;
74  std::pair<AnyReference, bool> convert(StructTypeInterface* targetType) const;
75  std::pair<AnyReference, bool> convert(MapTypeInterface* targetType) const;
76  std::pair<AnyReference, bool> convert(IntTypeInterface* targetType) const;
77  std::pair<AnyReference, bool> convert(FloatTypeInterface* targetType) const;
78  std::pair<AnyReference, bool> convert(RawTypeInterface* targetType) const;
79  std::pair<AnyReference, bool> convert(StringTypeInterface* targetType) const;
80  std::pair<AnyReference, bool> convert(PointerTypeInterface* targetType) const;
81  std::pair<AnyReference, bool> convert(DynamicTypeInterface* targetType) const;
82 
88  template<typename T>
89  T* ptr(bool check = true);
90 
91  bool isValid() const;
93  bool isValue() const;
94 
96  AnyReference convertCopy(TypeInterface* targetType) const;
98 
100 
108  template <typename T>
109  static AnyReference from(const T& ref);
110 
111  template <typename T>
112  static AnyReference fromPtr(const T* ptr);
113 
114  AnyReference clone() const;
116  void destroy();
118 
119 
121 
126  template<typename T>
128  T to() const;
129 
131  template<typename T>
132  T to(const T&) const;
133 
134  int64_t toInt() const;
135  uint64_t toUInt() const;
136  float toFloat() const;
137  double toDouble() const;
138  std::string toString() const;
139 
140  template<typename T>
141  std::vector<T> toList() const;
142 
143  template<typename K, typename V>
144  std::map<K, V> toMap() const;
145 
146  AnyObject toObject() const;
147 
155  AnyValue toTuple(bool homogeneous) const;
157 
158  qi::Signature signature(bool resolveDynamic = false) const;
159  TypeKind kind() const;
160  AnyReference unwrap() const;
161 
163 
179  template<typename T>
180  T& as();
181 
182  int64_t& asInt64() { return as<int64_t>();}
183  uint64_t& asUInt64() { return as<uint64_t>();}
184  int32_t& asInt32() { return as<int32_t>();}
185  uint32_t& asUInt32() { return as<uint32_t>();}
186  int16_t& asInt16() { return as<int16_t>();}
187  uint16_t& asUInt16() { return as<uint16_t>();}
188  int8_t& asInt8() { return as<int8_t>();}
189  uint8_t& asUInt8() { return as<uint8_t>();}
190  double& asDouble() { return as<double>();}
191  float& asFloat() { return as<float>();}
192  std::string& asString() { return as<std::string>();}
193 
195  std::pair<char*, size_t> asRaw() const;
196 
200  AnyReference content() const;
201 
203 
210  AnyReferenceVector asTupleValuePtr();
211  AnyReferenceVector asListValuePtr();
212  std::map<AnyReference, AnyReference> asMapValuePtr();
214 
217  void update(const AutoAnyReference& b);
218 
220  template<typename T>
221  void set(const T& val);
222 
223  void set(int64_t v) { setInt(v); }
224  void set(int32_t v) { setInt(v); }
225  void set(uint64_t v) { setUInt(v); }
226  void set(uint32_t v) { setUInt(v); }
227  void set(float v) { setFloat(v); }
228  void set(double v) { setDouble(v); }
229  void set(const std::string& v) { setString(v); }
230 
231  void setInt(int64_t v);
232  void setUInt(uint64_t v);
233  void setFloat(float v);
234  void setDouble(double v);
235  void setString(const std::string& v);
236  void setDynamic(const AnyReference &value);
237 
240  void setRaw(const char *buffer, size_t size);
241 
244  void setTuple(const AnyReferenceVector& values);
245 
248 
260  template<typename K>
261  AnyReference operator[](const K& key);
262  AnyReference operator[](const AnyReference& key);
263 
265  template<typename E, typename K>
266  E& element(const K& key);
267 
274  template<typename K>
275  AnyReference at(const K& key);
276  template<typename K>
277  AnyReference at(const K& key) const;
278  AnyReference at(const AnyReference& key);
279  AnyReference at(const AnyReference& key) const;
280 
281  size_t size() const;
282 
283  //TODO: use AutoAnyReference
284  template<typename T>
285  void append(const T& element);
286  void append(const AnyReference& element);
287 
288  template<typename K, typename V>
289  void insert(const K& key, const V& val);
290  void insert(const AnyReference& key, const AnyReference& val);
291 
292 
296  template<typename K>
297  AnyReference find(const K& key);
298 
300  AnyIterator begin() const; //we lie on const but GV does not honor it yet
302  AnyIterator end() const;
303 
305  AnyReference operator*() const;
307 
309 
310  TypeInterface* type() const { return _type; }
312  std::vector<TypeInterface*> membersType() const;
313  void* rawValue() const { return _value; }
314 
315 protected:
317  void* _value;
318 };
319 
320 } // namespace detail
321 
323 public:
325  : AnyReferenceBase()
326  {}
327 
328  AnyReference(const AnyReferenceBase& rhs)
329  : AnyReferenceBase(rhs)
330  {}
331 
332  explicit AnyReference(TypeInterface* type)
333  : AnyReferenceBase(type)
334  {}
335 
336  AnyReference(TypeInterface* type, void* value)
337  : AnyReferenceBase(type, value)
338  {}
339 
340 private:
341  //block the dangerous automatic cast from AnyValue to AnyRef
342  //use AnyValue::asReference() to take a ref on the content of a value.
343  //use AnyValue::from(value) to take a ref on the value.
344  AnyReference(const AnyValue& val)
345  {
346  throw std::runtime_error("invalid internal operation.");
347  };
348 };
349 
350 using AnyReferenceVector = std::vector<AnyReference>;
351 QI_API bool operator< (const AnyReference& a, const AnyReference& b);
352 QI_API bool operator==(const AnyReference& a, const AnyReference& b);
353 QI_API bool operator!=(const AnyReference& a, const AnyReference& b);
354 
356 QI_API AnyReference makeGenericTuple(const AnyReferenceVector& values);
357 
359 QI_API AnyReference makeGenericTuplePtr(
360  const std::vector<TypeInterface*>& types,
361  const std::vector<void*>& values);
362 
372 {
373 public:
376  : AnyReference()
377  {
378  _value = b._value;
379  _type = b._type;
380  }
381 
383  : AnyReference(self)
384  {}
385 
387  : AnyReference(self)
388  {}
389 
390  template<typename T>
391  AutoAnyReference(const T& ptr)
392  {
393  *(AnyReference*)this = AnyReference::from(ptr);
394  }
395 };
396 
397 } // namespace qi
398 
400 
401 #include <qi/type/typeinterface.hpp>
402 
403 /* Since AnyReference does not handle its memory, it cannot be used
404  * inside a AnyReference. use AnyValue instead.
405  */
408 
409 #endif
AnyReference(TypeInterface *type)
void destroy()
Stop and flush the logging system.
int64_t int64_t
Definition: types.hpp:61
AutoAnyReference(const AutoAnyReference &b)
#define QI_API
Definition: api.hpp:33
uint32_t uint32_t
Definition: types.hpp:65
void set(const std::string &v)
uint16_t uint16_t
Definition: types.hpp:64
dll import/export and compiler message
AutoAnyReference(const T &ptr)
AutoAnyReference(const AnyReference &self)
AnyReference makeGenericTuplePtr(const std::vector< TypeInterface * > &types, const std::vector< void * > &values)
AnyReference makeGenericTuple(const AnyReferenceVector &values)
AutoAnyReference(const detail::AnyReferenceBase &self)
std::vector< AnyReference > AnyReferenceVector
bool operator==(const Signature &lhs, const Signature &rhs)
TypeInterface * type() const
AnyReferenceBase(TypeInterface *type, void *value)
int32_t int32_t
Definition: types.hpp:60
uint8_t uint8_t
Definition: types.hpp:63
QI_NO_TYPE(qi::AnyReference)
TypeKind
Definition: fwd.hpp:53
AnyReference(const AnyReferenceBase &rhs)
static AnyReference from(const T &ref)
bool operator<(const AnyReference &a, const AnyReference &b)
int8_t int8_t
Definition: types.hpp:58
AnyReference(TypeInterface *type, void *value)
bool operator!=(const Signature &lhs, const Signature &rhs)
Definition: signature.hpp:154
uint64_t uint64_t
Definition: types.hpp:66
int16_t int16_t
Definition: types.hpp:59