libqi-api  2.8.7.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
anyvalue.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_ANYVALUE_HXX_
8 #define _QI_TYPE_DETAIL_ANYVALUE_HXX_
9 
10 #include <cmath>
11 
12 #include <boost/type_traits/remove_const.hpp>
13 #include <boost/type_traits/is_floating_point.hpp>
16 
17 namespace qi {
18 
19 template<>
21 {
22 public:
23  AnyReference get(void* storage) override
24  {
25  AnyValue* ptr = (AnyValue*)ptrFromStorage(&storage);
26  return ptr->asReference();
27  }
28 
29  void set(void** storage, AnyReference src) override
30  {
31  AnyValue* val = (AnyValue*)ptrFromStorage(storage);
32  val->reset(src, true, true);
33  }
34 
35  // Default cloner will do just right since AnyValue is by-value.
38 };
39 
40 inline AnyValue
42 {
43  return AnyValue(makeGenericTuple(values), false, true);
44 }
45 
46 inline AnyValue
48  const AutoAnyReference& v1,
49  const AutoAnyReference& v2,
50  const AutoAnyReference& v3,
51  const AutoAnyReference& v4,
52  const AutoAnyReference& v5,
53  const AutoAnyReference& v6,
54  const AutoAnyReference& v7,
55  const AutoAnyReference& v8,
56  const AutoAnyReference& v9)
57 {
58  const AnyReference* vect[10] = { &v0, &v1, &v2, &v3, &v4, &v5, &v6, &v7, &v8, &v9 };
60 
61  for (unsigned int i = 0; i < 10; ++i) {
62  if (!vect[i]->isValid())
63  break;
64  arv.push_back(*vect[i]);
65  }
66  return AnyValue(makeGenericTuple(arv), false, true);
67 }
68 
69 
70 template<typename T>
72 {
73  AnyValue res = make<std::vector<T> >();
74  for (unsigned i=0; i<values.size(); ++i)
75  res.append(values[i].to<T>());
76  return res;
77 }
78 inline
80 {
81  return makeList<AnyValue>(values);
82 }
83 template<typename K, typename V>
84 AnyValue AnyValue::makeMap(const std::map<AnyReference, AnyReference>& values)
85 {
86  AnyValue res = make<std::map<K, V> >();
87  std::map<AnyReference, AnyReference>::const_iterator it;
88  for(it = values.begin(); it != values.end(); ++it)
89  res.insert(it->first.to<K>(), it->second.to<V>());
90  return res;
91 }
92 
94  AnyReference>& values)
95 {
96  return makeMap<AnyValue, AnyValue>(values);
97 }
98 
100 {
101  return qi::AnyValue(qi::typeOf<void>());
102 }
103 
105 : _allocated(false)
106 {}
107 
108 
109 inline AnyValue::AnyValue(const AnyValue& b)
110 : AnyReferenceBase()
111 , _allocated(false)
112 {
113  *this = b;
114 }
115 
117  : AnyReferenceBase(type)
118  , _allocated(true)
119 {
120 }
121 
122 inline AnyValue::AnyValue(const AnyReference& b, bool copy, bool free)
123 : _allocated(false)
124 {
125  reset(b, copy, free);
126 }
127 
129 : _allocated(false)
130 {
131  reset(b);
132 }
133 
134 template<typename T>
136 {
137  return AnyValue(AnyReference(typeOf<T>()), false, true);
138 }
139 
141 {
142  if (&b == this)
143  return *this;
144 
145  reset(b.asReference(), true, true);
146  return *this;
147 }
148 
150 {
151  reset(b, true, true);
152  return *this;
153 }
154 
155 inline void AnyValue::reset(const AnyReference& b)
156 {
157  reset(b, true, true);
158 }
159 
160 inline void AnyValue::reset(const AnyReference& b, bool copy, bool free)
161 {
162  reset();
163  *(AnyReferenceBase*)this = b;
164  _allocated = free;
165  if (copy)
166  *(AnyReferenceBase*)this = clone();
167 }
168 
169 inline void AnyValue::reset()
170 {
171  if (_allocated)
173  _type = 0;
174  _value = 0;
175 }
176 
178 {
179  reset();
180  _allocated = true;
181  _type = ttype;
183 }
184 
186 {
187  reset();
188 }
189 
190 inline void AnyValue::swap(AnyValue& b)
191 {
192  std::swap((::qi::AnyReference&)*this, (::qi::AnyReference&)b);
193  std::swap(_allocated, b._allocated);
194 }
195 
196 inline bool operator != (const AnyValue& a, const AnyValue& b)
197 {
198  return !(a==b);
199 }
200 
201 template <typename T1, typename T2>
202 struct FutureValueConverter;
203 
204 template <typename T>
206 {
207  void operator()(const T& in, qi::AnyValue &out)
208  {
209  out = qi::AnyValue::from(in);
210  }
211 };
212 
213 template <>
215 {
216  void operator()(void *in, qi::AnyValue &out)
217  {
218  out = qi::AnyValue::make<void>();
219  }
220 };
221 
223  AnyReferenceVector result;
224  result.resize(vect.size());
225  for (unsigned int i = 0; i < vect.size(); ++i) {
226  result[i] = vect[i].asReference();
227  }
228  return result;
229 }
230 
231 inline bool operator< (const AnyValue& a, const AnyValue& b)
232 {
233  return a.asReference() < b.asReference();
234 }
235 
236 inline bool operator==(const AnyValue& a, const AnyValue& b)
237 {
238  return a.asReference() == b.asReference();
239 }
240 
241 } // namespace qi
242 
243 namespace std
244 {
245  inline void swap(::qi::AnyValue& a, ::qi::AnyValue& b)
246  {
247  a.swap(b);
248  }
249 }
250 
251 #endif // _QI_TYPE_DETAIL_ANYVALUE_HXX_
static AnyValue makeGenericMap(const std::map< AnyReference, AnyReference > &values)
Definition: anyvalue.hxx:93
void destroy()
Stop and flush the logging system.
static AnyValue makeGenericList(const AnyReferenceVector &values)
Definition: anyvalue.hxx:79
void set(void **storage, AnyReference src) override
Set the underlying element.
Definition: anyvalue.hxx:29
static AnyValue makeList(const AnyReferenceVector &values)
Definition: anyvalue.hxx:71
static AnyValue from(const T &r)
Definition: anyvalue.hpp:94
static AnyValue makeTupleFromValue(const AutoAnyReference &v0, const AutoAnyReference &v1, const AutoAnyReference &v2, const AutoAnyReference &v3, const AutoAnyReference &v4, const AutoAnyReference &v5, const AutoAnyReference &v6, const AutoAnyReference &v7, const AutoAnyReference &v8, const AutoAnyReference &v9)
Definition: anyvalue.hxx:47
AnyValue & operator=(const AnyReference &b)
Definition: anyvalue.hxx:149
AnyReferenceBase()
Constructs an invalid reference, pointing to nothing.
Specialize this struct to provide conversion between future values.
virtual void * initializeStorage(void *ptr=nullptr)=0
void insert(const K &key, const V &val)
void operator()(void *in, qi::AnyValue &out)
Definition: anyvalue.hxx:216
AnyReference makeGenericTuple(const AnyReferenceVector &values)
std::vector< AnyReference > AnyReferenceVector
static AnyValue makeVoid()
Construct a void AnyValue: defined, but with no data.
Definition: anyvalue.hxx:99
bool operator==(const Signature &lhs, const Signature &rhs)
AnyReferenceVector asAnyReferenceVector(const AnyValueVector &vect)
Definition: anyvalue.hxx:222
#define _QI_BOUNCE_TYPE_METHODS(Bounce)
Implement all methods of Type as bouncers to Bouncer.
Definition: typeimpl.hxx:273
void swap(::qi::AnyFunction &a,::qi::AnyFunction &b)
Definition: anyfunction.hxx:92
void * ptrFromStorage(void **s) override
Definition: typeimpl.hxx:292
static AnyValue makeMap(const std::map< AnyReference, AnyReference > &values)
Definition: anyvalue.hxx:84
static AnyValue make()
Create and return a AnyValue of type T.
Definition: anyvalue.hxx:135
AnyReference asReference() const
Definition: anyvalue.hpp:87
void operator()(const T &in, qi::AnyValue &out)
Definition: anyvalue.hxx:207
void swap(AnyValue &b)
Definition: anyvalue.hxx:190
T src(const std::atomic< T > &x)
Definition: atomic.hpp:318
AnyReference clone() const
static AnyValue makeTuple(const AnyReferenceVector &values)
Definition: anyvalue.hxx:41
bool operator<(const AnyReference &a, const AnyReference &b)
bool operator!=(const Signature &lhs, const Signature &rhs)
Definition: signature.hpp:157
void reset()
Definition: anyvalue.hxx:169
std::vector< AnyValue > AnyValueVector
Definition: anyvalue.hpp:116
void append(const T &element)