stl_tempbuf.h

Go to the documentation of this file.
00001 // Temporary buffer implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 2, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00020 // USA.
00021 
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 /*
00032  *
00033  * Copyright (c) 1994
00034  * Hewlett-Packard Company
00035  *
00036  * Permission to use, copy, modify, distribute and sell this software
00037  * and its documentation for any purpose is hereby granted without fee,
00038  * provided that the above copyright notice appear in all copies and
00039  * that both that copyright notice and this permission notice appear
00040  * in supporting documentation.  Hewlett-Packard Company makes no
00041  * representations about the suitability of this software for any
00042  * purpose.  It is provided "as is" without express or implied warranty.
00043  *
00044  *
00045  * Copyright (c) 1996,1997
00046  * Silicon Graphics Computer Systems, Inc.
00047  *
00048  * Permission to use, copy, modify, distribute and sell this software
00049  * and its documentation for any purpose is hereby granted without fee,
00050  * provided that the above copyright notice appear in all copies and
00051  * that both that copyright notice and this permission notice appear
00052  * in supporting documentation.  Silicon Graphics makes no
00053  * representations about the suitability of this software for any
00054  * purpose.  It is provided "as is" without express or implied warranty.
00055  */
00056 
00057 /** @file stl_tempbuf.h
00058  *  This is an internal header file, included by other library headers.
00059  *  You should not attempt to use it directly.
00060  */
00061 
00062 #ifndef _TEMPBUF_H
00063 #define _TEMPBUF_H 1
00064 
00065 #include <memory>
00066 
00067 _GLIBCXX_BEGIN_NAMESPACE(std)
00068 
00069   /**
00070    *  @if maint
00071    *  This class is used in two places: stl_algo.h and ext/memory,
00072    *  where it is wrapped as the temporary_buffer class.  See
00073    *  temporary_buffer docs for more notes.
00074    *  @endif
00075    */
00076   template<typename _ForwardIterator, typename _Tp>
00077     class _Temporary_buffer
00078     {
00079       // concept requirements
00080       __glibcxx_class_requires(_ForwardIterator, _ForwardIteratorConcept)
00081 
00082     public:
00083       typedef _Tp         value_type;
00084       typedef value_type* pointer;
00085       typedef pointer     iterator;
00086       typedef ptrdiff_t   size_type;
00087 
00088     protected:
00089       size_type  _M_original_len;
00090       size_type  _M_len;
00091       pointer    _M_buffer;
00092 
00093       void
00094       _M_initialize_buffer(const _Tp&, __true_type) { }
00095 
00096       void
00097       _M_initialize_buffer(const _Tp& __val, __false_type)
00098       { std::uninitialized_fill_n(_M_buffer, _M_len, __val); }
00099 
00100     public:
00101       /// As per Table mumble.
00102       size_type
00103       size() const
00104       { return _M_len; }
00105 
00106       /// Returns the size requested by the constructor; may be >size().
00107       size_type
00108       requested_size() const
00109       { return _M_original_len; }
00110 
00111       /// As per Table mumble.
00112       iterator
00113       begin()
00114       { return _M_buffer; }
00115 
00116       /// As per Table mumble.
00117       iterator
00118       end()
00119       { return _M_buffer + _M_len; }
00120 
00121       /**
00122        * Constructs a temporary buffer of a size somewhere between
00123        * zero and the size of the given range.
00124        */
00125       _Temporary_buffer(_ForwardIterator __first, _ForwardIterator __last);
00126 
00127       ~_Temporary_buffer()
00128       {
00129     std::_Destroy(_M_buffer, _M_buffer + _M_len);
00130     std::return_temporary_buffer(_M_buffer);
00131       }
00132 
00133     private:
00134       // Disable copy constructor and assignment operator.
00135       _Temporary_buffer(const _Temporary_buffer&);
00136 
00137       void
00138       operator=(const _Temporary_buffer&);
00139     };
00140 
00141 
00142   template<typename _ForwardIterator, typename _Tp>
00143     _Temporary_buffer<_ForwardIterator, _Tp>::
00144     _Temporary_buffer(_ForwardIterator __first, _ForwardIterator __last)
00145     : _M_original_len(std::distance(__first, __last)),
00146       _M_len(0), _M_buffer(0)
00147     {
00148       // Workaround for a __type_traits bug in the pre-7.3 compiler.
00149       typedef typename std::__is_scalar<_Tp>::__type _Trivial;
00150 
00151       try
00152     {
00153       pair<pointer, size_type> __p(get_temporary_buffer<
00154                        value_type>(_M_original_len));
00155       _M_buffer = __p.first;
00156       _M_len = __p.second;
00157       if (_M_len > 0)
00158         _M_initialize_buffer(*__first, _Trivial());
00159     }
00160       catch(...)
00161     {
00162       std::return_temporary_buffer(_M_buffer);
00163       _M_buffer = 0;
00164       _M_len = 0;
00165       __throw_exception_again;
00166     }
00167     }
00168 
00169 _GLIBCXX_END_NAMESPACE
00170 
00171 #endif /* _TEMPBUF_H */
00172 

Generated on Thu Nov 1 13:12:34 2007 for libstdc++ by  doxygen 1.5.1