Rcpp Version 1.0.9
SlotProxy.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_proxy_SlotProxy_h
19 #define Rcpp_proxy_SlotProxy_h
20 
21 namespace Rcpp{
22 
23 template <typename CLASS>
25 public:
26 
27  class SlotProxy : public GenericProxy<SlotProxy>{
28  public:
29  SlotProxy( CLASS& v, const std::string& name) : parent(v), slot_name(Rf_install(name.c_str())) {
30  if( !R_has_slot( v, slot_name) ){
31  throw no_such_slot(name);
32  }
33  }
34 
36  set( rhs.get() ) ;
37  return *this ;
38  }
39 
40  template <typename T> SlotProxy& operator=(const T& rhs);
41 
42  template <typename T> operator T() const;
43  inline operator SEXP() const {
44  return get() ;
45  }
46 
47  private:
48  CLASS& parent;
49  SEXP slot_name ;
50 
51  SEXP get() const {
52  return R_do_slot( parent, slot_name ) ;
53  }
54  void set(SEXP x ) {
55  parent = R_do_slot_assign(parent, slot_name, x);
56  }
57  } ;
58 
59  class const_SlotProxy : public GenericProxy<const_SlotProxy> {
60  public:
61  const_SlotProxy( const CLASS& v, const std::string& name) : parent(v), slot_name(Rf_install(name.c_str())) {
62  if( !R_has_slot( v, slot_name) ){
63  throw no_such_slot(name);
64  }
65  }
66 
67  template <typename T> operator T() const {
68  return as<T>( get() );
69  }
70  inline operator SEXP() const {
71  return get() ;
72  }
73 
74  private:
75  const CLASS& parent;
76  SEXP slot_name ;
77 
78  SEXP get() const {
79  return R_do_slot( parent, slot_name ) ;
80  }
81  } ;
82 
83  SlotProxy slot(const std::string& name){
84  SEXP x = static_cast<CLASS&>(*this) ;
85  if( !Rf_isS4(x) ) throw not_s4() ;
86  return SlotProxy( static_cast<CLASS&>(*this) , name ) ;
87  }
88  const_SlotProxy slot(const std::string& name) const {
89  SEXP x = static_cast<const CLASS&>(*this) ;
90  if( !Rf_isS4(x) ) throw not_s4() ;
91  return const_SlotProxy( static_cast<const CLASS&>(*this) , name ) ;
92  }
93 
94  bool hasSlot(const std::string& name) const{
95  SEXP x = static_cast<const CLASS&>(*this).get__() ;
96  if( !Rf_isS4(x) ) throw not_s4() ;
97  return R_has_slot( x, Rf_mkString(name.c_str()) ) ;
98  }
99 
100 } ;
101 
102 }
103 #endif
SlotProxy & operator=(const T &rhs)
SlotProxy(CLASS &v, const std::string &name)
Definition: SlotProxy.h:29
SlotProxy & operator=(const SlotProxy &rhs)
Definition: SlotProxy.h:35
const_SlotProxy(const CLASS &v, const std::string &name)
Definition: SlotProxy.h:61
SlotProxy slot(const std::string &name)
Definition: SlotProxy.h:83
bool hasSlot(const std::string &name) const
Definition: SlotProxy.h:94
const_SlotProxy slot(const std::string &name) const
Definition: SlotProxy.h:88
Rcpp API.
Definition: algo.h:28