Rcpp Version 0.12.12
FieldProxy.h
Go to the documentation of this file.
1 // Copyright (C) 2013 Romain Francois
2 //
3 // This file is part of Rcpp.
4 //
5 // Rcpp is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // Rcpp is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
17 
18 #ifndef Rcpp_FieldProxy_h
19 #define Rcpp_FieldProxy_h
20 
21 namespace Rcpp{
22 
23 template <typename CLASS>
25 public:
26 
27  class FieldProxy : public GenericProxy<FieldProxy> {
28  public:
29  FieldProxy( CLASS& v, const std::string& name) :
30  parent(v), field_name(name) {}
31 
32  FieldProxy& operator=(const FieldProxy& rhs);
33 
34  template <typename T> FieldProxy& operator=(const T& rhs);
35 
36  template <typename T> operator T() const;
37  inline operator SEXP() const { return get(); }
38 
39  private:
40  CLASS& parent;
41  const std::string& field_name ;
42 
43  SEXP get() const {
44  Shield<SEXP> call( Rf_lang3( R_DollarSymbol, parent, Rf_mkString(field_name.c_str()) ) ) ;
45  return Rcpp_eval( call, R_GlobalEnv ) ;
46  }
47  void set(SEXP x ) {
48  SEXP dollarGetsSym = Rf_install( "$<-");
49  Shield<SEXP> call( Rf_lang4( dollarGetsSym, parent, Rf_mkString(field_name.c_str()) , x ) ) ;
50  parent.set__( Rcpp_eval( call, R_GlobalEnv ) );
51  }
52  } ;
53 
54  class const_FieldProxy : public GenericProxy<const_FieldProxy> {
55  public:
56  const_FieldProxy( const CLASS& v, const std::string& name) :
57  parent(v), field_name(name) {}
58 
59  template <typename T> operator T() const;
60  inline operator SEXP() const {
61  return get() ;
62  }
63 
64  private:
65  const CLASS& parent;
66  const std::string& field_name ;
67 
68  SEXP get() const {
69  Shield<SEXP> call( Rf_lang3( R_DollarSymbol, parent, Rf_mkString(field_name.c_str()) ) ) ;
70  return Rcpp_eval( call, R_GlobalEnv ) ;
71  }
72  } ;
73 
74  FieldProxy field(const std::string& name){
75  return FieldProxy( static_cast<CLASS&>(*this), name ) ;
76  }
77  const_FieldProxy field(const std::string& name) const {
78  return const_FieldProxy( static_cast<const CLASS&>(*this), name ) ;
79  }
80 
81 } ;
82 
83 }
84 #endif
FieldProxy field(const std::string &name)
Definition: FieldProxy.h:74
const_FieldProxy field(const std::string &name) const
Definition: FieldProxy.h:77
FieldProxy(CLASS &v, const std::string &name)
Definition: FieldProxy.h:29
FieldProxy & operator=(const FieldProxy &rhs)
SEXP Rcpp_eval(SEXP expr, SEXP env)
Definition: Rcpp_eval.h:25
const_FieldProxy(const CLASS &v, const std::string &name)
Definition: FieldProxy.h:56
Rcpp API.
Definition: algo.h:28
const std::string & field_name
Definition: FieldProxy.h:41