- 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
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>
14 #include <qi/type/detail/anyiterator.hpp>
15 #include <qi/type/detail/anyreference.hpp>
16
17 namespace qi {
18
19 template<>
20 class TypeImpl<AnyValue>: public DynamicTypeInterface
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.
36 using Methods = DefaultTypeImplMethods<AnyValue, TypeByPointerPOD<AnyValue>>;
37 _QI_BOUNCE_TYPE_METHODS(Methods);
38 };
39
40 inline AnyValue
41 AnyValue::makeTuple(const AnyReferenceVector& values)
42 {
43 return AnyValue(makeGenericTuple(values), false, true);
44 }
45
46 inline AnyValue
47 AnyValue::makeTupleFromValue(const AutoAnyReference& v0,
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 };
59 AnyReferenceVector arv;
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>
71 AnyValue AnyValue::makeList(const AnyReferenceVector& values)
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
79 AnyValue AnyValue::makeGenericList(const AnyReferenceVector& values)
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
93 inline AnyValue AnyValue::makeGenericMap(const std::map<AnyReference,
94 AnyReference>& values)
95 {
96 return makeMap<AnyValue, AnyValue>(values);
97 }
98
99 inline AnyValue AnyValue::makeVoid()
100 {
101 return qi::AnyValue(qi::typeOf<void>());
102 }
103
104 inline AnyValue::AnyValue()
105 : _allocated(false)
106 {}
107
108
109 inline AnyValue::AnyValue(const AnyValue& b)
110 : AnyReferenceBase()
111 , _allocated(false)
112 {
113 *this = b;
114 }
115
116 inline AnyValue::AnyValue(qi::TypeInterface *type)
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
128 inline AnyValue::AnyValue(const AutoAnyReference& b)
129 : _allocated(false)
130 {
131 reset(b);
132 }
133
134 template<typename T>
135 AnyValue AnyValue::make()
136 {
137 return AnyValue(AnyReference(typeOf<T>()), false, true);
138 }
139
140 inline AnyValue& AnyValue::operator=(const AnyValue& b)
141 {
142 if (&b == this)
143 return *this;
144
145 reset(b.asReference(), true, true);
146 return *this;
147 }
148
149 inline AnyValue& AnyValue::operator=(const AnyReference& b)
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)
172 AnyReferenceBase::destroy();
173 _type = 0;
174 _value = 0;
175 }
176
177 inline void AnyValue::reset(qi::TypeInterface *ttype)
178 {
179 reset();
180 _allocated = true;
181 _type = ttype;
182 _value = _type->initializeStorage();
183 }
184
185 inline AnyValue::~AnyValue()
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>
205 struct FutureValueConverter<T, qi::AnyValue>
206 {
207 void operator()(const T& in, qi::AnyValue &out)
208 {
209 out = qi::AnyValue::from(in);
210 }
211 };
212
213 template <>
214 struct FutureValueConverter<void, qi::AnyValue>
215 {
216 void operator()(void *in, qi::AnyValue &out)
217 {
218 out = qi::AnyValue::make<void>();
219 }
220 };
221
222 inline AnyReferenceVector asAnyReferenceVector(const AnyValueVector& vect) {
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_
qi::AnyValue::makeGenericMap
static AnyValue makeGenericMap(const std::map< AnyReference, AnyReference > &values)
Definition: anyvalue.hxx:93
qi::TypeImpl
Definition: typeimpl.hxx:300
qi::AnyValue
Definition: anyvalue.hpp:19
qi::log::destroy
void destroy()
Stop and flush the logging system.
qi::AnyValue::makeGenericList
static AnyValue makeGenericList(const AnyReferenceVector &values)
Definition: anyvalue.hxx:79
qi::TypeImpl< AnyValue >::set
void set(void **storage, AnyReference src) override
Set the underlying element.
Definition: anyvalue.hxx:29
qi::AnyValue::makeList
static AnyValue makeList(const AnyReferenceVector &values)
Definition: anyvalue.hxx:71
qi::AnyReference
Definition: anyreference.hpp:360
qi::AnyValue::from
static AnyValue from(const T &r)
Definition: anyvalue.hpp:94
qi::AnyValue::makeTupleFromValue
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
qi::AnyValue::operator=
AnyValue & operator=(const AnyReference &b)
Definition: anyvalue.hxx:149
qi::detail::AnyReferenceBase::AnyReferenceBase
AnyReferenceBase()
Constructs an invalid reference, pointing to nothing.
Definition: anyreference.hxx:41
qi::FutureValueConverter
Specialize this struct to provide conversion between future values.
Definition: future_fwd.hpp:1064
qi::TypeInterface::initializeStorage
virtual void * initializeStorage(void *ptr=nullptr)=0
qi::detail::AnyReferenceBase::insert
void insert(const K &key, const V &val)
Definition: anyreference.hxx:276
qi::detail::AnyReferenceBase::_value
void * _value
Definition: anyreference.hpp:355
qi::AnyValue::~AnyValue
~AnyValue()
Definition: anyvalue.hxx:185
qi::FutureValueConverter< void, qi::AnyValue >::operator()
void operator()(void *in, qi::AnyValue &out)
Definition: anyvalue.hxx:216
qi::TypeInterface
Definition: typeinterface.hpp:129
anyreference.hpp
qi::makeGenericTuple
AnyReference makeGenericTuple(const AnyReferenceVector &values)
qi::AnyReferenceVector
std::vector< AnyReference > AnyReferenceVector
Definition: anyreference.hpp:26
qi::AnyValue::makeVoid
static AnyValue makeVoid()
Construct a void AnyValue: defined, but with no data.
Definition: anyvalue.hxx:99
qi::operator==
bool operator==(const Signature &lhs, const Signature &rhs)
qi::asAnyReferenceVector
AnyReferenceVector asAnyReferenceVector(const AnyValueVector &vect)
Definition: anyvalue.hxx:222
_QI_BOUNCE_TYPE_METHODS
#define _QI_BOUNCE_TYPE_METHODS(Bounce)
Implement all methods of Type as bouncers to Bouncer.
Definition: typeimpl.hxx:273
std::swap
void swap(::qi::AnyFunction &a,::qi::AnyFunction &b)
Definition: anyfunction.hxx:92
qi::DefaultTypeImpl< T >::ptrFromStorage
void * ptrFromStorage(void **s) override
Definition: typeimpl.hxx:292
qi::AnyValue::makeMap
static AnyValue makeMap(const std::map< AnyReference, AnyReference > &values)
Definition: anyvalue.hxx:84
qi::DefaultTypeImplMethods
Definition: typeimpl.hxx:228
qi::AnyValue::make
static AnyValue make()
Create and return a AnyValue of type T.
Definition: anyvalue.hxx:135
qi::AnyValue::asReference
AnyReference asReference() const
Definition: anyvalue.hpp:87
qi::FutureValueConverter< T, qi::AnyValue >::operator()
void operator()(const T &in, qi::AnyValue &out)
Definition: anyvalue.hxx:207
qi::AnyValue::AnyValue
AnyValue()
Definition: anyvalue.hxx:104
anyiterator.hpp
qi::detail::AnyReferenceBase::isValid
bool isValid() const
Definition: anyreference.hxx:156
qi::AnyValue::swap
void swap(AnyValue &b)
Definition: anyvalue.hxx:190
qi::src
T src(const std::atomic< T > &x)
Definition: atomic.hpp:318
qi::detail::AnyReferenceBase::clone
AnyReference clone() const
Definition: anyreference.hxx:18
qi::DynamicTypeInterface
Definition: typeinterface.hpp:310
qi::AnyValue::makeTuple
static AnyValue makeTuple(const AnyReferenceVector &values)
Definition: anyvalue.hxx:41
qi::AutoAnyReference
Definition: anyreference.hpp:409
qi::detail::AnyReferenceBase::_type
TypeInterface * _type
Definition: anyreference.hpp:354
qi::operator<
bool operator<(const AnyReference &a, const AnyReference &b)
qi::operator!=
bool operator!=(const Signature &lhs, const Signature &rhs)
Definition: signature.hpp:157
qi::AnyValue::reset
void reset()
Definition: anyvalue.hxx:169
qi::AnyValueVector
std::vector< AnyValue > AnyValueVector
Definition: anyvalue.hpp:116
qi::detail::AnyReferenceBase::append
void append(const T &element)
Definition: anyreference.hxx:270