Files
boost_intrusive/example/doc_auto_unlink.cpp

83 lines
2.1 KiB
C++
Raw Normal View History

2007-05-04 21:30:54 +00:00
/////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2006-2008
2007-05-04 21:30:54 +00:00
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/intrusive for documentation.
//
/////////////////////////////////////////////////////////////////////////////
//[doc_auto_unlink_code
#include <boost/intrusive/list.hpp>
#include <cassert>
using namespace boost::intrusive;
typedef list_base_hook<link_mode<auto_unlink> > auto_unlink_hook;
class MyClass : public auto_unlink_hook
2007-05-04 21:30:54 +00:00
//This hook removes the node in the destructor
{
int int_;
public:
MyClass(int i = 0) : int_(i) {}
void unlink() { auto_unlink_hook::unlink(); }
bool is_linked() { return auto_unlink_hook::is_linked(); }
2007-05-04 21:30:54 +00:00
};
//Define a list that will store values using the base hook
2007-05-04 21:30:54 +00:00
//The list can't have constant-time size!
typedef list< MyClass, constant_time_size<false> > List;
2007-05-04 21:30:54 +00:00
int main()
{
//Create the list
List l;
2007-05-04 21:30:54 +00:00
{
//Create myclass and check it's linked
MyClass myclass;
assert(myclass.is_linked() == false);
//Insert the object
l.push_back(myclass);
2007-05-04 21:30:54 +00:00
//Check that we have inserted the object
assert(l.empty() == false);
assert(&l.front() == &myclass);
2007-05-04 21:30:54 +00:00
assert(myclass.is_linked() == true);
//Now myclass' destructor will unlink it
//automatically
}
//Check auto-unlink has been executed
assert(l.empty() == true);
2007-05-04 21:30:54 +00:00
{
//Now test the unlink() function
//Create myclass and check it's linked
MyClass myclass;
assert(myclass.is_linked() == false);
//Insert the object
l.push_back(myclass);
2007-05-04 21:30:54 +00:00
//Check that we have inserted the object
assert(l.empty() == false);
assert(&l.front() == &myclass);
2007-05-04 21:30:54 +00:00
assert(myclass.is_linked() == true);
//Now unlink the node
myclass.unlink();
//Check auto-unlink has been executed
assert(l.empty() == true);
2007-05-04 21:30:54 +00:00
}
return 0;
}
//]