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