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
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/
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
15 * The Original Code is mozilla.org code.
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.
23 * Scott Collins <scc@mozilla.org>
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.
37 * ***** END LICENSE BLOCK ***** */
39 #ifndef nsStdStringWrapper_h___
40 #define nsStdStringWrapper_h___
44 #ifndef nsAString_h___
45 #include "nsAString.h"
50 class nsStringAllocator
51 : public std::allocator
<T
> // temporarily
57 template < class CharT
, class TraitsT
= nsCharTraits
<CharT
>, class AllocatorT
= nsStringAllocator
<CharT
> >
58 class basic_nsStdStringWrapper
59 : public basic_nsAString
<CharT
>
65 std::basic_string
<CharT
, TraitsT
, AllocatorT
> mRawString
;
67 typedef std::basic_string
<CharT
, TraitsT
, AllocatorT
> basic_string_t
;
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
;
83 static const size_type npos
= size_type(-1);
86 virtual const void* Implementation() const;
88 virtual const CharT
* GetReadableFragment( nsReadableFragment
<CharT
>&, nsFragmentRequest
, PRUint32
) const;
89 virtual CharT
* GetWritableFragment( nsWritableFragment
<CharT
>&, nsFragmentRequest
, PRUint32
);
92 basic_nsStdStringWrapper() { }
96 basic_nsStdStringWrapper( const AllocatorT
& a
= AllocatorT() )
103 basic_nsStdStringWrapper( const basic_nsAString
<CharT
>& str
)
110 basic_nsStdStringWrapper( const basic_string_t
& str
, size_type pos
= 0, size_type n
= npos
)
111 : mRawString(str
, pos
, n
)
115 basic_nsStdStringWrapper( const basic_string_t
& str
, size_type pos
, size_type n
, const AllocatorT
& a
)
116 : mRawString(str
, pos
, n
, a
)
121 basic_nsStdStringWrapper( const CharT
* s
, size_type n
, const AllocatorT
& a
= AllocatorT() )
122 : mRawString(s
, n
, a
)
127 basic_nsStdStringWrapper( const CharT
* s
, const AllocatorT
& a
= AllocatorT() )
133 basic_nsStdStringWrapper( size_type n
, CharT c
, const AllocatorT
& a
= AllocatorT() )
134 : mRawString(n
, c
, a
)
143 return mRawString
.length();
148 SetCapacity( PRUint32 aNewCapacity
)
150 mRawString
.reserve(aNewCapacity
);
155 SetLength( PRUint32 aNewLength
)
157 mRawString
.resize(aNewLength
);
161 virtual void do_AssignFromReadable( const basic_nsAString
<CharT
>& );
166 NS_DEF_TEMPLATE_STRING_COMPARISON_OPERATORS(basic_nsStdStringWrapper
<CharT
>, CharT
)
170 template <class CharT
, class TraitsT
, class AllocatorT
>
172 basic_nsStdStringWrapper
<CharT
, TraitsT
, AllocatorT
>::Implementation() const
174 static const char* implementation
= "nsStdStringWrapper";
175 return implementation
;
179 template <class CharT
, class TraitsT
, class AllocatorT
>
181 basic_nsStdStringWrapper
<CharT
, TraitsT
, AllocatorT
>::GetReadableFragment( nsReadableFragment
<CharT
>& aFragment
, nsFragmentRequest aRequest
, PRUint32 aOffset
) const
188 aFragment
.mEnd
= (aFragment
.mStart
= mRawString
.data()) + mRawString
.length();
189 return aFragment
.mStart
+ aOffset
;
198 template <class CharT
, class TraitsT
, class AllocatorT
>
200 basic_nsStdStringWrapper
<CharT
, TraitsT
, AllocatorT
>::GetWritableFragment( nsWritableFragment
<CharT
>& aFragment
, nsFragmentRequest aRequest
, PRUint32 aOffset
)
207 aFragment
.mEnd
= (aFragment
.mStart
= const_cast<CharT
*>(mRawString
.data())) + mRawString
.length();
208 return aFragment
.mStart
+ aOffset
;
217 template <class CharT
, class TraitsT
, class AllocatorT
>
219 basic_nsStdStringWrapper
<CharT
, TraitsT
, AllocatorT
>::do_AssignFromReadable( const basic_nsAString
<CharT
>& rhs
)
221 typedef basic_nsStdStringWrapper
<CharT
, TraitsT
, AllocatorT
> this_t
;
223 if ( SameImplementation(*this, rhs
) )
224 mRawString
= static_cast<this_t
>(rhs
).mRawString
;
226 basic_nsAString
<CharT
>::do_AssignFromReadable(rhs
);
230 typedef basic_nsStdStringWrapper
<PRUnichar
> nsStdString
;
231 typedef basic_nsStdStringWrapper
<char> nsStdCString
;
234 #endif // !defined(nsStdStringWrapper_h___)