Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
3.7 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) Microsoft Corporation
  4. //
  5. // SYNOPSIS
  6. //
  7. // Declares the class StdAllocator and the typedefs StdString and StdWString.
  8. //
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef STDSTRING_H
  11. #define STDSTRING_H
  12. #pragma once
  13. #include <cstddef>
  14. #include <string>
  15. // Implements a standard library conformant allocator that uses the run-time's
  16. // private heap for allocations.
  17. template <class T>
  18. class StdAllocator
  19. {
  20. public:
  21. typedef size_t size_type;
  22. typedef ptrdiff_t difference_type;
  23. typedef T* pointer;
  24. typedef const T* const_pointer;
  25. typedef T& reference;
  26. typedef const T& const_reference;
  27. typedef T value_type;
  28. template <class U>
  29. struct rebind
  30. {
  31. typedef StdAllocator<U> other;
  32. };
  33. StdAllocator() throw ();
  34. // Lint is not smart enough to recognize copy constructors for template
  35. //lint -esym(1931, StdAllocator<*>::StdAllocator*)
  36. StdAllocator(const StdAllocator&) throw ()
  37. {
  38. }
  39. template <class U>
  40. StdAllocator(const StdAllocator<U>&) throw ()
  41. {
  42. }
  43. ~StdAllocator() throw ();
  44. pointer address(reference x) const throw ();
  45. const_pointer address(const_reference x) const throw ();
  46. pointer allocate(size_type n, const void* hint = 0);
  47. void deallocate(pointer p, size_type n) throw ();
  48. size_type max_size() const throw ();
  49. void construct(pointer p, const T& val);
  50. void destroy(pointer p) throw ();
  51. // Non-standard member required by Microsoft's implementation.
  52. bool operator==(const StdAllocator& rhs) const throw ();
  53. };
  54. // Replacement for std::string.
  55. typedef std::basic_string<
  56. char,
  57. std::char_traits<char>,
  58. StdAllocator<char>
  59. > StdString;
  60. // Replacement for std::wstring.
  61. typedef std::basic_string<
  62. wchar_t,
  63. std::char_traits<wchar_t>,
  64. StdAllocator<wchar_t>
  65. > StdWString;
  66. template <class T>
  67. inline StdAllocator<T>::StdAllocator() throw ()
  68. {
  69. }
  70. template <class T>
  71. inline StdAllocator<T>::~StdAllocator() throw ()
  72. {
  73. }
  74. template <class T>
  75. inline __TYPENAME StdAllocator<T>::pointer StdAllocator<T>::address(
  76. reference x
  77. ) const throw ()
  78. {
  79. return &x;
  80. }
  81. template <class T>
  82. inline __TYPENAME StdAllocator<T>::const_pointer StdAllocator<T>::address(
  83. const_reference x
  84. ) const throw ()
  85. {
  86. return &x;
  87. }
  88. template <class T>
  89. inline __TYPENAME StdAllocator<T>::pointer StdAllocator<T>::allocate(
  90. size_type n,
  91. const void*
  92. )
  93. {
  94. return static_cast<pointer>(operator new (n * sizeof(T)));
  95. }
  96. template <class T>
  97. inline void StdAllocator<T>::deallocate(pointer p, size_type) throw ()
  98. {
  99. operator delete(p);
  100. }
  101. template <class T>
  102. inline __TYPENAME StdAllocator<T>::size_type StdAllocator<T>::max_size() const throw ()
  103. {
  104. // Max size supported by the NT heap.
  105. return MAXINT_PTR;
  106. }
  107. template <class T>
  108. inline void StdAllocator<T>::construct(pointer p, const T& val)
  109. {
  110. new (p) T(val);
  111. }
  112. template <class T>
  113. inline void StdAllocator<T>::destroy(pointer p) throw ()
  114. {
  115. p->~T();
  116. }
  117. template <class T>
  118. inline bool StdAllocator<T>::operator==(const StdAllocator&) const throw ()
  119. {
  120. return true;
  121. }
  122. #endif // STDSTRING_H