節點儲存空間是容器中物件的屬性 您就能檢查物件本身 目前是否包含在容器中這項檢查程序 O(1) 作業。
本指南的章節將介紹多種測試物件的方式 目前是容器目前所屬容器的成員 。
使用混合功能測試單一容器物件
所有節點狀態類別都提供公開的 InContainer() 方法,可用於
,測試節點狀態執行個體目前是否為
就像物件的 Listable 或 Containable 混合使用
衍生的內容由於物件源自於混和,因此也會公開
方法,因此測試以混合方式的物件,並且只能
適用於容器成員,就像呼叫容器一樣簡單
物件的 InContainer()。
class Obj : public fbl::SinglyLinkedLisable<Obj*> { /* ... */ };
Obj* the_obj = new Obj();
fbl::SinglyLinkedList<Obj*> the_list;
ASSERT(the_obj->InContainer() == false);
the_list.push_front(the_obj);
ASSERT(the_obj->InContainer() == true);
the_list.clear();
ASSERT(the_obj->InContainer() == false);
delete the_obj;
使用混合變數的多個容器物件,不含 ContainableBaseClasses
當某個物件因為衍生自多個容器而成為多個容器的成員時
提供多種混合使用,情況可能會比較複雜。物件本身
現在,有多個沿用自上層項目的 InContainer() 實作
因此,呼叫端必須明確指出想致電的商家。
雖然這當然可行,但語法並不難理解,但若使用
包裝內容物例如:
class Obj : public fbl::DoublyLinkedListable<Obj*>,
            public fbl::WAVLTreeContainable<Obj*> {
 public:
   // ...
   bool InList() const { return this->DoublyLinkedListable<Obj*>::InContainer(); }
   bool InTree() const { return this->WAVLTreeContainable<Obj*>::InContainer(); }
   // ...
};
void test(const Obj& obj) {
  bool in_list, in_tree;
  // The hard way
  in_list = obj.DoublyLinkedListable<Obj*>::InContainer();
  in_tree = obj.WAVLTreeContainable<Obj*>::InContainer();
  // The slightly easier way (the class still needs to implement the hard way)
  in_list = obj.InList();
  in_tree = obj.InTree();
}
混合使用多個容器物件,其中 ContainableBaseClasses
將 ContainableBaseClasses 用於多個容器中的現有項目
還能省去管理廣告代碼的麻煩
選取要測試成員資格的容器。「fbl::」提供
獨立的 InContainer 函式,可與標記搭配使用
可以輕鬆測試成員資格我們來看看上一個範例
使用 ContainableBaseClasses。
struct ListTag {};
struct TreeTag {};
class Obj
  : public fbl::ContainableBaseClasses<fbl::TaggedDoublyLinkedListable<Obj*, ListTag>,
                                       fbl::TaggedWAVLTreeContainable<Obj*, TreeTag>> { /* ... */ };
void test(const Obj& obj) {
  bool in_list = fbl::InContainer<ListTag>(obj);
  bool in_tree = fbl::InContainer<TreeTag>(obj);
}
直接使用 NodeState 物件進行測試
最後,如果使用 NodeState 物件和自訂特徵
您還是可以測試容器成員資格,但必須詢問 NodeState
執行個體。
class Obj {
 public:
  // Obj impl here
  bool InFooList() const { return foo_list_node_.InContainer(); }
  bool InBarList() const { return bar_list_node_.InContainer(); }
 private:
  struct FooListTraits {
    static auto& node_state(Obj& obj) {
      return obj.foo_list_node_;
    }
  };
  struct BarListTraits {
    static auto& node_state(Obj& obj) {
      return obj.bar_list_node_;
    }
  };
  friend struct FooListTraits;
  friend struct BarListTraits;
  fbl::DoublyLinkedListNodeState<Obj*> foo_list_node_;
  fbl::DoublyLinkedListNodeState<fbl::RefPtr<Obj>> bar_list_node_;
 public:
  using FooList = fbl::DoublyLinkedListCustomTraits<Obj*, FooListTraits>;
  using BarList = fbl::DoublyLinkedListCustomTraits<fbl::RefPtr<Obj>, BarListTraits>;
};