Rcpp Version 0.12.12
proxy.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2 //
3 // proxy.h: Rcpp R/C++ interface class library -- proxies
4 //
5 // Copyright (C) 2010 - 2014 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__vector__proxy_h
23 #define Rcpp__vector__proxy_h
24 
25 namespace Rcpp{
26 namespace internal{
27 
28  template <int RTYPE> class simple_name_proxy {
29  public:
30  typedef ::Rcpp::Vector<RTYPE> VECTOR ;
31  typedef typename ::Rcpp::traits::storage_type<RTYPE>::type CTYPE ;
32  simple_name_proxy( VECTOR& v, const std::string& name_) :
33  parent(v), name(name_){} ;
35  parent(other.parent), name(other.name){} ;
37 
39  set( rhs ) ;
40  return *this ;
41  }
43  set( other.get() ) ;
44  return *this ;
45  }
46 
47  template <typename T>
48  simple_name_proxy& operator=( const T& rhs ){
49  set( caster<T,CTYPE>(rhs) ) ;
50  return *this ;
51  }
52 
53  // TODO: other operators +=, -=, ...
54 
55  operator CTYPE() const {
56  return get() ;
57  }
58 
59  // this helps wrap, for example : wrap( x["foo"] )
60  operator SEXP() const {
61  return ::Rcpp::wrap(get()) ;
62  }
63 
64  private:
65  VECTOR& parent ;
66  std::string name;
67  void set( CTYPE rhs ){
68  R_xlen_t index = 0 ;
69  try{
70  index = parent.offset(name) ;
71  parent[ index ] = rhs ;
72  } catch( const index_out_of_bounds& ex ){
73  parent.push_back( rhs, name );
74  }
75  }
76  CTYPE get() const {
77  return parent[ parent.offset(name) ];
78  }
79  } ;
80 
81  template <int RTYPE>
82  class string_name_proxy{
83  public:
84  typedef typename ::Rcpp::Vector<RTYPE> VECTOR ;
85  typedef const char* iterator ;
86  typedef const char& reference ;
87 
88  string_name_proxy( VECTOR& v, const std::string& name_) :
89  parent(v), name(name_){} ;
91  parent(other.parent), name(other.name){} ;
93 
94  string_name_proxy& operator=( const std::string& rhs ){
95  set( rhs ) ;
96  return *this ;
97  }
99  set( other.get() ) ;
100  return *this ;
101  }
102 
103  operator char* (){
104  return get() ;
105  }
106 
107  operator SEXP(){
108  return ::Rf_mkString(get()) ;
109  }
110 
111  inline iterator begin() { return get() ; }
112  inline iterator end(){ return begin() + size() ; }
113  inline reference operator[]( R_xlen_t i ){ return *( get() + i ) ; }
114  inline R_xlen_t size(){ return strlen( get() ) ; }
115 
116  private:
117  VECTOR& parent ;
118  std::string name;
119  void set( const std::string& rhs ){
120  R_xlen_t index = 0 ;
121  try{
122  index = parent.offset(name) ;
123  parent[ index ] = rhs ;
124  } catch( const index_out_of_bounds& ex ){
125  parent.push_back( rhs, name );
126  }
127  }
128  char* get(){
129  return parent[ parent.offset(name) ];
130  }
131 
132  } ;
133 
134  template <int RTYPE> class generic_name_proxy {
135  public:
136  typedef ::Rcpp::Vector<RTYPE> VECTOR ;
137  generic_name_proxy( VECTOR& v, const std::string& name_) :
138  parent(v), name(name_){}
140  parent(other.parent), name(other.name){}
142 
144  set( rhs ) ;
145  return *this ;
146  }
148  set( other.get() ) ;
149  return *this ;
150  }
151 
152  template <typename T>
153  generic_name_proxy& operator=( const T& rhs ){
154  set( ::Rcpp::wrap(rhs) ) ;
155  return *this ;
156  }
157 
158  // TODO: other operators +=, -=, ...
159 
160  operator SEXP() const {
161  return get() ;
162  }
163 
164  template <typename T>
165  operator T() const {
166  #if RCPP_DEBUG_LEVEL > 0
167  SEXP res = get() ;
168  RCPP_DEBUG_1( "generic_name_proxy::get() = <%p> ", res ) ;
169  return ::Rcpp::as<T>( res ) ;
170  #else
171  return ::Rcpp::as<T>( get() ) ;
172  #endif
173  }
174 
175  private:
176  VECTOR& parent ;
177  std::string name;
178  void set( SEXP rhs ){
179  R_xlen_t index = 0 ;
180  try{
181  index = parent.offset(name) ;
182  parent[ index ] = rhs ;
183  } catch( const index_out_of_bounds& ex ){
184  parent.push_back( rhs, name );
185  }
186  }
187  SEXP get() const {
188  return parent[ parent.offset(name) ];
189  }
190  } ;
191 }
192 
193 namespace traits {
194 
195  template <int RTYPE>
196  struct r_vector_name_proxy{
197  typedef typename ::Rcpp::internal::simple_name_proxy<RTYPE> type ;
198  } ;
199  template<> struct r_vector_name_proxy<STRSXP>{
200  typedef ::Rcpp::internal::string_name_proxy<STRSXP> type ;
201  } ;
202  template<> struct r_vector_name_proxy<VECSXP>{
203  typedef ::Rcpp::internal::generic_name_proxy<VECSXP> type ;
204  } ;
205  template<> struct r_vector_name_proxy<EXPRSXP>{
206  typedef ::Rcpp::internal::generic_name_proxy<EXPRSXP> type ;
207  } ;
208 
209  template <int RTYPE>
210  struct r_vector_proxy{
211  typedef typename storage_type<RTYPE>::type& type ;
212  } ;
213  template<> struct r_vector_proxy<STRSXP> {
214  typedef ::Rcpp::internal::string_proxy<STRSXP> type ;
215  } ;
216  template<> struct r_vector_proxy<EXPRSXP> {
217  typedef ::Rcpp::internal::generic_proxy<EXPRSXP> type ;
218  } ;
219  template<> struct r_vector_proxy<VECSXP> {
220  typedef ::Rcpp::internal::generic_proxy<VECSXP> type ;
221  } ;
222 
223  template <int RTYPE>
224  struct r_vector_const_proxy{
225  typedef const typename storage_type<RTYPE>::type& type ;
226  } ;
227  template<> struct r_vector_const_proxy<STRSXP> {
228  typedef ::Rcpp::internal::const_string_proxy<STRSXP> type ;
229  } ;
230  template<> struct r_vector_const_proxy<VECSXP> {
231  typedef ::Rcpp::internal::const_generic_proxy<VECSXP> type ;
232  } ;
233  template<> struct r_vector_const_proxy<EXPRSXP> {
234  typedef ::Rcpp::internal::const_generic_proxy<EXPRSXP> type ;
235  } ;
236 
237  template <int RTYPE>
238  struct r_vector_iterator {
239  typedef typename storage_type<RTYPE>::type* type ;
240  };
241  template <int RTYPE>
242  struct r_vector_const_iterator {
243  typedef const typename storage_type<RTYPE>::type* type ;
244  };
245 
246  template <int RTYPE> struct proxy_based_iterator{
247  typedef ::Rcpp::internal::Proxy_Iterator< typename r_vector_proxy<RTYPE>::type > type ;
248  } ;
249  template<> struct r_vector_iterator<VECSXP> : proxy_based_iterator<VECSXP>{} ;
250  template<> struct r_vector_iterator<EXPRSXP> : proxy_based_iterator<EXPRSXP>{} ;
251  template<> struct r_vector_iterator<STRSXP> : proxy_based_iterator<STRSXP>{} ;
252 
253  template <int RTYPE> struct proxy_based_const_iterator{
254  typedef ::Rcpp::internal::Proxy_Iterator< typename r_vector_const_proxy<RTYPE>::type > type ;
255  } ;
256  template<> struct r_vector_const_iterator<VECSXP> : proxy_based_const_iterator<VECSXP>{} ;
257  template<> struct r_vector_const_iterator<EXPRSXP> : proxy_based_const_iterator<EXPRSXP>{} ;
258  template<> struct r_vector_const_iterator<STRSXP> : proxy_based_const_iterator<STRSXP>{} ;
259 
260 } // traits
261 }
262 
263 #endif
string_name_proxy(VECTOR &v, const std::string &name_)
Definition: proxy.h:88
simple_name_proxy & operator=(const simple_name_proxy &other)
Definition: proxy.h:42
simple_name_proxy & operator=(CTYPE rhs)
Definition: proxy.h:38
generic_name_proxy(const generic_name_proxy &other)
Definition: proxy.h:139
::Rcpp::internal::const_string_proxy< STRSXP > type
Definition: proxy.h:228
::Rcpp::Vector< RTYPE > VECTOR
Definition: proxy.h:84
::Rcpp::traits::storage_type< RTYPE >::type CTYPE
Definition: proxy.h:31
::Rcpp::internal::const_generic_proxy< EXPRSXP > type
Definition: proxy.h:234
generic_name_proxy & operator=(const generic_name_proxy &other)
Definition: proxy.h:147
::Rcpp::internal::string_proxy< STRSXP > type
Definition: proxy.h:214
generic_name_proxy & operator=(SEXP rhs)
Definition: proxy.h:143
const storage_type< RTYPE >::type & type
Definition: proxy.h:225
simple_name_proxy & operator=(const T &rhs)
Definition: proxy.h:48
#define RCPP_DEBUG_1(fmt, MSG)
Definition: debug.h:44
storage_type< RTYPE >::type & type
Definition: proxy.h:211
void push_back(const T &object)
Definition: Vector.h:448
R_xlen_t offset(const int &i, const int &j) const
Definition: Vector.h:281
::Rcpp::internal::generic_proxy< VECSXP > type
Definition: proxy.h:220
const storage_type< RTYPE >::type * type
Definition: proxy.h:243
::Rcpp::internal::Proxy_Iterator< typename r_vector_proxy< RTYPE >::type > type
Definition: proxy.h:247
reference operator[](R_xlen_t i)
Definition: proxy.h:113
string_name_proxy(const string_name_proxy &other)
Definition: proxy.h:90
::Rcpp::internal::generic_name_proxy< VECSXP > type
Definition: proxy.h:203
::Rcpp::internal::const_generic_proxy< VECSXP > type
Definition: proxy.h:231
simple_name_proxy(const simple_name_proxy &other)
Definition: proxy.h:34
::Rcpp::Vector< RTYPE > VECTOR
Definition: proxy.h:136
::Rcpp::internal::generic_proxy< EXPRSXP > type
Definition: proxy.h:217
simple_name_proxy(VECTOR &v, const std::string &name_)
Definition: proxy.h:32
string_name_proxy & operator=(const string_name_proxy &other)
Definition: proxy.h:98
::Rcpp::internal::simple_name_proxy< RTYPE > type
Definition: proxy.h:197
generic_name_proxy(VECTOR &v, const std::string &name_)
Definition: proxy.h:137
SEXP wrap(const Date &date)
Definition: Date.h:38
::Rcpp::internal::Proxy_Iterator< typename r_vector_const_proxy< RTYPE >::type > type
Definition: proxy.h:254
generic_name_proxy & operator=(const T &rhs)
Definition: proxy.h:153
::Rcpp::internal::generic_name_proxy< EXPRSXP > type
Definition: proxy.h:206
Rcpp API.
Definition: algo.h:28
::Rcpp::Vector< RTYPE > VECTOR
Definition: proxy.h:30
string_name_proxy & operator=(const std::string &rhs)
Definition: proxy.h:94
::Rcpp::internal::string_name_proxy< STRSXP > type
Definition: proxy.h:200
storage_type< RTYPE >::type * type
Definition: proxy.h:239