-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathInputParameter.h
More file actions
90 lines (74 loc) · 2.56 KB
/
InputParameter.h
File metadata and controls
90 lines (74 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#ifndef Rcpp__InputParameter__h
#define Rcpp__InputParameter__h
namespace Rcpp {
// default implementation used for pass by value
// as<> is called on the conversion operator
template <typename T>
class InputParameter {
public:
typedef T type ;
InputParameter(SEXP x_) : x(x_){}
InputParameter() : x(R_NilValue){}
void set(SEXP x_){ x = x_ ; }
inline operator T() { return as<T>(x) ; }
inline T get() { return as<T>(x) ; }
SEXP x ;
} ;
// same for const
template <typename T>
class ConstInputParameter {
public:
typedef const T const_nonref ;
ConstInputParameter(SEXP x_) : x(x_) {}
inline void set(SEXP x_){ x = x_; }
inline operator const_nonref() { return get() ; }
inline const_nonref get() { return as<T>(x) ; }
SEXP x ;
} ;
// impl for references. It holds an object at the constructor and then
// returns a reference in the reference operator
template <typename T>
class ReferenceInputParameter {
public:
typedef T& reference ;
ReferenceInputParameter() : x(R_NilValue), obj(){}
ReferenceInputParameter(SEXP x_) : x(x_), obj( as<T>(x_) ) {}
void set(SEXP x_){ x = x_ ; obj = as<T>(x); }
inline operator reference() { return get() ; }
inline reference get(){ return obj; }
SEXP x ;
T obj ;
} ;
// same for const references
template <typename T>
class ConstReferenceInputParameter {
public:
typedef const T& const_reference ;
ConstReferenceInputParameter(SEXP x_) : x(x_), obj( as<T>(x_) ){}
ConstReferenceInputParameter(): x(R_NilValue),obj(){}
inline operator const_reference() { return obj ; }
void set(SEXP x_){ x = x_ ; obj = as<T>(x); }
private:
SEXP x ;
T obj ;
} ;
namespace traits{
template <typename T>
struct input_parameter {
typedef typename Rcpp::InputParameter<T> type ;
} ;
template <typename T>
struct input_parameter<T&> {
typedef typename Rcpp::ReferenceInputParameter<T> type ;
} ;
template <typename T>
struct input_parameter<const T> {
typedef typename Rcpp::ConstInputParameter<T> type ;
} ;
template <typename T>
struct input_parameter<const T&> {
typedef typename Rcpp::ConstReferenceInputParameter<T> type ;
} ;
}
}
#endif