Rcpp Version 0.12.12
Module_Property.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 //
3 // Module_Property.h: Rcpp R/C++ interface class library -- Rcpp modules
4 //
5 // Copyright (C) 2010 - 2017 Dirk Eddelbuettel and Romain Francois
6 //
7 // This file is part of Rcpp.
8 //
9 // Rcpp is free software: you can redistribute it and/or modify it
10 // under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // Rcpp is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21 
22 #ifndef Rcpp_Module_Property_h
23 #define Rcpp_Module_Property_h
24 
25 // getter through a member function
26 template <typename Class, typename PROP>
27 class CppProperty_GetMethod : public CppProperty<Class> {
28 public:
29  typedef PROP (Class::*GetMethod)(void);
30  typedef CppProperty<Class> prop_class;
31 
32  CppProperty_GetMethod(GetMethod getter_, const char* doc = 0) :
33  prop_class(doc), getter(getter_), class_name(DEMANGLE(PROP)){}
34 
35  SEXP get(Class* object) { return Rcpp::wrap((object->*getter)()); }
36  void set(Class*, SEXP) { throw std::range_error("property is read only"); }
37  bool is_readonly(){ return true; }
38  std::string get_class(){ return class_name; }
39 
40 private:
42  std::string class_name;
43 
44 };
45 
46 // getter through a const member function
47 template <typename Class, typename PROP>
48 class CppProperty_GetConstMethod : public CppProperty<Class> {
49 public:
50  typedef PROP (Class::*GetMethod)(void) const;
51  typedef CppProperty<Class> prop_class;
52 
53  CppProperty_GetConstMethod(GetMethod getter_ , const char* doc = 0) :
54  prop_class(doc), getter(getter_), class_name(DEMANGLE(PROP)){}
55 
56  SEXP get(Class* object) { return Rcpp::wrap((object->*getter)()); }
57  void set(Class*, SEXP) { throw std::range_error("property is read only"); }
58  bool is_readonly(){ return true; }
59  std::string get_class(){ return class_name; }
60 
61 private:
63  std::string class_name;
64 
65 };
66 
67 
68 // getter through a free function taking a pointer to Class
69 template <typename Class, typename PROP>
70 class CppProperty_GetPointerMethod : public CppProperty<Class> {
71 public:
72  typedef PROP (*GetMethod)(Class*);
73  typedef CppProperty<Class> prop_class;
74 
75  CppProperty_GetPointerMethod(GetMethod getter_ , const char* doc = 0) :
76  prop_class(doc), getter(getter_), class_name(DEMANGLE(PROP)){}
77 
78  SEXP get(Class* object) { return Rcpp::wrap(getter(object)); }
79  void set(Class*, SEXP) { throw std::range_error("property is read only"); }
80  bool is_readonly(){ return true; }
81  std::string get_class(){ return class_name; }
82 
83 private:
85  std::string class_name;
86 };
87 
88 
89 // getter and setter through member functions
90 template <typename Class, typename PROP>
91 class CppProperty_GetMethod_SetMethod : public CppProperty<Class> {
92 public:
93  typedef PROP (Class::*GetMethod)(void);
94  typedef void (Class::*SetMethod)(PROP);
95  typedef CppProperty<Class> prop_class;
96 
97  CppProperty_GetMethod_SetMethod(GetMethod getter_, SetMethod setter_, const char* doc = 0) :
98  prop_class(doc), getter(getter_), setter(setter_), class_name(DEMANGLE(PROP)){}
99 
100  SEXP get(Class* object) {
101  return Rcpp::wrap((object->*getter)());
102  }
103  void set(Class* object, SEXP value) {
104  (object->*setter)(Rcpp::as< typename Rcpp::traits::remove_const_and_reference< PROP >::type >(value));
105  }
106  bool is_readonly(){ return false; }
107  std::string get_class(){ return class_name; }
108 
109 private:
111  SetMethod setter;
112  std::string class_name;
113 };
114 template <typename Class, typename PROP>
115 class CppProperty_GetConstMethod_SetMethod : public CppProperty<Class> {
116 public:
117  typedef PROP (Class::*GetMethod)(void) const;
118  typedef void (Class::*SetMethod)(PROP);
119  typedef CppProperty<Class> prop_class;
120 
121  CppProperty_GetConstMethod_SetMethod(GetMethod getter_, SetMethod setter_, const char* doc = 0) :
122  prop_class(doc), getter(getter_), setter(setter_), class_name(DEMANGLE(PROP)){}
123 
124  SEXP get(Class* object) {
125  return Rcpp::wrap((object->*getter)());
126  }
127  void set(Class* object, SEXP value) {
128  (object->*setter)(Rcpp::as< typename Rcpp::traits::remove_const_and_reference< PROP >::type >(value));
129  }
130  bool is_readonly(){ return false; }
131  std::string get_class(){ return class_name; }
132 
133 private:
135  SetMethod setter;
136  std::string class_name;
137 
138 };
139 
140 
141 
142 
143 // getter though a member function, setter through a pointer function
144 template <typename Class, typename PROP>
145 class CppProperty_GetMethod_SetPointer : public CppProperty<Class> {
146 public:
147  typedef PROP (Class::*GetMethod)(void);
148  typedef void (*SetMethod)(Class*,PROP);
149  typedef CppProperty<Class> prop_class;
150 
151  CppProperty_GetMethod_SetPointer(GetMethod getter_, SetMethod setter_, const char* doc = 0) :
152  prop_class(doc), getter(getter_), setter(setter_), class_name(DEMANGLE(PROP)){}
153 
154  SEXP get(Class* object) {
155  return Rcpp::wrap((object->*getter)());
156  }
157  void set(Class* object, SEXP value) {
158  setter(object, Rcpp::as< typename Rcpp::traits::remove_const_and_reference< PROP >::type >(value));
159  }
160  bool is_readonly(){ return false; }
161  std::string get_class(){ return class_name; }
162 
163 private:
165  SetMethod setter;
166  std::string class_name;
167 
168 };
169 template <typename Class, typename PROP>
170 class CppProperty_GetConstMethod_SetPointer : public CppProperty<Class> {
171 public:
172  typedef PROP (Class::*GetMethod)(void) const;
173  typedef void (*SetMethod)(Class*,PROP);
174  typedef CppProperty<Class> prop_class;
175 
176  CppProperty_GetConstMethod_SetPointer(GetMethod getter_, SetMethod setter_, const char* doc = 0) :
177  prop_class(doc), getter(getter_), setter(setter_), class_name(DEMANGLE(PROP)){}
178 
179  SEXP get(Class* object) {
180  return Rcpp::wrap((object->*getter)());
181  }
182  void set(Class* object, SEXP value) {
183  setter(object, Rcpp::as< typename Rcpp::traits::remove_const_and_reference< PROP >::type >(value));
184  }
185  bool is_readonly(){ return false; }
186  std::string get_class(){ return class_name; }
187 
188 private:
190  SetMethod setter;
191  std::string class_name;
192 
193 };
194 
195 // getter through pointer function, setter through member function
196 template <typename Class, typename PROP>
197 class CppProperty_GetPointer_SetMethod : public CppProperty<Class> {
198 public:
199  typedef PROP (*GetMethod)(Class*);
200  typedef void (Class::*SetMethod)(PROP);
201  typedef CppProperty<Class> prop_class;
202 
203  CppProperty_GetPointer_SetMethod(GetMethod getter_, SetMethod setter_, const char* doc = 0) :
204  prop_class(doc), getter(getter_), setter(setter_), class_name(DEMANGLE(PROP)){}
205 
206  SEXP get(Class* object) {
207  return Rcpp::wrap(getter(object));
208  }
209  void set(Class* object, SEXP value) {
210  (object->*setter)(Rcpp::as< typename Rcpp::traits::remove_const_and_reference< PROP >::type >(value));
211  }
212  bool is_readonly(){ return false; }
213  std::string get_class(){ return class_name; }
214 
215 private:
217  SetMethod setter;
218  std::string class_name;
219 
220 };
221 
222 // getter and setter through pointer functions
223 // getter through pointer function, setter through member function
224 template <typename Class, typename PROP>
225 class CppProperty_GetPointer_SetPointer : public CppProperty<Class> {
226 public:
227  typedef PROP (*GetMethod)(Class*);
228  typedef void (*SetMethod)(Class*,PROP);
229  typedef CppProperty<Class> prop_class;
230 
231  CppProperty_GetPointer_SetPointer(GetMethod getter_, SetMethod setter_, const char* doc = 0) :
232  prop_class(doc), getter(getter_), setter(setter_), class_name(DEMANGLE(PROP)){}
233 
234  SEXP get(Class* object) {
235  return Rcpp::wrap(getter(object));
236  }
237  void set(Class* object, SEXP value) {
238  setter(object, Rcpp::as< typename Rcpp::traits::remove_const_and_reference< PROP >::type >(value));
239  }
240  bool is_readonly(){ return false; }
241  std::string get_class(){ return class_name; }
242 
243 private:
245  SetMethod setter;
246  std::string class_name;
247 
248 };
249 
250 
251 #endif
T as(SEXP x)
Definition: as.h:151
CppProperty_GetConstMethod_SetPointer(GetMethod getter_, SetMethod setter_, const char *doc=0)
CppProperty_GetMethod_SetPointer(GetMethod getter_, SetMethod setter_, const char *doc=0)
#define DEMANGLE(__TYPE__)
Definition: exceptions.h:315
CppProperty< Class > prop_class
std::string get_class()
CppProperty_GetPointerMethod(GetMethod getter_, const char *doc=0)
remove_const< typename remove_reference< T >::type >::type type
CppProperty< Class > prop_class
CppProperty< Class > prop_class
CppProperty_GetMethod_SetMethod(GetMethod getter_, SetMethod setter_, const char *doc=0)
CppProperty_GetMethod(GetMethod getter_, const char *doc=0)
CppProperty< Class > prop_class
SEXP wrap(const Date &date)
Definition: Date.h:38
PROP(Class::* GetMethod)(void)
CppProperty_GetConstMethod(GetMethod getter_, const char *doc=0)
CppProperty_GetPointer_SetPointer(GetMethod getter_, SetMethod setter_, const char *doc=0)
CppProperty_GetPointer_SetMethod(GetMethod getter_, SetMethod setter_, const char *doc=0)
CppProperty_GetConstMethod_SetMethod(GetMethod getter_, SetMethod setter_, const char *doc=0)