VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/tests/StringFactoringTests/nsStdStringWrapper.h@ 6542

Last change on this file since 6542 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.0 KB
Line 
1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is mozilla.org code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 * Scott Collins <[email protected]>
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38
39#ifndef nsStdStringWrapper_h___
40#define nsStdStringWrapper_h___
41
42#include <string>
43
44#ifndef nsAString_h___
45#include "nsAString.h"
46#endif
47
48
49template <class T>
50class nsStringAllocator
51 : public std::allocator<T> // temporarily
52 {
53 // ...
54 };
55
56
57template < class CharT, class TraitsT = nsCharTraits<CharT>, class AllocatorT = nsStringAllocator<CharT> >
58class basic_nsStdStringWrapper
59 : public basic_nsAString<CharT>
60 /*
61 ...
62 */
63 {
64 protected:
65 std::basic_string<CharT, TraitsT, AllocatorT> mRawString;
66
67 typedef std::basic_string<CharT, TraitsT, AllocatorT> basic_string_t;
68
69 using typename basic_string_t::traits_type;
70 using typename basic_string_t::value_type;
71 using typename basic_string_t::allocator_type;
72 using typename basic_string_t::size_type;
73 using typename basic_string_t::difference_type;
74 using typename basic_string_t::reference;
75 using typename basic_string_t::const_reference;
76 using typename basic_string_t::pointer;
77 using typename basic_string_t::const_pointer;
78 using typename basic_string_t::iterator;
79 using typename basic_string_t::const_iterator;
80 using typename basic_string_t::reverse_iterator;
81 using typename basic_string_t::const_reverse_iterator;
82
83 static const size_type npos = size_type(-1);
84
85 protected:
86 virtual const void* Implementation() const;
87
88 virtual const CharT* GetReadableFragment( nsReadableFragment<CharT>&, nsFragmentRequest, PRUint32 ) const;
89 virtual CharT* GetWritableFragment( nsWritableFragment<CharT>&, nsFragmentRequest, PRUint32 );
90
91 public:
92 basic_nsStdStringWrapper() { }
93
94#if 0
95 explicit
96 basic_nsStdStringWrapper( const AllocatorT& a = AllocatorT() )
97 : mRawString(a)
98 {
99 }
100#endif
101
102 explicit
103 basic_nsStdStringWrapper( const basic_nsAString<CharT>& str )
104 {
105 Assign(str);
106 }
107
108#if 0
109 explicit
110 basic_nsStdStringWrapper( const basic_string_t& str, size_type pos = 0, size_type n = npos )
111 : mRawString(str, pos, n)
112 {
113 }
114
115 basic_nsStdStringWrapper( const basic_string_t& str, size_type pos, size_type n, const AllocatorT& a )
116 : mRawString(str, pos, n, a)
117 {
118 }
119#endif
120
121 basic_nsStdStringWrapper( const CharT* s, size_type n, const AllocatorT& a = AllocatorT() )
122 : mRawString(s, n, a)
123 {
124 }
125
126 explicit
127 basic_nsStdStringWrapper( const CharT* s, const AllocatorT& a = AllocatorT() )
128 : mRawString(s, a)
129 {
130 }
131
132#if 0
133 basic_nsStdStringWrapper( size_type n, CharT c, const AllocatorT& a = AllocatorT() )
134 : mRawString(n, c, a)
135 {
136 }
137#endif
138
139 virtual
140 PRUint32
141 Length() const
142 {
143 return mRawString.length();
144 }
145
146 virtual
147 void
148 SetCapacity( PRUint32 aNewCapacity )
149 {
150 mRawString.reserve(aNewCapacity);
151 }
152
153 virtual
154 void
155 SetLength( PRUint32 aNewLength )
156 {
157 mRawString.resize(aNewLength);
158 }
159
160 protected:
161 virtual void do_AssignFromReadable( const basic_nsAString<CharT>& );
162
163 // ...
164 };
165
166NS_DEF_TEMPLATE_STRING_COMPARISON_OPERATORS(basic_nsStdStringWrapper<CharT>, CharT)
167
168
169
170template <class CharT, class TraitsT, class AllocatorT>
171const void*
172basic_nsStdStringWrapper<CharT, TraitsT, AllocatorT>::Implementation() const
173 {
174 static const char* implementation = "nsStdStringWrapper";
175 return implementation;
176 }
177
178
179template <class CharT, class TraitsT, class AllocatorT>
180const CharT*
181basic_nsStdStringWrapper<CharT, TraitsT, AllocatorT>::GetReadableFragment( nsReadableFragment<CharT>& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) const
182 {
183 switch ( aRequest )
184 {
185 case kFirstFragment:
186 case kLastFragment:
187 case kFragmentAt:
188 aFragment.mEnd = (aFragment.mStart = mRawString.data()) + mRawString.length();
189 return aFragment.mStart + aOffset;
190
191 case kPrevFragment:
192 case kNextFragment:
193 default:
194 return 0;
195 }
196 }
197
198template <class CharT, class TraitsT, class AllocatorT>
199CharT*
200basic_nsStdStringWrapper<CharT, TraitsT, AllocatorT>::GetWritableFragment( nsWritableFragment<CharT>& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset )
201 {
202 switch ( aRequest )
203 {
204 case kFirstFragment:
205 case kLastFragment:
206 case kFragmentAt:
207 aFragment.mEnd = (aFragment.mStart = NS_CONST_CAST(CharT*, mRawString.data())) + mRawString.length();
208 return aFragment.mStart + aOffset;
209
210 case kPrevFragment:
211 case kNextFragment:
212 default:
213 return 0;
214 }
215 }
216
217template <class CharT, class TraitsT, class AllocatorT>
218void
219basic_nsStdStringWrapper<CharT, TraitsT, AllocatorT>::do_AssignFromReadable( const basic_nsAString<CharT>& rhs )
220 {
221 typedef basic_nsStdStringWrapper<CharT, TraitsT, AllocatorT> this_t;
222
223 if ( SameImplementation(*this, rhs) )
224 mRawString = NS_STATIC_CAST(this_t, rhs).mRawString;
225 else
226 basic_nsAString<CharT>::do_AssignFromReadable(rhs);
227 }
228
229
230typedef basic_nsStdStringWrapper<PRUnichar> nsStdString;
231typedef basic_nsStdStringWrapper<char> nsStdCString;
232
233
234#endif // !defined(nsStdStringWrapper_h___)
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette