C++ STL源码剖析

   1. // Filename:    stl_config.h  
   2.   
   3. // Comment By:  凝霜  
   4. // E-mail:      [email protected]  
   5. // Blog:        http://blog.csdn.net/mdl13412  
   6.   
   7. /* 
   8.  * Copyright (c) 1996-1997 
   9.  * Silicon Graphics Computer Systems, Inc. 
  10.  * 
  11.  * Permission to use, copy, modify, distribute and sell this software 
  12.  * and its documentation for any purpose is hereby granted without fee, 
  13.  * provided that the above copyright notice appear in all copies and 
  14.  * that both that copyright notice and this permission notice appear 
  15.  * in supporting documentation.  Silicon Graphics makes no 
  16.  * representations about the suitability of this software for any 
  17.  * purpose.  It is provided "as is" without express or implied warranty. 
  18.  */  
  19.   
  20. /* NOTE: This is an internal header file, included by other STL headers. 
  21.  *   You should not attempt to use it directly. 
  22.  */  
  23.   
  24. #ifndef __STL_CONFIG_H  
  25. #define __STL_CONFIG_H  
  26.   
  27. //  本配置文件功能表:  
  28. //  (1)  如果不编译器没有定义bool, true, false则定义  
  29. //  (2)  如果编译器不支持drand48()函数则定义__STL_NO_DRAND48  
  30. //       注: drand48产生双精度的伪随机数, 因为采用了48bit计算, 故名drand48  
  31. //  (3)  如果编译器不支持static members of template classes(模板类静态成员),  
  32. //       则定义__STL_STATIC_TEMPLATE_MEMBER_BUG  
  33. //  (4)  如果编译器不支持‘typename‘关键字, 则将‘typename‘定义为空(null macro)  
  34. //  (5)  如果编译器支持partial specialization of class templates(模板类偏特化),  
  35. //       则定义__STL_CLASS_PARTIAL_SPECIALIZATION  
  36. //       参考文献: http://msdn.microsoft.com/en-us/library/9w7t3kf1(v=VS.71).aspx  
  37. //  (6)  如果编译器支持partial ordering of function templates(模板函数特化优先级),  
  38. //       则定义__STL_FUNCTION_TMPL_PARTIAL_ORDER  
  39. //       参考资料: http://msdn.microsoft.com/zh-cn/library/zaycz069.aspx  
  40. //  (7)  如果编译器支持calling a function template by providing its template  
  41. //       arguments explicitly(显式指定调用模板函数的模板参数)  
  42. //       则定义__STL_EXPLICIT_FUNCTION_TMPL_ARGS  
  43. //  (8)  如果编译器支持template members of classes(类模板成员),  
  44. //       则定义__STL_MEMBER_TEMPLATES  
  45. //  (9)  如果编译器不支持‘explicit‘关键字, 则将‘explicit‘定义为空(null macro)  
  46. //  (10) 如果编译器不能根据前一个模板参数设定后面的默认模板参数,  
  47. //       则定义__STL_LIMITED_DEFAULT_TEMPLATES  
  48. //  (11) 如果编译器处理模板函数的non-type模板参数类型推断有困难,  
  49. //       则定义__STL_NON_TYPE_TMPL_PARAM_BUG  
  50. //  (12) 如果编译器不支持迭代器使用‘->‘操作符,  
  51. //       则定义__SGI_STL_NO_ARROW_OPERATOR  
  52. //  (13) 如果编译器(在当前编译模式下)支持异常,  
  53. //       则定义__STL_USE_EXCEPTIONS  
  54. //  (14) 如果我们将STL放进命名空间中,  
  55. //       则定义__STL_USE_NAMESPACES  
  56. //  (15) 如果本STL在SGI的编译器上编译, 并且用户没有选择pthreads或者no threads,  
  57. //       则默认使用__STL_SGI_THREADS  
  58. //       注: POSIX thread 简称为pthread, Posix线程是一个POSIX标准线程.  
  59. //  (16) 如果本STL在Win32平台的编译器上使用多线程模式编译,  
  60. //       则定义__STL_WIN32THREADS  
  61. //  (17) 适当的定义命名空间相关的宏(__STD, __STL_BEGIN_NAMESPACE, 等)  
  62. //  (18) 适当的定义异常相关的宏(__STL_TRY, __STL_UNWIND, 等)  
  63. //  (19) 根据是否定义__STL_ASSERTIONS, 将__stl_assert定义为断言或者空(null macro)  
  64.   
  65. #ifdef _PTHREADS  
  66. #   define __STL_PTHREADS  
  67. #endif  
  68.   
  69. // 如果编译器不提供本STL需要的一些功能,则定义__STL_NEED_XXX  
  70. # if defined(__sgi) && !defined(__GNUC__)  
  71. #   if !defined(_BOOL)  
  72. #     define __STL_NEED_BOOL  
  73. #   endif  
  74. #   if !defined(_TYPENAME_IS_KEYWORD)  
  75. #     define __STL_NEED_TYPENAME  
  76. #   endif  
  77. #   ifdef _PARTIAL_SPECIALIZATION_OF_CLASS_TEMPLATES  
  78. #     define __STL_CLASS_PARTIAL_SPECIALIZATION  
  79. #   endif  
  80. #   ifdef _MEMBER_TEMPLATES  
  81. #     define __STL_MEMBER_TEMPLATES  
  82. #   endif  
  83. #   if !defined(_EXPLICIT_IS_KEYWORD)  
  84. #     define __STL_NEED_EXPLICIT  
  85. #   endif  
  86. #   ifdef __EXCEPTIONS  
  87. #     define __STL_USE_EXCEPTIONS  
  88. #   endif  
  89. #   if (_COMPILER_VERSION >= 721) && defined(_NAMESPACES)  
  90. #     define __STL_USE_NAMESPACES  
  91. #   endif  
  92. #   if !defined(_NOTHREADS) && !defined(__STL_PTHREADS)  
  93. #     define __STL_SGI_THREADS  
  94. #   endif  
  95. # endif  
  96.   
  97. # ifdef __GNUC__  
  98. #   include <_G_config.h>  
  99. #   if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8)  
 100. #     define __STL_STATIC_TEMPLATE_MEMBER_BUG  
 101. #     define __STL_NEED_TYPENAME  
 102. #     define __STL_NEED_EXPLICIT  
 103. #   else  
 104. #     define __STL_CLASS_PARTIAL_SPECIALIZATION  
 105. #     define __STL_FUNCTION_TMPL_PARTIAL_ORDER  
 106. #     define __STL_EXPLICIT_FUNCTION_TMPL_ARGS  
 107. #     define __STL_MEMBER_TEMPLATES  
 108. #   endif  
 109.     /* glibc pre 2.0 is very buggy. We have to disable thread for it. 
 110.        It should be upgraded to glibc 2.0 or later. */  
 111. #   if !defined(_NOTHREADS) && __GLIBC__ >= 2 && defined(_G_USING_THUNKS)  
 112. #     define __STL_PTHREADS  
 113. #   endif  
 114. #   ifdef __EXCEPTIONS  
 115. #     define __STL_USE_EXCEPTIONS  
 116. #   endif  
 117. # endif  
 118.   
 119. // Sun C++ compiler  
 120. # if defined(__SUNPRO_CC)  
 121. #   define __STL_NEED_BOOL  
 122. #   define __STL_NEED_TYPENAME  
 123. #   define __STL_NEED_EXPLICIT  
 124. #   define __STL_USE_EXCEPTIONS  
 125. # endif  
 126.   
 127. // TODO: 这个我没找到资料, 如果你知道或者有相关资料请联系我, Thank U  
 128. # if defined(__COMO__)  
 129. #   define __STL_MEMBER_TEMPLATES  
 130. #   define __STL_CLASS_PARTIAL_SPECIALIZATION  
 131. #   define __STL_USE_EXCEPTIONS  
 132. #   define __STL_USE_NAMESPACES  
 133. # endif  
 134.   
 135. // _MSC_VER 定义微软编译器的版本  
 136. // MS VC++ 10.0 _MSC_VER = 1600  
 137. // MS VC++ 9.0 _MSC_VER = 1500  
 138. // MS VC++ 8.0 _MSC_VER = 1400  
 139. // MS VC++ 7.1 _MSC_VER = 1310  
 140. // MS VC++ 7.0 _MSC_VER = 1300  
 141. // MS VC++ 6.0 _MSC_VER = 1200  
 142. // MS VC++ 5.0 _MSC_VER = 1100  
 143. # if defined(_MSC_VER)  
 144. #   if _MSC_VER > 1000  
 145. #     include <yvals.h>  
 146. #   else  
 147. #     define __STL_NEED_BOOL  
 148. #   endif  
 149. #   define __STL_NO_DRAND48  
 150. #   define __STL_NEED_TYPENAME  
 151. #   if _MSC_VER < 1100  
 152. #     define __STL_NEED_EXPLICIT  
 153. #   endif  
 154. #   define __STL_NON_TYPE_TMPL_PARAM_BUG  
 155. #   define __SGI_STL_NO_ARROW_OPERATOR  
 156. #   ifdef _CPPUNWIND  
 157. #     define __STL_USE_EXCEPTIONS  
 158. #   endif  
 159. #   ifdef _MT  
 160. #     define __STL_WIN32THREADS  
 161. #   endif  
 162. # endif  
 163.   
 164. # if defined(__BORLANDC__)  
 165. #   define __STL_NO_DRAND48  
 166. #   define __STL_NEED_TYPENAME  
 167. #   define __STL_LIMITED_DEFAULT_TEMPLATES  
 168. #   define __SGI_STL_NO_ARROW_OPERATOR  
 169. #   define __STL_NON_TYPE_TMPL_PARAM_BUG  
 170. #   ifdef _CPPUNWIND  
 171. #     define __STL_USE_EXCEPTIONS  
 172. #   endif  
 173. #   ifdef __MT__  
 174. #     define __STL_WIN32THREADS  
 175. #   endif  
 176. # endif  
 177.   
 178.   
 179. # if defined(__STL_NEED_BOOL)  
 180.     typedef int bool;  
 181. #   define true 1  
 182. #   define false 0  
 183. # endif  
 184.   
 185. # ifdef __STL_NEED_TYPENAME  
 186. #   define typename  
 187. # endif  
 188.   
 189. # ifdef __STL_NEED_EXPLICIT  
 190. #   define explicit  
 191. # endif  
 192.   
 193. # ifdef __STL_EXPLICIT_FUNCTION_TMPL_ARGS  
 194. #   define __STL_NULL_TMPL_ARGS <>  
 195. # else  
 196. #   define __STL_NULL_TMPL_ARGS  
 197. # endif  
 198.   
 199. # ifdef __STL_CLASS_PARTIAL_SPECIALIZATION  
 200. #   define __STL_TEMPLATE_NULL template<>  
 201. # else  
 202. #   define __STL_TEMPLATE_NULL  
 203. # endif  
 204.   
 205. // __STL_NO_NAMESPACES is a hook so that users can disable namespaces  
 206. // without having to edit library headers.  
 207. # if defined(__STL_USE_NAMESPACES) && !defined(__STL_NO_NAMESPACES)  
 208. #   define __STD std  
 209. #   define __STL_BEGIN_NAMESPACE namespace std {  
 210. #   define __STL_END_NAMESPACE }  
 211. #   define __STL_USE_NAMESPACE_FOR_RELOPS  
 212. #   define __STL_BEGIN_RELOPS_NAMESPACE namespace std {  
 213. #   define __STL_END_RELOPS_NAMESPACE }  
 214. #   define __STD_RELOPS std  
 215. # else  
 216. #   define __STD  
 217. #   define __STL_BEGIN_NAMESPACE  
 218. #   define __STL_END_NAMESPACE  
 219. #   undef  __STL_USE_NAMESPACE_FOR_RELOPS  
 220. #   define __STL_BEGIN_RELOPS_NAMESPACE  
 221. #   define __STL_END_RELOPS_NAMESPACE  
 222. #   define __STD_RELOPS  
 223. # endif  
 224.   
 225. # ifdef __STL_USE_EXCEPTIONS  
 226. #   define __STL_TRY try  
 227. #   define __STL_CATCH_ALL catch(...)  
 228. #   define __STL_RETHROW throw  
 229. #   define __STL_NOTHROW throw()  
 230. #   define __STL_UNWIND(action) catch(...) { action; throw; }  
 231. # else  
 232. #   define __STL_TRY  
 233. #   define __STL_CATCH_ALL if (false)  
 234. #   define __STL_RETHROW  
 235. #   define __STL_NOTHROW  
 236. #   define __STL_UNWIND(action)  
 237. # endif  
 238.   
 239. #ifdef __STL_ASSERTIONS  
 240. # include <stdio.h>  
 241. # define __stl_assert(expr) \  
 242.     if (!(expr)) { fprintf(stderr, "%s:%d STL assertion failure: %s\n", \  
 243.               __FILE__, __LINE__, # expr); abort(); }  
 244. #else  
 245. # define __stl_assert(expr)  
 246. #endif  
 247.   
 248. #endif /* __STL_CONFIG_H */  
 249.   
 250. // Local Variables:  
 251. // mode:C++  
 252. // End:
stl_config.h
   1. // Filename:    defalloc.h  
   2.   
   3. // Comment By:  凝霜  
   4. // E-mail:      [email protected]  
   5. // Blog:        http://blog.csdn.net/mdl13412  
   6.   
   7. /* 
   8.  * 
   9.  * Copyright (c) 1994 
  10.  * Hewlett-Packard Company 
  11.  * 
  12.  * Permission to use, copy, modify, distribute and sell this software 
  13.  * and its documentation for any purpose is hereby granted without fee, 
  14.  * provided that the above copyright notice appear in all copies and 
  15.  * that both that copyright notice and this permission notice appear 
  16.  * in supporting documentation.  Hewlett-Packard Company makes no 
  17.  * representations about the suitability of this software for any 
  18.  * purpose.  It is provided "as is" without express or implied warranty. 
  19.  * 
  20.  */  
  21.   
  22. // 这个文件提供原始的HP默认allocator, 仅仅是为了向后兼容  
  23. //  
  24. // 不要使用这个文件,除非你使用一个需要HP-style allocator的旧容器  
  25. // SGI STL使用一个不同的allocator接口  
  26. // SGI-style的allocator不针对对象类型进行参数化, 他使用void *指针  
  27.   
  28. #ifndef DEFALLOC_H  
  29. #define DEFALLOC_H  
  30.   
  31. #include <new.h>  
  32. #include <stddef.h>  
  33. #include <stdlib.h>  
  34. #include <limits.h>  
  35. #include <iostream.h>  
  36. #include <algobase.h>  
  37.   
  38. // 如果内存分配失败, 则直接退出程序  
  39. template <class T>  
  40. inline T* allocate(ptrdiff_t size, T*)  
  41. {  
  42.     set_new_handler(0);  
  43.     T* tmp = (T*)(::operator new((size_t)(size * sizeof(T))));  
  44.     if (tmp == 0) {  
  45.     cerr << "out of memory" << endl;  
  46.     exit(1);  
  47.     }  
  48.     return tmp;  
  49. }  
  50.   
  51. template <class T>  
  52. inline void deallocate(T* buffer)  
  53. {  
  54.     ::operator delete(buffer);  
  55. }  
  56.   
  57. // 标准的STL allocator接口  
  58. template <class T>  
  59. class allocator  
  60. {  
  61. public:  
  62.     // STL type_traits需要的标准定义  
  63.     typedef T value_type;  
  64.     typedef T* pointer;  
  65.     typedef const T* const_pointer;  
  66.     typedef T& reference;  
  67.     typedef const T& const_reference;  
  68.     typedef size_t size_type;  
  69.     typedef ptrdiff_t difference_type;  
  70.   
  71.   
  72.     pointer allocate(size_type n)  
  73.     {  
  74.     return ::allocate((difference_type)n, (pointer)0);  
  75.     }  
  76.     void deallocate(pointer p) { ::deallocate(p); }  
  77.     pointer address(reference x) { return (pointer)&x; }  
  78.     const_pointer const_address(const_reference x)  
  79.     {  
  80.     return (const_pointer)&x;  
  81.     }  
  82.     //   
  83.     size_type init_page_size()  
  84.     {  
  85.     return max(size_type(1), size_type(4096/sizeof(T)));  
  86.     }  
  87.     size_type max_size() const  
  88.     {  
  89.     return max(size_type(1), size_type(UINT_MAX/sizeof(T)));  
  90.     }  
  91. };  
  92.   
  93. // 仅使用void *类型的指针  
  94. class allocator<void>  
  95. {  
  96. public:  
  97.     typedef void* pointer;  
  98. };  
  99.   
 100. #endif
defalloc.h

http://www.cnblogs.com/lfsblack/archive/2012/11/10/2764334.html

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。