/home/ntakagi/work/STLport-5.1.5/stlport/stl/_iostream_string.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2004
00003  * Francois Dumont
00004  *
00005  * This material is provided "as is", with absolutely no warranty expressed
00006  * or implied. Any use is at your own risk.
00007  *
00008  * Permission to use or copy this software for any purpose is hereby granted
00009  * without fee, provided the above notices are retained on all copies.
00010  * Permission to modify the code and to distribute modified code is granted,
00011  * provided the above notices are retained, and a notice that the code was
00012  * modified is included with the above copyright notice.
00013  *
00014  */
00015 
00016  /*
00017   * This is an internal string for the STLport own iostream implementation.
00018   * The only diference rely on the allocator used to instanciate the basic_string.
00019   * Its goals is to improve performance limitating the number of dynamic allocation
00020   * that could occur when requesting a big float ouput for instance. This allocator
00021   * is not standard conformant as it has an internal state (the static buffer)
00022   */
00023 
00024 
00025 #ifndef _STLP_INTERNAL_IOSTREAM_STRING_H
00026 #define _STLP_INTERNAL_IOSTREAM_STRING_H
00027 
00028 #ifndef _STLP_INTERNAL_ALLOC_H
00029 #  include <stl/_alloc.h>
00030 #endif /* _STLP_INTERNAL_ALLOC_H */
00031 
00032 #ifndef _STLP_INTERNAL_STRING_H
00033 #  include <stl/_string.h>
00034 #endif /* _STLP_INTERNAL_STRING_H */
00035 
00036 _STLP_BEGIN_NAMESPACE
00037 
00038 _STLP_MOVE_TO_PRIV_NAMESPACE
00039 
00040 template <class _CharT>
00041 class __iostring_allocator : public allocator<_CharT> {
00042 public:
00043   enum { _STR_SIZE = 256 };
00044 
00045 private:
00046   enum { _BUF_SIZE = _STR_SIZE + 1 };
00047   typedef allocator<_CharT> _Base;
00048   _CharT _M_static_buf[_BUF_SIZE];
00049 
00050 public:
00051   typedef typename _Base::size_type size_type;
00052   typedef typename _Base::pointer pointer;
00053 #if defined (_STLP_MEMBER_TEMPLATE_CLASSES)
00054   template <class _Tp1> struct rebind {
00055 #  if !defined (_STLP_MSVC) || (_STLP_MSVC >= 1300)
00056     typedef __iostring_allocator<_Tp1> other;
00057 #  else
00058     typedef _STLP_PRIV __iostring_allocator<_Tp1> other;
00059 #  endif
00060   };
00061 #endif
00062 
00063   _CharT* allocate(size_type __n, const void* __ptr = 0) {
00064     if (__n > _BUF_SIZE) {
00065       return _Base::allocate(__n, __ptr);
00066     }
00067     return _M_static_buf;
00068   }
00069   void deallocate(pointer __p, size_type __n) {
00070     if (__p != _M_static_buf) _Base::deallocate(__p, __n);
00071   }
00072 };
00073 
00074 #if defined (_STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE) || !defined (_STLP_MEMBER_TEMPLATES)
00075 /*
00076  * As the __iostring_allocator allocator will only be used in the basic_string implementation
00077  * we known that it is never going to be bound to another type that the one used to instantiate
00078  * the basic_string. This is why the associated __stl_alloc_rebind has only one template
00079  * parameter.
00080  */
00081 _STLP_MOVE_TO_STD_NAMESPACE
00082 
00083 template <class _Tp>
00084 inline _STLP_PRIV __iostring_allocator<_Tp>& _STLP_CALL
00085 __stl_alloc_rebind(_STLP_PRIV __iostring_allocator<_Tp>& __a, const _Tp*)
00086 { return __a; }
00087 template <class _Tp>
00088 inline _STLP_PRIV __iostring_allocator<_Tp> _STLP_CALL
00089 __stl_alloc_create(const _STLP_PRIV __iostring_allocator<_Tp>&, const _Tp*)
00090 { return _STLP_PRIV __iostring_allocator<_Tp>(); }
00091 
00092 _STLP_MOVE_TO_PRIV_NAMESPACE
00093 #endif /* _STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE */
00094 
00095 #if !defined (_STLP_DEBUG)
00096 template <class _CharT>
00097 struct __basic_iostring : public basic_string<_CharT, char_traits<_CharT>, __iostring_allocator<_CharT> > {
00098   /*
00099    * A consequence of the non standard conformant allocator is that a string using it
00100    * must always be presized to the allocator static buffer size because the basic_string implementation
00101    * do not manage an allocator returning always the same memory adress as long as the
00102    * requested memory block size is under a certain value.
00103    */
00104   typedef __basic_iostring<_CharT> _Self;
00105   typedef basic_string<_CharT, char_traits<_CharT>, __iostring_allocator<_CharT> > _Base;
00106   typedef typename _Base::_Reserve_t _Reserve_t;
00107 
00108   __basic_iostring() : _Base(_Reserve_t(), __iostring_allocator<_CharT>::_STR_SIZE)
00109   {}
00110 
00111   _Self& operator=(const _CharT* __s) {
00112     _Base::operator=(__s);
00113     return *this;
00114   }
00115 };
00116 
00117 typedef __basic_iostring<char> __iostring;
00118 
00119 #  if !defined (_STLP_NO_WCHAR_T)
00120 typedef __basic_iostring<wchar_t> __iowstring;
00121 #  endif
00122 
00123 #  define _STLP_BASIC_IOSTRING(_CharT) _STLP_PRIV __basic_iostring<_CharT>
00124 
00125 #else
00126 
00127 typedef string __iostring;
00128 #  if !defined (_STLP_NO_WCHAR_T)
00129 typedef wstring __iowstring;
00130 #  endif
00131 
00132 #  define _STLP_BASIC_IOSTRING(_CharT) basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
00133 
00134 #endif
00135 
00136 _STLP_MOVE_TO_STD_NAMESPACE
00137 
00138 _STLP_END_NAMESPACE
00139 
00140 #endif /* _STLP_INTERNAL_IOSTREAM_STRING_H */



Generated on Mon Mar 10 15:32:25 2008 by  doxygen 1.5.1