Choreonoid  1.5
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 
11 namespace cnoid {
12 
13 template<class ItemType = Item>
14 class ItemList : public PolymorphicPointerArray<ItemType, ref_ptr<ItemType> >
15 {
17 
18 public:
19  ItemList() { }
20 
21  template <class RhsObjectType>
23  : ArrayBase(rhs) { }
24 
25  template <class SubType>
26  SubType* get(int index) const { return dynamic_cast<SubType*>(ArrayBase::operator[](index).get()); }
27 
28  ItemType* get(int index) const { return ArrayBase::operator[](index).get(); }
29 
32  extractChildItemsSub(item->childItem());
33  return !ArrayBase::empty();
34  }
35 
36  ItemType* toSingle(bool allowFromMultiElements = false) const {
37  return (ArrayBase::size() == 1 || (allowFromMultiElements && !ArrayBase::empty())) ?
38  ArrayBase::front().get() : 0;
39  }
40 
43  for(Item* child = item->childItem(); child; child = child->nextItem()){
44  if(child->isSubItem()){
45  ItemType* targetItem = dynamic_cast<ItemType*>(child);
46  if(targetItem){
47  ArrayBase::push_back(targetItem);
48  }
49  }
50  }
51  return !ArrayBase::empty();
52  }
53 
54  ItemType* find(const std::string& name){
55  for(size_t i=0; i < ArrayBase::size(); ++i){
56  if((*this)[i]->name() == name){
57  return (*this)[i];
58  }
59  }
60  return 0;
61  }
62 
63 private:
64  void extractChildItemsSub(Item* item){
65  if(item){
66  ItemType* targetItem = dynamic_cast<ItemType*>(item);
67  if(targetItem){
68  ArrayBase::push_back(targetItem);
69  }
70  extractChildItemsSub(item->childItem());
71  extractChildItemsSub(item->nextItem());
72  }
73  }
74 };
75 
76 }
77 
78 #endif
ItemType * toSingle(bool allowFromMultiElements=false) const
Definition: ItemList.h:36
Definition: ItemList.h:14
Item * nextItem() const
Definition: Item.h:76
ref_ptr< ItemType > & operator[](std::size_t i)
Definition: PolymorphicPointerArray.h:109
Item * childItem() const
Definition: Item.h:74
ref_ptr< ItemType > & front()
Definition: PolymorphicPointerArray.h:101
ItemList(const ItemList< RhsObjectType > &rhs)
Definition: ItemList.h:22
bool extractSubItems(ItemPtr item)
Definition: ItemList.h:41
bool extractChildItems(ItemPtr item)
Definition: ItemList.h:30
Definition: PolymorphicPointerArray.h:21
Definition: Item.h:38
Defines the minimum processing for performing pasing file for STL.
Definition: AbstractSceneLoader.h:9
ItemType * find(const std::string &name)
Definition: ItemList.h:54
std::size_t size() const
Definition: PolymorphicPointerArray.h:73
bool empty() const
Definition: PolymorphicPointerArray.h:61
T * get() const
Definition: Referenced.h:197
void clear()
Definition: PolymorphicPointerArray.h:117
void push_back(const ref_ptr< ItemType > &pointer)
Definition: PolymorphicPointerArray.h:121
ItemList()
Definition: ItemList.h:19