Rcpp Version 0.12.12
generic_proxy.h
Go to the documentation of this file.
1 // generic_proxy.h: Rcpp R/C++ interface class library --
2 //
3 // Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
4 //
5 // This file is part of Rcpp.
6 //
7 // Rcpp is free software: you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // Rcpp is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
19 
20 #ifndef Rcpp__vector__generic_proxy_h
21 #define Rcpp__vector__generic_proxy_h
22 
23 namespace Rcpp{
24 namespace internal{
25 
26  template <int RTYPE>
27  class generic_proxy : public GenericProxy< generic_proxy<RTYPE> > {
28  public:
29  typedef typename ::Rcpp::Vector<RTYPE> VECTOR ;
30 
31  generic_proxy(): parent(0), index(-1){}
32 
33  generic_proxy( const generic_proxy& other ) :
34  parent(other.parent), index(other.index){} ;
35 
36  generic_proxy( VECTOR& v, R_xlen_t i ) : parent(&v), index(i){} ;
37 
38  generic_proxy& operator=(SEXP rhs) {
39  set(rhs) ;
40  return *this ;
41  }
42 
44  set(rhs.get());
45  return *this ;
46  }
47 
48  template <typename T>
49  generic_proxy& operator=( const T& rhs){
50  set(wrap(rhs)) ;
51  return *this;
52  }
53 
54  operator SEXP() const {
55  return get() ;
56  }
57 
58  template <typename U> operator U() const {
59  return ::Rcpp::as<U>(get()) ;
60  }
61 
62  // helping the compiler (not sure why it can't help itself)
63  operator bool() const { return ::Rcpp::as<bool>(get()) ; }
64  operator int() const { return ::Rcpp::as<int>(get()) ; }
65 
66  void swap(generic_proxy& other){
67  Shield<SEXP> tmp(get()) ;
68  set( other.get() ) ;
69  other.set(tmp) ;
70  }
71 
72  VECTOR* parent;
73  R_xlen_t index ;
74  inline void move(R_xlen_t n) { index += n ; }
75 
76  void import( const generic_proxy& other){
77  parent = other.parent ;
78  index = other.index ;
79  }
80 
81  private:
82  inline void set(SEXP x) {
83  SET_VECTOR_ELT( *parent, index, x ) ;
84  }
85  inline SEXP get() const {
86  return VECTOR_ELT(*parent, index );
87  }
88 
89  } ;
90 
91 }
92 }
93 
94 #endif
generic_proxy & operator=(SEXP rhs)
Definition: generic_proxy.h:38
generic_proxy(const generic_proxy &other)
Definition: generic_proxy.h:33
generic_proxy & operator=(const T &rhs)
Definition: generic_proxy.h:49
SEXP wrap(const Date &date)
Definition: Date.h:38
generic_proxy & operator=(const generic_proxy &rhs)
Definition: generic_proxy.h:43
Rcpp API.
Definition: algo.h:28
void swap(generic_proxy &other)
Definition: generic_proxy.h:66
::Rcpp::Vector< RTYPE > VECTOR
Definition: generic_proxy.h:29
generic_proxy(VECTOR &v, R_xlen_t i)
Definition: generic_proxy.h:36