1 // const_generic_proxy.h: Rcpp R/C++ interface class library -- 2 // 3 // Copyright (C) 2013 - 2018 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__const_generic_proxy_h 21 #define Rcpp__vector__const_generic_proxy_h 22 23 namespace Rcpp{ 24 namespace internal{ 25 26 template <int RTYPE, template <class> class StoragePolicy> 27 class const_generic_proxy : public GenericProxy< const_generic_proxy<RTYPE, StoragePolicy> > { 28 public: 29 typedef typename ::Rcpp::Vector<RTYPE, StoragePolicy> VECTOR ; 30 const_generic_proxy()31 const_generic_proxy(): parent(0), index(-1){} 32 const_generic_proxy(const const_generic_proxy & other)33 const_generic_proxy( const const_generic_proxy& other ) : 34 parent(other.parent), index(other.index){} ; 35 const_generic_proxy(const VECTOR & v,R_xlen_t i)36 const_generic_proxy( const VECTOR& v, R_xlen_t i ) : parent(&v), index(i){} ; 37 SEXP()38 operator SEXP() const { 39 return get() ; 40 } 41 U()42 template <typename U> operator U() const { 43 return ::Rcpp::as<U>(get()) ; 44 } 45 46 // helping the compiler (not sure why it can't help itself) 47 operator bool() const { return ::Rcpp::as<bool>(get()) ; } 48 operator int() const { return ::Rcpp::as<int>(get()) ; } 49 move(R_xlen_t n)50 inline void move(R_xlen_t n) { index += n ; } 51 52 const VECTOR* parent; 53 R_xlen_t index ; 54 55 private: 56 get()57 inline SEXP get() const { 58 return VECTOR_ELT(*parent, index ); 59 } 60 61 } ; 62 63 } 64 } 65 66 #endif 67