Rcpp Version 0.12.12
const_string_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 // const_string_proxy.h: Rcpp R/C++ interface class library --
4 //
5 // Copyright (C) 2013 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__const_string_proxy_h
23 #define Rcpp__vector__const_string_proxy_h
24 
25 namespace Rcpp{
26 namespace internal{
27 
28  template<int RTYPE> class const_string_proxy {
29  public:
30 
31  typedef typename ::Rcpp::Vector<RTYPE> VECTOR ;
32  typedef const char* iterator ;
33  typedef const char& reference ;
34 
36 
43  const_string_proxy( const VECTOR& v, R_xlen_t index_ ) : parent(&v), index(index_){}
44 
45  const_string_proxy(SEXP x): parent(0), index(0) {
46  Vector<RTYPE> tmp(x);
47  parent = &tmp;
48  }
49 
51  parent(other.parent), index(other.index){} ;
52 
53  void import( const const_string_proxy& other){
54  parent = other.parent ;
55  index = other.index ;
56  }
57 
62  operator SEXP() const {
63  return get() ;
64  }
65 
71  operator /* const */ char*() const {
72  return const_cast<char*>( CHAR(get()) );
73  }
74 
79  template <int RT>
80  friend std::ostream& operator<<(std::ostream& os, const const_string_proxy<RT>& proxy);
81 
82  template <int RT>
83  friend std::string operator+( const std::string& x, const const_string_proxy<RT>& proxy);
84 
85  const VECTOR* parent;
86  R_xlen_t index ;
87  inline void move( R_xlen_t n ){ index += n ;}
88 
89  inline SEXP get() const {
90  return STRING_ELT( *parent, index ) ;
91  }
92 
93  inline iterator begin() const { return CHAR( STRING_ELT( *parent, index ) ) ; }
94  inline iterator end() const { return begin() + size() ; }
95  inline R_xlen_t size() const { return strlen( begin() ) ; }
96  inline bool empty() const { return *begin() == '\0' ; }
97  inline reference operator[]( R_xlen_t n ){ return *( begin() + n ) ; }
98 
99  bool operator==( const char* other){
100  return strcmp( begin(), other ) == 0 ;
101  }
102  bool operator!=( const char* other){
103  return strcmp( begin(), other ) != 0 ;
104  }
105 
106  bool operator==( const const_string_proxy& other){
107  return strcmp( begin(), other.begin() ) == 0 ;
108  }
109  bool operator!=( const const_string_proxy& other){
110  return strcmp( begin(), other.begin() ) != 0 ;
111  }
112 
113  bool operator==( SEXP other ) const {
114  return get() == other;
115  }
116 
117  bool operator!=( SEXP other ) const {
118  return get() != other;
119  }
120 
121  private:
122  static std::string buffer ;
123 
124  } ;
125 
126  template <int RT>
127  bool operator<( const const_string_proxy<RT>& lhs, const const_string_proxy<RT>& rhs) {
128  return strcmp(
129  const_cast<char *>(lhs.begin() ),
130  const_cast<char *>(rhs.begin())
131  ) < 0 ;
132  }
133 
134  template <int RT>
136  return strcmp(
137  const_cast<char *>(lhs.begin() ),
138  const_cast<char *>(rhs.begin())
139  ) > 0 ;
140  }
141 
142  template <int RT>
144  return strcmp(
145  const_cast<char *>(lhs.begin() ),
146  const_cast<char *>(rhs.begin())
147  ) >= 0 ;
148  }
149 
150  template <int RT>
151  bool operator<=( const const_string_proxy<RT>& lhs, const const_string_proxy<RT>& rhs) {
152  return strcmp(
153  const_cast<char *>(lhs.begin() ),
154  const_cast<char *>(rhs.begin())
155  ) <= 0 ;
156  }
157 
158  template<int RTYPE> std::string const_string_proxy<RTYPE>::buffer ;
159 
160  inline std::ostream& operator<<(std::ostream& os, const const_string_proxy<STRSXP>& proxy) {
161  os << static_cast<const char*>(proxy) ;
162  return os;
163  }
164 
165  inline std::string operator+( const std::string& x, const const_string_proxy<STRSXP>& y ){
166  return x + static_cast<const char*>(y) ;
167  }
168 
169 
170  template <int RTYPE>
172  set( other.get() ) ;
173  return *this ;
174  }
175 
176 }
177 }
178 
179 #endif
string_proxy & operator=(const string_proxy &other)
Definition: string_proxy.h:55
bool operator>=(const const_string_proxy< RT > &lhs, const const_string_proxy< RT > &rhs)
friend std::string operator+(const std::string &x, const const_string_proxy< RT > &proxy)
const_string_proxy(const const_string_proxy &other)
bool operator==(const const_string_proxy &other)
const_string_proxy(const VECTOR &v, R_xlen_t index_)
Rcpp API.
Definition: algo.h:28
bool operator!=(const const_string_proxy &other)
bool operator==(const char *other)
bool operator>(const const_string_proxy< RT > &lhs, const const_string_proxy< RT > &rhs)