stl_tempbuf.h

00001 /*
00002  *
00003  * Copyright (c) 1994
00004  * Hewlett-Packard Company
00005  *
00006  * Permission to use, copy, modify, distribute and sell this software
00007  * and its documentation for any purpose is hereby granted without fee,
00008  * provided that the above copyright notice appear in all copies and
00009  * that both that copyright notice and this permission notice appear
00010  * in supporting documentation.  Hewlett-Packard Company makes no
00011  * representations about the suitability of this software for any
00012  * purpose.  It is provided "as is" without express or implied warranty.
00013  *
00014  *
00015  * Copyright (c) 1996,1997
00016  * Silicon Graphics Computer Systems, Inc.
00017  *
00018  * Permission to use, copy, modify, distribute and sell this software
00019  * and its documentation for any purpose is hereby granted without fee,
00020  * provided that the above copyright notice appear in all copies and
00021  * that both that copyright notice and this permission notice appear
00022  * in supporting documentation.  Silicon Graphics makes no
00023  * representations about the suitability of this software for any
00024  * purpose.  It is provided "as is" without express or implied warranty.
00025  */
00026 
00027 /* NOTE: This is an internal header file, included by other STL headers.
00028  *   You should not attempt to use it directly.
00029  */
00030 
00031 #ifndef __SGI_STL_INTERNAL_TEMPBUF_H
00032 #define __SGI_STL_INTERNAL_TEMPBUF_H
00033 
00034 
00035 __STL_BEGIN_NAMESPACE
00036 
00037 template <class _Tp>
00038 pair<_Tp*, ptrdiff_t> 
00039 __get_temporary_buffer(ptrdiff_t __len, _Tp*)
00040 {
00041   if (__len > ptrdiff_t(INT_MAX / sizeof(_Tp)))
00042     __len = INT_MAX / sizeof(_Tp);
00043 
00044   while (__len > 0) {
00045     _Tp* __tmp = (_Tp*) malloc((size_t)__len * sizeof(_Tp));
00046     if (__tmp != 0)
00047       return pair<_Tp*, ptrdiff_t>(__tmp, __len);
00048     __len /= 2;
00049   }
00050 
00051   return pair<_Tp*, ptrdiff_t>((_Tp*)0, 0);
00052 }
00053 
00054 #ifdef __STL_EXPLICIT_FUNCTION_TMPL_ARGS
00055 
00056 template <class _Tp>
00057 inline pair<_Tp*, ptrdiff_t> get_temporary_buffer(ptrdiff_t __len) {
00058   return __get_temporary_buffer(__len, (_Tp*) 0);
00059 }
00060 
00061 #endif /* __STL_EXPLICIT_FUNCTION_TMPL_ARGS */
00062 
00063 // This overload is not required by the standard; it is an extension.
00064 // It is supported for backward compatibility with the HP STL, and
00065 // because not all compilers support the language feature (explicit
00066 // function template arguments) that is required for the standard
00067 // version of get_temporary_buffer.
00068 template <class _Tp>
00069 inline pair<_Tp*, ptrdiff_t> get_temporary_buffer(ptrdiff_t __len, _Tp*) {
00070   return __get_temporary_buffer(__len, (_Tp*) 0);
00071 }
00072 
00073 template <class _Tp>
00074 void return_temporary_buffer(_Tp* __p) {
00075   free(__p);
00076 }
00077 
00078 template <class _ForwardIterator, class _Tp>
00079 class _Temporary_buffer {
00080 private:
00081   ptrdiff_t  _M_original_len;
00082   ptrdiff_t  _M_len;
00083   _Tp*       _M_buffer;
00084 
00085   void _M_allocate_buffer() {
00086     _M_original_len = _M_len;
00087     _M_buffer = 0;
00088 
00089     if (_M_len > (ptrdiff_t)(INT_MAX / sizeof(_Tp)))
00090       _M_len = INT_MAX / sizeof(_Tp);
00091 
00092     while (_M_len > 0) {
00093       _M_buffer = (_Tp*) malloc(_M_len * sizeof(_Tp));
00094       if (_M_buffer)
00095         break;
00096       _M_len /= 2;
00097     }
00098   }
00099 
00100   void _M_initialize_buffer(const _Tp&, __true_type) {}
00101   void _M_initialize_buffer(const _Tp& val, __false_type) {
00102     uninitialized_fill_n(_M_buffer, _M_len, val);
00103   }
00104 
00105 public:
00106   ptrdiff_t size() const { return _M_len; }
00107   ptrdiff_t requested_size() const { return _M_original_len; }
00108   _Tp* begin() { return _M_buffer; }
00109   _Tp* end() { return _M_buffer + _M_len; }
00110 
00111   _Temporary_buffer(_ForwardIterator __first, _ForwardIterator __last) {
00112     // Workaround for a __type_traits bug in the pre-7.3 compiler.
00113 #   if defined(__sgi) && !defined(__GNUC__) && _COMPILER_VERSION < 730
00114     typedef typename __type_traits<_Tp>::is_POD_type _Trivial;
00115 #   else
00116     typedef typename __type_traits<_Tp>::has_trivial_default_constructor
00117             _Trivial;
00118 #   endif
00119 
00120     __STL_TRY {
00121       _M_len = 0;
00122       distance(__first, __last, _M_len);
00123       _M_allocate_buffer();
00124       if (_M_len > 0)
00125         _M_initialize_buffer(*__first, _Trivial());
00126     }
00127     __STL_UNWIND(free(_M_buffer); _M_buffer = 0; _M_len = 0);
00128   }
00129  
00130   ~_Temporary_buffer() {  
00131     destroy(_M_buffer, _M_buffer + _M_len);
00132     free(_M_buffer);
00133   }
00134 
00135 private:
00136   // Disable copy constructor and assignment operator.
00137   _Temporary_buffer(const _Temporary_buffer&) {}
00138   void operator=(const _Temporary_buffer&) {}
00139 };
00140 
00141 // Class temporary_buffer is not part of the standard.  It is an extension.
00142 
00143 template <class _ForwardIterator, 
00144           class _Tp 
00145 #ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
00146                     = typename iterator_traits<_ForwardIterator>::value_type
00147 #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
00148          >
00149 struct temporary_buffer : public _Temporary_buffer<_ForwardIterator, _Tp>
00150 {
00151   temporary_buffer(_ForwardIterator __first, _ForwardIterator __last)
00152     : _Temporary_buffer<_ForwardIterator, _Tp>(__first, __last) {}
00153   ~temporary_buffer() {}
00154 };
00155     
00156 __STL_END_NAMESPACE
00157 
00158 #endif /* __SGI_STL_INTERNAL_TEMPBUF_H */
00159 
00160 // Local Variables:
00161 // mode:C++
00162 // End:

Generated on Mon Jun 5 10:20:44 2006 for Intelligence.kdevelop by  doxygen 1.4.6