[857] | 1 | // Boost.Assign library
|
---|
| 2 | //
|
---|
| 3 | // Copyright Thorsten Ottosen 2003-2005. Use, modification and
|
---|
| 4 | // distribution is subject to the Boost Software License, Version
|
---|
| 5 | // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
---|
| 6 | // http://www.boost.org/LICENSE_1_0.txt)
|
---|
| 7 | //
|
---|
| 8 | // For more information, see http://www.boost.org/libs/assign/
|
---|
| 9 | //
|
---|
| 10 |
|
---|
| 11 |
|
---|
| 12 | #ifndef BOOST_ASSIGN_PTR_LIST_OF_HPP
|
---|
| 13 | #define BOOST_ASSIGN_PTR_LIST_OF_HPP
|
---|
| 14 |
|
---|
| 15 | #if defined(_MSC_VER) && (_MSC_VER >= 1020)
|
---|
| 16 | # pragma once
|
---|
| 17 | #endif
|
---|
| 18 |
|
---|
| 19 | #include <boost/assign/assignment_exception.hpp>
|
---|
| 20 | #include <boost/type_traits/remove_const.hpp>
|
---|
| 21 | #include <boost/type_traits/remove_reference.hpp>
|
---|
| 22 | #include <boost/type_traits/is_reference.hpp>
|
---|
| 23 | #include <boost/static_assert.hpp>
|
---|
| 24 | #include <boost/type_traits/detail/yes_no_type.hpp>
|
---|
| 25 | #include <boost/type_traits/decay.hpp>
|
---|
| 26 | #include <boost/type_traits/is_array.hpp>
|
---|
| 27 | #include <boost/mpl/if.hpp>
|
---|
| 28 | #include <boost/ptr_container/ptr_vector.hpp>
|
---|
| 29 |
|
---|
| 30 | #include <boost/preprocessor/repetition/enum_binary_params.hpp>
|
---|
| 31 | #include <boost/preprocessor/repetition/enum_params.hpp>
|
---|
| 32 | #include <boost/preprocessor/iteration/local.hpp>
|
---|
| 33 |
|
---|
| 34 | namespace boost
|
---|
| 35 | {
|
---|
| 36 |
|
---|
| 37 | namespace assign_detail
|
---|
| 38 | {
|
---|
| 39 | /////////////////////////////////////////////////////////////////////////
|
---|
| 40 | // Part 0: common conversion code
|
---|
| 41 | /////////////////////////////////////////////////////////////////////////
|
---|
| 42 |
|
---|
| 43 | template< class T >
|
---|
| 44 | struct assign_decay
|
---|
| 45 | {
|
---|
| 46 | //
|
---|
| 47 | // Add constness to array parameters
|
---|
| 48 | // to support string literals properly
|
---|
| 49 | //
|
---|
| 50 | typedef BOOST_DEDUCED_TYPENAME mpl::eval_if<
|
---|
| 51 | ::boost::is_array<T>,
|
---|
| 52 | ::boost::decay<const T>,
|
---|
| 53 | ::boost::decay<T> >::type type;
|
---|
| 54 | };
|
---|
| 55 |
|
---|
| 56 | /////////////////////////////////////////////////////////////////////////
|
---|
| 57 | // Part 1: flexible and efficient interface
|
---|
| 58 | /////////////////////////////////////////////////////////////////////////
|
---|
| 59 |
|
---|
| 60 | template< class T >
|
---|
| 61 | class generic_ptr_list
|
---|
| 62 | {
|
---|
| 63 | protected:
|
---|
| 64 | typedef boost::ptr_vector<T> impl_type;
|
---|
| 65 | typedef std::auto_ptr<impl_type> release_type;
|
---|
| 66 | mutable impl_type values_;
|
---|
| 67 |
|
---|
| 68 | public:
|
---|
| 69 | typedef BOOST_DEDUCED_TYPENAME impl_type::iterator iterator;
|
---|
| 70 | typedef BOOST_DEDUCED_TYPENAME impl_type::const_iterator const_iterator;
|
---|
| 71 | typedef BOOST_DEDUCED_TYPENAME impl_type::value_type value_type;
|
---|
| 72 | typedef BOOST_DEDUCED_TYPENAME impl_type::size_type size_type;
|
---|
| 73 | typedef BOOST_DEDUCED_TYPENAME impl_type::difference_type difference_type;
|
---|
| 74 | public:
|
---|
| 75 | generic_ptr_list() : values_( 32u )
|
---|
| 76 | { }
|
---|
| 77 |
|
---|
| 78 | generic_ptr_list( const generic_ptr_list& r )
|
---|
| 79 | {
|
---|
| 80 | values_.swap(r.values_);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | generic_ptr_list( release_type r ) : values_(r)
|
---|
| 84 | { }
|
---|
| 85 |
|
---|
| 86 | release_type release()
|
---|
| 87 | {
|
---|
| 88 | return values_.release();
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | public:
|
---|
| 92 | iterator begin() const { return values_.begin(); }
|
---|
| 93 | iterator end() const { return values_.end(); }
|
---|
| 94 | bool empty() const { return values_.empty(); }
|
---|
| 95 | size_type size() const { return values_.size(); }
|
---|
| 96 |
|
---|
| 97 | public:
|
---|
| 98 |
|
---|
| 99 | template< class PtrContainer >
|
---|
| 100 | operator std::auto_ptr<PtrContainer>() const
|
---|
| 101 | {
|
---|
| 102 | PtrContainer* type = 0;
|
---|
| 103 | return convert( type );
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | template< class PtrContainer >
|
---|
| 107 | std::auto_ptr<PtrContainer> convert( const PtrContainer* c ) const
|
---|
| 108 | {
|
---|
| 109 | std::auto_ptr<PtrContainer> res( new PtrContainer() );
|
---|
| 110 | while( !empty() )
|
---|
| 111 | res->insert( res->end(),
|
---|
| 112 | values_.pop_back().release() );
|
---|
| 113 | return res;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | template< class PtrContainer >
|
---|
| 117 | std::auto_ptr<PtrContainer> to_container( const PtrContainer& c ) const
|
---|
| 118 | {
|
---|
| 119 | return convert( &c );
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | protected:
|
---|
| 123 | void push_back( T* r ) { values_.push_back( r ); }
|
---|
| 124 |
|
---|
| 125 | public:
|
---|
| 126 | generic_ptr_list& operator()()
|
---|
| 127 | {
|
---|
| 128 | this->push_back( new T() );
|
---|
| 129 | return *this;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | template< class U >
|
---|
| 133 | generic_ptr_list& operator()( const U& u )
|
---|
| 134 | {
|
---|
| 135 | this->push_back( new T(u) );
|
---|
| 136 | return *this;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 |
|
---|
| 140 | #ifndef BOOST_ASSIGN_MAX_PARAMS // use user's value
|
---|
| 141 | #define BOOST_ASSIGN_MAX_PARAMS 5
|
---|
| 142 | #endif
|
---|
| 143 | #define BOOST_ASSIGN_MAX_PARAMETERS (BOOST_ASSIGN_MAX_PARAMS - 1)
|
---|
| 144 | #define BOOST_ASSIGN_PARAMS1(n) BOOST_PP_ENUM_PARAMS(n, class U)
|
---|
| 145 | #define BOOST_ASSIGN_PARAMS2(n) BOOST_PP_ENUM_BINARY_PARAMS(n, U, const& u)
|
---|
| 146 | #define BOOST_ASSIGN_PARAMS3(n) BOOST_PP_ENUM_PARAMS(n, u)
|
---|
| 147 |
|
---|
| 148 | #define BOOST_PP_LOCAL_LIMITS (1, BOOST_ASSIGN_MAX_PARAMETERS)
|
---|
| 149 | #define BOOST_PP_LOCAL_MACRO(n) \
|
---|
| 150 | template< class U, BOOST_ASSIGN_PARAMS1(n) > \
|
---|
| 151 | generic_ptr_list& operator()(U const& u, BOOST_ASSIGN_PARAMS2(n) ) \
|
---|
| 152 | { \
|
---|
| 153 | this->push_back( new T(u, BOOST_ASSIGN_PARAMS3(n))); \
|
---|
| 154 | return *this; \
|
---|
| 155 | } \
|
---|
| 156 | /**/
|
---|
| 157 |
|
---|
| 158 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
| 159 |
|
---|
| 160 | }; // class 'generic_ptr_list'
|
---|
| 161 |
|
---|
| 162 | } // namespace 'assign_detail'
|
---|
| 163 |
|
---|
| 164 | namespace assign
|
---|
| 165 | {
|
---|
| 166 | template< class T >
|
---|
| 167 | inline assign_detail::generic_ptr_list<T>
|
---|
| 168 | ptr_list_of()
|
---|
| 169 | {
|
---|
| 170 | return assign_detail::generic_ptr_list<T>()();
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | template< class T, class U >
|
---|
| 174 | inline assign_detail::generic_ptr_list<T>
|
---|
| 175 | ptr_list_of( const U& t )
|
---|
| 176 | {
|
---|
| 177 | return assign_detail::generic_ptr_list<T>()( t );
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 |
|
---|
| 181 | #define BOOST_PP_LOCAL_LIMITS (1, BOOST_ASSIGN_MAX_PARAMETERS)
|
---|
| 182 | #define BOOST_PP_LOCAL_MACRO(n) \
|
---|
| 183 | template< class T, class U, BOOST_ASSIGN_PARAMS1(n) > \
|
---|
| 184 | inline assign_detail::generic_ptr_list<T> \
|
---|
| 185 | ptr_list_of(U const& u, BOOST_ASSIGN_PARAMS2(n) ) \
|
---|
| 186 | { \
|
---|
| 187 | return assign_detail::generic_ptr_list<T>()(u, BOOST_ASSIGN_PARAMS3(n)); \
|
---|
| 188 | } \
|
---|
| 189 | /**/
|
---|
| 190 |
|
---|
| 191 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
| 192 |
|
---|
| 193 |
|
---|
| 194 | } // namespace 'assign'
|
---|
| 195 | } // namespace 'boost'
|
---|
| 196 |
|
---|
| 197 |
|
---|
| 198 | #undef BOOST_ASSIGN_PARAMS1
|
---|
| 199 | #undef BOOST_ASSIGN_PARAMS2
|
---|
| 200 | #undef BOOST_ASSIGN_PARAMS3
|
---|
| 201 | #undef BOOST_ASSIGN_MAX_PARAMETERS
|
---|
| 202 |
|
---|
| 203 | #endif
|
---|