diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
commit | 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch) | |
tree | e5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/boost/libs/intrusive/example/doc_auto_unlink.cpp | |
parent | Initial commit. (diff) | |
download | ceph-upstream.tar.xz ceph-upstream.zip |
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/intrusive/example/doc_auto_unlink.cpp')
-rw-r--r-- | src/boost/libs/intrusive/example/doc_auto_unlink.cpp | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/src/boost/libs/intrusive/example/doc_auto_unlink.cpp b/src/boost/libs/intrusive/example/doc_auto_unlink.cpp new file mode 100644 index 00000000..b44dd311 --- /dev/null +++ b/src/boost/libs/intrusive/example/doc_auto_unlink.cpp @@ -0,0 +1,83 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2006-2013 +// +// 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 + //This hook removes the node in the destructor +{ + int int_; + + public: + MyClass(int i = 0) : int_(i) {} + int get_int() { return int_; } + void unlink() { auto_unlink_hook::unlink(); } + bool is_linked() { return auto_unlink_hook::is_linked(); } +}; + +//Define a list that will store values using the base hook +//The list can't have constant-time size! +typedef list< MyClass, constant_time_size<false> > List; + +int main() +{ + //Create the list + List l; + { + //Create myclass and check it's linked + MyClass myclass; + assert(myclass.is_linked() == false); + + //Insert the object + l.push_back(myclass); + + //Check that we have inserted the object + assert(l.empty() == false); + assert(&l.front() == &myclass); + assert(myclass.is_linked() == true); + + //Now myclass' destructor will unlink it + //automatically + } + + //Check auto-unlink has been executed + assert(l.empty() == true); + + { + //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); + + //Check that we have inserted the object + assert(l.empty() == false); + assert(&l.front() == &myclass); + assert(myclass.is_linked() == true); + + //Now unlink the node + myclass.unlink(); + + //Check auto-unlink has been executed + assert(l.empty() == true); + } + return 0; +} +//] |