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