libqi-api  release-2.5.3-2016-11-18
 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:
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 <>
36  public:
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 
51 }
52 
54 
55 
56 #ifdef _MSC_VER
57 # pragma warning( push )
58 # pragma warning( disable: 4251 )
59 #endif
60 
61 namespace qi {
62 
64  _args.push_back(qi::AnyValue(t));
65  }
66 
68  _args.push_back(qi::AnyValue(t));
69  return *this;
70  }
71 
72 
75  {
76  public:
78  TypeInterface* resultType();
79  const std::vector<TypeInterface*>& argumentsType();
80  qi::Signature parametersSignature() const;
81  qi::Signature returnSignature() const;
82  protected:
84  // C4251
85  std::vector<TypeInterface*> _argumentsType;
86  };
87 
89  {
90  public:
94  virtual void* call(void* storage, void** args, unsigned int argc) = 0;
95  };
96 
97  template<typename T> FunctionTypeInterface* makeFunctionTypeInterface();
98 
100  {
101  public:
102  // Drop first argument
103  bool dropFirst;
104  // Prepend boundValue to argument list
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)
112  , boundValue(value)
113  {}
114 
115  void* boundValue;
116  };
117 
118  template <typename T = AnyValue>
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
129  {
130  public:
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 
150  {
151  public:
152  AnyFunction();
153  ~AnyFunction();
154  AnyFunction(const AnyFunction& b);
155  AnyFunction(FunctionTypeInterface* type, void* value);
156  AnyFunction& operator = (const AnyFunction& b);
158  AnyReference call(const AnyReferenceVector& args);
160  AnyReference call(AnyReference arg1, const AnyReferenceVector& args);
162  AnyReference operator()(const AnyReferenceVector& args);
163 
164 #ifdef DOXYGEN
165  template <typename R>
167  R call(
176 
178  template<typename R>
179  AnyReference operator()(
188 #else
189 #define pushi(z, n, _) params.push_back(p ## n);
190 #define genCall(n, ATYPEDECL, ATYPES, ADECL, AUSE, comma) \
191  template <typename R> R call( \
192  QI_GEN_ARGSDECLSAMETYPE(n, qi::AutoAnyReference)) \
193  { \
194  AnyValue ret(this->operator()(AUSE), false, true); \
195  return ret.to<R>(); \
196  } \
197  AnyReference operator()( \
198  QI_GEN_ARGSDECLSAMETYPE(n, qi::AutoAnyReference)) \
199  { \
200  std::vector<qi::AnyReference> params; \
201  params.reserve(n); \
202  BOOST_PP_REPEAT(n, pushi, _) \
203  return call(params); \
204  }
206 #undef genCall
207 #undef pushi
208 #endif
209 
211  const AnyFunction& dropFirstArgument() const;
213  const AnyFunction& replaceFirstArgument(void* value) const;
215  const AnyFunction& prependArgument(void* value) const;
216 
218  std::vector<TypeInterface*> argumentsType() const;
219  TypeInterface* resultType() const;
220  //dropfirst is useful when you want the parameters signature of a method.
221  Signature parametersSignature(bool dropFirst=false) const;
222  Signature returnSignature() const;
223 
224  void swap(AnyFunction& b);
225 
226  operator bool() const;
227  FunctionTypeInterface* functionType() const;
228 
229  /*** @return an AnyFunction wrapping func.
230  * func can be:
231  * - a boost::bind object
232  * - a boost::function
233  * - a function pointer
234  * - a member function pointer
235  *
236  */
237  template<typename F>
238  static AnyFunction from(F&& func);
240  template<typename F, typename C>
241  static AnyFunction from(F func, C instance);
242 
243 
247  static AnyFunction fromDynamicFunction(DynamicFunction f);
248 
249  private:
250  FunctionTypeInterface* type;
251  void* value; //type-dependant storage
252  mutable ArgumentTransformation transform;
253  };
254 
255 
263  {
264  public:
268  GenericFunctionParameters copy(bool notFirst=false) const;
270  GenericFunctionParameters convert(const Signature& sig) const;
271  qi::Signature signature(bool dyn) const;
272  void destroy(bool notFirst = false);
273  };
274 
277 }
278 
281 
282 #ifdef _MSC_VER
283 # pragma warning( pop )
284 #endif
285 
286 #endif // _QITYPE_ANYFUNCTION_HPP_
boost::function< AnyReference(const AnyReferenceVector &)> DynamicFunction
void destroy()
Stop and flush the logging system.
AnyValueVector & args()
#define QI_API
Definition: api.hpp:33
std::map< std::string, T > values
std::vector< AnyValue > VectorType
Definition: anyfunction.hpp:41
VarArguments(const T &t)
Definition: anyfunction.hpp:22
dll import/export and compiler message
std::vector< T > VectorType
Definition: anyfunction.hpp:25
VarArguments & operator()(const T &t)
Definition: anyfunction.hpp:23
ArgumentTransformation(bool dropFirst=false, bool prependValue=false, void *value=0)
VectorType & args()
Definition: anyfunction.hpp:27
#define genCall(n, ATYPEDECL, ATYPES, ADECL, AUSE, comma)
Definition: session.hpp:107
FunctionTypeInterface * dynamicFunctionTypeInterface()
A function with AnyArguments as its sole argument will behave as if AnyFunction::fromDynamicFunction ...
std::vector< AnyReference > AnyReferenceVector
void swap(::qi::AnyFunction &a,::qi::AnyFunction &b)
Definition: anyfunction.hxx:92
const VectorType & args() const
Definition: anyfunction.hpp:44
AnyArguments(const AnyValueVector &args)
TypeInterface * _resultType
Definition: anyfunction.hpp:83
Signature information for both callable types FunctionTypeInterface and MethodType.
Definition: anyfunction.hpp:74
const AnyValueVector & args() const
const VectorType & args() const
Definition: anyfunction.hpp:28
FunctionTypeInterface * makeFunctionTypeInterface()
std::vector< TypeInterface * > _argumentsType
Definition: anyfunction.hpp:85
std::vector< AnyValue > AnyValueVector
Definition: anyvalue.hpp:112
KeywordArguments & operator()(const std::string &name, const T &t)
#define QI_GEN(f)
Definition: preproc.hpp:476