![]() |
Home | Libraries | People | FAQ | More |
This section will expand the explanation of previously presented basic concepts
before explaining the customization options of Boost.Intrusive.
在說明 Boost.Intrusive 的定制化選項之前,本節先展開對先前提出的基本概念的解釋。
template<class NodeTraits> struct my_slist_algorithms { typedef typename NodeTraits::node_ptr node_ptr; typedef typename NodeTraits::const_node_ptr const_node_ptr; //Get the previous node of "this_node" 取得 "this_node" 的前一個節點
static node_ptr get_prev_node(node_ptr this_node) { node_ptr p = this_node; while (this_node != NodeTraits::get_next(p)) p = NodeTraits::get_next(p); return p; } // number of elements in the group of nodes containing "this_node" 含有 "this_node" 的節點組的元素數量
static std::size_t count(const_node_ptr this_node)
{ std::size_t result = 0; const_node_ptr p = this_node; do{ p = NodeTraits::get_next(p); ++result; } while (p != this_node); return result; } // More operations 更多操作
// ...
};
my_slist_algorithms:my_slist_algorithms:
struct my_slist_node_traits { //The type of the node 節點的類型
struct node { node *next_; };
typedef node * node_ptr; typedef const node * const_node_ptr; //A function to obtain a pointer to the next node 取得指向下一個節點的指針的函數
static node_ptr get_next(const_node_ptr n) { return n->next_; }
//A function to set the pointer to the next node 設置下一個節點的指針的函數
static void set_next(node_ptr n, node_ptr next) { n->next_ = next; }
};
class my_slist_base_hook //This hook contains a node, that will be used
//to link the user object in the group of nodes
//這個鉤子包含一個節點,用於將用戶對像鏈接到節點組
: private my_slist_node_traits::node { typedef my_slist_node_traits::node_ptr node_ptr; typedef my_slist_node_traits::const_node_ptr const_node_ptr; //Converts the generic node to the hook 將普通節點轉換為鉤子
static my_slist_base_hook *to_hook_ptr(node_ptr p) { return static_cast<my_slist_base_hook*>(p); } //Returns the generic node stored by this hook 返回由本鉤子保存的普通節點
node_ptr to_node_ptr() { return static_cast<node *const>(this); } // More operations 更多操作
// ...
}; //To make MyClass compatible with an intrusive singly linked list
//derive our class from the hook.
//要使得 MyClass 兼容於介入式單鏈表,從鉤子派生我們的類
class MyClass : public my_slist_base_hook { void set(int value); int get() const; private: int value_; };
slist
container (intrusive singly linked list) should be able to hold MyClass objects that might have decided
to store the hook as a base class or as a member. Internally, the container
will use Node Algorithms to implement its
operations, and an intrusive container is configured using a template parameter
called ValueTraits. ValueTraits
will contain the information to convert user classes in nodes compatible
with Node Algorithms. For example, this
a possible slist implementation:slist
容器(介入式單鏈表)可以保存 MyClass 對象,後者可以決定以基類方式或成員方式保存鉤子。在容器的內部,使用 節點算法 來實現各種操作,並且,介入式容器使用一個名為 ValueTraits 的模板參數進行配置上。ValueTraits
包含有一些信息,以將用戶類轉換為與 節點算法 相兼容的節點。例如,下面是一個可能的 slist 實現:
template<class ValueTraits, ...> class slist { public: typedef typename ValueTraits::value_type value_type; //More typedefs and functions 更多 typedef 和函數
// ...
//Insert the value as the first element of the list 將 value 作為鏈表的第一個元素插入
void push_front (reference value)
{ node_ptr to_insert(ValueTraits::to_node_ptr(value)); circular_list_algorithms::link_after(to_insert, get_root_node());
} // More operations 更多操作
// ...
};
struct my_slist_derivation_value_traits { public: typedef slist_node_traits node_traits; typedef MyClass value_type; typedef node_traits::node_ptr node_ptr; typedef value_type* pointer; //...
//Converts user's value to a generic node 將用戶的值轉換為普通節點
static node_ptr to_node_ptr(reference value) { return static_cast<slist_base_hook &>(value).to_node_ptr(); } //Converts a generic node into user's value 將普通節點轉換為用戶值
static value_type *to_value_ptr(node_traits::node *n)
{ static_cast<value_type*>(slist_base_hook::to_hook_ptr(n)); } // More operations 更多操作
// ...
};