📄 ptr_map_adapter.hpp
字号:
typedef BOOST_DEDUCED_TYPENAME VoidPtrMap::allocator_type
allocator_type;
typedef BOOST_DEDUCED_TYPENAME base_type::mapped_type
mapped_type;
private:
void safe_insert( const key_type& key, auto_type ptr ) // strong
{
std::pair<BOOST_DEDUCED_TYPENAME base_type::ptr_iterator,bool>
res =
this->c_private().insert( std::make_pair( key, ptr.get() ) ); // strong, commit
if( res.second ) // nothrow
ptr.release(); // nothrow
}
template< class II >
void map_basic_clone_and_insert( II first, II last )
{
while( first != last )
{
if( this->find( first->first ) == this->end() )
{
const_reference p = *first.base(); // nothrow
auto_type ptr( this->null_policy_allocate_clone( p.second ) );
// strong
this->safe_insert( p.first, ptr_container_detail::
move( ptr ) );// strong, commit
}
++first;
}
}
public:
explicit ptr_map_adapter( const key_compare& comp = key_compare(),
const allocator_type& a = allocator_type() )
: base_type( comp, a ) { }
template< class InputIterator >
ptr_map_adapter( InputIterator first, InputIterator last,
const key_compare& comp = key_compare(),
const allocator_type& a = allocator_type() )
: base_type( comp, a )
{
map_basic_clone_and_insert( first, last );
}
template< class U >
ptr_map_adapter( std::auto_ptr<U> r ) : base_type( r )
{ }
template< class U >
void operator=( std::auto_ptr<U> r )
{
base_type::operator=( r );
}
using base_type::release;
template< typename InputIterator >
void insert( InputIterator first, InputIterator last ) // basic
{
map_basic_clone_and_insert( first, last );
}
template< class Range >
void insert( const Range& r )
{
insert( boost::begin(r), boost::end(r) );
}
private:
std::pair<iterator,bool> insert_impl( const key_type& key, mapped_type x ) // strong
{
this->enforce_null_policy( x, "Null pointer in ptr_map_adapter::insert()" );
auto_type ptr( x ); // nothrow
std::pair<BOOST_DEDUCED_TYPENAME base_type::ptr_iterator,bool>
res = this->c_private().insert( std::make_pair( key, x ) ); // strong, commit
if( res.second ) // nothrow
ptr.release(); // nothrow
return std::make_pair( iterator( res.first ), res.second ); // nothrow
}
public:
std::pair<iterator,bool> insert( key_type& key, mapped_type x )
{
return insert_impl( key, x );
}
template< class U >
std::pair<iterator,bool> insert( const key_type& key, std::auto_ptr<U> x )
{
return insert_impl( key, x.release() );
}
template< class PtrMapAdapter >
bool transfer( BOOST_DEDUCED_TYPENAME PtrMapAdapter::iterator object,
PtrMapAdapter& from ) // strong
{
return this->single_transfer( object, from );
}
template< class PtrMapAdapter >
size_type transfer( BOOST_DEDUCED_TYPENAME PtrMapAdapter::iterator first,
BOOST_DEDUCED_TYPENAME PtrMapAdapter::iterator last,
PtrMapAdapter& from ) // basic
{
return this->single_transfer( first, last, from );
}
#if defined(BOOST_NO_SFINAE) || BOOST_WORKAROUND(__SUNPRO_CC, <= 0x580)
#else
template< class PtrMapAdapter, class Range >
BOOST_DEDUCED_TYPENAME boost::disable_if< boost::is_same< Range,
BOOST_DEDUCED_TYPENAME PtrMapAdapter::iterator >,
size_type >::type
transfer( const Range& r, PtrMapAdapter& from ) // basic
{
return transfer( boost::begin(r), boost::end(r), from );
}
#endif
template< class PtrMapAdapter >
size_type transfer( PtrMapAdapter& from ) // basic
{
return transfer( from.begin(), from.end(), from );
}
public: // serialization
template< class Archive >
void load( Archive& ar, const unsigned ) // strong
{
this->clear();
size_type n;
ar & n;
for( size_type i = 0u; i != n; ++i )
{
key_type key;
T* value;
ar & key;
ar & value;
std::pair<iterator,bool> p = this->insert( key, value );
ar.reset_object_address( &p.first->first, &key );
}
}
BOOST_SERIALIZATION_SPLIT_MEMBER()
};
/////////////////////////////////////////////////////////////////////////
// ptr_multimap_adapter
/////////////////////////////////////////////////////////////////////////
template
<
class T,
class VoidPtrMultiMap,
class CloneAllocator = heap_clone_allocator
>
class ptr_multimap_adapter :
public ptr_container_detail::ptr_map_adapter_base<T,VoidPtrMultiMap,CloneAllocator>
{
typedef ptr_container_detail::ptr_map_adapter_base<T,VoidPtrMultiMap,CloneAllocator>
base_type;
public: // typedefs
typedef BOOST_DEDUCED_TYPENAME base_type::iterator
iterator;
typedef BOOST_DEDUCED_TYPENAME base_type::const_iterator
const_iterator;
typedef BOOST_DEDUCED_TYPENAME base_type::size_type
size_type;
typedef BOOST_DEDUCED_TYPENAME base_type::key_type
key_type;
typedef BOOST_DEDUCED_TYPENAME base_type::const_reference
const_reference;
typedef BOOST_DEDUCED_TYPENAME base_type::mapped_type
mapped_type;
typedef BOOST_DEDUCED_TYPENAME base_type::auto_type
auto_type;
typedef BOOST_DEDUCED_TYPENAME VoidPtrMultiMap::key_compare
key_compare;
typedef BOOST_DEDUCED_TYPENAME VoidPtrMultiMap::allocator_type
allocator_type;
private:
void safe_insert( const key_type& key, auto_type ptr ) // strong
{
this->c_private().insert(
std::make_pair( key, ptr.get() ) ); // strong, commit
ptr.release(); // nothrow
}
template< typename II >
void map_basic_clone_and_insert( II first, II last )
{
while( first != last )
{
const_reference pair = *first.base(); // nothrow
auto_type ptr( this->null_policy_allocate_clone( pair.second ) );
// strong
safe_insert( pair.first, ptr_container_detail::
move( ptr ) ); // strong, commit
++first;
}
}
public:
explicit ptr_multimap_adapter( const key_compare& comp = key_compare(),
const allocator_type& a = allocator_type() )
: base_type( comp, a ) { }
template< class InputIterator >
ptr_multimap_adapter( InputIterator first, InputIterator last,
const key_compare& comp = key_compare(),
const allocator_type& a = allocator_type() )
: base_type( comp, a )
{
map_basic_clone_and_insert( first, last );
}
template< class U >
ptr_multimap_adapter( std::auto_ptr<U> r ) : base_type( r )
{ }
template< class U >
void operator=( std::auto_ptr<U> r )
{
base_type::operator=( r );
}
using base_type::release;
template< typename InputIterator >
void insert( InputIterator first, InputIterator last ) // basic
{
map_basic_clone_and_insert( first, last );
}
template< class Range >
void insert( const Range& r )
{
insert( boost::begin(r), boost::end(r) );
}
iterator insert( key_type& key, mapped_type x ) // strong
{
this->enforce_null_policy( x,
"Null pointer in 'ptr_multimap_adapter::insert()'" );
auto_type ptr( x ); // nothrow
BOOST_DEDUCED_TYPENAME base_type::ptr_iterator
res = this->c_private().insert( std::make_pair( key, x ) );
// strong, commit
ptr.release(); // notrow
return iterator( res );
}
template< class U >
iterator insert( key_type& key, std::auto_ptr<U> x )
{
return insert( key, x.release() );
}
template< class PtrMapAdapter >
void transfer( BOOST_DEDUCED_TYPENAME PtrMapAdapter::iterator object,
PtrMapAdapter& from ) // strong
{
this->multi_transfer( object, from );
}
template< class PtrMapAdapter >
size_type transfer( BOOST_DEDUCED_TYPENAME PtrMapAdapter::iterator first,
BOOST_DEDUCED_TYPENAME PtrMapAdapter::iterator last,
PtrMapAdapter& from ) // basic
{
return this->multi_transfer( first, last, from );
}
#if defined(BOOST_NO_SFINAE) || BOOST_WORKAROUND(__SUNPRO_CC, <= 0x580)
#else
template< class PtrMapAdapter, class Range >
BOOST_DEDUCED_TYPENAME boost::disable_if< boost::is_same< Range,
BOOST_DEDUCED_TYPENAME PtrMapAdapter::iterator >,
size_type >::type
transfer( const Range& r, PtrMapAdapter& from ) // basic
{
return transfer( boost::begin(r), boost::end(r), from );
}
#endif
template< class PtrMapAdapter >
void transfer( PtrMapAdapter& from ) // basic
{
transfer( from.begin(), from.end(), from );
BOOST_ASSERT( from.empty() );
}
public: // serialization
template< class Archive >
void load( Archive& ar, const unsigned ) // basic
{
this->clear();
size_type n;
ar & n;
for( size_type i = 0u; i != n; ++i )
{
key_type key;
T* value;
ar & key;
ar & value;
iterator p = this->insert( key, value );
ar.reset_object_address( &p->first, &key );
}
}
BOOST_SERIALIZATION_SPLIT_MEMBER()
};
template< class I, class F, class S >
inline bool is_null( const ptr_map_iterator<I,F,S>& i )
{
return i->second == 0;
}
} // namespace 'boost'
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -