1 /*
2  * Copyright (c) 2004
3  * Francois Dumont
4  *
5  * This material is provided "as is", with absolutely no warranty expressed
6  * or implied. Any use is at your own risk.
7  *
8  * Permission to use or copy this software for any purpose is hereby granted
9  * without fee, provided the above notices are retained on all copies.
10  * Permission to modify the code and to distribute modified code is granted,
11  * provided the above notices are retained, and a notice that the code was
12  * modified is included with the above copyright notice.
13  *
14  */
15 
16  /*
17   * This is an internal string for the STLport own iostream implementation.
18   * The only diference rely on the allocator used to instanciate the basic_string.
19   * Its goals is to improve performance limitating the number of dynamic allocation
20   * that could occur when requesting a big float ouput for instance. This allocator
21   * is not standard conformant as it has an internal state (the static buffer)
22   */
23 
24 
25 #ifndef _STLP_INTERNAL_IOSTREAM_STRING_H
26 #define _STLP_INTERNAL_IOSTREAM_STRING_H
27 
28 #ifndef _STLP_INTERNAL_ALLOC_H
29 #  include <stl/_alloc.h>
30 #endif /* _STLP_INTERNAL_ALLOC_H */
31 
32 #ifndef _STLP_INTERNAL_STRING_H
33 #  include <stl/_string.h>
34 #endif /* _STLP_INTERNAL_STRING_H */
35 
36 _STLP_BEGIN_NAMESPACE
37 
38 _STLP_MOVE_TO_PRIV_NAMESPACE
39 
40 template <class _CharT>
41 class __iostring_allocator : public allocator<_CharT> {
42 public:
43   enum { _STR_SIZE = 256 };
44 
45 private:
46   enum { _BUF_SIZE = _STR_SIZE + 1 };
47   typedef allocator<_CharT> _Base;
48   _CharT _M_static_buf[_BUF_SIZE];
49 
50 public:
51   typedef typename _Base::size_type size_type;
52   typedef typename _Base::pointer pointer;
53 #if defined (_STLP_MEMBER_TEMPLATE_CLASSES)
54   template <class _Tp1> struct rebind {
55 #  if !defined (_STLP_MSVC) || (_STLP_MSVC >= 1300)
56     typedef __iostring_allocator<_Tp1> other;
57 #  else
58     typedef _STLP_PRIV __iostring_allocator<_Tp1> other;
59 #  endif
60   };
61 #endif
62 
63   _CharT* allocate(size_type __n, const void* __ptr = 0) {
64     if (__n > _BUF_SIZE) {
65       return _Base::allocate(__n, __ptr);
66     }
67     return _M_static_buf;
68   }
deallocate(pointer __p,size_type __n)69   void deallocate(pointer __p, size_type __n) {
70     if (__p != _M_static_buf) _Base::deallocate(__p, __n);
71   }
72 };
73 
74 #if defined (_STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE) || !defined (_STLP_MEMBER_TEMPLATES)
75 /*
76  * As the __iostring_allocator allocator will only be used in the basic_string implementation
77  * we known that it is never going to be bound to another type that the one used to instantiate
78  * the basic_string. This is why the associated __stl_alloc_rebind has only one template
79  * parameter.
80  */
81 _STLP_MOVE_TO_STD_NAMESPACE
82 
83 template <class _Tp>
84 inline _STLP_PRIV __iostring_allocator<_Tp>& _STLP_CALL
__stl_alloc_rebind(_STLP_PRIV __iostring_allocator<_Tp> & __a,const _Tp *)85 __stl_alloc_rebind(_STLP_PRIV __iostring_allocator<_Tp>& __a, const _Tp*)
86 { return __a; }
87 template <class _Tp>
88 inline _STLP_PRIV __iostring_allocator<_Tp> _STLP_CALL
__stl_alloc_create(const _STLP_PRIV __iostring_allocator<_Tp> &,const _Tp *)89 __stl_alloc_create(const _STLP_PRIV __iostring_allocator<_Tp>&, const _Tp*)
90 { return _STLP_PRIV __iostring_allocator<_Tp>(); }
91 
92 _STLP_MOVE_TO_PRIV_NAMESPACE
93 #endif /* _STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE */
94 
95 #if !defined (_STLP_DEBUG)
96 template <class _CharT>
97 struct __basic_iostring : public basic_string<_CharT, char_traits<_CharT>, __iostring_allocator<_CharT> > {
98   /*
99    * A consequence of the non standard conformant allocator is that a string using it
100    * must always be presized to the allocator static buffer size because the basic_string implementation
101    * do not manage an allocator returning always the same memory address as long as the
102    * requested memory block size is under a certain value.
103    */
104   typedef __basic_iostring<_CharT> _Self;
105   typedef basic_string<_CharT, char_traits<_CharT>, __iostring_allocator<_CharT> > _Base;
106   typedef typename _Base::_Reserve_t _Reserve_t;
107 
__basic_iostring__basic_iostring108   __basic_iostring() : _Base(_Reserve_t(), __iostring_allocator<_CharT>::_STR_SIZE)
109   {}
110 
111   _Self& operator=(const _CharT* __s) {
112     _Base::operator=(__s);
113     return *this;
114   }
115 };
116 
117 typedef __basic_iostring<char> __iostring;
118 
119 #  if !defined (_STLP_NO_WCHAR_T)
120 typedef __basic_iostring<wchar_t> __iowstring;
121 #  endif
122 
123 #  define _STLP_BASIC_IOSTRING(_CharT) _STLP_PRIV __basic_iostring<_CharT>
124 
125 #else
126 
127 typedef string __iostring;
128 #  if !defined (_STLP_NO_WCHAR_T)
129 typedef wstring __iowstring;
130 #  endif
131 
132 #  define _STLP_BASIC_IOSTRING(_CharT) basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
133 
134 #endif
135 
136 _STLP_MOVE_TO_STD_NAMESPACE
137 
138 _STLP_END_NAMESPACE
139 
140 #endif /* _STLP_INTERNAL_IOSTREAM_STRING_H */
141