1 ///////////////////////////////////////////////////////////////////////////// 2 // 3 // (C) Copyright Ion Gaztanaga 2006-2013 4 // 5 // Distributed under the Boost Software License, Version 1.0. 6 // (See accompanying file LICENSE_1_0.txt or copy at 7 // http://www.boost.org/LICENSE_1_0.txt) 8 // 9 // See http://www.boost.org/libs/intrusive for documentation. 10 // 11 ///////////////////////////////////////////////////////////////////////////// 12 //[doc_entity_code 13 #include <boost/intrusive/list.hpp> 14 15 using namespace boost::intrusive; 16 17 //A class that can be inserted in an intrusive list 18 class entity : public list_base_hook<> 19 { 20 public: 21 virtual ~entity(); 22 //... 23 }; 24 25 //"some_entity" derives from "entity" 26 class some_entity : public entity 27 {/**/}; 28 29 //Definition of the intrusive list 30 struct entity_list : list<entity> 31 { ~entity_listentity_list32 ~entity_list() 33 { 34 // entity's destructor removes itself from the global list implicitly 35 while (!this->empty()) 36 delete &this->front(); 37 } 38 }; 39 40 //A global list 41 entity_list global_list; 42 43 //The destructor removes itself from the global list ~entity()44entity::~entity() 45 { global_list.erase(entity_list::s_iterator_to(*this)); } 46 47 //Function to insert a new "some_entity" in the global list insert_some_entity()48void insert_some_entity() 49 { global_list.push_back (*new some_entity(/*...*/)); } 50 main()51int main() 52 { 53 //Insert some new entities 54 insert_some_entity(); 55 insert_some_entity(); 56 //global_list's destructor will free objects 57 return 0; 58 } 59 60 //] 61