|
1 | | -/* |
2 | | - * |
3 | | - * Copyright (c) 1994 |
4 | | - * Hewlett-Packard Company |
5 | | - * |
6 | | - * Permission to use, copy, modify, distribute and sell this software |
7 | | - * and its documentation for any purpose is hereby granted without fee, |
8 | | - * provided that the above copyright notice appear in all copies and |
9 | | - * that both that copyright notice and this permission notice appear |
10 | | - * in supporting documentation. Hewlett-Packard Company makes no |
11 | | - * representations about the suitability of this software for any |
12 | | - * purpose. It is provided "as is" without express or implied warranty. |
13 | | - * |
14 | | - * |
15 | | - * Copyright (c) 1996,1997 |
16 | | - * Silicon Graphics Computer Systems, Inc. |
17 | | - * |
18 | | - * Permission to use, copy, modify, distribute and sell this software |
19 | | - * and its documentation for any purpose is hereby granted without fee, |
20 | | - * provided that the above copyright notice appear in all copies and |
21 | | - * that both that copyright notice and this permission notice appear |
22 | | - * in supporting documentation. Silicon Graphics makes no |
23 | | - * representations about the suitability of this software for any |
24 | | - * purpose. It is provided "as is" without express or implied warranty. |
25 | | - */ |
26 | | - |
27 | | -/* NOTE: This is an internal header file, included by other STL headers. |
28 | | - * You should not attempt to use it directly. |
29 | | - */ |
30 | | - |
31 | | -#ifndef __SGI_STL_INTERNAL_CONSTRUCT_H |
32 | | -#define __SGI_STL_INTERNAL_CONSTRUCT_H |
33 | | - |
34 | | -#include <new.h> // 使用placement new |
35 | | - |
36 | | -__STL_BEGIN_NAMESPACE |
37 | | - |
38 | | -// construct and destroy. These functions are not part of the C++ standard, |
39 | | -// and are provided for backward compatibility with the HP STL. We also |
40 | | -// provide internal names _Construct and _Destroy that can be used within |
41 | | -// the library, so that standard-conforming pieces don't have to rely on |
42 | | -// non-standard extensions. |
43 | | - |
44 | | -// Internal names |
45 | | - |
46 | | -template <class _T1, class _T2> |
47 | | -inline void _Construct(_T1* __p, const _T2& __value) { |
48 | | - new ((void*) __p) _T1(__value); // placement new; 唤起T1::T1(value) |
49 | | -} |
50 | | - |
51 | | -template <class _T1> |
52 | | -inline void _Construct(_T1* __p) { |
53 | | - new ((void*) __p) _T1(); |
54 | | -} |
55 | | - |
56 | | - |
57 | | -// 第一个版本,接受一个参数 |
58 | | -template <class _Tp> |
59 | | -inline void _Destroy(_Tp* __pointer) { |
60 | | - __pointer->~_Tp(); // 调用dtor ~T() |
61 | | -} |
62 | | - |
63 | | - |
64 | | - |
65 | | -// 如果元素value type 有 non-trivial destructor |
66 | | -template <class _ForwardIterator> |
67 | | -void |
68 | | -__destroy_aux(_ForwardIterator __first, _ForwardIterator __last, __false_type) |
69 | | -{ |
70 | | - for ( ; __first != __last; ++__first) |
71 | | - destroy(&*__first); |
72 | | -} |
73 | | - |
74 | | -// 如果元素value type 有 trivial destructor |
75 | | -template <class _ForwardIterator> |
76 | | -inline void __destroy_aux(_ForwardIterator, _ForwardIterator, __true_type) {} |
77 | | - |
78 | | - |
79 | | -// 判断元素value type是否有trivial destructor |
80 | | -template <class _ForwardIterator, class _Tp> |
81 | | -inline void |
82 | | -__destroy(_ForwardIterator __first, _ForwardIterator __last, _Tp*) |
83 | | -{ |
84 | | - typedef typename __type_traits<_Tp>::has_trivial_destructor |
85 | | - _Trivial_destructor; |
86 | | - __destroy_aux(__first, __last, _Trivial_destructor()); |
87 | | -} |
88 | | - |
89 | | - |
90 | | -// 第二个版本,接受两个迭代器,函数设法找出元素类别,进而利用 __type_traits<>使用最佳措施 |
91 | | -template <class _ForwardIterator> |
92 | | -inline void _Destroy(_ForwardIterator __first, _ForwardIterator __last) { |
93 | | - __destroy(__first, __last, __VALUE_TYPE(__first)); |
94 | | -} |
95 | | - |
96 | | -// destroy()特化版 |
97 | | -inline void _Destroy(char*, char*) {} |
98 | | -inline void _Destroy(int*, int*) {} |
99 | | -inline void _Destroy(long*, long*) {} |
100 | | -inline void _Destroy(float*, float*) {} |
101 | | -inline void _Destroy(double*, double*) {} |
102 | | -#ifdef __STL_HAS_WCHAR_T |
103 | | -inline void _Destroy(wchar_t*, wchar_t*) {} |
104 | | -#endif /* __STL_HAS_WCHAR_T */ |
105 | | - |
106 | | -// -------------------------------------------------- |
107 | | -// Old names from the HP STL. |
108 | | - |
109 | | -template <class _T1, class _T2> |
110 | | -inline void construct(_T1* __p, const _T2& __value) { |
111 | | - _Construct(__p, __value); |
112 | | -} |
113 | | - |
114 | | -template <class _T1> |
115 | | -inline void construct(_T1* __p) { |
116 | | - _Construct(__p); |
117 | | -} |
118 | | - |
119 | | -template <class _Tp> |
120 | | -inline void destroy(_Tp* __pointer) { |
121 | | - _Destroy(__pointer); |
122 | | -} |
123 | | - |
124 | | -template <class _ForwardIterator> |
125 | | -inline void destroy(_ForwardIterator __first, _ForwardIterator __last) { |
126 | | - _Destroy(__first, __last); |
127 | | -} |
128 | | - |
129 | | -__STL_END_NAMESPACE |
130 | | - |
131 | | -#endif /* __SGI_STL_INTERNAL_CONSTRUCT_H */ |
132 | | - |
133 | | -// Local Variables: |
134 | | -// mode:C++ |
135 | | -// End: |
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 1994 |
| 4 | + * Hewlett-Packard Company |
| 5 | + * |
| 6 | + * Permission to use, copy, modify, distribute and sell this software |
| 7 | + * and its documentation for any purpose is hereby granted without fee, |
| 8 | + * provided that the above copyright notice appear in all copies and |
| 9 | + * that both that copyright notice and this permission notice appear |
| 10 | + * in supporting documentation. Hewlett-Packard Company makes no |
| 11 | + * representations about the suitability of this software for any |
| 12 | + * purpose. It is provided "as is" without express or implied warranty. |
| 13 | + * |
| 14 | + * |
| 15 | + * Copyright (c) 1996,1997 |
| 16 | + * Silicon Graphics Computer Systems, Inc. |
| 17 | + * |
| 18 | + * Permission to use, copy, modify, distribute and sell this software |
| 19 | + * and its documentation for any purpose is hereby granted without fee, |
| 20 | + * provided that the above copyright notice appear in all copies and |
| 21 | + * that both that copyright notice and this permission notice appear |
| 22 | + * in supporting documentation. Silicon Graphics makes no |
| 23 | + * representations about the suitability of this software for any |
| 24 | + * purpose. It is provided "as is" without express or implied warranty. |
| 25 | + */ |
| 26 | + |
| 27 | +/* NOTE: This is an internal header file, included by other STL headers. |
| 28 | + * You should not attempt to use it directly. |
| 29 | + */ |
| 30 | + |
| 31 | +#ifndef __SGI_STL_INTERNAL_CONSTRUCT_H |
| 32 | +#define __SGI_STL_INTERNAL_CONSTRUCT_H |
| 33 | + |
| 34 | +#include <new.h> |
| 35 | + |
| 36 | +__STL_BEGIN_NAMESPACE |
| 37 | + |
| 38 | +template <class T1, class T2> |
| 39 | +inline void construct(T1 *p, const T2 &value) |
| 40 | +{ |
| 41 | + // placement new,调用 T1::T1(value) |
| 42 | + new (p) T1(value); |
| 43 | +} |
| 44 | + |
| 45 | +// destory 第一个版本,接受一个指针 |
| 46 | +template <class T> |
| 47 | +inline void destroy(T *pointer) |
| 48 | +{ |
| 49 | + pointer->~T(); |
| 50 | +} |
| 51 | + |
| 52 | +// dsetory 第二版本,接收两个迭代器。此函数设法找出元素的数值型别 |
| 53 | +// 进而利用 __type_traits<> 求取最适当措施 |
| 54 | +template <class ForwardIterator> |
| 55 | +inline void destroy(ForwardIterator first, ForwardIterator last) |
| 56 | +{ |
| 57 | + __destroy(first, last, value_type(first)); |
| 58 | +} |
| 59 | + |
| 60 | +// 判断元素的数值型别(value type) 是否有 trivial destructor |
| 61 | +template <class ForwardIterator, class T> |
| 62 | +inline void __destroy(ForwardIterator first, ForwardIterator last, T *) |
| 63 | +{ |
| 64 | + typedef typename __type_traits<T>::has_trivial_destructor trivial_destructor; |
| 65 | + __destroy_aux(first, last, trivial_destructor()); |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +// 如果元素的数值型别(value type) 有 non-trivial destructor |
| 70 | +template <class ForwardIterator> |
| 71 | +inline void |
| 72 | +__destroy_aux(ForwardIterator first, ForwardIterator last, __false_type) |
| 73 | +{ |
| 74 | + for (; first < last; ++first) |
| 75 | + destroy(&*first); |
| 76 | +} |
| 77 | + |
| 78 | +// 如果元素的数值型别(value type) 有 trivial destructor |
| 79 | +template <class ForwardIterator> |
| 80 | +inline void __destroy_aux(ForwardIterator, ForwardIterator, __true_type) {} |
| 81 | + |
| 82 | +// 以下是 destroy 第二版针对迭代器为 char* 和 wchar_t* 的特化版 |
| 83 | +inline void destroy(char *, char *) {} |
| 84 | +inline void destroy(wchar_t *, wchar_t *) {} |
| 85 | + |
| 86 | +__STL_END_NAMESPACE |
| 87 | + |
| 88 | +#endif /* __SGI_STL_INTERNAL_CONSTRUCT_H */ |
| 89 | + |
| 90 | +// Local Variables: |
| 91 | +// mode:C++ |
| 92 | +// End: |
0 commit comments