Skip to content

Commit 8721c9e

Browse files
committed
Merge pull request #162 from cauthmann/std_function
Support std::function in InternalFunction
2 parents 3d9e34e + e40bb7d commit 8721c9e

File tree

5 files changed

+2676
-10
lines changed

5 files changed

+2676
-10
lines changed

inst/include/Rcpp/InternalFunction.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,29 @@
2626

2727
#include <Rcpp/grow.h>
2828

29+
#ifdef RCPP_USE_STD_FUNCTION
30+
#include <Rcpp/InternalFunctionWithStdFunction.h>
31+
#endif
32+
2933
namespace Rcpp{
3034

3135
RCPP_API_CLASS(InternalFunction_Impl) {
3236
public:
3337

3438
RCPP_GENERATE_CTOR_ASSIGN(InternalFunction_Impl)
3539

40+
#ifdef RCPP_USE_STD_FUNCTION
41+
template <typename RESULT_TYPE, typename... Args>
42+
InternalFunction_Impl(const std::function<RESULT_TYPE(Args...)> &fun) {
43+
set(
44+
XPtr<Rcpp::InternalFunctionWithStdFunction::CppFunctionBaseFromStdFunction<RESULT_TYPE, Args...> >(
45+
new Rcpp::InternalFunctionWithStdFunction::CppFunctionBaseFromStdFunction<RESULT_TYPE, Args...>(fun),
46+
false
47+
)
48+
);
49+
}
50+
#endif
51+
3652
#include <Rcpp/generated/InternalFunction__ctors.h>
3753
void update(SEXP){}
3854
private:
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
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+
//
7+
// This file is part of Rcpp.
8+
//
9+
// Rcpp is free software: you can redistribute it and/or modify it
10+
// under the terms of the GNU General Public License as published by
11+
// the Free Software Foundation, either version 2 of the License, or
12+
// (at your option) any later version.
13+
//
14+
// Rcpp is distributed in the hope that it will be useful, but
15+
// WITHOUT ANY WARRANTY; without even the implied warranty of
16+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
// GNU General Public License for more details.
18+
//
19+
// You should have received a copy of the GNU General Public License
20+
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21+
22+
#ifndef Rcpp_InternalFunctionWithStdFunction_h
23+
#define Rcpp_InternalFunctionWithStdFunction_h
24+
25+
#include <functional>
26+
27+
namespace Rcpp{
28+
29+
namespace InternalFunctionWithStdFunction {
30+
31+
#include <Rcpp/generated/InternalFunctionWithStdFunction_call.h>
32+
33+
template <typename RESULT_TYPE, typename... Args>
34+
class CppFunctionBaseFromStdFunction : public CppFunctionBase {
35+
public:
36+
CppFunctionBaseFromStdFunction(const std::function<RESULT_TYPE(Args...)> &fun) : fun(fun) {}
37+
virtual ~CppFunctionBaseFromStdFunction() {}
38+
39+
SEXP operator()(SEXP* args) {
40+
BEGIN_RCPP
41+
auto result = call<RESULT_TYPE, Args...>(fun, args);
42+
return Rcpp::module_wrap<RESULT_TYPE>(result);
43+
END_RCPP
44+
}
45+
46+
private:
47+
const std::function<RESULT_TYPE(Args...)> fun;
48+
};
49+
50+
template <typename... Args>
51+
class CppFunctionBaseFromStdFunction<void, Args...> : public CppFunctionBase {
52+
public:
53+
CppFunctionBaseFromStdFunction(const std::function<void(Args...)> &fun) : fun(fun) {}
54+
virtual ~CppFunctionBaseFromStdFunction() {}
55+
56+
SEXP operator()(SEXP* args) {
57+
BEGIN_RCPP
58+
call<void, Args...>(fun, args);
59+
END_RCPP
60+
}
61+
62+
private:
63+
const std::function<void(Args...)> fun;
64+
};
65+
66+
} // namespace InternalFunctionWithStdFunction
67+
} // namespace Rcpp
68+
69+
#endif

0 commit comments

Comments
 (0)