libqi-api  2.4.3.28
 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  virtual AnyReference get(void* storage)
24  {
25  AnyValue* ptr = (AnyValue*)ptrFromStorage(&storage);
26  return ptr->asReference();
27  }
28 
29  virtual void set(void** storage, AnyReference src)
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 : _allocated(false)
101 {}
102 
103 
104 inline AnyValue::AnyValue(const AnyValue& b)
105 : AnyReferenceBase()
106 , _allocated(false)
107 {
108  *this = b;
109 }
110 
112  : AnyReferenceBase(type)
113  , _allocated(true)
114 {
115 }
116 
117 inline AnyValue::AnyValue(const AnyReference& b, bool copy, bool free)
118 : _allocated(false)
119 {
120  reset(b, copy, free);
121 }
122 
124 : _allocated(false)
125 {
126  reset(b);
127 }
128 
129 template<typename T>
131 {
132  return AnyValue(AnyReference(typeOf<T>()), false, true);
133 }
134 
135 inline void AnyValue::operator=(const AnyValue& b)
136 {
137  reset(b.asReference(), true, true);
138 }
139 
140 inline void AnyValue::operator=(const AnyReference& b)
141 {
142  reset(b, true, true);
143 }
144 
145 inline void AnyValue::reset(const AnyReference& b)
146 {
147  reset(b, true, true);
148 }
149 
150 inline void AnyValue::reset(const AnyReference& b, bool copy, bool free)
151 {
152  reset();
153  *(AnyReferenceBase*)this = b;
154  _allocated = free;
155  if (copy)
156  *(AnyReferenceBase*)this = clone();
157 }
158 
159 inline void AnyValue::reset()
160 {
161  if (_allocated)
163  _type = 0;
164  _value = 0;
165 }
166 
168 {
169  reset();
170  _allocated = true;
171  _type = ttype;
173 }
174 
176 {
177  reset();
178 }
179 
180 inline void AnyValue::swap(AnyValue& b)
181 {
182  std::swap((::qi::AnyReference&)*this, (::qi::AnyReference&)b);
183  std::swap(_allocated, b._allocated);
184 }
185 
186 inline bool operator != (const AnyValue& a, const AnyValue& b)
187 {
188  return !(a==b);
189 }
190 
191 template <typename T1, typename T2>
192 struct FutureValueConverter;
193 
194 template <typename T>
196 {
197  void operator()(const T& in, qi::AnyValue &out)
198  {
199  out = qi::AnyValue::from(in);
200  }
201 };
202 
203 template <>
205 {
206  void operator()(void *in, qi::AnyValue &out)
207  {
208  out = qi::AnyValue::make<void>();
209  }
210 };
211 
213  AnyReferenceVector result;
214  result.resize(vect.size());
215  for (unsigned int i = 0; i < vect.size(); ++i) {
216  result[i] = vect[i].asReference();
217  }
218  return result;
219 }
220 
221 inline bool operator< (const AnyValue& a, const AnyValue& b)
222 {
223  return a.asReference() < b.asReference();
224 }
225 
226 inline bool operator==(const AnyValue& a, const AnyValue& b)
227 {
228  return a.asReference() == b.asReference();
229 }
230 
231 } // namespace qi
232 
233 namespace std
234 {
235  inline void swap(::qi::AnyValue& a, ::qi::AnyValue& b)
236  {
237  a.swap(b);
238  }
239 }
240 
241 #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
static AnyValue makeList(const AnyReferenceVector &values)
Definition: anyvalue.hxx:71
static AnyValue from(const T &r)
Definition: anyvalue.hpp:87
virtual void set(void **storage, AnyReference src)
Set the underlying element.
Definition: anyvalue.hxx:29
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
Specialize this struct to provide conversion between future values.
Definition: future_fwd.hpp:793
void insert(const K &key, const V &val)
void operator=(const AnyReference &b)
Definition: anyvalue.hxx:140
void operator()(void *in, qi::AnyValue &out)
Definition: anyvalue.hxx:206
AnyReference makeGenericTuple(const AnyReferenceVector &values)
std::vector< AnyReference > AnyReferenceVector
bool operator==(const Signature &lhs, const Signature &rhs)
AnyReferenceVector asAnyReferenceVector(const AnyValueVector &vect)
Definition: anyvalue.hxx:212
#define _QI_BOUNCE_TYPE_METHODS(Bounce)
Implement all methods of Type as bouncers to Bouncer.
Definition: typeimpl.hxx:275
void swap(::qi::AnyFunction &a,::qi::AnyFunction &b)
Definition: anyfunction.hxx:89
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:130
AnyReference asReference() const
Definition: anyvalue.hpp:80
DefaultTypeImplMethods< AnyValue, TypeByPointerPOD< AnyValue > > Methods
Definition: anyvalue.hxx:36
void operator()(const T &in, qi::AnyValue &out)
Definition: anyvalue.hxx:197
void swap(AnyValue &b)
Definition: anyvalue.hxx:180
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:154
void reset()
Definition: anyvalue.hxx:159
virtual void * ptrFromStorage(void **s)
Definition: typeimpl.hxx:294
std::vector< AnyValue > AnyValueVector
Definition: anyvalue.hpp:109
void append(const T &element)
virtual void * initializeStorage(void *ptr=0)=0