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) :
prop_class(doc)33         prop_class(doc), getter(getter_), class_name(DEMANGLE(PROP)){}
34 
get(Class * object)35     SEXP get(Class* object) { return Rcpp::wrap((object->*getter)()); }
set(Class *,SEXP)36     void set(Class*, SEXP) { throw std::range_error("property is read only"); }
is_readonly()37     bool is_readonly(){ return true; }
get_class()38     std::string get_class(){ return class_name; }
39 
40 private:
41     GetMethod getter;
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) :
prop_class(doc)54         prop_class(doc), getter(getter_), class_name(DEMANGLE(PROP)){}
55 
get(Class * object)56     SEXP get(Class* object) { return Rcpp::wrap((object->*getter)()); }
set(Class *,SEXP)57     void set(Class*, SEXP) { throw std::range_error("property is read only"); }
is_readonly()58     bool is_readonly(){ return true; }
get_class()59     std::string get_class(){ return class_name; }
60 
61 private:
62     GetMethod getter;
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) :
prop_class(doc)76         prop_class(doc), getter(getter_), class_name(DEMANGLE(PROP)){}
77 
get(Class * object)78     SEXP get(Class* object) { return Rcpp::wrap(getter(object)); }
set(Class *,SEXP)79     void set(Class*, SEXP) { throw std::range_error("property is read only"); }
is_readonly()80     bool is_readonly(){ return true; }
get_class()81     std::string get_class(){ return class_name; }
82 
83 private:
84     GetMethod getter;
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) :
prop_class(doc)98         prop_class(doc), getter(getter_), setter(setter_), class_name(DEMANGLE(PROP)){}
99 
get(Class * object)100     SEXP get(Class* object) {
101         return Rcpp::wrap((object->*getter)());
102     }
set(Class * object,SEXP value)103     void set(Class* object, SEXP value) {
104         (object->*setter)(Rcpp::as< typename Rcpp::traits::remove_const_and_reference< PROP >::type >(value));
105     }
is_readonly()106     bool is_readonly(){ return false; }
get_class()107     std::string get_class(){ return class_name; }
108 
109 private:
110     GetMethod getter;
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) :
prop_class(doc)122         prop_class(doc), getter(getter_), setter(setter_), class_name(DEMANGLE(PROP)){}
123 
get(Class * object)124     SEXP get(Class* object) {
125         return Rcpp::wrap((object->*getter)());
126     }
set(Class * object,SEXP value)127     void set(Class* object, SEXP value) {
128         (object->*setter)(Rcpp::as< typename Rcpp::traits::remove_const_and_reference< PROP >::type >(value));
129     }
is_readonly()130     bool is_readonly(){ return false; }
get_class()131     std::string get_class(){ return class_name; }
132 
133 private:
134     GetMethod getter;
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) :
prop_class(doc)152         prop_class(doc), getter(getter_), setter(setter_), class_name(DEMANGLE(PROP)){}
153 
get(Class * object)154     SEXP get(Class* object) {
155         return Rcpp::wrap((object->*getter)());
156     }
set(Class * object,SEXP value)157     void set(Class* object, SEXP value) {
158         setter(object, Rcpp::as< typename Rcpp::traits::remove_const_and_reference< PROP >::type >(value));
159     }
is_readonly()160     bool is_readonly(){ return false; }
get_class()161     std::string get_class(){ return class_name; }
162 
163 private:
164     GetMethod getter;
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) :
prop_class(doc)177         prop_class(doc), getter(getter_), setter(setter_), class_name(DEMANGLE(PROP)){}
178 
get(Class * object)179     SEXP get(Class* object) {
180         return Rcpp::wrap((object->*getter)());
181     }
set(Class * object,SEXP value)182     void set(Class* object, SEXP value) {
183         setter(object, Rcpp::as< typename Rcpp::traits::remove_const_and_reference< PROP >::type >(value));
184     }
is_readonly()185     bool is_readonly(){ return false; }
get_class()186     std::string get_class(){ return class_name; }
187 
188 private:
189     GetMethod getter;
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) :
prop_class(doc)204         prop_class(doc), getter(getter_), setter(setter_), class_name(DEMANGLE(PROP)){}
205 
get(Class * object)206     SEXP get(Class* object) {
207         return Rcpp::wrap(getter(object));
208     }
set(Class * object,SEXP value)209     void set(Class* object, SEXP value) {
210         (object->*setter)(Rcpp::as< typename Rcpp::traits::remove_const_and_reference< PROP >::type >(value));
211     }
is_readonly()212     bool is_readonly(){ return false; }
get_class()213     std::string get_class(){ return class_name; }
214 
215 private:
216     GetMethod getter;
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) :
prop_class(doc)232         prop_class(doc), getter(getter_), setter(setter_), class_name(DEMANGLE(PROP)){}
233 
get(Class * object)234     SEXP get(Class* object) {
235         return Rcpp::wrap(getter(object));
236     }
set(Class * object,SEXP value)237     void set(Class* object, SEXP value) {
238         setter(object, Rcpp::as< typename Rcpp::traits::remove_const_and_reference< PROP >::type >(value));
239     }
is_readonly()240     bool is_readonly(){ return false; }
get_class()241     std::string get_class(){ return class_name; }
242 
243 private:
244     GetMethod getter;
245     SetMethod setter;
246     std::string class_name;
247 
248 };
249 
250 
251 #endif
252