当前位置:   article > 正文

《STL源码剖析》-- stl_construct.h

《STL源码剖析》-- stl_construct.h
  1. // Filename: stl_construct.h
  2. // Comment By: 凝霜
  3. // E-mail: mdl2009@vip.qq.com
  4. // Blog: http://blog.csdn.net/mdl13412
  5. /*
  6. *
  7. * Copyright (c) 1994
  8. * Hewlett-Packard Company
  9. *
  10. * Permission to use, copy, modify, distribute and sell this software
  11. * and its documentation for any purpose is hereby granted without fee,
  12. * provided that the above copyright notice appear in all copies and
  13. * that both that copyright notice and this permission notice appear
  14. * in supporting documentation. Hewlett-Packard Company makes no
  15. * representations about the suitability of this software for any
  16. * purpose. It is provided "as is" without express or implied warranty.
  17. *
  18. *
  19. * Copyright (c) 1996,1997
  20. * Silicon Graphics Computer Systems, Inc.
  21. *
  22. * Permission to use, copy, modify, distribute and sell this software
  23. * and its documentation for any purpose is hereby granted without fee,
  24. * provided that the above copyright notice appear in all copies and
  25. * that both that copyright notice and this permission notice appear
  26. * in supporting documentation. Silicon Graphics makes no
  27. * representations about the suitability of this software for any
  28. * purpose. It is provided "as is" without express or implied warranty.
  29. */
  30. /* NOTE: This is an internal header file, included by other STL headers.
  31. * You should not attempt to use it directly.
  32. */
  33. #ifndef __SGI_STL_INTERNAL_CONSTRUCT_H
  34. #define __SGI_STL_INTERNAL_CONSTRUCT_H
  35. #include <new.h> // 需要placement new的原型
  36. __STL_BEGIN_NAMESPACE
  37. // 调用成员的析构函数, 需要类型具有non-trivial destructor
  38. template <class T>
  39. inline void destroy(T* pointer)
  40. {
  41. pointer->~T();
  42. }
  43. // 使用placement new在已经分配的内存上构造对象
  44. // 如果你不熟悉placement new, 请参考
  45. // http://msdn.microsoft.com/en-us/library/kewsb8ba.aspx
  46. // http://blogs.msdn.com/b/jaredpar/archive/
  47. // 2007/10/16/c-new-operator-and-placement-new.aspx
  48. template <class T1, class T2>
  49. inline void construct(T1* p, const T2& value)
  50. {
  51. new (p) T1(value);
  52. }
  53. // 析构一组对象, 用于具有non-trivial destructor
  54. template <class ForwardIterator>
  55. inline void
  56. __destroy_aux(ForwardIterator first, ForwardIterator last, __false_type)
  57. {
  58. for ( ; first < last; ++first)
  59. destroy(&*first);
  60. }
  61. // 如果没有类型non-trivial destructor, 则使用此函数
  62. template <class ForwardIterator>
  63. inline void __destroy_aux(ForwardIterator, ForwardIterator, __true_type) {}
  64. // 使用traits技术, 判断类型是否就有non-trivial destructor, 然后调用不同的函数
  65. template <class ForwardIterator, class T>
  66. inline void __destroy(ForwardIterator first, ForwardIterator last, T*)
  67. {
  68. typedef typename __type_traits<T>::has_trivial_destructor trivial_destructor;
  69. __destroy_aux(first, last, trivial_destructor());
  70. }
  71. // 用于销毁一组对象
  72. // char *特化版本
  73. // ---------- destroy不进行析构
  74. // |
  75. // destroy(first, last) -------------------------- 特化
  76. // | |
  77. // | 泛化 ----------- destroy不进行析构
  78. // | wchar_t *特化版本
  79. // ↓
  80. // 调用 __destroy(first, last, value_type(first));
  81. // 根据是否具有trivial destructor进行函数转发
  82. // |
  83. // |---------------- has trivial destructor?
  84. // |
  85. // -------------------------------------------
  86. // No | | Yes
  87. // | |
  88. // ↓ ↓
  89. // __destroy_aux(..., __true_type) __destroy_aux(..., __false_type)
  90. // 不进需要行析构操作 for ( ; first < last; ++first)
  91. // destroy(&*first);
  92. template <class ForwardIterator>
  93. inline void destroy(ForwardIterator first, ForwardIterator last)
  94. {
  95. __destroy(first, last, value_type(first));
  96. }
  97. // 特化版本
  98. inline void destroy(char*, char*) {}
  99. inline void destroy(wchar_t*, wchar_t*) {}
  100. __STL_END_NAMESPACE
  101. #endif /* __SGI_STL_INTERNAL_CONSTRUCT_H */
  102. // Local Variables:
  103. // mode:C++
  104. // End:

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号