2
2
//
3
3
// as.h: Rcpp R/C++ interface class library -- convert SEXP to C++ objects
4
4
//
5
- // Copyright (C) 2009 - 2013 Dirk Eddelbuettel and Romain Francois
5
+ // Copyright (C) 2009 - 2015 Dirk Eddelbuettel and Romain Francois
6
6
//
7
7
// This file is part of Rcpp.
8
8
//
24
24
25
25
#include < Rcpp/internal/Exporter.h>
26
26
27
- namespace Rcpp {
27
+ namespace Rcpp {
28
28
29
- namespace internal {
29
+ namespace internal {
30
30
31
- template <typename T> T primitive_as ( SEXP x ) {
32
- if ( ::Rf_length (x) != 1 ) throw ::Rcpp::not_compatible ( " expecting a single value" ) ;
33
- const int RTYPE = ::Rcpp::traits::r_sexptype_traits<T>::rtype ;
34
- Shield<SEXP> y ( r_cast<RTYPE>(x) );
31
+ template <typename T> T primitive_as (SEXP x) {
32
+ if ( ::Rf_length (x) != 1 ) throw ::Rcpp::not_compatible (" expecting a single value" ) ;
33
+ const int RTYPE = ::Rcpp::traits::r_sexptype_traits<T>::rtype;
34
+ Shield<SEXP> y (r_cast<RTYPE>(x));
35
35
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type STORAGE;
36
- T res = caster<STORAGE,T>( *r_vector_start<RTYPE>( y ) ) ;
37
- return res ;
36
+ T res = caster<STORAGE,T>(*r_vector_start<RTYPE>(y)) ;
37
+ return res;
38
38
}
39
39
40
- template <typename T> T as ( SEXP x, ::Rcpp::traits::r_type_primitive_tag ) {
41
- return primitive_as<T>(x) ;
40
+ template <typename T> T as (SEXP x, ::Rcpp::traits::r_type_primitive_tag) {
41
+ return primitive_as<T>(x);
42
42
}
43
43
44
- inline const char * check_single_string ( SEXP x){
45
- if ( TYPEOF (x) == CHARSXP ) return CHAR ( x ) ;
46
- if ( ! ::Rf_isString (x) )
47
- throw ::Rcpp::not_compatible ( " expecting a string" ) ;
44
+ inline const char * check_single_string (SEXP x) {
45
+ if ( TYPEOF (x) == CHARSXP) return CHAR (x) ;
46
+ if ( ! ::Rf_isString (x))
47
+ throw ::Rcpp::not_compatible (" expecting a string" ) ;
48
48
if (Rf_length (x) != 1 )
49
- throw ::Rcpp::not_compatible ( " expecting a single value" );
50
- return CHAR ( STRING_ELT ( ::Rcpp::r_cast<STRSXP>(x) , 0 ) ) ;
49
+ throw ::Rcpp::not_compatible (" expecting a single value" );
50
+ return CHAR (STRING_ELT (::Rcpp::r_cast<STRSXP>(x), 0 )) ;
51
51
}
52
52
53
53
54
- template <typename T> T as_string ( SEXP x, Rcpp::traits::true_type){
55
- const char * y = check_single_string (x) ;
56
- return std::wstring ( y, y+strlen (y)) ;
54
+ template <typename T> T as_string (SEXP x, Rcpp::traits::true_type) {
55
+ const char * y = check_single_string (x);
56
+ return std::wstring (y, y+strlen (y));
57
57
}
58
58
59
- template <typename T> T as_string ( SEXP x, Rcpp::traits::false_type){
60
- return check_single_string (x) ;
59
+ template <typename T> T as_string (SEXP x, Rcpp::traits::false_type) {
60
+ return check_single_string (x);
61
61
}
62
62
63
- template <typename T> T as (SEXP x, ::Rcpp::traits::r_type_string_tag ) {
64
- return as_string<T>( x, typename Rcpp::traits::is_wide_string<T>::type () );
63
+ template <typename T> T as (SEXP x, ::Rcpp::traits::r_type_string_tag) {
64
+ return as_string<T>(x, typename Rcpp::traits::is_wide_string<T>::type ());
65
65
}
66
66
67
- template <typename T> T as (SEXP x, ::Rcpp::traits::r_type_RcppString_tag ) {
68
- if ( ! ::Rf_isString (x) ) {
69
- throw ::Rcpp::not_compatible ( " expecting a string" ) ;
67
+ template <typename T> T as (SEXP x, ::Rcpp::traits::r_type_RcppString_tag) {
68
+ if ( ! ::Rf_isString (x)) {
69
+ throw ::Rcpp::not_compatible (" expecting a string" ) ;
70
70
}
71
71
if (Rf_length (x) != 1 ) {
72
- throw ::Rcpp::not_compatible ( " expecting a single value" );
72
+ throw ::Rcpp::not_compatible (" expecting a single value" );
73
73
}
74
- return STRING_ELT ( ::Rcpp::r_cast<STRSXP>(x) , 0 ) ;
74
+ return STRING_ELT (::Rcpp::r_cast<STRSXP>(x), 0 ) ;
75
75
}
76
76
77
- template <typename T> T as (SEXP x, ::Rcpp::traits::r_type_generic_tag ) {
78
- RCPP_DEBUG_1 ( " as(SEXP = <%p>, r_type_generic_tag )" , x ) ;
77
+ template <typename T> T as (SEXP x, ::Rcpp::traits::r_type_generic_tag) {
78
+ RCPP_DEBUG_1 (" as(SEXP = <%p>, r_type_generic_tag )" , x) ;
79
79
::Rcpp::traits::Exporter<T> exporter (x);
80
- RCPP_DEBUG_1 ( " exporter type = %s" , DEMANGLE (exporter) ) ;
81
- return exporter.get () ;
80
+ RCPP_DEBUG_1 (" exporter type = %s" , DEMANGLE (exporter)) ;
81
+ return exporter.get ();
82
82
}
83
83
84
- void * as_module_object_internal (SEXP obj) ;
84
+ void * as_module_object_internal (SEXP obj);
85
85
86
- template <typename T> object<T> as_module_object (SEXP x){
87
- return (T*) as_module_object_internal (x) ;
86
+ template <typename T> object<T> as_module_object (SEXP x) {
87
+ return (T*) as_module_object_internal (x);
88
88
}
89
89
90
90
/* * handling object<T> */
91
- template <typename T> T as (SEXP x, ::Rcpp::traits::r_type_module_object_const_pointer_tag ) {
92
- typedef typename Rcpp::traits::remove_const<T>::type T_NON_CONST ;
93
- return const_cast <T>( (T_NON_CONST)as_module_object_internal (x) ) ;
91
+ template <typename T> T as (SEXP x, ::Rcpp::traits::r_type_module_object_const_pointer_tag) {
92
+ typedef typename Rcpp::traits::remove_const<T>::type T_NON_CONST;
93
+ return const_cast <T>((T_NON_CONST)as_module_object_internal (x)) ;
94
94
}
95
95
96
- template <typename T> T as (SEXP x, ::Rcpp::traits::r_type_module_object_pointer_tag ) {
97
- return as_module_object<typename traits::un_pointer<T>::type>( x ) ;
96
+ template <typename T> T as (SEXP x, ::Rcpp::traits::r_type_module_object_pointer_tag) {
97
+ return as_module_object<typename traits::un_pointer<T>::type>(x) ;
98
98
}
99
99
100
100
/* * handling T such that T is exposed by a module */
101
- template <typename T> T as (SEXP x, ::Rcpp::traits::r_type_module_object_tag ) {
102
- T* obj = as_module_object<T>(x) ;
103
- return *obj ;
101
+ template <typename T> T as (SEXP x, ::Rcpp::traits::r_type_module_object_tag) {
102
+ T* obj = as_module_object<T>(x);
103
+ return *obj;
104
104
}
105
105
106
106
/* * handling T such that T is a reference of a class handled by a module */
107
- template <typename T> T as (SEXP x, ::Rcpp::traits::r_type_module_object_reference_tag ) {
108
- typedef typename traits::remove_reference<T>::type KLASS ;
109
- KLASS* obj = as_module_object<KLASS>(x) ;
110
- return *obj ;
107
+ template <typename T> T as (SEXP x, ::Rcpp::traits::r_type_module_object_reference_tag) {
108
+ typedef typename traits::remove_reference<T>::type KLASS;
109
+ KLASS* obj = as_module_object<KLASS>(x);
110
+ return *obj;
111
111
}
112
112
113
113
/* * handling T such that T is a reference of a class handled by a module */
114
- template <typename T> T as (SEXP x, ::Rcpp::traits::r_type_module_object_const_reference_tag ) {
115
- typedef typename traits::remove_const_and_reference<T>::type KLASS ;
116
- KLASS* obj = as_module_object<KLASS>(x) ;
117
- return const_cast <T>( *obj ) ;
114
+ template <typename T> T as (SEXP x, ::Rcpp::traits::r_type_module_object_const_reference_tag) {
115
+ typedef typename traits::remove_const_and_reference<T>::type KLASS;
116
+ KLASS* obj = as_module_object<KLASS>(x);
117
+ return const_cast <T>(*obj) ;
118
118
}
119
119
120
120
/* * handling enums by converting to int first */
121
- template <typename T> T as (SEXP x, ::Rcpp::traits::r_type_enum_tag ) {
122
- return T ( primitive_as<int >(x) ) ;
121
+ template <typename T> T as (SEXP x, ::Rcpp::traits::r_type_enum_tag) {
122
+ return T (primitive_as<int >(x)) ;
123
123
}
124
124
125
125
}
@@ -140,20 +140,20 @@ namespace Rcpp{
140
140
* Environment x = ... ; // some environment
141
141
* Foo y = x["bla"] ; // if as<Foo> makes sense then this works !!
142
142
*/
143
- template <typename T> T as ( SEXP x) {
144
- return internal::as<T>( x, typename traits::r_type_traits<T>::r_category () ) ;
143
+ template <typename T> T as (SEXP x) {
144
+ return internal::as<T>(x, typename traits::r_type_traits<T>::r_category ()) ;
145
145
}
146
146
147
- template <> inline char as<char >( SEXP x ) {
148
- return internal::check_single_string (x)[0 ] ;
147
+ template <> inline char as<char >(SEXP x) {
148
+ return internal::check_single_string (x)[0 ];
149
149
}
150
150
151
151
template <typename T>
152
- inline typename traits::remove_const_and_reference<T>::type bare_as ( SEXP x ) {
153
- return as< typename traits::remove_const_and_reference<T>::type >( x ) ;
152
+ inline typename traits::remove_const_and_reference<T>::type bare_as (SEXP x) {
153
+ return as< typename traits::remove_const_and_reference<T>::type >(x) ;
154
154
}
155
155
156
- template <> inline SEXP as (SEXP x) { return x ; }
156
+ template <> inline SEXP as (SEXP x) { return x; }
157
157
158
158
} // Rcpp
159
159
0 commit comments