_iostream_string.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. * This is an internal string for the STLport own iostream implementation.
  17. * The only diference rely on the allocator used to instanciate the basic_string.
  18. * Its goals is to improve performance limitating the number of dynamic allocation
  19. * that could occur when requesting a big float ouput for instance. This allocator
  20. * is not standard conformant as it has an internal state (the static buffer)
  21. */
  22. #ifndef _STLP_INTERNAL_IOSTREAM_STRING_H
  23. #define _STLP_INTERNAL_IOSTREAM_STRING_H
  24. #ifndef _STLP_INTERNAL_ALLOC_H
  25. # include <stl/_alloc.h>
  26. #endif /* _STLP_INTERNAL_ALLOC_H */
  27. #ifndef _STLP_INTERNAL_STRING_H
  28. # include <stl/_string.h>
  29. #endif /* _STLP_INTERNAL_STRING_H */
  30. _STLP_BEGIN_NAMESPACE
  31. _STLP_MOVE_TO_PRIV_NAMESPACE
  32. template <class _CharT>
  33. class __iostring_allocator : public allocator<_CharT> {
  34. public:
  35. enum { _STR_SIZE = 256 };
  36. private:
  37. enum { _BUF_SIZE = _STR_SIZE + 1 };
  38. typedef allocator<_CharT> _Base;
  39. _CharT _M_static_buf[_BUF_SIZE];
  40. public:
  41. typedef typename _Base::size_type size_type;
  42. typedef typename _Base::pointer pointer;
  43. #if defined (_STLP_MEMBER_TEMPLATE_CLASSES)
  44. template <class _Tp1> struct rebind {
  45. # if !defined (_STLP_MSVC) || (_STLP_MSVC >= 1300)
  46. typedef __iostring_allocator<_Tp1> other;
  47. # else
  48. typedef _STLP_PRIV __iostring_allocator<_Tp1> other;
  49. # endif
  50. };
  51. #endif
  52. _CharT* allocate(size_type __n, const void* __ptr = 0) {
  53. if (__n > _BUF_SIZE) {
  54. return _Base::allocate(__n, __ptr);
  55. }
  56. return _M_static_buf;
  57. }
  58. void deallocate(pointer __p, size_type __n) {
  59. if (__p != _M_static_buf) _Base::deallocate(__p, __n);
  60. }
  61. };
  62. #if defined (_STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE) || !defined (_STLP_MEMBER_TEMPLATES)
  63. /*
  64. * As the __iostring_allocator allocator will only be used in the basic_string implementation
  65. * we known that it is never going to be bound to another type that the one used to instantiate
  66. * the basic_string. This is why the associated __stl_alloc_rebind has only one template
  67. * parameter.
  68. */
  69. _STLP_MOVE_TO_STD_NAMESPACE
  70. template <class _Tp>
  71. inline _STLP_PRIV __iostring_allocator<_Tp>& _STLP_CALL
  72. __stl_alloc_rebind(_STLP_PRIV __iostring_allocator<_Tp>& __a, const _Tp*)
  73. { return __a; }
  74. template <class _Tp>
  75. inline _STLP_PRIV __iostring_allocator<_Tp> _STLP_CALL
  76. __stl_alloc_create(const _STLP_PRIV __iostring_allocator<_Tp>&, const _Tp*)
  77. { return _STLP_PRIV __iostring_allocator<_Tp>(); }
  78. _STLP_MOVE_TO_PRIV_NAMESPACE
  79. #endif /* _STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE */
  80. #if !defined (_STLP_DEBUG)
  81. template <class _CharT>
  82. struct __basic_iostring : public basic_string<_CharT, char_traits<_CharT>, __iostring_allocator<_CharT> > {
  83. /*
  84. * A consequence of the non standard conformant allocator is that a string using it
  85. * must always be presized to the allocator static buffer size because the basic_string implementation
  86. * do not manage an allocator returning always the same memory adress as long as the
  87. * requested memory block size is under a certain value.
  88. */
  89. typedef __basic_iostring<_CharT> _Self;
  90. typedef basic_string<_CharT, char_traits<_CharT>, __iostring_allocator<_CharT> > _Base;
  91. typedef typename _Base::_Reserve_t _Reserve_t;
  92. __basic_iostring() : _Base(_Reserve_t(), __iostring_allocator<_CharT>::_STR_SIZE)
  93. {}
  94. _Self& operator=(const _CharT* __s) {
  95. _Base::operator=(__s);
  96. return *this;
  97. }
  98. };
  99. typedef __basic_iostring<char> __iostring;
  100. # if !defined (_STLP_NO_WCHAR_T)
  101. typedef __basic_iostring<wchar_t> __iowstring;
  102. # endif
  103. # define _STLP_BASIC_IOSTRING(_CharT) _STLP_PRIV __basic_iostring<_CharT>
  104. #else
  105. typedef string __iostring;
  106. # if !defined (_STLP_NO_WCHAR_T)
  107. typedef wstring __iowstring;
  108. # endif
  109. # define _STLP_BASIC_IOSTRING(_CharT) basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
  110. #endif
  111. _STLP_MOVE_TO_STD_NAMESPACE
  112. _STLP_END_NAMESPACE
  113. #endif /* _STLP_INTERNAL_IOSTREAM_STRING_H */