Boost.Intrusive 容器 - 不同大小的元素
Boost.Intrusive Containers - Elements with different size
在 "When to use?" https://www.boost.org/doc/libs/1_72_0/doc/html/intrusive/usage_when.html 章的 Boost.Intrusive 文档中,声明您可以使用包含不同或未知大小对象的侵入式容器。
#include <boost/intrusive/list.hpp>
using namespace boost::intrusive;
//An abstract class that can be inserted in an intrusive list
class Window : public list_base_hook<>
{
public:
//This is a container those value is an abstract class: you can't do this with std::list.
typedef list<Window> win_list;
//A static intrusive list declaration
static win_list all_windows;
//Constructor. Includes this window in the list
Window() { all_windows.push_back(*this); }
//Destructor. Removes this node from the list
virtual ~Window() { all_windows.erase(win_list::s_iterator_to(*this)); }
//Pure virtual function to be implemented by derived classes
virtual void Paint() = 0;
};
//The static intrusive list declaration
Window::win_list Window::all_windows;
//Some Window derived classes
class FrameWindow : public Window
{ void Paint(){/**/} };
class EditWindow : public Window
{ void Paint(){/**/} };
class CanvasWindow : public Window
{ void Paint(){/**/} };
//A function that prints all windows stored in the intrusive list
void paint_all_windows()
{
for(Window::win_list::iterator i(Window::all_windows.begin())
, e(Window::all_windows.end())
; i != e; ++i)
i->Paint();
}
//...
//A class derived from Window
class MainWindow : public Window
{
FrameWindow frame_; //these are derived from Window too
EditWindow edit_;
CanvasWindow canvas_;
public:
void Paint(){/**/}
//...
};
//Main function
int main()
{
//When a Window class is created, is automatically registered in the global list
MainWindow window;
//Paint all the windows, sub-windows and so on
paint_all_windows();
//All the windows are automatically unregistered in their destructors.
return 0;
}
我的问题是我不明白什么时候有人会需要它(用法示例?)。而且我不明白给定示例中的哪一部分显示了这种行为以及为什么不能使用标准容器执行此操作。
My problem is that I don't understand when someone would need that
他们测量了程序的性能,发现使用 std::vector<Window *>
的速度下降是不可接受的。给定的示例显示 "how to",它不是 "when to".
的示例
And I dont understand which part in the given example shows this behaviour and why you cannot do this with standard containers.
你不能有 std::vector<Window>
(也不可能有 std::list<Window>
等等),因为 Window
是一个抽象类型。 std::
个容器拥有它们包含的 Window
个对象,并为它们分配了 space 个对象。
侵入式列表之所以有效,是因为 boost::intrusive::list_base_hook
基包含上一个/下一个指针,并且对象包含钩子。这意味着 Window
对象可以存在于任何地方,例如在 main
中以 MainWindow window
为例,它的成员是 Windows
.
在 "When to use?" https://www.boost.org/doc/libs/1_72_0/doc/html/intrusive/usage_when.html 章的 Boost.Intrusive 文档中,声明您可以使用包含不同或未知大小对象的侵入式容器。
#include <boost/intrusive/list.hpp>
using namespace boost::intrusive;
//An abstract class that can be inserted in an intrusive list
class Window : public list_base_hook<>
{
public:
//This is a container those value is an abstract class: you can't do this with std::list.
typedef list<Window> win_list;
//A static intrusive list declaration
static win_list all_windows;
//Constructor. Includes this window in the list
Window() { all_windows.push_back(*this); }
//Destructor. Removes this node from the list
virtual ~Window() { all_windows.erase(win_list::s_iterator_to(*this)); }
//Pure virtual function to be implemented by derived classes
virtual void Paint() = 0;
};
//The static intrusive list declaration
Window::win_list Window::all_windows;
//Some Window derived classes
class FrameWindow : public Window
{ void Paint(){/**/} };
class EditWindow : public Window
{ void Paint(){/**/} };
class CanvasWindow : public Window
{ void Paint(){/**/} };
//A function that prints all windows stored in the intrusive list
void paint_all_windows()
{
for(Window::win_list::iterator i(Window::all_windows.begin())
, e(Window::all_windows.end())
; i != e; ++i)
i->Paint();
}
//...
//A class derived from Window
class MainWindow : public Window
{
FrameWindow frame_; //these are derived from Window too
EditWindow edit_;
CanvasWindow canvas_;
public:
void Paint(){/**/}
//...
};
//Main function
int main()
{
//When a Window class is created, is automatically registered in the global list
MainWindow window;
//Paint all the windows, sub-windows and so on
paint_all_windows();
//All the windows are automatically unregistered in their destructors.
return 0;
}
我的问题是我不明白什么时候有人会需要它(用法示例?)。而且我不明白给定示例中的哪一部分显示了这种行为以及为什么不能使用标准容器执行此操作。
My problem is that I don't understand when someone would need that
他们测量了程序的性能,发现使用 std::vector<Window *>
的速度下降是不可接受的。给定的示例显示 "how to",它不是 "when to".
And I dont understand which part in the given example shows this behaviour and why you cannot do this with standard containers.
你不能有 std::vector<Window>
(也不可能有 std::list<Window>
等等),因为 Window
是一个抽象类型。 std::
个容器拥有它们包含的 Window
个对象,并为它们分配了 space 个对象。
侵入式列表之所以有效,是因为 boost::intrusive::list_base_hook
基包含上一个/下一个指针,并且对象包含钩子。这意味着 Window
对象可以存在于任何地方,例如在 main
中以 MainWindow window
为例,它的成员是 Windows
.