doc_window.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_window_code
  13. #include <boost/intrusive/list.hpp>
  14. using namespace boost::intrusive;
  15. //An abstract class that can be inserted in an intrusive list
  16. class Window : public list_base_hook<>
  17. {
  18. public:
  19. //This is a container those value is an abstract class: you can't do this with std::list.
  20. typedef list<Window> win_list;
  21. //A static intrusive list declaration
  22. static win_list all_windows;
  23. //Constructor. Includes this window in the list
  24. Window() { all_windows.push_back(*this); }
  25. //Destructor. Removes this node from the list
  26. virtual ~Window() { all_windows.erase(win_list::s_iterator_to(*this)); }
  27. //Pure virtual function to be implemented by derived classes
  28. virtual void Paint() = 0;
  29. };
  30. //The static intrusive list declaration
  31. Window::win_list Window::all_windows;
  32. //Some Window derived classes
  33. class FrameWindow : public Window
  34. { void Paint(){/**/} };
  35. class EditWindow : public Window
  36. { void Paint(){/**/} };
  37. class CanvasWindow : public Window
  38. { void Paint(){/**/} };
  39. //A function that prints all windows stored in the intrusive list
  40. void paint_all_windows()
  41. {
  42. for(Window::win_list::iterator i(Window::all_windows.begin())
  43. , e(Window::all_windows.end())
  44. ; i != e; ++i)
  45. i->Paint();
  46. }
  47. //...
  48. //A class derived from Window
  49. class MainWindow : public Window
  50. {
  51. FrameWindow frame_; //these are derived from Window too
  52. EditWindow edit_;
  53. CanvasWindow canvas_;
  54. public:
  55. void Paint(){/**/}
  56. //...
  57. };
  58. //Main function
  59. int main()
  60. {
  61. //When a Window class is created, is automatically registered in the global list
  62. MainWindow window;
  63. //Paint all the windows, sub-windows and so on
  64. paint_all_windows();
  65. //All the windows are automatically unregistered in their destructors.
  66. return 0;
  67. }
  68. //]