Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Any Hooks: A single hook for any Intrusive container 任意鉤子:單個鉤子用於任意介入式容器

Sometimes, a class programmer wants to place a class in several intrusive containers but no at the same time. In this case, the programmer might decide to insert two hooks in the same class.
有時候,程序員想將某個類放入多個介入式容器,但不是同時放入。在這種情況下,程序員可能決定在同一個類中插入兩個鉤子。

class MyClass
   : public list_base_hook<>, public slist_base_hook<> //...
{};

However, there is a more size-efficient alternative in Boost.Intrusive: "any" hooks (any_base_hook and any_member_hook. These hooks can be used to store a type in several containers offered by Boost.Intrusive minimizing the size of the class.
不過,Boost.Intrusive 中有一個更為節省空間的辦法:"任意"鉤子(any_base_hookany_member_hook)。這些鉤子可以用於將一個類型存入由 Boost.Intrusive 提供的多種容器,並且使得類的大小最小化。

These hooks support these options:
這些鉤子支持以下選項:

auto_unlink can't be supported because the hook does not know in which type of might be inserted container. Additionally, these hooks don't support unlink() and swap_nodes() operations for the same reason.
auto_unlink 不被支持,因為鉤子不知道它可能被插入到哪一類容器中。另外,因為同樣的原因,這些鉤子也不支持 unlink()swap_nodes() 操作。

Here's an example that creates a class with two any hooks, and uses one to insert the class in a slist and the other one in a list.
以下例子創建了一個帶有兩個任意鉤子的類,使用其中一個鉤子將類插入到一個 slist 並用另一個鉤子插入到一個 list 中。

#include <vector>
#include <boost/intrusive/any_hook.hpp>
#include <boost/intrusive/slist.hpp>
#include <boost/intrusive/list.hpp>

using namespace boost::intrusive;

class MyClass : public any_base_hook<> //Base hook 基類鉤子
{ int int_; public: any_member_hook<> member_hook_; //Member hook 成員鉤子
MyClass(int i = 0) : int_(i) {} }; int main() { //Define a base hook option that converts any_base_hook to a slist hook
//定義將 any_base_hook 轉換為 slist 鉤子的基類鉤子選項
typedef any_to_slist_hook < base_hook< any_base_hook<> > > BaseSlistOption; typedef slist<MyClass, BaseSlistOption> BaseSList; //Define a member hook option that converts any_base_hook to a list hook
//定義將 any_member_hook 轉換為 list 鉤子的成員鉤子選項
 typedef any_to_list_hook< member_hook < MyClass, any_member_hook<>, &MyClass::member_hook_> > MemberListOption; typedef list<MyClass, MemberListOption> MemberList; //Create several MyClass objects, each one with a different value 創建幾個 MyClass 對象,每個具有不同的值
std::vector<MyClass> values; for(int i = 0; i < 100; ++i){ values.push_back(MyClass(i)); } BaseSList base_slist; MemberList member_list; //Now insert them in reverse order in the slist and in order in the list
//現在將它們以反序插入到 slist 中,並以正序插入到 list 中
for(std::vector<MyClass>::iterator it(values.begin()), itend(values.end()); it != itend; ++it) base_slist.push_front(*it), member_list.push_back(*it); //Now test lists 現在測試兩個鏈表
BaseSList::iterator bit(base_slist.begin()), bitend(base_slist.end()); MemberList::reverse_iterator mrit(member_list.rbegin()), mritend(member_list.rend()); std::vector<MyClass>::reverse_iterator rit(values.rbegin()), ritend(values.rend()); //Test the objects inserted in the base hook list 測試插入到基類鉤子鏈表中的對象
for(; rit != ritend; ++rit, ++bit, ++mrit) if(&*bit != &*rit || &*mrit != &*rit) return 1; return 0; }


PrevUpHomeNext