libqi-api  2.8.7.4
 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);
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(
180 
182  template<typename R>
183  AnyReference operator()(
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  }
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 
267  {
268  public:
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 
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 
296 
297 #ifdef _MSC_VER
298 # pragma warning( pop )
299 #endif
300 
301 #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
const std::size_t maxAnyFunctionArgsCountHint
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:175
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:116
KeywordArguments & operator()(const std::string &name, const T &t)
#define QI_GEN(f)
Definition: preproc.hpp:476