Rcpp Version 1.0.9
DottedPairProxy.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_DottedPairProxy_h
19 #define Rcpp_DottedPairProxy_h
20 
21 namespace Rcpp{
22 
23 template <typename CLASS>
25 public:
26 
27  class DottedPairProxy : public GenericProxy<DottedPairProxy> {
28  public:
29  DottedPairProxy( CLASS& v, int index_ ): node(R_NilValue){
30  if( index_ >= v.length() ) {
31  const char* fmt = "Dotted Pair index is out of bounds: "
32  "[index=%i; extent=%i].";
33  throw index_out_of_bounds(fmt, index_, v.length());
34  }
35 
36  SEXP x = v ; /* implicit conversion */
37  for( int i = 0; i<index_; i++, x = CDR(x) ) ;
38  node = x ;
39  }
40 
42  return set(rhs.get());
43  }
45  return set(rhs) ;
46  }
47 
48  template <typename T>
49  DottedPairProxy& operator=(const T& rhs);
50 
51  template <typename T>
53 
54  template <typename T> operator T() const;
55 
56  inline SEXP get() const {
57  return CAR(node);
58  }
59  inline operator SEXP() const {
60  return get() ;
61  }
62  inline DottedPairProxy& set(SEXP x){
63  SETCAR( node, x ) ;
64  return *this ;
65  }
66  inline DottedPairProxy& set(SEXP x, const char* name){
67  SETCAR( node, x ) ;
68  SEXP rhsNameSym = ::Rf_install( name );
69  SET_TAG( node, rhsNameSym ) ;
70  return *this ;
71  }
72 
73  private:
74  SEXP node ;
75  } ;
76 
77  class const_DottedPairProxy : public GenericProxy<const_DottedPairProxy>{
78  public:
79  const_DottedPairProxy( const CLASS& v, int index_ ): node(R_NilValue){
80  if( index_ >= v.length() ) {
81  const char* fmt = "Dotted Pair index is out of bounds: "
82  "[index=%i; extent=%i].";
83  throw index_out_of_bounds(fmt, index_, v.length());
84  }
85 
86  SEXP x = v ; /* implicit conversion */
87  for( int i = 0; i<index_; i++, x = CDR(x) ) ;
88  node = x ;
89  }
90 
91  template <typename T> operator T() const;
92 
93  inline SEXP get() const {
94  return CAR(node);
95  }
96  inline operator SEXP() const {
97  return get() ;
98  }
99 
100  private:
101  SEXP node ;
102  } ;
103 
104 
106  return DottedPairProxy( static_cast<CLASS&>(*this), i ) ;
107  }
109  return const_DottedPairProxy( static_cast<const CLASS&>(*this), i ) ;
110  }
111 
112 } ;
113 
114 }
115 #endif
DottedPairProxy & set(SEXP x, const char *name)
DottedPairProxy & operator=(const traits::named_object< T > &rhs)
DottedPairProxy & operator=(const DottedPairProxy &rhs)
DottedPairProxy & operator=(const T &rhs)
DottedPairProxy operator[](int i)
const_DottedPairProxy operator[](int i) const
Rcpp API.
Definition: algo.h:28