Rcpp Version 1.0.9
InternalFunctionWithStdFunction.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 //
3 // InternalFunction_with_std_function.h: Rcpp R/C++ interface class library -- exposing C++ std::function's
4 //
5 // Copyright (C) 2014 Christian Authmann
6 // Copyright (C) 2015 Romain Francois and Dirk Eddelbuettel
7 //
8 // This file is part of Rcpp.
9 //
10 // Rcpp is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 2 of the License, or
13 // (at your option) any later version.
14 //
15 // Rcpp is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
22 
23 #ifndef Rcpp_InternalFunctionWithStdFunction_h
24 #define Rcpp_InternalFunctionWithStdFunction_h
25 
26 #include <functional>
27 
28 namespace Rcpp {
29 
30  namespace InternalFunctionWithStdFunction {
31 
32  #include <Rcpp/generated/InternalFunctionWithStdFunction_call.h>
33 
34  template <typename RESULT_TYPE, typename... Args>
36  public:
37  CppFunctionBaseFromStdFunction(const std::function<RESULT_TYPE(Args...)> &fun) : fun(fun) {}
39 
40  SEXP operator()(SEXP* args) {
42  auto result = call<RESULT_TYPE, Args...>(fun, args);
43  return Rcpp::module_wrap<RESULT_TYPE>(result);
44  END_RCPP
45  }
46 
47  private:
48  const std::function<RESULT_TYPE(Args...)> fun;
49  };
50 
51  template <typename... Args>
52  class CppFunctionBaseFromStdFunction<void, Args...> : public CppFunctionBase {
53  public:
54  CppFunctionBaseFromStdFunction(const std::function<void(Args...)> &fun) : fun(fun) {}
56 
57  SEXP operator()(SEXP* args) {
59  call<void, Args...>(fun, args);
60  END_RCPP
61  }
62 
63  private:
64  const std::function<void(Args...)> fun;
65  };
66 
67  } // namespace InternalFunctionWithStdFunction
68 } // namespace Rcpp
69 
70 #endif
#define END_RCPP
Definition: macros.h:99
#define BEGIN_RCPP
Definition: macros.h:49
Rcpp API.
Definition: algo.h:28