Rcpp Version 1.0.9
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 
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> str(Rf_mkString(field_name.c_str()));
45  Shield<SEXP> call(Rf_lang3(R_DollarSymbol, parent, str));
46  return Rcpp_fast_eval( call, R_GlobalEnv ) ;
47  }
48  void set(SEXP x ) {
49  SEXP dollarGetsSym = Rf_install( "$<-");
50  Shield<SEXP> str(Rf_mkString(field_name.c_str()));
51  Shield<SEXP> call(Rf_lang4(dollarGetsSym, parent, str, x));
52  parent.set__( Rcpp_fast_eval( call, R_GlobalEnv ) );
53  }
54  } ;
55 
56  class const_FieldProxy : public GenericProxy<const_FieldProxy> {
57  public:
58  const_FieldProxy( const CLASS& v, const std::string& name) :
59  parent(v), field_name(name) {}
60 
61  template <typename T> operator T() const;
62  inline operator SEXP() const {
63  return get() ;
64  }
65 
66  private:
67  const CLASS& parent;
68  const std::string& field_name ;
69 
70  SEXP get() const {
71  Shield<SEXP> str(Rf_mkString(field_name.c_str()));
72  Shield<SEXP> call(Rf_lang3(R_DollarSymbol, parent, str));
73  return Rcpp_fast_eval( call, R_GlobalEnv ) ;
74  }
75  } ;
76 
77  FieldProxy field(const std::string& name){
78  return FieldProxy( static_cast<CLASS&>(*this), name ) ;
79  }
80  const_FieldProxy field(const std::string& name) const {
81  return const_FieldProxy( static_cast<const CLASS&>(*this), name ) ;
82  }
83 
84 } ;
85 
86 }
87 #endif
FieldProxy(CLASS &v, const std::string &name)
Definition: FieldProxy.h:29
FieldProxy & operator=(const FieldProxy &rhs)
FieldProxy & operator=(const T &rhs)
const std::string & field_name
Definition: FieldProxy.h:41
const_FieldProxy(const CLASS &v, const std::string &name)
Definition: FieldProxy.h:58
FieldProxy field(const std::string &name)
Definition: FieldProxy.h:77
const_FieldProxy field(const std::string &name) const
Definition: FieldProxy.h:80
Rcpp API.
Definition: algo.h:28
SEXP Rcpp_fast_eval(SEXP expr, SEXP env)
Definition: Rcpp_eval.h:68