File: string_proxy.h

package info (click to toggle)
rcpp 1.0.14-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 11,560 kB
  • sloc: ansic: 44,118; cpp: 40,958; sh: 53; makefile: 2
file content (266 lines) | stat: -rw-r--r-- 7,291 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
//
// string_proxy.h: Rcpp R/C++ interface class library --
//
// Copyright (C) 2010 - 2018 Dirk Eddelbuettel and Romain Francois
//
// This file is part of Rcpp.
//
// Rcpp is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// Rcpp is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.

#ifndef Rcpp__vector__string_proxy_h
#define Rcpp__vector__string_proxy_h

namespace Rcpp{
namespace internal{

	template<int RTYPE, template <class> class StoragePolicy>
	class string_proxy {
	public:

		typedef typename ::Rcpp::Vector<RTYPE, StoragePolicy> VECTOR ;
		typedef const char* iterator ;
		typedef const char& reference ;

		string_proxy() : parent(0), index(-1){};

		/**
		 * Creates a proxy
		 *
		 * @param v reference to the associated character vector
		 * @param index index
		 */
		string_proxy( VECTOR& v, R_xlen_t index_ ) : parent(&v), index(index_){}

		string_proxy( const string_proxy& other ) :
			parent(other.parent), index(other.index)
		{} ;

		/**
		 * lhs use. Assign the value of the referred element to
		 * the current content of the element referred by the
		 * rhs proxy
		 *
		 * @param rhs another proxy, possibly from another vector
		 */
		string_proxy& operator=( const string_proxy<RTYPE, StoragePolicy>& other) {
		    set( other.get() ) ;
		    return *this ;
		}

		template <template <class> class StoragePolicy2>
		string_proxy& operator=( const string_proxy<RTYPE, StoragePolicy2>& other) {
		    set( other.get() ) ;
		    return *this ;
		}

		template <template <class> class StoragePolicy2>
		string_proxy& operator=( const const_string_proxy<RTYPE, StoragePolicy2>& other) ;

		string_proxy& operator=( const String& s) ;

		/**
		 * lhs use. Assigns the value of the referred element
		 * of the character vector
		 *
		 * @param rhs new content for the element referred by this proxy
		 */
		template <typename T>
		string_proxy& operator=(const std::basic_string<T>& rhs){
			set( rhs ) ;
			return *this ;
		}

		string_proxy& operator=(const char* rhs){
			set( Rf_mkChar( rhs ) ) ;
			return *this ;
		}

		string_proxy& operator=(const wchar_t* rhs){
			set( internal::make_charsexp( rhs ) ) ;
			return *this ;
		}


		string_proxy& operator=(SEXP rhs){
			// TODO: check this is a CHARSXP
			set( rhs ) ;
			return *this ;
		}

		void import( const string_proxy& other){
			parent = other.parent ;
			index  = other.index ;
		}

        /**
         * lhs use. Adds the content of the rhs proxy to the
         * element this proxy refers to.
         */
        template <typename T>
        string_proxy& operator+=(const T& rhs) ;

		/**
		 * rhs use. Retrieves the current value of the
		 * element this proxy refers to.
		 */
		operator SEXP() const {
			return get() ;
		}

		/**
		 * rhs use. Retrieves the current value of the
		 * element this proxy refers to and convert it to a
		 * C string
		 */
		 operator /* const */ char*() const {
		 	 return const_cast<char*>( CHAR(get()) );
		 }

		 // operator std::wstring() const {
		 //     const char* st = CHAR(get()) ;
		 //     return std::wstring( st, st+strlen(st) ) ;
		 // }

		/**
		 * Prints the element this proxy refers to to an
		 * output stream
		 */
		template <int RT>
		friend std::ostream& operator<<(std::ostream& os, const string_proxy<RT>& proxy);

		template <int RT>
		friend std::string operator+( const std::string& x, const string_proxy<RT>& proxy);

