_tempbuf.h

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

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