Skip to content

Commit 751c025

Browse files
introduce traits::input_parameter so that we can have std::vector<T>& again
1 parent 3979394 commit 751c025

File tree

4 files changed

+73
-4
lines changed

4 files changed

+73
-4
lines changed

ChangeLog

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
2013-09-15 Romain Francois <romain@r-enthusiasts.com>
2+
3+
* include/Rcpp/InputParameter.h : added the traits::input_parameter trait
4+
to add another layer of abstration.
5+
* include/Rcpp/macros/module.h : taking advantage of input_parameter to
6+
specialize how to work with module objects
7+
* src/attributes.cpp : using traits::input_parameter<T> instead of
8+
InputParameter<T>
9+
110
2013-09-14 Dirk Eddelbuettel <edd@debian.org>
211

12+
* src/attributes.cpp : Precede closing '>' by space to avoid '>>'
313
* inst/include/Rcpp/platform/compiler.h: Further refine #if test for
414
'long long' by conditioning __LP64__ on also using clang/llvm
515

6-
* src/attributes.cpp (Rcpp): Precede closing '>' by space to avoid '>>'
16+
* src/attributes.cpp : Precede closing '>' by space to avoid '>>'
717

818
2013-09-13 Romain Francois <romain@r-enthusiasts.com>
919

inst/include/Rcpp/InputParameter.h

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
#ifndef Rcpp__InputParameter__h
2323
#define Rcpp__InputParameter__h
2424

25-
namespace Rcpp {
25+
namespace Rcpp {
2626

27+
// default implementation used for pass by value and modules objects
28+
// as<> is called on the conversion operator
2729
template <typename T>
2830
class InputParameter {
2931
public:
@@ -35,6 +37,48 @@ namespace Rcpp {
3537
SEXP x ;
3638
} ;
3739

40+
// impl for references. It holds an object at the constructor and then
41+
// returns a reference in the reference operator
42+
template <typename T>
43+
class ReferenceInputParameter {
44+
public:
45+
typedef T& reference ;
46+
ReferenceInputParameter(SEXP x_) : obj( as<T>(x_) ){}
47+
48+
inline operator reference() { return obj ; }
49+
50+
private:
51+
T obj ;
52+
} ;
53+
54+
// same for const references
55+
template <typename T>
56+
class ConstReferenceInputParameter {
57+
public:
58+
typedef const T& const_reference ;
59+
ConstReferenceInputParameter(SEXP x_) : obj( as<T>(x_) ){}
60+
61+
inline operator const_reference() { return obj ; }
62+
63+
private:
64+
T obj ;
65+
} ;
66+
67+
namespace traits{
68+
template <typename T>
69+
struct input_parameter {
70+
typedef typename Rcpp::InputParameter<T> type ;
71+
} ;
72+
template <typename T>
73+
struct input_parameter<T&> {
74+
typedef typename Rcpp::ReferenceInputParameter<T> type ;
75+
} ;
76+
template <typename T>
77+
struct input_parameter<const T&> {
78+
typedef typename Rcpp::ConstReferenceInputParameter<T> type ;
79+
} ;
80+
}
81+
3882
}
3983

4084
#endif

inst/include/Rcpp/macros/module.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@
4444
template<> struct r_type_traits< const CLASS& >{ \
4545
typedef r_type_module_object_const_reference_tag r_category ; \
4646
} ; \
47+
template<> struct input_parameter< CLASS* >{ \
48+
typedef Rcpp::InputParameter<CLASS*> type ; \
49+
} ; \
50+
template<> struct input_parameter< const CLASS* >{ \
51+
typedef Rcpp::InputParameter<const CLASS*> type ; \
52+
} ; \
53+
template<> struct input_parameter< CLASS >{ \
54+
typedef Rcpp::InputParameter<CLASS> type ; \
55+
} ; \
56+
template<> struct input_parameter< CLASS& >{ \
57+
typedef Rcpp::InputParameter<CLASS&> type ; \
58+
} ; \
59+
template<> struct input_parameter< const CLASS& >{ \
60+
typedef Rcpp::InputParameter<const CLASS&> type ; \
61+
} ; \
4762
}}
4863

4964
#define RCPP_EXPOSED_WRAP(CLASS) namespace Rcpp{ namespace traits{ template<> struct wrap_type_traits< CLASS >{typedef wrap_type_module_object_tag wrap_category ; } ; }}

src/attributes.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,8 +2167,8 @@ namespace attributes {
21672167
for (size_t i = 0; i<arguments.size(); i++) {
21682168
const Argument& argument = arguments[i];
21692169

2170-
ostr << " Rcpp::InputParameter< "
2171-
<< argument.type().full_name() << " > " << argument.name()
2170+
ostr << " Rcpp::traits::input_parameter< "
2171+
<< argument.type().full_name() << " >::type " << argument.name()
21722172
<< "(" << argument.name() << "SEXP );" << std::endl;
21732173
}
21742174

0 commit comments

Comments
 (0)