src/Utility/Collection/fixed_size_buffer.h

説明を見る。
00001 // 
00002 // Copyright (c) 2005-2008 Kenichi Watanabe.
00003 // Copyright (c) 2005-2008 Yasuhiro Watari.
00004 // Copyright (c) 2005-2008 Hironori Ichibayashi.
00005 // Copyright (c) 2008-2009 Kazuo Horio.
00006 // Copyright (c) 2009-2013 Naruki Kurata.
00007 // Copyright (c) 2005-2013 Ryota Shioya.
00008 // Copyright (c) 2005-2013 Masahiro Goshima.
00009 // 
00010 // This software is provided 'as-is', without any express or implied
00011 // warranty. In no event will the authors be held liable for any damages
00012 // arising from the use of this software.
00013 // 
00014 // Permission is granted to anyone to use this software for any purpose,
00015 // including commercial applications, and to alter it and redistribute it
00016 // freely, subject to the following restrictions:
00017 // 
00018 // 1. The origin of this software must not be misrepresented; you must not
00019 // claim that you wrote the original software. If you use this software
00020 // in a product, an acknowledgment in the product documentation would be
00021 // appreciated but is not required.
00022 // 
00023 // 2. Altered source versions must be plainly marked as such, and must not be
00024 // misrepresented as being the original software.
00025 // 
00026 // 3. This notice may not be removed or altered from any source
00027 // distribution.
00028 // 
00029 // 
00030 
00031 
00032 #ifndef UTILITY_COLLECTION_FIXED_SIZE_BUFFER_H
00033 #define UTILITY_COLLECTION_FIXED_SIZE_BUFFER_H
00034 
00035 namespace Onikiri
00036 {
00037     template < typename T, size_t SIZE, typename Tag = int >
00038     class fixed_sized_buffer : private boost::array< T, SIZE >
00039     {
00040     public:
00041         typedef typename boost::array< T, SIZE > base_type;
00042 
00043         typedef typename base_type::iterator        iterator;
00044         typedef typename base_type::const_iterator  const_iterator;
00045         typedef typename base_type::reference       reference;
00046         typedef typename base_type::const_reference const_reference;
00047         typedef typename base_type::size_type       size_type;
00048 
00049         fixed_sized_buffer() : 
00050             m_end( base_type::begin() + 0 )
00051         {
00052 
00053         }
00054                 
00055         fixed_sized_buffer( const fixed_sized_buffer& rhs )
00056         {
00057             std::copy( rhs.begin(), rhs.end(), begin() );
00058             m_end = begin() + rhs.size();
00059         }
00060 
00061         fixed_sized_buffer& operator= ( const fixed_sized_buffer& rhs )
00062         {
00063             std::copy( rhs.begin(), rhs.end(), begin() );
00064             m_end = begin() + rhs.size();
00065             return *this;
00066         }
00067 
00068         // Same as boost::array.
00069         reference at( size_type i ) 
00070         { 
00071             return base_type::at( i );
00072         }
00073 
00074         const_reference at( size_type i ) const
00075         {
00076             return base_type::at( i );
00077         }
00078 
00079         reference operator[]( size_type i ) 
00080         { 
00081             return (*static_cast<base_type*>(this))[i];
00082         }
00083         
00084         const_reference operator[]( size_type i ) const 
00085         {     
00086             return (*static_cast<base_type*>(this))[i];
00087         }
00088 
00089         const T* data() const 
00090         {
00091             return base_type::data(); 
00092         }
00093 
00094         T* data() 
00095         {
00096             return base_type::data(); 
00097         }
00098 
00099         // Original
00100         size_type size() const
00101         {
00102             return m_end - begin();
00103         }
00104 
00105         void push_back( const T& value )
00106         {
00107             if( size() >= base_type::size() ){
00108                 buffer_overflow();
00109             }
00110 
00111             *m_end = value;
00112             m_end++;
00113         }
00114 
00115         void pop_front()
00116         {
00117             erase( begin() );
00118         }
00119         
00120         iterator begin()
00121         {
00122             return base_type::begin();
00123         }
00124 
00125         iterator end()
00126         {
00127             return m_end;
00128         }
00129 
00130         const_iterator begin() const
00131         {
00132             return base_type::begin();
00133         }
00134 
00135         const_iterator end() const
00136         {
00137             return m_end;
00138         }
00139 
00140         void clear()
00141         {
00142             m_end = begin();
00143         }
00144 
00145         void resize( size_t size, const T& value = T() )
00146         {
00147             if( size >= base_type::size() ){
00148                 buffer_overflow();
00149             }
00150             m_end = begin() + size;
00151             std::fill( begin(), end(), value );
00152         }
00153         
00154         iterator erase( const_iterator where )
00155         {
00156             const_iterator s = where + 1;
00157             iterator d = iterator( where );
00158             while( s != end() ){
00159                 *d = *s;
00160                 s++;
00161                 d++;
00162             }
00163             //std::move< const_iterator, iterator >( where + 1, end(), iterator( where ) );
00164             m_end--;
00165             return iterator( where );
00166         }
00167 
00168     protected:
00169         iterator  m_end;
00170 
00171         NOINLINE void buffer_overflow()
00172         {
00173             THROW_RUNTIME_ERROR( 
00174                 "A buffer is resized in excess of a max buffer size. "
00175                 "The max buffer size (%d) is too small. "
00176                 "Type tag: %s",
00177                 (int)SIZE,
00178                 typeid( Tag ).name()
00179             );
00180         }
00181     };
00182 }
00183 
00184 #endif

Onikiri2に対してTue Jun 18 14:34:26 2013に生成されました。  doxygen 1.4.7