activemq-cpp-3.9.5
StlSet.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef _DECAF_UTIL_STLSET_H_
19 #define _DECAF_UTIL_STLSET_H_
20 
21 #include <set>
22 #include <vector>
23 #include <memory>
27 #include <decaf/util/Iterator.h>
28 #include <decaf/util/AbstractSet.h>
29 
30 namespace decaf {
31 namespace util {
32 
38  template <typename E>
39  class StlSet : public decaf::util::AbstractSet<E> {
40  private:
41 
42  std::set<E> values;
43 
44  private:
45 
46  class SetIterator : public Iterator<E> {
47  private:
48 
49  typename std::set<E>::iterator current;
50  typename std::set<E>::iterator previous;
51  typename std::set<E>* set;
52 
53  private:
54 
55  SetIterator(const SetIterator&);
56  SetIterator operator=(const SetIterator&);
57 
58  public:
59 
60  SetIterator(typename std::set<E>* set) :
61  Iterator<E>(), current(set->begin()), previous(set->begin()), set(set) {
62  }
63 
64  virtual ~SetIterator() {}
65 
66  virtual E next() {
67  if (this->current == set->end()) {
69  __FILE__, __LINE__, "Set::Iterator::next - No more elements to return");
70  }
71 
72  this->previous = this->current;
73  return *(this->current++);
74  }
75 
76  virtual bool hasNext() const {
77  return (this->current != set->end());
78  }
79 
80  virtual void remove() {
81  if (this->previous == set->end()) {
83  __FILE__, __LINE__, "Set::Iterator::remove - Invalid State to call remove");
84  }
85 
86  this->set->erase(this->previous);
87  this->previous = this->set->end();
88  }
89  };
90 
91  class ConstSetIterator : public Iterator<E> {
92  private:
93 
94  typename std::set<E>::const_iterator current;
95  typename std::set<E>::const_iterator previous;
96  const typename std::set<E>* set;
97 
98  private:
99 
100  ConstSetIterator(const ConstSetIterator&);
101  ConstSetIterator operator=(const ConstSetIterator&);
102 
103  public:
104 
105  ConstSetIterator(const typename std::set<E>* set) :
106  Iterator<E>(), current(set->begin()), previous(set->begin()), set(set) {
107  }
108 
109  virtual ~ConstSetIterator() {}
110 
111  virtual E next() {
112  if (this->current == set->end()) {
114  __FILE__, __LINE__, "Set::Iterator::next - No more elements to return");
115  }
116 
117  this->previous = this->current;
118  return *(this->current++);
119  }
120 
121  virtual bool hasNext() const {
122  return (this->current != set->end());
123  }
124 
125  virtual void remove() {
127  __FILE__, __LINE__, "Set::Iterator::remove - Not Valid on a Const Iterator");
128  }
129  };
130 
131  public:
132 
136  StlSet() : AbstractSet<E>(), values() {}
137 
142  StlSet(const StlSet& source) : AbstractCollection<E>(), AbstractSet<E>(), values() {
143  copy(source);
144  }
145 
150  StlSet(const Collection<E>& source) : AbstractSet<E>(), values() {
151  AbstractSet<E>::copy(source);
152  }
153 
154  virtual ~StlSet() {}
155 
160  return new SetIterator(&values);
161  }
163  return new ConstSetIterator(&values);
164  }
165 
169  virtual bool equals(const Collection<E>& collection) const {
170 
171  const StlSet<E>* setptr = dynamic_cast<const StlSet<E>*>(&collection);
172  if (setptr == NULL) {
173  return AbstractSet<E>::equals(collection);
174  }
175 
176  return this->values == setptr->values;
177  }
178 
182  virtual void copy(const Collection<E>& collection) {
183 
184  const StlSet<E>* setptr = dynamic_cast<const StlSet<E>*>(&collection);
185  if (setptr == NULL) {
186  AbstractSet<E>::copy(collection);
187  return;
188  }
189 
190  this->values.clear();
191  this->values = setptr->values;
192  }
193 
197  virtual void clear() {
198  values.clear();
199  }
200 
204  virtual bool contains(const E& value) const {
205  typename std::set<E>::const_iterator iter;
206  iter = values.find(value);
207  return iter != values.end();
208  }
209 
213  virtual bool isEmpty() const {
214  return values.empty();
215  }
216 
220  virtual int size() const {
221  return (int) values.size();
222  }
223 
227  virtual bool add(const E& value) {
228  return values.insert(value).second;
229  }
230 
234  virtual bool remove(const E& value) {
235  return values.erase(value) != 0;
236  }
237 
238  };
239 
240 }}
241 
242 #endif /*_DECAF_UTIL_STLSET_H_*/
The root interface in the collection hierarchy.
Definition: Collection.h:68
This class provides a skeletal implementation of the Collection interface, to minimize the effort req...
Definition: AbstractCollection.h:58
#define NULL
Definition: Config.h:33
virtual void copy(const Collection< E > &collection)
Renders this Collection as a Copy of the given Collection.The default implementation iterates over th...
Definition: StlSet.h:182
Iterator< E > * iterator()
an iterator over a set of elements of type T.
Definition: StlSet.h:159
virtual bool add(const E &value)
Returns true if this collection changed as a result of the call.(Returns false if this collection doe...
Definition: StlSet.h:227
Defines an object that can be used to iterate over the elements of a collection.
Definition: Iterator.h:34
virtual bool isEmpty() const
Definition: StlSet.h:213
StlSet(const Collection< E > &source)
Copy constructor - copies the content of the given set into this one.
Definition: StlSet.h:150
Definition: UnsupportedOperationException.h:32
Set template that wraps around a std::set to provide a more user-friendly interface and to provide co...
Definition: StlSet.h:39
AbstractCollection< E > & operator=(const AbstractCollection< E > &collection)
Assignment Operator, copy element from the source collection to this collection after clearing any el...
Definition: AbstractCollection.h:92
virtual ~StlSet()
Definition: StlSet.h:154
virtual bool equals(const Collection< E > &collection) const
Answers true if this Collection and the one given are the same size and if each element contained in ...
Definition: StlSet.h:169
virtual bool contains(const E &value) const
Returns true if this collection contains the specified element.More formally, returns true if and onl...
Definition: StlSet.h:204
Definition: IllegalStateException.h:32
StlSet(const StlSet &source)
Copy constructor - copies the content of the given set into this one.
Definition: StlSet.h:142
virtual void clear()
Removes all of the elements from this collection (optional operation).The collection will be empty af...
Definition: StlSet.h:197
StlSet()
Default constructor - does nothing.
Definition: StlSet.h:136
Iterator< E > * iterator() const
Definition: StlSet.h:162
Definition: NoSuchElementException.h:31
This class provides a skeletal implementation of the Set interface to minimize the effort required to...
Definition: AbstractSet.h:46
virtual int size() const
Definition: StlSet.h:220
virtual void copy(const Collection< E > &collection)
Renders this Collection as a Copy of the given Collection.
Definition: AbstractCollection.h:198
virtual bool equals(const Collection< E > &collection) const
Answers true if this Collection and the one given are the same size and if each element contained in ...
Definition: AbstractCollection.h:172