Choreonoid  1.8
PolymorphicReferencedArray.h
Go to the documentation of this file.
1 
5 #ifndef CNOID_UTIL_POLYMORPHIC_REFERENCED_ARRAY_H
6 #define CNOID_UTIL_POLYMORPHIC_REFERENCED_ARRAY_H
7 
8 #include "Referenced.h"
9 #include <vector>
10 
11 namespace cnoid {
12 
13 template <class BaseReferencedType = Referenced>
15 {
16 public:
18  virtual bool try_push_back(BaseReferencedType* obj) = 0;
19  virtual BaseReferencedType* get_element(size_t i) = 0;
20  virtual const BaseReferencedType* get_element(size_t i) const = 0;
21  virtual size_t get_size() const = 0;
22  virtual void clear_elements() = 0;
23 };
24 
25 
26 template <class ReferencedType, class BaseReferencedType = Referenced, class PointerType = ReferencedType*>
28 {
29  typedef std::vector<PointerType> Container;
30  Container elements;
31 
32 public:
34  typedef typename Container::value_type value_type;
35  typedef typename Container::iterator iterator;
36  typedef typename Container::const_iterator const_iterator;
37  typedef typename Container::reference reference;
38  typedef typename Container::const_reference const_reference;
39 
41 
42  template <class RhsReferencedType, class RhsPointerType>
45  (*this) << rhs;
46  }
47 
49 
50  virtual bool try_push_back(BaseReferencedType* obj) {
51  if(ReferencedType* p = dynamic_cast<ReferencedType*>(obj)){
52  push_back(p);
53  return true;
54  }
55  return false;
56  }
57 
58  bool operator==(const PolymorphicReferencedArray& rhs) const {
59  if(size() == rhs.size()){
60  return std::equal(begin(), end(), rhs.begin());
61  }
62  return false;
63  }
64 
65  bool operator!=(const PolymorphicReferencedArray& rhs) const {
66  return !operator==(rhs);
67  }
68 
69  template <class RhsReferencedType, class RhsPointerType>
72  clear();
73  (*this) << another;
74  return *this;
75  }
76 
77  template <class RetReferencedType, class RetPointerType>
79  for(std::size_t i=0; i < rhs.size(); ++i){
80  try_push_back(rhs[i]);
81  }
82  return *this;
83  }
84 
85  template <class RetReferencedType, class RetPointerType>
87  size_t orgSize = size();
89  typename ArgType::iterator p = another.begin();
90  while(p != another.end()){
91  if(ReferencedType* element = dynamic_cast<ReferencedType*>(p->get())){
92  push_back(element);
93  p = another.erase(p);
94  } else{
95  ++p;
96  }
97  }
98  return (size() > orgSize);
99  }
100 
101  template <class RetReferencedType> PolymorphicReferencedArray extract() {
102  PolymorphicReferencedArray extracted;
103  iterator p = begin();
104  while(p != end()){
105  if(RetReferencedType* element = dynamic_cast<RetReferencedType*>(p->get())){
106  extracted.push_back(element);
107  p = erase(p);
108  } else{
109  ++p;
110  }
111  }
112  return extracted;
113  }
114 
115  bool empty() const {
116  return elements.empty();
117  }
118 
119  void reserve(size_t size) {
120  elements.reserve(size);
121  }
122 
123  void resize(size_t size) {
124  elements.resize(size);
125  }
126 
127  std::size_t size() const {
128  return elements.size();
129  }
130 
131  virtual size_t get_size() const {
132  return size();
133  }
134 
136  return elements.begin();
137  }
138 
140  return elements.begin();
141  }
142 
144  return elements.end();
145  }
146 
147  const_iterator end() const {
148  return elements.end();
149  }
150 
151  PointerType& back() {
152  return elements.back();
153  }
154 
155  const PointerType& back() const {
156  return elements.back();
157  }
158 
159  PointerType& front() {
160  return elements.front();
161  }
162 
163  const PointerType& front() const {
164  return elements.front();
165  }
166 
167  PointerType& operator[](std::size_t i) {
168  return elements[i];
169  }
170 
171  const PointerType& operator[](std::size_t i) const {
172  return elements[i];
173  }
174 
175  virtual BaseReferencedType* get_element(size_t i) {
176  return elements[i];
177  }
178 
179  virtual const BaseReferencedType* get_element(size_t i) const {
180  return elements[i];
181  }
182 
183  void clear(){
184  elements.clear();
185  }
186 
187  virtual void clear_elements() {
188  clear();
189  }
190 
191  void push_back(const PointerType& obj){
192  elements.push_back(obj);
193  }
194 
195  void pop_back(){
196  elements.pop_back();
197  }
198 
200  return elements.erase(pos);
201  }
202 };
203 
204 }
205 
206 #endif
cnoid::PolymorphicReferencedArray::iterator
Container::iterator iterator
Definition: PolymorphicReferencedArray.h:35
cnoid::PolymorphicReferencedArrayBase::get_size
virtual size_t get_size() const =0
cnoid::PolymorphicReferencedArray::empty
bool empty() const
Definition: PolymorphicReferencedArray.h:115
Referenced.h
cnoid::PolymorphicReferencedArray::PolymorphicReferencedArray
PolymorphicReferencedArray(const PolymorphicReferencedArray< RhsReferencedType, BaseReferencedType, RhsPointerType > &rhs)
Definition: PolymorphicReferencedArray.h:43
cnoid::PolymorphicReferencedArray::ArrayBase
PolymorphicReferencedArrayBase< BaseReferencedType > ArrayBase
Definition: PolymorphicReferencedArray.h:33
cnoid::PolymorphicReferencedArray::begin
const_iterator begin() const
Definition: PolymorphicReferencedArray.h:139
cnoid::PolymorphicReferencedArray::push_back
void push_back(const PointerType &obj)
Definition: PolymorphicReferencedArray.h:191
cnoid::PolymorphicReferencedArray::end
iterator end()
Definition: PolymorphicReferencedArray.h:143
cnoid::PolymorphicReferencedArrayBase::clear_elements
virtual void clear_elements()=0
cnoid::PolymorphicReferencedArray::clear
void clear()
Definition: PolymorphicReferencedArray.h:183
cnoid::PolymorphicReferencedArray::back
const PointerType & back() const
Definition: PolymorphicReferencedArray.h:155
cnoid::PolymorphicReferencedArrayBase::try_push_back
virtual bool try_push_back(BaseReferencedType *obj)=0
cnoid::PolymorphicReferencedArray::clear_elements
virtual void clear_elements()
Definition: PolymorphicReferencedArray.h:187
cnoid::PolymorphicReferencedArrayBase::get_element
virtual BaseReferencedType * get_element(size_t i)=0
cnoid::PolymorphicReferencedArray::pop_back
void pop_back()
Definition: PolymorphicReferencedArray.h:195
cnoid::PolymorphicReferencedArray::value_type
Container::value_type value_type
Definition: PolymorphicReferencedArray.h:34
cnoid::PolymorphicReferencedArray::operator[]
const PointerType & operator[](std::size_t i) const
Definition: PolymorphicReferencedArray.h:171
cnoid::PolymorphicReferencedArray::PolymorphicReferencedArray
PolymorphicReferencedArray()
Definition: PolymorphicReferencedArray.h:40
cnoid::PolymorphicReferencedArray::get_element
virtual BaseReferencedType * get_element(size_t i)
Definition: PolymorphicReferencedArray.h:175
cnoid::PolymorphicReferencedArrayBase::~PolymorphicReferencedArrayBase
virtual ~PolymorphicReferencedArrayBase()
Definition: PolymorphicReferencedArray.h:17
cnoid::PolymorphicReferencedArray::get_size
virtual size_t get_size() const
Definition: PolymorphicReferencedArray.h:131
cnoid::PolymorphicReferencedArray::operator[]
PointerType & operator[](std::size_t i)
Definition: PolymorphicReferencedArray.h:167
cnoid::PolymorphicReferencedArray::assign
PolymorphicReferencedArray & assign(const PolymorphicReferencedArray< RhsReferencedType, BaseReferencedType, RhsPointerType > &another)
Definition: PolymorphicReferencedArray.h:70
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::PolymorphicReferencedArray::const_reference
Container::const_reference const_reference
Definition: PolymorphicReferencedArray.h:38
cnoid::PolymorphicReferencedArray::extractFrom
bool extractFrom(PolymorphicReferencedArray< RetReferencedType, BaseReferencedType, RetPointerType > &another)
Definition: PolymorphicReferencedArray.h:86
cnoid::PolymorphicReferencedArray::erase
iterator erase(iterator pos)
Definition: PolymorphicReferencedArray.h:199
cnoid::PolymorphicReferencedArray::end
const_iterator end() const
Definition: PolymorphicReferencedArray.h:147
cnoid::PolymorphicReferencedArray::back
PointerType & back()
Definition: PolymorphicReferencedArray.h:151
cnoid::PolymorphicReferencedArray::const_iterator
Container::const_iterator const_iterator
Definition: PolymorphicReferencedArray.h:36
cnoid::PolymorphicReferencedArray::extract
PolymorphicReferencedArray extract()
Definition: PolymorphicReferencedArray.h:101
cnoid::PolymorphicReferencedArray::~PolymorphicReferencedArray
virtual ~PolymorphicReferencedArray()
Definition: PolymorphicReferencedArray.h:48
cnoid::PolymorphicReferencedArrayBase
Definition: PolymorphicReferencedArray.h:14
cnoid::PolymorphicReferencedArray::operator==
bool operator==(const PolymorphicReferencedArray &rhs) const
Definition: PolymorphicReferencedArray.h:58
cnoid::PolymorphicReferencedArray::begin
iterator begin()
Definition: PolymorphicReferencedArray.h:135
cnoid::PolymorphicReferencedArray::operator!=
bool operator!=(const PolymorphicReferencedArray &rhs) const
Definition: PolymorphicReferencedArray.h:65
cnoid::PolymorphicReferencedArray::try_push_back
virtual bool try_push_back(BaseReferencedType *obj)
Definition: PolymorphicReferencedArray.h:50
cnoid::PolymorphicReferencedArray::resize
void resize(size_t size)
Definition: PolymorphicReferencedArray.h:123
cnoid::PolymorphicReferencedArray::operator<<
PolymorphicReferencedArray & operator<<(const PolymorphicReferencedArray< RetReferencedType, BaseReferencedType, RetPointerType > &rhs)
Definition: PolymorphicReferencedArray.h:78
cnoid::PolymorphicReferencedArray::size
std::size_t size() const
Definition: PolymorphicReferencedArray.h:127
cnoid::PolymorphicReferencedArray
Definition: PolymorphicReferencedArray.h:27
cnoid::PolymorphicReferencedArray::front
PointerType & front()
Definition: PolymorphicReferencedArray.h:159
cnoid::PolymorphicReferencedArray::get_element
virtual const BaseReferencedType * get_element(size_t i) const
Definition: PolymorphicReferencedArray.h:179
cnoid::PolymorphicReferencedArray::reserve
void reserve(size_t size)
Definition: PolymorphicReferencedArray.h:119
cnoid::PolymorphicReferencedArray::front
const PointerType & front() const
Definition: PolymorphicReferencedArray.h:163
cnoid::PolymorphicReferencedArray::reference
Container::reference reference
Definition: PolymorphicReferencedArray.h:37