Skip to content

Commit 344dab3

Browse files
let the compiler generate the code rather than previous code bloat
1 parent 4de17a2 commit 344dab3

10 files changed

Lines changed: 134 additions & 5271 deletions

File tree

inst/include/Rcpp/Module.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ namespace Rcpp{
8383
#include <Rcpp/module/get_return_type.h>
8484
#include <Rcpp/module/get_signature.h>
8585

86-
#include <Rcpp/module/generated_function_invoke.h>
87-
#include <Rcpp/module/generated_void_function_invoke.h>
86+
#include <Rcpp/module/FunctionInvoker.h>
87+
8888
#include <Rcpp/module/CppFunction.h>
8989
#include <Rcpp/module/class_Base.h>
9090
#include <Rcpp/module/Module.h>

inst/include/Rcpp/module/CppFunction.h

Lines changed: 14 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -75,79 +75,43 @@
7575
template <typename OUT, typename... Args>
7676
class CppFunction_Impl : public CppFunction {
7777
public:
78-
CppFunction_Impl(OUT (*fun)(Args...), const char* docstring = 0 ) :
78+
typedef OUT (*Fun)(Args...) ;
79+
80+
CppFunction_Impl(Fun fun, const char* docstring = 0 ) :
7981
CppFunction(docstring), ptr_fun(fun){}
8082

81-
SEXP operator()(SEXP* args) {
82-
return function_invoke<OUT,Args...>( typename traits::number_to_type<sizeof...(Args)>(), ptr_fun, args ) ;
83+
inline SEXP operator()(SEXP* args) {
84+
return FunctionInvoker<OUT,Args...>( ptr_fun, args ).invoke() ;
8385
}
8486

8587
inline int nargs(){ return sizeof...(Args); }
8688
inline void signature(std::string& s, const char* name){ Rcpp::signature<OUT, Args...>(s, name) ; }
8789
inline DL_FUNC get_function_ptr(){ return (DL_FUNC)ptr_fun ; }
8890

8991
private:
90-
OUT (*ptr_fun)(Args...) ;
92+
Fun ptr_fun ;
9193
} ;
9294

93-
template <typename... Args>
94-
class CppFunction_Impl<void,Args...> : public CppFunction {
95-
public:
96-
CppFunction_Impl(void (*fun)(Args...), const char* docstring = 0 ) :
97-
CppFunction(docstring), ptr_fun(fun){}
98-
99-
SEXP operator()(SEXP* args) {
100-
void_function_invoke( typename traits::number_to_type<sizeof...(Args)>(), ptr_fun, args ) ;
101-
return R_NilValue ;
102-
}
103-
104-
inline int nargs(){ return sizeof...(Args); }
105-
inline void signature(std::string& s, const char* name){ Rcpp::signature<void, Args...>(s, name) ; }
106-
inline DL_FUNC get_function_ptr(){ return (DL_FUNC)ptr_fun ; }
107-
inline bool is_void(){ return true; }
108-
109-
private:
110-
void (*ptr_fun)(Args...) ;
111-
} ;
112-
11395
template <typename OUT, typename... Args>
11496
class CppFunction_WithFormals_Impl : public CppFunction {
11597
public:
116-
CppFunction_WithFormals_Impl(OUT (*fun)(Args...), Rcpp::List formals_, const char* docstring = 0 ) : CppFunction(docstring), formals(formals_), ptr_fun(fun){}
117-
SEXP operator()(SEXP* args) {
118-
return function_invoke<OUT, Args...>( typename traits::number_to_type<sizeof...(Args)>(), ptr_fun, args) ;
119-
}
120-
121-
inline int nargs(){ return sizeof...(Args) ; }
122-
inline void signature(std::string& s, const char* name){ Rcpp::signature<OUT,Args...>(s, name) ; }
123-
inline DL_FUNC get_function_ptr(){ return (DL_FUNC)ptr_fun ; }
124-
inline SEXP get_formals(){ return formals; }
98+
typedef OUT (*Fun)(Args...) ;
12599

126-
private:
127-
Rcpp::List formals ;
128-
OUT (*ptr_fun)(void) ;
129-
} ;
130-
131-
template <typename... Args>
132-
class CppFunction_WithFormals_Impl<void,Args...> : public CppFunction {
133-
public:
134-
CppFunction_WithFormals_Impl(void (*fun)(Args...), Rcpp::List formals_, const char* docstring = 0 ) : CppFunction(docstring), formals(formals_), ptr_fun(fun){}
135-
SEXP operator()(SEXP* args) {
136-
void_function_invoke( typename traits::number_to_type<sizeof...(Args)>(), ptr_fun, args) ;
137-
return R_NilValue ;
100+
CppFunction_WithFormals_Impl(Fun fun, Rcpp::List formals_, const char* docstring = 0 ) :
101+
CppFunction(docstring), formals(formals_), ptr_fun(fun){}
102+
103+
inline SEXP operator()(SEXP* args) {
104+
return FunctionInvoker<OUT,Args...>( ptr_fun, args ).invoke() ;
138105
}
139106

140107
inline int nargs(){ return sizeof...(Args) ; }
141-
inline void signature(std::string& s, const char* name){ Rcpp::signature<void,Args...>(s, name) ; }
108+
inline void signature(std::string& s, const char* name){ Rcpp::signature<OUT,Args...>(s, name) ; }
142109
inline DL_FUNC get_function_ptr(){ return (DL_FUNC)ptr_fun ; }
143110
inline SEXP get_formals(){ return formals; }
144-
inline bool is_void(){ return true; }
145111

146112
private:
147113
Rcpp::List formals ;
148-
void (*ptr_fun)(Args...) ;
114+
Fun ptr_fun ;
149115
} ;
150-
151-
152116

153117
#endif
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright (C) 2013 Romain Francois
2+
//
3+
// This file is part of Rcpp11.
4+
//
5+
// Rcpp11 is free software: you can redistribute it and/or modify it
6+
// under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 2 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// Rcpp11 is distributed in the hope that it will be useful, but
11+
// WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with Rcpp11. If not, see <http://www.gnu.org/licenses/>.
17+
18+
#ifndef Rcpp_Module_FunctionInvoker_h
19+
#define Rcpp_Module_FunctionInvoker_h
20+
21+
template <typename OUT, typename... Args>
22+
class FunctionInvoker {
23+
public:
24+
typedef OUT (*Fun)(Args...) ;
25+
typedef typename std::tuple< typename traits::input_parameter<Args>::type ...> Tuple ;
26+
27+
FunctionInvoker( Fun fun_, SEXP* args_ ) : fun(fun_), args(args_){}
28+
29+
inline SEXP invoke( ){
30+
return invoke_dispatch( typename traits::index_sequence<Args...>::type() ) ;
31+
}
32+
33+
template <int... S>
34+
inline SEXP invoke_dispatch( traits::sequence<S...> ){
35+
return wrap( fun( get<S>() ... ) ) ;
36+
}
37+
38+
template <int i>
39+
typename std::tuple_element<i, Tuple>::type get(){
40+
return typename std::tuple_element<i, Tuple>::type( args[i] ) ;
41+
}
42+
43+
Fun fun ;
44+
SEXP* args ;
45+
} ;
46+
47+
template <typename... Args>
48+
class FunctionInvoker<void,Args...> {
49+
public:
50+
typedef void (*Fun)(Args...) ;
51+
typedef typename std::tuple< typename traits::input_parameter<Args>::type ...> Tuple ;
52+
53+
FunctionInvoker( Fun fun_, SEXP* args_ ) : fun(fun_), args(args_){}
54+
55+
inline SEXP invoke( ){
56+
invoke_dispatch( typename traits::index_sequence<Args...>::type() ) ;
57+
return R_NilValue ;
58+
}
59+
60+
template <int... S>
61+
inline void invoke_dispatch( traits::sequence<S...> ){
62+
fun( get<S>() ... ) ;
63+
}
64+
65+
template <int i>
66+
typename std::tuple_element<i, Tuple>::type get(){
67+
return typename std::tuple_element<i, Tuple>::type( args[i] ) ;
68+
}
69+
70+
Fun fun ;
71+
SEXP* args ;
72+
} ;
73+
74+
75+
#endif

inst/include/Rcpp/module/Module.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
#include <Rcpp/module/debug_method.h>
2626
#include <Rcpp/module/debug_constructor.h>
27-
27+
2828
/**
2929
* holds information about exposed functions and classes
3030
*/

0 commit comments

Comments
 (0)