Rcpp Version 1.0.14
Loading...
Searching...
No Matches
Module_Field.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_Add_Property.h: Rcpp R/C++ interface class library -- Rcpp modules
4//
5// Copyright (C) 2010 - 2011 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_Field_h
23#define Rcpp_Module_Field_h
24
25// getter through a member function
26template <typename PROP>
27class CppProperty_Getter_Setter : public CppProperty<Class> {
28public:
29 typedef PROP Class::*pointer ;
30 typedef CppProperty<Class> prop_class ;
31
32 CppProperty_Getter_Setter( pointer ptr_ , const char* doc) :
33 prop_class(doc), ptr(ptr_), class_name(DEMANGLE(PROP)) {}
34
35 SEXP get(Class* object) { return Rcpp::wrap( object->*ptr ) ; }
36 void set(Class* object, SEXP value) { object->*ptr = Rcpp::as<PROP>( value ) ; }
37 bool is_readonly(){ return false ; }
38 std::string get_class(){ return class_name; }
39
40private:
42 std::string class_name ;
43} ;
44
45
46// getter through a member function
47template <typename PROP>
48class CppProperty_Getter : public CppProperty<Class> {
49public:
50 typedef PROP Class::*pointer ;
51 typedef CppProperty<Class> prop_class ;
52
53 CppProperty_Getter( pointer ptr_, const char* doc = 0 ) :
54 prop_class(doc), ptr(ptr_), class_name(DEMANGLE(PROP)) {}
55
56 SEXP get(Class* object) { return Rcpp::wrap( object->*ptr ) ; }
57 void set(Class* object, SEXP value) { throw std::range_error("read only data member") ; }
58 bool is_readonly(){ return true ; }
59 std::string get_class(){ return class_name; }
60
61private:
63 std::string class_name ;
64} ;
65
66
67template <typename T>
68self& field( const char* name_, T Class::*ptr, const char* docstring = 0){
69 AddProperty( name_,
70 new CppProperty_Getter_Setter<T>( ptr, docstring )
71 ) ;
72 return *this ;
73}
74
75template <typename T>
76self& field_readonly( const char* name_, T Class::*ptr, const char* docstring = 0 ){
77 AddProperty( name_,
78 new CppProperty_Getter<T>( ptr, docstring )
79 ) ;
80 return *this ;
81}
82
83#endif
self & field_readonly(const char *name_, T Class::*ptr, const char *docstring=0)
self & field(const char *name_, T Class::*ptr, const char *docstring=0)
void set(Class *object, SEXP value)
CppProperty< Class > prop_class
CppProperty_Getter_Setter(pointer ptr_, const char *doc)
SEXP get(Class *object)
CppProperty< Class > prop_class
void set(Class *object, SEXP value)
SEXP get(Class *object)
PROP Class::* pointer
CppProperty_Getter(pointer ptr_, const char *doc=0)
std::string class_name
std::string get_class()
#define DEMANGLE(__TYPE__)
Definition exceptions.h:382
T as(SEXP x)
Definition as.h:151
SEXP wrap(const Date &date)
Definition Date.h:38