		void swap( string_proxy& other ){
			Shield<SEXP> tmp( STRING_ELT(*parent, index)) ;
			SET_STRING_ELT( *parent, index, STRING_ELT( *(other.parent), other.index) ) ;
			SET_STRING_ELT( *(other.parent), other.index, tmp ) ;
		}

		VECTOR* parent;
		R_xlen_t index ;
		inline void move( R_xlen_t n ){ index += n ;}

		inline SEXP get() const {
			return STRING_ELT( *parent, index ) ;
		}
		template <typename T>
		inline void set( const T& x ){
			set( internal::make_charsexp(x) ) ;
		}
		inline void set(SEXP x){
			SET_STRING_ELT( *parent, index, x ) ;
		}

		inline iterator begin() const { return CHAR( STRING_ELT( *parent, index ) ) ; }
		inline iterator end() const { return begin() + size() ; }
		inline R_xlen_t size() const { return strlen( begin() ) ; }
		inline bool empty() const { return *begin() == '\0' ; }
		inline reference operator[]( R_xlen_t n ){ return *( begin() + n ) ; }

		template <typename UnaryOperator>
		void transform( UnaryOperator op ){
			buffer = begin() ;
			std::transform( buffer.begin(), buffer.end(), buffer.begin(), op ) ;
			set( buffer ) ;
		}

		template <typename OutputIterator, typename UnaryOperator>
		void apply( OutputIterator target, UnaryOperator op){
			std::transform( begin(), end(), target, op ) ;
		}

		template <typename UnaryOperator>
		void apply( UnaryOperator op){
			std::for_each( begin(), end(), op );
		}

		bool operator==( const char* other) const {
			return strcmp( begin(), other ) == 0 ;
		}
		bool operator!=( const char* other) const {
			return strcmp( begin(), other ) != 0 ;
		}

		template<template <class> class SP>
		bool operator==( const string_proxy<STRSXP, SP>& other) const {
			return strcmp( begin(), other.begin() ) == 0 ;
		}

		template<template <class> class SP>
		bool operator!=( const string_proxy<STRSXP,SP>& other) const {
			return strcmp( begin(), other.begin() ) != 0 ;
		}

        bool operator==( SEXP other ) const {
            return get() == other;
        }

        bool operator!=( SEXP other ) const {
            return get() != other;
        }

	private:
		static std::string buffer ;

	} ;

	template <int RT>
	bool operator<( const string_proxy<RT>& lhs, const string_proxy<RT>& rhs) {
		return strcmp(
			const_cast<char *>(lhs.begin() ),
			const_cast<char *>(rhs.begin())
			) < 0 ;
	}

	template <int RT>
	bool operator>( const string_proxy<RT>& lhs, const string_proxy<RT>& rhs) {
		return strcmp(
			const_cast<char *>(lhs.begin() ),
			const_cast<char *>(rhs.begin())
			) > 0 ;
	}

	template <int RT>
	bool operator>=( const string_proxy<RT>& lhs, const string_proxy<RT>& rhs) {
		return strcmp(
			const_cast<char *>(lhs.begin() ),
			const_cast<char *>(rhs.begin())
			) >= 0 ;
	}

	template <int RT>
	bool operator<=( const string_proxy<RT>& lhs, const string_proxy<RT>& rhs) {
		return strcmp(
			const_cast<char *>(lhs.begin() ),
			const_cast<char *>(rhs.begin())
			) <= 0 ;
	}

	template<int RTYPE, template <class> class StoragePolicy> std::string string_proxy<RTYPE, StoragePolicy>::buffer ;

	inline std::ostream& operator<<(std::ostream& os, const string_proxy<STRSXP>& proxy) {
	    os << static_cast<const char*>(proxy) ;
	    return os;
	}

	inline std::string operator+( const std::string& x, const string_proxy<STRSXP>& y ){
		return x + static_cast<const char*>(y) ;
	}

}
}

#endif