Rcpp Version 1.0.9
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
26 template <typename PROP>
27 class CppProperty_Getter_Setter : public CppProperty<Class> {
28 public:
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 
40 private:
42  std::string class_name ;
43 } ;
44 
45 
46 // getter through a member function
47 template <typename PROP>
48 class CppProperty_Getter : public CppProperty<Class> {
49 public:
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 
61 private:
63  std::string class_name ;
64 } ;
65 
66 
67 template <typename T>
68 self& 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 
75 template <typename T>
76 self& 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)
Definition: Module_Field.h:76
self & field(const char *name_, T Class::*ptr, const char *docstring=0)
Definition: Module_Field.h:68
void set(Class *object, SEXP value)
Definition: Module_Field.h:36
CppProperty< Class > prop_class
Definition: Module_Field.h:30
CppProperty_Getter_Setter(pointer ptr_, const char *doc)
Definition: Module_Field.h:32
SEXP get(Class *object)
Definition: Module_Field.h:35
CppProperty< Class > prop_class
Definition: Module_Field.h:51
void set(Class *object, SEXP value)
Definition: Module_Field.h:57
SEXP get(Class *object)
Definition: Module_Field.h:56
PROP Class::* pointer
Definition: Module_Field.h:50
CppProperty_Getter(pointer ptr_, const char *doc=0)
Definition: Module_Field.h:53
std::string class_name
Definition: Module_Field.h:63
std::string get_class()
Definition: Module_Field.h:59
#define DEMANGLE(__TYPE__)
Definition: exceptions.h:382
SEXP wrap(const Date &date)
Definition: Date.h:38