Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/boost/ptr_container/ptr_array.hpp @ 44

Last change on this file since 44 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 6.5 KB
Line 
1//
2// Boost.Pointer Container
3//
4//  Copyright Thorsten Ottosen 2003-2005. Use, modification and
5//  distribution is subject to the Boost Software License, Version
6//  1.0. (See accompanying file LICENSE_1_0.txt or copy at
7//  http://www.boost.org/LICENSE_1_0.txt)
8//
9// For more information, see http://www.boost.org/libs/ptr_container/
10//
11
12#ifndef BOOST_PTR_CONTAINER_PTR_ARRAY_HPP
13#define BOOST_PTR_CONTAINER_PTR_ARRAY_HPP
14
15#if defined(_MSC_VER) && (_MSC_VER >= 1200)
16# pragma once
17#endif
18
19#include <boost/array.hpp>
20#include <boost/static_assert.hpp>
21#include <boost/ptr_container/ptr_sequence_adapter.hpp>
22
23namespace boost
24{
25
26    namespace ptr_container_detail
27    {
28        template
29        <
30            class T,
31            size_t N,
32            class Allocator = int // dummy
33        >
34        class ptr_array_impl : public boost::array<T,N>
35        {
36        public:
37            typedef Allocator allocator_type;
38
39            ptr_array_impl( Allocator a = Allocator() )
40            {
41                this->assign( 0 );
42            }
43
44            ptr_array_impl( size_t, T*, Allocator a = Allocator() )
45            {
46                this->assign( 0 );
47            }
48        };
49    }
50
51    template
52    <
53        class T,
54        size_t N,
55        class CloneAllocator = heap_clone_allocator
56    >
57    class ptr_array : public
58        ptr_sequence_adapter< T,
59                              ptr_container_detail::ptr_array_impl<void*,N>,
60                              CloneAllocator >
61    {
62    private:
63        typedef ptr_sequence_adapter< T,
64                                      ptr_container_detail::ptr_array_impl<void*,N>,
65                                      CloneAllocator >
66            base_class;
67
68        typedef BOOST_DEDUCED_TYPENAME remove_nullable<T>::type U;
69
70        typedef ptr_array<T,N,CloneAllocator>
71                          this_type;
72
73        ptr_array( const this_type& );
74        void operator=( const this_type& );
75
76    public:
77        typedef std::size_t size_type;
78        typedef U*          value_type;
79        typedef U*          pointer;
80        typedef U&          reference;
81        typedef const U&    const_reference;
82        typedef BOOST_DEDUCED_TYPENAME base_class::auto_type
83                            auto_type;
84
85    public: // constructors
86        ptr_array() : base_class()
87        { }
88
89        ptr_array( std::auto_ptr<this_type> r )
90        : base_class( r ) { }
91
92        void operator=( std::auto_ptr<this_type> r )
93        {
94            base_class::operator=(r);
95        }
96
97        std::auto_ptr<this_type> release()
98        {
99            std::auto_ptr<this_type> ptr( new this_type );
100            this->swap( *ptr );
101            return ptr;
102        }
103
104        std::auto_ptr<this_type> clone() const
105        {
106            std::auto_ptr<this_type> pa( new this_type );
107            for( size_t i = 0; i != N; ++i )
108            {
109                if( ! is_null(i) )
110                    pa->replace( i, CloneAllocator::allocate_clone( (*this)[i] ) );
111            }
112            return pa;
113        }
114
115    private: // hide some members
116        using base_class::insert;
117        using base_class::erase;
118        using base_class::push_back;
119        using base_class::push_front;
120        using base_class::pop_front;
121        using base_class::pop_back;
122        using base_class::transfer;
123        using base_class::get_allocator;
124
125    public: // compile-time interface
126
127        template< size_t idx >
128        auto_type replace( U* r ) // strong
129        {
130            BOOST_STATIC_ASSERT( idx < N );
131
132            this->enforce_null_policy( r, "Null pointer in 'ptr_array::replace()'" );
133
134            auto_type res( static_cast<U*>( this->c_private()[idx] ) ); // nothrow
135            this->c_private()[idx] = r;                                 // nothrow
136            return move(res);                                           // nothrow
137        }
138
139        template< size_t idx, class V >
140        auto_type replace( std::auto_ptr<V> r )
141        {
142            return replace<idx>( r.release() );
143        }
144
145        auto_type replace( size_t idx, U* r ) // strong
146        {
147            this->enforce_null_policy( r, "Null pointer in 'ptr_array::replace()'" );
148
149            auto_type ptr( r );
150
151            BOOST_PTR_CONTAINER_THROW_EXCEPTION( idx >= N, bad_index,
152                                                 "'replace()' aout of bounds" );
153
154            auto_type res( static_cast<U*>( this->c_private()[idx] ) ); // nothrow
155            this->c_private()[idx] = ptr.release();                     // nothrow
156            return move(res);                                           // nothrow
157        }
158
159        template< class V >
160        auto_type replace( size_t idx, std::auto_ptr<V> r )
161        {
162            return replace( idx, r.release() );
163        }
164
165        using base_class::at;
166
167        template< size_t idx >
168        T& at()
169        {
170            BOOST_STATIC_ASSERT( idx < N );
171            return (*this)[idx];
172        }
173
174        template< size_t idx >
175        const T& at() const
176        {
177            BOOST_STATIC_ASSERT( idx < N );
178            return (*this)[idx];
179        }
180
181        bool is_null( size_t idx ) const
182        {
183            return base_class::is_null(idx);
184        }
185
186        template< size_t idx >
187        bool is_null() const
188        {
189            BOOST_STATIC_ASSERT( idx < N );
190            return this->c_private()[idx] == 0;
191        }
192       
193    public: // serialization
194
195        template< class Archive >
196        void save( Archive& ar, const unsigned ) const
197        {
198            this->save_helper( ar );
199        }
200
201        template< class Archive >
202        void load( Archive& ar, const unsigned ) // basic
203        {
204            for( size_type i = 0u; i != N; ++i )
205            {
206                //
207                // Remark: pointers are not tracked,
208                // so we need not call ar.reset_object_address(v, u)
209                //
210                T* p;
211                ar & p;
212                this->replace( i, p );
213            }
214        }
215
216        BOOST_SERIALIZATION_SPLIT_MEMBER()
217
218    };
219
220    //////////////////////////////////////////////////////////////////////////////
221    // clonability
222
223    template< typename T, size_t size, typename CA >
224    inline ptr_array<T,size,CA>* new_clone( const ptr_array<T,size,CA>& r )
225    {
226        return r.clone().release();
227    }
228
229    /////////////////////////////////////////////////////////////////////////
230    // swap
231
232    template< typename T, size_t size, typename CA >
233    inline void swap( ptr_array<T,size,CA>& l, ptr_array<T,size,CA>& r )
234    {
235        l.swap(r);
236    }
237}
238
239#endif
Note: See TracBrowser for help on using the repository browser.