|
Rcpp Version 0.9.10
|
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*- 00002 // 00003 // Module_Add_Property.h: Rcpp R/C++ interface class library -- Rcpp modules 00004 // 00005 // Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois 00006 // 00007 // This file is part of Rcpp. 00008 // 00009 // Rcpp is free software: you can redistribute it and/or modify it 00010 // under the terms of the GNU General Public License as published by 00011 // the Free Software Foundation, either version 2 of the License, or 00012 // (at your option) any later version. 00013 // 00014 // Rcpp is distributed in the hope that it will be useful, but 00015 // WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 // GNU General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU General Public License 00020 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>. 00021 00022 #ifndef Rcpp_Module_Field_h 00023 #define Rcpp_Module_Field_h 00024 00025 // getter through a member function 00026 template <typename PROP> 00027 class CppProperty_Getter_Setter : public CppProperty<Class> { 00028 public: 00029 typedef PROP Class::*pointer ; 00030 typedef CppProperty<Class> prop_class ; 00031 00032 CppProperty_Getter_Setter( pointer ptr_ , const char* doc) : 00033 prop_class(doc), ptr(ptr_), class_name(DEMANGLE(PROP)) {} 00034 00035 SEXP get(Class* object) { return Rcpp::wrap( object->*ptr ) ; } 00036 void set(Class* object, SEXP value) { object->*ptr = Rcpp::as<PROP>( value ) ; } 00037 bool is_readonly(){ return false ; } 00038 std::string get_class(){ return class_name; } 00039 00040 private: 00041 pointer ptr ; 00042 std::string class_name ; 00043 } ; 00044 00045 00046 // getter through a member function 00047 template <typename PROP> 00048 class CppProperty_Getter : public CppProperty<Class> { 00049 public: 00050 typedef PROP Class::*pointer ; 00051 typedef CppProperty<Class> prop_class ; 00052 00053 CppProperty_Getter( pointer ptr_, const char* doc = 0 ) : 00054 prop_class(doc), ptr(ptr_), class_name(DEMANGLE(PROP)) {} 00055 00056 SEXP get(Class* object) { return Rcpp::wrap( object->*ptr ) ; } 00057 void set(Class* object, SEXP value) { throw std::range_error("read only data member") ; } 00058 bool is_readonly(){ return true ; } 00059 std::string get_class(){ return class_name; } 00060 00061 private: 00062 pointer ptr ; 00063 std::string class_name ; 00064 } ; 00065 00066 00067 template <typename T> 00068 self& field( const char* name_, T Class::*ptr, const char* docstring = 0){ 00069 AddProperty( name_, 00070 new CppProperty_Getter_Setter<T>( ptr, docstring ) 00071 ) ; 00072 return *this ; 00073 } 00074 00075 template <typename T> 00076 self& field_readonly( const char* name_, T Class::*ptr, const char* docstring = 0 ){ 00077 AddProperty( name_, 00078 new CppProperty_Getter<T>( ptr, docstring ) 00079 ) ; 00080 return *this ; 00081 } 00082 00083 #endif