Rcpp Version 1.0.9
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 - 2018 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, template <class> class StoragePolicy>
27  class generic_proxy : public GenericProxy< generic_proxy<RTYPE, StoragePolicy> > {
28  public:
29  typedef typename ::Rcpp::Vector<RTYPE, StoragePolicy> 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 
37  generic_proxy( VECTOR& v, R_xlen_t i ) :
38  parent(&v), index(i)
39  {}
40 
41  generic_proxy& operator=(SEXP rhs) {
42  set(rhs) ;
43  return *this ;
44  }
45 
47  set(rhs.get());
48  return *this ;
49  }
50 
51  template <template <class> class StoragePolicy2>
53  set(rhs.get());
54  return *this ;
55  }
56 
57  template <typename T>
58  generic_proxy& operator=( const T& rhs){
59  set(Shield<SEXP>(wrap(rhs))) ;
60  return *this;
61  }
62 
63  operator SEXP() const {
64  return get() ;
65  }
66 
67  template <typename U> operator U() const {
68  return ::Rcpp::as<U>(get()) ;
69  }
70 
71  // helping the compiler (not sure why it can't help itself)
72  operator bool() const { return ::Rcpp::as<bool>(get()) ; }
73  operator int() const { return ::Rcpp::as<int>(get()) ; }
74 
75  void swap(generic_proxy& other){
76  Shield<SEXP> tmp(get()) ;
77  set( other.get() ) ;
78  other.set(tmp) ;
79  }
80 
82  R_xlen_t index ;
83  inline void move(R_xlen_t n) { index += n ; }
84 
85  void import( const generic_proxy& other){
86  parent = other.parent ;
87  index = other.index ;
88  }
89 
90  inline SEXP get() const {
91  return VECTOR_ELT(*parent, index );
92  }
93 
94  private:
95  inline void set(SEXP x) {
96  SET_VECTOR_ELT( *parent, index, x ) ;
97  }
98 
99 
100  } ;
101 
102 }
103 }
104 
105 #endif
generic_proxy & operator=(SEXP rhs)
Definition: generic_proxy.h:41
generic_proxy & operator=(const generic_proxy< RTYPE, StoragePolicy2 > &rhs)
Definition: generic_proxy.h:52
void swap(generic_proxy &other)
Definition: generic_proxy.h:75
generic_proxy(const generic_proxy &other)
Definition: generic_proxy.h:33
::Rcpp::Vector< RTYPE, StoragePolicy > VECTOR
Definition: generic_proxy.h:29
generic_proxy & operator=(const generic_proxy &rhs)
Definition: generic_proxy.h:46
generic_proxy & operator=(const T &rhs)
Definition: generic_proxy.h:58
generic_proxy(VECTOR &v, R_xlen_t i)
Definition: generic_proxy.h:37
Rcpp API.
Definition: algo.h:28
SEXP wrap(const Date &date)
Definition: Date.h:38