Choreonoid  1.8
ItemList.h
Go to the documentation of this file.
1 
5 #ifndef CNOID_BASE_ITEM_LIST_H
6 #define CNOID_BASE_ITEM_LIST_H
7 
8 #include "Item.h"
9 #include <cnoid/PolymorphicPointerArray>
10 #include "exportdecl.h"
11 
12 namespace cnoid {
13 
14 class Item;
15 
16 class CNOID_EXPORT ItemListBase
17 {
18 public:
19  virtual bool push_back_if_type_matches(Item* item) = 0;
20  bool extractSubTreeItemsSub(Item* item);
21  bool extractParentItemsSub(Item* item);
22  bool extractAssociatedItemsSub(Item* item);
23  bool extractSubItemsSub(Item* item);
24 };
25 
26 
27 template<class ItemType>
28 class ItemList : public PolymorphicPointerArray<ItemType, ref_ptr<ItemType>>, public ItemListBase
29 {
31 
32 public:
33  ItemList() { }
34 
35  template <class RhsObjectType>
37  : ArrayBase(rhs) { }
38 
39  template <class SubType>
40  SubType* get(int index) const { return dynamic_cast<SubType*>(ArrayBase::operator[](index).get()); }
41 
42  ItemType* get(int index) const { return ArrayBase::operator[](index).get(); }
43 
44  ItemType* toSingle(bool allowFromMultiElements = false) const {
45  return (ArrayBase::size() == 1 || (allowFromMultiElements && !ArrayBase::empty())) ?
46  ArrayBase::front().get() : nullptr;
47  }
48 
50  bool extractSubTreeItems(const Item* root){
52  return ItemListBase::extractSubTreeItemsSub(const_cast<Item*>(root));
53  }
54 
56  bool extractChildItems(const Item* item){
57  return extractSubTreeItems(item);
58  }
59 
60  bool extractAssociatedItems(const Item* item){
62  return ItemListBase::extractAssociatedItemsSub(const_cast<Item*>(item));
63  }
64 
65  bool extractSubItems(const Item* item){
67  return ItemListBase::extractSubItemsSub(const_cast<Item*>(item));
68  }
69 
70  ItemType* find(const std::string& name){
71  for(size_t i=0; i < ArrayBase::size(); ++i){
72  if((*this)[i]->name() == name){
73  return (*this)[i];
74  }
75  }
76  return nullptr;
77  }
78 
79 private:
80  virtual bool push_back_if_type_matches(Item* item) override {
81  if(ItemType* casted = dynamic_cast<ItemType*>(item)){
82  ArrayBase::push_back(casted);
83  return true;
84  }
85  return false;
86  }
87 };
88 
89 }
90 
91 #endif
cnoid::ItemListBase
Definition: ItemList.h:16
cnoid::PolymorphicPointerArray::size
std::size_t size() const
Definition: PolymorphicPointerArray.h:73
cnoid::PolymorphicPointerArray::push_back
void push_back(const PointerType &pointer)
Definition: PolymorphicPointerArray.h:121
Item.h
cnoid::PolymorphicPointerArray::clear
void clear()
Definition: PolymorphicPointerArray.h:117
cnoid::PolymorphicPointerArray::operator[]
PointerType & operator[](std::size_t i)
Definition: PolymorphicPointerArray.h:109
cnoid::ItemList::extractChildItems
bool extractChildItems(const Item *item)
Definition: ItemList.h:56
cnoid::ItemList::extractSubTreeItems
bool extractSubTreeItems(const Item *root)
Definition: ItemList.h:50
cnoid::PolymorphicPointerArray
Definition: PolymorphicPointerArray.h:21
cnoid::PolymorphicPointerArray::empty
bool empty() const
Definition: PolymorphicPointerArray.h:61
cnoid::ItemList::ItemList
ItemList(const ItemList< RhsObjectType > &rhs)
Definition: ItemList.h:36
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::Item
Definition: Item.h:29
cnoid::ItemList::extractSubItems
bool extractSubItems(const Item *item)
Definition: ItemList.h:65
cnoid::ItemList
Definition: Item.h:20
cnoid::ItemList::find
ItemType * find(const std::string &name)
Definition: ItemList.h:70
cnoid::ItemList::get
SubType * get(int index) const
Definition: ItemList.h:40
cnoid::ItemList::get
ItemType * get(int index) const
Definition: ItemList.h:42
cnoid::ItemListBase::extractAssociatedItemsSub
bool extractAssociatedItemsSub(Item *item)
Definition: ItemList.cpp:56
cnoid::PolymorphicPointerArray::front
PointerType & front()
Definition: PolymorphicPointerArray.h:101
cnoid::ItemList::extractAssociatedItems
bool extractAssociatedItems(const Item *item)
Definition: ItemList.h:60
cnoid::ItemListBase::extractSubTreeItemsSub
bool extractSubTreeItemsSub(Item *item)
Definition: ItemList.cpp:28
cnoid::ItemList::ItemList
ItemList()
Definition: ItemList.h:33
cnoid::ItemListBase::extractSubItemsSub
bool extractSubItemsSub(Item *item)
Definition: ItemList.cpp:66
cnoid::ItemList::toSingle
ItemType * toSingle(bool allowFromMultiElements=false) const
Definition: ItemList.h:44