Back to index

lightning-sunbird  0.9+nobinonly
nsStdStringWrapper.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Scott Collins <scc@mozilla.org>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #ifndef nsStdStringWrapper_h___
00040 #define nsStdStringWrapper_h___
00041 
00042 #include <string>
00043 
00044 #ifndef nsAString_h___
00045 #include "nsAString.h"
00046 #endif
00047 
00048 
00049 template <class T>
00050 class nsStringAllocator
00051      : public std::allocator<T> // temporarily
00052   {
00053     // ...
00054   };
00055 
00056 
00057 template < class CharT, class TraitsT = nsCharTraits<CharT>, class AllocatorT = nsStringAllocator<CharT> >
00058 class basic_nsStdStringWrapper
00059       : public basic_nsAString<CharT>
00060     /*
00061       ...
00062     */
00063   {
00064     protected:
00065       std::basic_string<CharT, TraitsT, AllocatorT> mRawString;
00066 
00067     typedef std::basic_string<CharT, TraitsT, AllocatorT> basic_string_t;
00068 
00069     using typename basic_string_t::traits_type;
00070     using typename basic_string_t::value_type;
00071     using typename basic_string_t::allocator_type;
00072     using typename basic_string_t::size_type;
00073     using typename basic_string_t::difference_type;
00074     using typename basic_string_t::reference;
00075     using typename basic_string_t::const_reference;
00076     using typename basic_string_t::pointer;
00077     using typename basic_string_t::const_pointer;
00078     using typename basic_string_t::iterator;
00079     using typename basic_string_t::const_iterator;
00080     using typename basic_string_t::reverse_iterator;
00081     using typename basic_string_t::const_reverse_iterator;
00082 
00083        static const size_type npos = size_type(-1);
00084 
00085     protected:
00086       virtual const void* Implementation() const;
00087 
00088       virtual const CharT* GetReadableFragment( nsReadableFragment<CharT>&, nsFragmentRequest, PRUint32 ) const;
00089       virtual CharT* GetWritableFragment( nsWritableFragment<CharT>&, nsFragmentRequest, PRUint32 );
00090 
00091     public:
00092       basic_nsStdStringWrapper() { }
00093 
00094 #if 0
00095        explicit
00096        basic_nsStdStringWrapper( const AllocatorT& a = AllocatorT() )
00097            : mRawString(a)
00098          {
00099          }
00100 #endif
00101 
00102       explicit
00103       basic_nsStdStringWrapper( const basic_nsAString<CharT>& str )
00104         {
00105           Assign(str);
00106         }
00107 
00108 #if 0
00109       explicit
00110        basic_nsStdStringWrapper( const basic_string_t& str, size_type pos = 0, size_type n = npos )
00111            : mRawString(str, pos, n)
00112          {
00113          }
00114 
00115        basic_nsStdStringWrapper( const basic_string_t& str, size_type pos, size_type n, const AllocatorT& a )
00116            : mRawString(str, pos, n, a)
00117          {
00118          }
00119 #endif
00120 
00121        basic_nsStdStringWrapper( const CharT* s, size_type n, const AllocatorT& a = AllocatorT() )
00122            : mRawString(s, n, a)
00123          {
00124          }
00125 
00126       explicit
00127        basic_nsStdStringWrapper( const CharT* s, const AllocatorT& a = AllocatorT() )
00128            : mRawString(s, a)
00129          {
00130          }
00131 
00132 #if 0
00133        basic_nsStdStringWrapper( size_type n, CharT c, const AllocatorT& a = AllocatorT() )
00134            : mRawString(n, c, a)
00135          {
00136          }
00137 #endif
00138 
00139       virtual
00140       PRUint32
00141       Length() const
00142         {
00143           return mRawString.length();
00144         }
00145 
00146       virtual
00147       void
00148       SetCapacity( PRUint32 aNewCapacity )
00149         {
00150           mRawString.reserve(aNewCapacity);
00151         }
00152 
00153       virtual
00154       void
00155       SetLength( PRUint32 aNewLength )
00156         {
00157           mRawString.resize(aNewLength);
00158         }
00159 
00160     protected:
00161       virtual void do_AssignFromReadable( const basic_nsAString<CharT>& );
00162 
00163     // ...
00164   };
00165 
00166 NS_DEF_TEMPLATE_STRING_COMPARISON_OPERATORS(basic_nsStdStringWrapper<CharT>, CharT)
00167 
00168 
00169 
00170 template <class CharT, class TraitsT, class AllocatorT>
00171 const void*
00172 basic_nsStdStringWrapper<CharT, TraitsT, AllocatorT>::Implementation() const
00173   {
00174     static const char* implementation = "nsStdStringWrapper";
00175     return implementation;
00176   }
00177 
00178 
00179 template <class CharT, class TraitsT, class AllocatorT>
00180 const CharT*
00181 basic_nsStdStringWrapper<CharT, TraitsT, AllocatorT>::GetReadableFragment( nsReadableFragment<CharT>& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) const
00182   {
00183     switch ( aRequest )
00184       {
00185         case kFirstFragment:
00186         case kLastFragment:
00187         case kFragmentAt:
00188           aFragment.mEnd = (aFragment.mStart = mRawString.data()) + mRawString.length();
00189           return aFragment.mStart + aOffset;
00190         
00191         case kPrevFragment:
00192         case kNextFragment:
00193         default:
00194           return 0;
00195       }
00196   }
00197 
00198 template <class CharT, class TraitsT, class AllocatorT>
00199 CharT*
00200 basic_nsStdStringWrapper<CharT, TraitsT, AllocatorT>::GetWritableFragment( nsWritableFragment<CharT>& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset )
00201   {
00202     switch ( aRequest )
00203       {
00204         case kFirstFragment:
00205         case kLastFragment:
00206         case kFragmentAt:
00207           aFragment.mEnd = (aFragment.mStart = NS_CONST_CAST(CharT*, mRawString.data())) + mRawString.length();
00208           return aFragment.mStart + aOffset;
00209         
00210         case kPrevFragment:
00211         case kNextFragment:
00212         default:
00213           return 0;
00214       }
00215   }
00216 
00217 template <class CharT, class TraitsT, class AllocatorT>
00218 void
00219 basic_nsStdStringWrapper<CharT, TraitsT, AllocatorT>::do_AssignFromReadable( const basic_nsAString<CharT>& rhs )
00220   {
00221     typedef basic_nsStdStringWrapper<CharT, TraitsT, AllocatorT> this_t;
00222 
00223     if ( SameImplementation(*this, rhs) )
00224       mRawString = NS_STATIC_CAST(this_t, rhs).mRawString;
00225     else
00226       basic_nsAString<CharT>::do_AssignFromReadable(rhs);
00227   }
00228 
00229 
00230 typedef basic_nsStdStringWrapper<PRUnichar> nsStdString;
00231 typedef basic_nsStdStringWrapper<char>      nsStdCString;
00232 
00233 
00234 #endif // !defined(nsStdStringWrapper_h___)