/home/ntakagi/work/STLport-5.1.5/stlport/stl/_tempbuf.h

Go to the documentation of this file.
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 
00037 #ifndef _STLP_INTERNAL_CSTDLIB
00038 #  include <stl/_cstdlib.h>
00039 #endif
00040 
00041 #ifndef _STLP_INTERNAL_UNINITIALIZED_H
00042 #  include <stl/_uninitialized.h>
00043 #endif
00044 
00045 _STLP_BEGIN_NAMESPACE
00046 
00047 template <class _Tp>
00048 pair<_Tp*, ptrdiff_t>  _STLP_CALL
00049 __get_temporary_buffer(ptrdiff_t __len, _Tp*);
00050 
00051 #ifndef _STLP_NO_EXPLICIT_FUNCTION_TMPL_ARGS
00052 
00053 template <class _Tp>
00054 inline pair<_Tp*, ptrdiff_t>  _STLP_CALL get_temporary_buffer(ptrdiff_t __len) {
00055   return __get_temporary_buffer(__len, (_Tp*) 0);
00056 }
00057 
00058 # if ! defined(_STLP_NO_EXTENSIONS)
00059 // This overload is not required by the standard; it is an extension.
00060 // It is supported for backward compatibility with the HP STL, and
00061 // because not all compilers support the language feature (explicit
00062 // function template arguments) that is required for the standard
00063 // version of get_temporary_buffer.
00064 template <class _Tp>
00065 inline pair<_Tp*, ptrdiff_t>  _STLP_CALL
00066 get_temporary_buffer(ptrdiff_t __len, _Tp*) {
00067   return __get_temporary_buffer(__len, (_Tp*) 0);
00068 }
00069 # endif
00070 #endif
00071 
00072 template <class _Tp>
00073 inline void  _STLP_CALL return_temporary_buffer(_Tp* __p) {
00074 // SunPro brain damage
00075   free((char*)__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&, const __true_type&) {}
00101   void _M_initialize_buffer(const _Tp& val, const __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  _Trivial;
00117 #   endif
00118     _STLP_TRY {
00119       _M_len = distance(__first, __last);
00120       _M_allocate_buffer();
00121       if (_M_len > 0)
00122         _M_initialize_buffer(*__first, _Trivial());
00123     }
00124     _STLP_UNWIND(free(_M_buffer); _M_buffer = 0; _M_len = 0)
00125   }
00126 
00127   ~_Temporary_buffer() {
00128     _STLP_STD::_Destroy_Range(_M_buffer, _M_buffer + _M_len);
00129     free(_M_buffer);
00130   }
00131 
00132 private:
00133   // Disable copy constructor and assignment operator.
00134   _Temporary_buffer(const _Temporary_buffer<_ForwardIterator, _Tp>&) {}
00135   void operator=(const _Temporary_buffer<_ForwardIterator, _Tp>&) {}
00136 };
00137 
00138 # ifndef _STLP_NO_EXTENSIONS
00139 
00140 // Class temporary_buffer is not part of the standard.  It is an extension.
00141 
00142 template <class _ForwardIterator,
00143           class _Tp
00144 #ifdef _STLP_CLASS_PARTIAL_SPECIALIZATION
00145                     = typename iterator_traits<_ForwardIterator>::value_type
00146 #endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */
00147          >
00148 struct temporary_buffer : public _Temporary_buffer<_ForwardIterator, _Tp>
00149 {
00150   temporary_buffer(_ForwardIterator __first, _ForwardIterator __last)
00151     : _Temporary_buffer<_ForwardIterator, _Tp>(__first, __last) {}
00152   ~temporary_buffer() {}
00153 };
00154 
00155 # endif /* _STLP_NO_EXTENSIONS */
00156 
00157 _STLP_END_NAMESPACE
00158 
00159 # ifndef _STLP_LINK_TIME_INSTANTIATION
00160 #  include <stl/_tempbuf.c>
00161 # endif
00162 
00163 #endif /* _STLP_INTERNAL_TEMPBUF_H */
00164 
00165 // Local Variables:
00166 // mode:C++
00167 // End:



Generated on Mon Mar 10 15:32:41 2008 by  doxygen 1.5.1