3
3
//
4
4
// Module.cpp: Rcpp R/C++ interface class library -- module unit tests
5
5
//
6
- // Copyright (C) 2013 - 2014 Dirk Eddelbuettel and Romain Francois
6
+ // Copyright (C) 2013 - 2015 Dirk Eddelbuettel and Romain Francois
7
7
//
8
8
// This file is part of Rcpp.
9
9
//
22
22
23
23
#include < Rcpp.h>
24
24
25
- using namespace Rcpp ;
25
+ using namespace Rcpp ;
26
26
27
27
std::string hello () {
28
- return " hello" ;
28
+ return " hello" ;
29
29
}
30
30
31
31
int bar (int x) {
32
- return x*2 ;
32
+ return x*2 ;
33
33
}
34
34
35
35
double foo (int x, double y) {
36
- return x * y ;
36
+ return x * y;
37
37
}
38
38
39
39
void bla () {
40
- Rprintf (" hello\\ n" ) ;
40
+ Rprintf (" hello\\ n" );
41
41
}
42
42
43
43
void bla1 (int x) {
44
- Rprintf (" hello (x = %d)\\ n" , x) ;
44
+ Rprintf (" hello (x = %d)\\ n" , x);
45
45
}
46
46
47
47
void bla2 (int x, double y) {
48
- Rprintf (" hello (x = %d, y = %5.2f)\\ n" , x, y) ;
48
+ Rprintf (" hello (x = %d, y = %5.2f)\\ n" , x, y);
49
49
}
50
50
51
51
int test_reference (std::vector<double >& ref) {
52
- return ref.size () ;
52
+ return ref.size ();
53
53
}
54
54
int test_const_reference (const std::vector<double >& ref) {
55
- return ref.size () ;
55
+ return ref.size ();
56
56
}
57
57
int test_const (const std::vector<double > ref) {
58
- return ref.size () ;
58
+ return ref.size ();
59
59
}
60
60
61
- class World {
61
+ class ModuleWorld {
62
62
public:
63
- World () : msg(" hello" ) {}
63
+ ModuleWorld () : msg(" hello" ) {}
64
64
void set (std::string msg_) { this ->msg = msg_; }
65
65
void set_ref (std::string& msg_) { this ->msg = msg_; }
66
66
void set_const_ref (const std::string& msg_) { this ->msg = msg_; }
@@ -70,73 +70,73 @@ class World {
70
70
std::string msg;
71
71
};
72
72
73
- void clearWorld (World * w) {
73
+ void clearWorld (ModuleWorld * w) {
74
74
w->set (" " );
75
75
}
76
76
77
- class Num {
77
+ class ModuleNum {
78
78
public:
79
- Num () : x(0.0 ), y(0 ) {} ;
79
+ ModuleNum () : x(0.0 ), y(0 ) {};
80
80
81
- double getX () const { return x ; }
82
- void setX (double value) { x = value ; }
81
+ double getX () const { return x; }
82
+ void setX (double value) { x = value; }
83
83
84
- int getY () { return y ; }
84
+ int getY () { return y; }
85
85
86
86
private:
87
- double x ;
88
- int y ;
87
+ double x;
88
+ int y;
89
89
};
90
90
91
- class Number {
91
+ class ModuleNumber {
92
92
public:
93
- Number () : x(0.0 ), y(0 ) {} ;
93
+ ModuleNumber () : x(0.0 ), y(0 ) {};
94
94
95
- double x ;
96
- int y ;
95
+ double x;
96
+ int y;
97
97
};
98
98
99
- class Randomizer {
99
+ class ModuleRandomizer {
100
100
public:
101
101
102
102
// Randomizer() : min(0), max(1) {}
103
- Randomizer (double min_, double max_) : min(min_), max(max_) {}
103
+ ModuleRandomizer (double min_, double max_) : min(min_), max(max_) {}
104
104
105
105
NumericVector get (int n) {
106
- RNGScope scope ;
106
+ RNGScope scope;
107
107
return runif (n, min, max);
108
108
}
109
109
110
110
private:
111
- double min, max ;
112
- } ;
111
+ double min, max;
112
+ };
113
113
114
- RCPP_EXPOSED_CLASS (Test )
115
- class Test {
114
+ RCPP_EXPOSED_CLASS (ModuleTest )
115
+ class ModuleTest {
116
116
public:
117
- double value ;
118
- Test (double v) : value (v) {}
117
+ double value;
118
+ ModuleTest (double v) : value (v) {}
119
119
private:
120
120
// hiding those on purpose
121
121
// we work by reference or pointers here. Not by copy.
122
- Test (const Test & other) ;
123
- Test & operator =(const Test&) ;
124
- } ;
122
+ ModuleTest (const ModuleTest & other);
123
+ ModuleTest & operator =(const ModuleTest&) ;
124
+ };
125
125
126
- double Test_get_x_const_ref (const Test & x) {
127
- return x.value ;
126
+ double Test_get_x_const_ref (const ModuleTest & x) {
127
+ return x.value ;
128
128
}
129
- double Test_get_x_ref (Test & x) {
129
+ double Test_get_x_ref (ModuleTest & x) {
130
130
return x.value ;
131
131
}
132
- double Test_get_x_const_pointer (const Test * x) {
133
- return x->value ;
132
+ double Test_get_x_const_pointer (const ModuleTest * x) {
133
+ return x->value ;
134
134
}
135
- double Test_get_x_pointer (Test * x) {
136
- return x->value ;
135
+ double Test_get_x_pointer (ModuleTest * x) {
136
+ return x->value ;
137
137
}
138
138
139
- RCPP_MODULE (yada ) {
139
+ RCPP_MODULE (demoModule ) {
140
140
function (" hello" , &hello);
141
141
function (" bar" , &bar );
142
142
function (" foo" , &foo );
@@ -145,43 +145,41 @@ RCPP_MODULE(yada) {
145
145
function (" bla2" , &bla2 );
146
146
147
147
function (" test_reference" , test_reference);
148
- function (" test_const_reference" , test_const_reference) ;
149
- function (" test_const" , test_const) ;
148
+ function (" test_const_reference" , test_const_reference);
149
+ function (" test_const" , test_const);
150
150
151
- class_<Test >(" Test " )
151
+ class_<ModuleTest >(" ModuleTest " )
152
152
.constructor <double >()
153
153
;
154
154
155
- class_<World>(" World" )
156
-
155
+ class_<ModuleWorld>(" ModuleWorld" )
157
156
.constructor ()
158
-
159
- .method (" greet" , &World::greet)
160
- .method (" set" , &World::set)
161
- .method (" set_ref" , &World::set_ref)
162
- .method (" set_const_ref" , &World::set_const_ref)
157
+ .method (" greet" , &ModuleWorld::greet)
158
+ .method (" set" , &ModuleWorld::set)
159
+ .method (" set_ref" , &ModuleWorld::set_ref)
160
+ .method (" set_const_ref" , &ModuleWorld::set_const_ref)
163
161
.method (" clear" , &clearWorld)
164
- ;
162
+ ;
165
163
166
- class_<Num >(" Num " )
164
+ class_<ModuleNum >(" ModuleNum " )
167
165
.constructor ()
168
166
169
167
// read and write property
170
- .property (" x" , &Num ::getX, &Num ::setX)
168
+ .property (" x" , &ModuleNum ::getX, &ModuleNum ::setX)
171
169
172
170
// read-only property
173
- .property (" y" , &Num ::getY)
171
+ .property (" y" , &ModuleNum ::getY)
174
172
;
175
173
176
- class_<Number >(" Number " )
174
+ class_<ModuleNumber >(" ModuleNumber " )
177
175
178
176
.constructor ()
179
177
180
178
// read and write data member
181
- .field (" x" , &Number ::x)
179
+ .field (" x" , &ModuleNumber ::x)
182
180
183
181
// read only data member
184
- .field_readonly (" y" , &Number ::y)
182
+ .field_readonly (" y" , &ModuleNumber ::y)
185
183
;
186
184
187
185
function (" Test_get_x_const_ref" , Test_get_x_const_ref);
@@ -190,32 +188,31 @@ RCPP_MODULE(yada) {
190
188
function (" Test_get_x_pointer" , Test_get_x_pointer);
191
189
192
190
193
- class_<Randomizer >(" Randomizer " )
191
+ class_<ModuleRandomizer >(" ModuleRandomizer " )
194
192
// No default: .default_constructor()
195
193
.constructor <double ,double >()
196
194
197
- .method (" get" , &Randomizer ::get)
195
+ .method (" get" , &ModuleRandomizer ::get)
198
196
;
199
197
}
200
198
201
199
// [[Rcpp::export]]
202
- double attr_Test_get_x_const_ref (const Test & x) {
203
- return x.value ;
200
+ double attr_Test_get_x_const_ref (const ModuleTest & x) {
201
+ return x.value ;
204
202
}
205
203
206
204
// [[Rcpp::export]]
207
- double attr_Test_get_x_ref (Test & x) {
205
+ double attr_Test_get_x_ref (ModuleTest & x) {
208
206
return x.value ;
209
207
}
210
208
211
209
// [[Rcpp::export]]
212
- double attr_Test_get_x_const_pointer (const Test * x) {
213
- return x->value ;
210
+ double attr_Test_get_x_const_pointer (const ModuleTest * x) {
211
+ return x->value ;
214
212
}
215
213
216
214
// [[Rcpp::export]]
217
- double attr_Test_get_x_pointer (Test * x) {
218
- return x->value ;
215
+ double attr_Test_get_x_pointer (ModuleTest * x) {
216
+ return x->value ;
219
217
}
220
218
221
-
0 commit comments