- 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
anyfunction.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_ANYFUNCTION_HPP_
8 #define _QI_ANYFUNCTION_HPP_
9
10 #include <qi/api.hpp>
11 #include <boost/function.hpp>
12 #include <vector>
13
14 namespace qi{
15 class AnyValue;
16 class AutoAnyReference;
17
18 template <typename T = AnyValue>
19 class VarArguments {
20 public:
21 VarArguments() {};
22 VarArguments(const T& t) { _args.push_back(t); }
23 VarArguments& operator()(const T& t) { _args.push_back(t); return *this; }
24
25 using VectorType = std::vector<T>;
26
27 VectorType &args() { return _args; }
28 const VectorType &args() const { return _args; }
29
30 private:
31 VectorType _args;
32 };
33
34 template <>
35 class VarArguments<AnyValue> {
36 public:
37 VarArguments() {};
38 VarArguments(const AutoAnyReference& t);
39 VarArguments& operator()(const AutoAnyReference& t);
40
41 using VectorType = std::vector<AnyValue>;
42
43 VectorType &args() { return _args; }
44 const VectorType &args() const { return _args; }
45
46 private:
47 VectorType _args;
48 };
49
50 using AnyVarArguments = VarArguments<>;
51 }
52
53 #include <qi/type/typeinterface.hpp>
54
55
56 #ifdef _MSC_VER
57 # pragma warning( push )
58 # pragma warning( disable: 4251 )
59 #endif
60
61 namespace qi {
62
63 inline VarArguments<AnyValue>::VarArguments(const AutoAnyReference& t) {
64 _args.push_back(qi::AnyValue(t));
65 }
66
67 inline VarArguments<AnyValue>& VarArguments<AnyValue>::operator()(const AutoAnyReference& t) {
68 _args.push_back(qi::AnyValue(t));
69 return *this;
70 }
71
72
74 class QI_API CallableTypeInterface
75 {
76 public:
77 CallableTypeInterface();
78 TypeInterface* resultType();
79 const std::vector<TypeInterface*>& argumentsType();
80 qi::Signature parametersSignature() const;
81 qi::Signature returnSignature() const;
82 protected:
83 TypeInterface* _resultType;
84 // C4251
85 std::vector<TypeInterface*> _argumentsType;
86 };
87
88 class QI_API FunctionTypeInterface: public TypeInterface, public CallableTypeInterface
89 {
90 public:
94 virtual void* call(void* storage, void** args, unsigned int argc) = 0;
95 };
96
97 template<typename T> FunctionTypeInterface* makeFunctionTypeInterface();
98
99 struct ArgumentTransformation
100 {
101 public:
102 // Drop first argument
103 bool dropFirst;
104 // Prepend boundValue to argument list
105 bool prependValue;
106
107 // So if both dropFirst and prependValue are set, first argument is
108 // replaced with boundValue.
109 ArgumentTransformation(bool dropFirst = false, bool prependValue=false, void* value = 0)
110 : dropFirst(dropFirst)
111 , prependValue(prependValue)
112 , boundValue(value)
113 {}
114
115 void* boundValue;
116 };
117
118 template <typename T = AnyValue>
119 class QI_API KeywordArguments {
120 public:
121 KeywordArguments& operator()(const std::string& name, const T& t) { values[name] = t; return *this; }
122
123 std::map<std::string, T> values;
124 };
125
127 // This is going to be deprecated in profit of VarArgument and AnyVarArgument
128 class QI_API AnyArguments
129 {
130 public:
131 AnyArguments() {};
132 AnyArguments(const AnyValueVector& args)
133 : _args(args) {}
134 operator const AnyValueVector&() const { return _args;}
135 AnyValueVector &args() { return _args; }
136 const AnyValueVector &args() const { return _args; }
137
138 private:
139 AnyValueVector _args;
140 };
141
142 using DynamicFunction = boost::function<AnyReference(const AnyReferenceVector&)>;
143
149 class QI_API AnyFunction
150 {
151 public:
152 AnyFunction();
153 ~AnyFunction();
154 AnyFunction(const AnyFunction& b);
155 AnyFunction(FunctionTypeInterface* type, void* value);
156 AnyFunction& operator = (const AnyFunction& b);
157
162 AnyReference call(const AnyReferenceVector& args);
164 AnyReference call(AnyReference arg1, const AnyReferenceVector& args);
166 AnyReference operator()(const AnyReferenceVector& args);
167
168 #ifdef DOXYGEN
169 template <typename R>
171 R call(
172 qi::AutoAnyReference p1 = qi::AutoAnyReference(),
173 qi::AutoAnyReference p2 = qi::AutoAnyReference(),
174 qi::AutoAnyReference p3 = qi::AutoAnyReference(),
175 qi::AutoAnyReference p4 = qi::AutoAnyReference(),
176 qi::AutoAnyReference p5 = qi::AutoAnyReference(),
177 qi::AutoAnyReference p6 = qi::AutoAnyReference(),
178 qi::AutoAnyReference p7 = qi::AutoAnyReference(),
179 qi::AutoAnyReference p8 = qi::AutoAnyReference());
180
182 template<typename R>
183 AnyReference operator()(
184 qi::AutoAnyReference p1 = qi::AutoAnyReference(),
185 qi::AutoAnyReference p2 = qi::AutoAnyReference(),
186 qi::AutoAnyReference p3 = qi::AutoAnyReference(),
187 qi::AutoAnyReference p4 = qi::AutoAnyReference(),
188 qi::AutoAnyReference p5 = qi::AutoAnyReference(),
189 qi::AutoAnyReference p6 = qi::AutoAnyReference(),
190 qi::AutoAnyReference p7 = qi::AutoAnyReference(),
191 qi::AutoAnyReference p8 = qi::AutoAnyReference());
192 #else
193 #define pushi(z, n, _) params.push_back(p ## n);
194 #define genCall(n, ATYPEDECL, ATYPES, ADECL, AUSE, comma) \
195 template <typename R> R call( \
196 QI_GEN_ARGSDECLSAMETYPE(n, qi::AutoAnyReference)) \
197 { \
198 AnyValue ret(this->operator()(AUSE), false, true); \
199 return ret.to<R>(); \
200 } \
201 AnyReference operator()( \
202 QI_GEN_ARGSDECLSAMETYPE(n, qi::AutoAnyReference)) \
203 { \
204 std::vector<qi::AnyReference> params; \
205 params.reserve(n); \
206 BOOST_PP_REPEAT(n, pushi, _) \
207 return call(params); \
208 }
209 QI_GEN(genCall)
210 #undef genCall
211 #undef pushi
212 #endif
213
215 const AnyFunction& dropFirstArgument() const;
217 const AnyFunction& replaceFirstArgument(void* value) const;
219 const AnyFunction& prependArgument(void* value) const;
220
222 std::vector<TypeInterface*> argumentsType() const;
223 TypeInterface* resultType() const;
224 //dropfirst is useful when you want the parameters signature of a method.
225 Signature parametersSignature(bool dropFirst=false) const;
226 Signature returnSignature() const;
227
228 void swap(AnyFunction& b);
229
230 operator bool() const;
231 FunctionTypeInterface* functionType() const;
232
233 /*** @return an AnyFunction wrapping func.
234 * func can be:
235 * - a boost::bind object
236 * - a boost::function
237 * - a function pointer
238 * - a member function pointer
239 *
240 */
241 template<typename F>
242 static AnyFunction from(F&& func);
244 template<typename F, typename C>
245 static AnyFunction from(F func, C instance);
246
247
251 static AnyFunction fromDynamicFunction(DynamicFunction f);
252
253 private:
254 FunctionTypeInterface* type;
255 void* value; //type-dependant storage
256 mutable ArgumentTransformation transform;
257 };
258
259
266 class QI_API GenericFunctionParameters: public AnyReferenceVector
267 {
268 public:
269 GenericFunctionParameters();
270 GenericFunctionParameters(const AnyReferenceVector&);
272 GenericFunctionParameters copy(bool notFirst=false) const;
274 GenericFunctionParameters convert(const Signature& sig) const;
275 qi::Signature signature(bool dyn) const;
276 void destroy(bool notFirst = false);
277 };
278
280 QI_API FunctionTypeInterface* dynamicFunctionTypeInterface();
281
282 namespace detail
283 {
284
285 // This is just a hint of the maximum of the number of arguments that can be passed to a function,
286 // that is used to preallocate on the stack some of the arguments containers. It should be a
287 // number that will cover, if not all, most cases of functions without being too big that it would
288 // be unused stack space.
289 const std::size_t maxAnyFunctionArgsCountHint = 8u;
290
291 }
292 }
293
294 #include <qi/type/detail/anyfunction.hxx>
295 #include <qi/type/detail/anyfunctionfactory.hxx>
296
297 #ifdef _MSC_VER
298 # pragma warning( pop )
299 #endif
300
301 #endif // _QITYPE_ANYFUNCTION_HPP_
anyfunction.hxx
qi::DynamicFunction
boost::function< AnyReference(const AnyReferenceVector &)> DynamicFunction
Definition: anyfunction.hpp:142
qi::ArgumentTransformation::dropFirst
bool dropFirst
Definition: anyfunction.hpp:103
qi::AnyValue
Definition: anyvalue.hpp:19
qi::log::destroy
void destroy()
Stop and flush the logging system.
qi::GenericFunctionParameters
Definition: anyfunction.hpp:266
qi::AnyArguments::args
AnyValueVector & args()
Definition: anyfunction.hpp:135
QI_API
#define QI_API
Definition: api.hpp:33
qi::KeywordArguments::values
std::map< std::string, T > values
Definition: anyfunction.hpp:123
qi::AnyFunction
Definition: anyfunction.hpp:149
qi::AnyReference
Definition: anyreference.hpp:360
qi::Signature
Definition: signature.hpp:66
qi::VarArguments< AnyValue >::VectorType
std::vector< AnyValue > VectorType
Definition: anyfunction.hpp:41
qi::VarArguments< AnyValue >::VarArguments
VarArguments()
Definition: anyfunction.hpp:37
qi::VarArguments::VarArguments
VarArguments(const T &t)
Definition: anyfunction.hpp:22
qi::VarArguments
Definition: anyfunction.hpp:19
qi::VarArguments< AnyValue >::args
VectorType & args()
Definition: anyfunction.hpp:43
qi::ArgumentTransformation
Definition: anyfunction.hpp:99
api.hpp
dll import/export and compiler message
qi::VarArguments::VectorType
std::vector< T > VectorType
Definition: anyfunction.hpp:25
qi::KeywordArguments
Definition: anyfunction.hpp:119
qi::detail::maxAnyFunctionArgsCountHint
const std::size_t maxAnyFunctionArgsCountHint
Definition: anyfunction.hpp:289
qi::VarArguments::operator()
VarArguments & operator()(const T &t)
Definition: anyfunction.hpp:23
qi::ArgumentTransformation::ArgumentTransformation
ArgumentTransformation(bool dropFirst=false, bool prependValue=false, void *value=0)
Definition: anyfunction.hpp:109
qi::TypeInterface
Definition: typeinterface.hpp:129
qi::VarArguments::args
VectorType & args()
Definition: anyfunction.hpp:27
genCall
#define genCall(n, ATYPEDECL, ATYPES, ADECL, AUSE, comma)
Definition: session.hpp:175
qi::dynamicFunctionTypeInterface
FunctionTypeInterface * dynamicFunctionTypeInterface()
qi::AnyArguments
A function with AnyArguments as its sole argument will behave as if AnyFunction::fromDynamicFunction ...
Definition: anyfunction.hpp:128
qi::FunctionTypeInterface
Definition: anyfunction.hpp:88
qi::AnyReferenceVector
std::vector< AnyReference > AnyReferenceVector
Definition: anyreference.hpp:26
qi::VarArguments< AnyValue >
Definition: anyfunction.hpp:35
std::swap
void swap(::qi::AnyFunction &a,::qi::AnyFunction &b)
Definition: anyfunction.hxx:92
qi::VarArguments< AnyValue >::args
const VectorType & args() const
Definition: anyfunction.hpp:44
qi::AnyArguments::AnyArguments
AnyArguments(const AnyValueVector &args)
Definition: anyfunction.hpp:132
qi::VarArguments::VarArguments
VarArguments()
Definition: anyfunction.hpp:21
qi::CallableTypeInterface::_resultType
TypeInterface * _resultType
Definition: anyfunction.hpp:83
qi::CallableTypeInterface
Signature information for both callable types FunctionTypeInterface and MethodType.
Definition: anyfunction.hpp:74
anyfunctionfactory.hxx
qi::AnyArguments::args
const AnyValueVector & args() const
Definition: anyfunction.hpp:136
qi::ArgumentTransformation::boundValue
void * boundValue
Definition: anyfunction.hpp:115
qi::log::env::QI_DEFAULT_LOGHANDLER::name
char const *const name
qi::VarArguments::args
const VectorType & args() const
Definition: anyfunction.hpp:28
qi::AutoAnyReference
Definition: anyreference.hpp:409
typeinterface.hpp
qi::makeFunctionTypeInterface
FunctionTypeInterface * makeFunctionTypeInterface()
qi::CallableTypeInterface::_argumentsType
std::vector< TypeInterface * > _argumentsType
Definition: anyfunction.hpp:85
qi::AnyArguments::AnyArguments
AnyArguments()
Definition: anyfunction.hpp:131
qi::AnyValueVector
std::vector< AnyValue > AnyValueVector
Definition: anyvalue.hpp:116
qi::KeywordArguments::operator()
KeywordArguments & operator()(const std::string &name, const T &t)
Definition: anyfunction.hpp:121
QI_GEN
#define QI_GEN(f)
Definition: preproc.hpp:476
qi::ArgumentTransformation::prependValue
bool prependValue
Definition: anyfunction.hpp:105