doc_entity.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. using namespace boost::intrusive;
  15. //A class that can be inserted in an intrusive list
  16. class entity : public list_base_hook<>
  17. {
  18. public:
  19. virtual ~entity();
  20. //...
  21. };
  22. //"some_entity" derives from "entity"
  23. class some_entity : public entity
  24. {/**/};
  25. //Definition of the intrusive list
  26. struct entity_list : list<entity>
  27. {
  28. ~entity_list()
  29. {
  30. // entity's destructor removes itself from the global list implicitly
  31. while (!this->empty())
  32. delete &this->front();
  33. }
  34. };
  35. //A global list
  36. entity_list global_list;
  37. //The destructor removes itself from the global list
  38. entity::~entity()
  39. { global_list.erase(entity_list::s_iterator_to(*this)); }
  40. //Function to insert a new "some_entity" in the global list
  41. void insert_some_entity()
  42. { global_list.push_back (*new some_entity(/*...*/)); }
  43. int main()
  44. {
  45. //Insert some new entities
  46. insert_some_entity();
  47. insert_some_entity();
  48. //global_list's destructor will free objects
  49. return 0;
  50. }
  51. //]