libqi-api  2.4.3.28
 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 
24 typedef std::vector<AnyReference> AnyReferenceVector;
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 
58 public:
60  std::pair<AnyReference, bool> convert(TypeInterface* targetType) const;
61  std::pair<AnyReference, bool> convert(ListTypeInterface* targetType) const;
62  std::pair<AnyReference, bool> convert(StructTypeInterface* targetType) const;
63  std::pair<AnyReference, bool> convert(MapTypeInterface* targetType) const;
64  std::pair<AnyReference, bool> convert(IntTypeInterface* targetType) const;
65  std::pair<AnyReference, bool> convert(FloatTypeInterface* targetType) const;
66  std::pair<AnyReference, bool> convert(RawTypeInterface* targetType) const;
67  std::pair<AnyReference, bool> convert(StringTypeInterface* targetType) const;
68  std::pair<AnyReference, bool> convert(PointerTypeInterface* targetType) const;
69  std::pair<AnyReference, bool> convert(DynamicTypeInterface* targetType) const;
70 
76  template<typename T>
77  T* ptr(bool check = true);
78 
79  bool isValid() const;
81  bool isValue() const;
82 
84  AnyReference convertCopy(TypeInterface* targetType) const;
85 
86  // get item with key/index 'key'. Return empty GVP or throw in case of failure
87  AnyReference _element(const AnyReference& key, bool throwOnFailure);
88  void _append(const AnyReference& element);
89  void _insert(const AnyReference& key, const AnyReference& val);
91 
93 
101  template <typename T>
102  static AnyReference from(const T& ref);
103 
104  template <typename T>
105  static AnyReference fromPtr(const T* ptr);
106 
107  AnyReference clone() const;
109  void destroy();
111 
112 
114 
119  template<typename T>
121  T to() const;
122 
124  template<typename T>
125  T to(const T&) const;
126 
127  int64_t toInt() const;
128  uint64_t toUInt() const;
129  float toFloat() const;
130  double toDouble() const;
131  std::string toString() const;
132 
133  template<typename T>
134  std::vector<T> toList() const;
135 
136  template<typename K, typename V>
137  std::map<K, V> toMap() const;
138 
139  AnyObject toObject() const;
140 
148  AnyValue toTuple(bool homogeneous) const;
150 
151  qi::Signature signature(bool resolveDynamic = false) const;
152  TypeKind kind() const;
153 
155 
171  template<typename T>
172  T& as();
173 
174  int64_t& asInt64() { return as<int64_t>();}
175  uint64_t& asUInt64() { return as<uint64_t>();}
176  int32_t& asInt32() { return as<int32_t>();}
177  uint32_t& asUInt32() { return as<uint32_t>();}
178  int16_t& asInt16() { return as<int16_t>();}
179  uint16_t& asUInt16() { return as<uint16_t>();}
180  int8_t& asInt8() { return as<int8_t>();}
181  uint8_t& asUInt8() { return as<uint8_t>();}
182  double& asDouble() { return as<double>();}
183  float& asFloat() { return as<float>();}
184  std::string& asString() { return as<std::string>();}
185 
187  std::pair<char*, size_t> asRaw() const;
188 
192  AnyReference content() const;
193 
195 
202  AnyReferenceVector asTupleValuePtr();
203  AnyReferenceVector asListValuePtr();
204  std::map<AnyReference, AnyReference> asMapValuePtr();
206 
209  void update(const AutoAnyReference& b);
210 
212  template<typename T>
213  void set(const T& val);
214 
215  void set(int64_t v) { setInt(v); }
216  void set(int32_t v) { setInt(v); }
217  void set(uint64_t v) { setUInt(v); }
218  void set(uint32_t v) { setUInt(v); }
219  void set(float v) { setFloat(v); }
220  void set(double v) { setDouble(v); }
221  void set(const std::string& v) { setString(v); }
222 
223  void setInt(int64_t v);
224  void setUInt(uint64_t v);
225  void setFloat(float v);
226  void setDouble(double v);
227  void setString(const std::string& v);
228  void setDynamic(const AnyReference &value);
229 
232  void setRaw(const char *buffer, size_t size);
233 
236  void setTuple(const AnyReferenceVector& values);
237 
240 
252  template<typename K>
253  AnyReference operator[](const K& key);
254 
256  template<typename E, typename K>
257  E& element(const K& key);
258 
259  size_t size() const;
260 
261  //TODO: use AutoAnyReference
262  template<typename T>
263  void append(const T& element);
264 
265  template<typename K, typename V>
266  void insert(const K& key, const V& val);
267 
271  template<typename K>
272  AnyReference find(const K& key);
273 
275  AnyIterator begin() const; //we lie on const but GV does not honor it yet
277  AnyIterator end() const;
278 
280  AnyReference operator*() const;
282 
284 
285  TypeInterface* type() const { return _type; }
287  std::vector<TypeInterface*> membersType() const;
288  void* rawValue() const { return _value; }
289 
290 protected:
292  void* _value;
293 };
294 
295 } // namespace detail
296 
298 public:
300  : AnyReferenceBase()
301  {}
302 
303  AnyReference(const AnyReferenceBase& rhs)
304  : AnyReferenceBase(rhs)
305  {}
306 
307  explicit AnyReference(TypeInterface* type)
308  : AnyReferenceBase(type)
309  {}
310 
311  AnyReference(TypeInterface* type, void* value)
312  : AnyReferenceBase(type, value)
313  {}
314 
315 private:
316  //block the dangerous automatic cast from AnyValue to AnyRef
317  //use AnyValue::asReference() to take a ref on the content of a value.
318  //use AnyValue::from(value) to take a ref on the value.
319  AnyReference(const AnyValue& val)
320  {
321  throw std::runtime_error("invalid internal operation.");
322  };
323 };
324 
325 typedef std::vector<AnyReference> AnyReferenceVector;
326 QI_API bool operator< (const AnyReference& a, const AnyReference& b);
327 QI_API bool operator==(const AnyReference& a, const AnyReference& b);
328 QI_API bool operator!=(const AnyReference& a, const AnyReference& b);
329 
331 QI_API AnyReference makeGenericTuple(const AnyReferenceVector& values);
332 
334 QI_API AnyReference makeGenericTuplePtr(
335  const std::vector<TypeInterface*>& types,
336  const std::vector<void*>& values);
337 
347 {
348 public:
351  : AnyReference()
352  {
353  _value = b._value;
354  _type = b._type;
355  }
356 
358  : AnyReference(self)
359  {}
360 
362  : AnyReference(self)
363  {}
364 
365  template<typename T>
366  AutoAnyReference(const T& ptr)
367  {
368  *(AnyReference*)this = AnyReference::from(ptr);
369  }
370 };
371 
372 } // namespace qi
373 
375 
376 #include <qi/type/typeinterface.hpp>
377 
378 /* Since AnyReference does not handle its memory, it cannot be used
379  * inside a AnyReference. use AnyValue instead.
380  */
382 
383 #endif
AnyReference(TypeInterface *type)
void destroy()
Stop and flush the logging system.
AutoAnyReference(const AutoAnyReference &b)
#define QI_API
Definition: api.hpp:34
uint16_t uint16_t
Cross-platform unsigned integer of length 16 bits (2 bytes).
Definition: types.hpp:79
void set(const std::string &v)
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)
int8_t int8_t
Cross-platform signed integer of length 8 bits (1 byte).
Definition: types.hpp:73
int32_t int32_t
Cross-platform signed integer of length 32 bits (4 bytes).
Definition: types.hpp:75
QI_NO_TYPE(qi::AnyReference)
uint64_t uint64_t
Cross-platform unsigned integer of length 64 bits (8 bytes).
Definition: types.hpp:81
TypeKind
Definition: fwd.hpp:53
AnyReference(const AnyReferenceBase &rhs)
static AnyReference from(const T &ref)
uint8_t uint8_t
Cross-platform unsigned integer of length 8 bits (1 byte).
Definition: types.hpp:78
bool operator<(const AnyReference &a, const AnyReference &b)
AnyReference(TypeInterface *type, void *value)
bool operator!=(const Signature &lhs, const Signature &rhs)
Definition: signature.hpp:154
int64_t int64_t
Cross-platform signed integer of length 64 bits (8 bytes).
Definition: types.hpp:76
int16_t int16_t
Cross-platform signed integer of length 16 bits (2 bytes).
Definition: types.hpp:74
uint32_t uint32_t
Cross-platform unsigned integer of length 32 bits (4 bytes).
Definition: types.hpp:80