activemq-cpp-3.8.2
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__,
70  "Set::Iterator::next - No more elements to return" );
71  }
72 
73  this->previous = this->current;
74  return *( this->current++ );
75  }
76 
77  virtual bool hasNext() const {
78  return ( this->current != set->end() );
79  }
80 
81  virtual void remove() {
82  if( this->previous == set->end() ) {
84  __FILE__, __LINE__,
85  "Set::Iterator::remove - Invalid State to call remove" );
86  }
87 
88  this->set->erase( this->previous );
89  this->previous = this->set->end();
90  }
91  };
92 
93  class ConstSetIterator : public Iterator<E> {
94  private:
95 
96  typename std::set<E>::const_iterator current;
97  typename std::set<E>::const_iterator previous;
98  const typename std::set<E>* set;
99 
100  private:
101 
102  ConstSetIterator( const ConstSetIterator& );
103  ConstSetIterator operator= ( const ConstSetIterator& );
104 
105  public:
106 
107  ConstSetIterator( const typename std::set<E>* set ) :
108  Iterator<E>(), current( set->begin() ), previous( set->begin() ), set( set ) {
109  }
110 
111  virtual ~ConstSetIterator() {}
112 
113  virtual E next() {
114  if( this->current == set->end() ) {
116  __FILE__, __LINE__,
117  "Set::Iterator::next - No more elements to return" );
118  }
119 
120  this->previous = this->current;
121  return *( this->current++ );
122  }
123 
124  virtual bool hasNext() const {
125  return ( this->current != set->end() );
126  }
127 
128  virtual void remove() {
130  __FILE__, __LINE__,
131  "Set::Iterator::remove - Not Valid on a Const Iterator" );
132  }
133  };
134 
135  public:
136 
140  StlSet() : AbstractSet<E>(), values() {}
141 
147  StlSet( const StlSet& source ) : AbstractSet<E>(), values() {
148  copy( source );
149  }
150 
156  StlSet( const Collection<E>& source ) : AbstractSet<E>(), values() {
157  AbstractSet<E>::copy( source );
158  }
159 
160  virtual ~StlSet() {}
161 
166  return new SetIterator( &values );
167  }
169  return new ConstSetIterator( &values );
170  }
171 
175  virtual bool equals( const Collection<E>& collection ) const {
176 
177  const StlSet<E>* setptr = dynamic_cast<const StlSet<E>*>( &collection );
178  if( setptr == NULL ) {
179  return AbstractSet<E>::equals( collection );
180  }
181 
182  return this->values == setptr->values;
183  }
184 
188  virtual void copy( const Collection<E>& collection ) {
189 
190  const StlSet<E>* setptr = dynamic_cast<const StlSet<E>*>( &collection );
191  if( setptr == NULL ) {
192  AbstractSet<E>::copy( collection );
193  return;
194  }
195 
196  this->values.clear();
197  this->values = setptr->values;
198  }
199 
203  virtual void clear() {
204  values.clear();
205  }
206 
210  virtual bool contains( const E& value ) const {
211  typename std::set<E>::const_iterator iter;
212  iter = values.find( value );
213  return iter != values.end();
214  }
215 
219  virtual bool isEmpty() const {
220  return values.empty();
221  }
222 
226  virtual int size() const {
227  return (int)values.size();
228  }
229 
233  virtual bool add( const E& value ) {
234  return values.insert( value ).second;
235  }
236 
240  virtual bool remove( const E& value ) {
241  return values.erase( value ) != 0;
242  }
243 
244  };
245 
246 }}
247 
248 #endif /*_DECAF_UTIL_STLSET_H_*/
virtual int size() const
Definition: StlSet.h:226
The root interface in the collection hierarchy.
Definition: Collection.h:68
virtual bool isEmpty() const
Definition: StlSet.h:219
#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:188
Iterator< E > * iterator()
an iterator over a set of elements of type T.
Definition: StlSet.h:165
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:233
Defines an object that can be used to iterate over the elements of a collection.
Definition: Iterator.h:34
StlSet(const Collection< E > &source)
Copy constructor - copies the content of the given set into this one.
Definition: StlSet.h:156
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:158
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:76
Iterator< E > * iterator() const
Definition: StlSet.h:168
virtual ~StlSet()
Definition: StlSet.h:160
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:210
Definition: IllegalStateException.h:32
StlSet(const StlSet &source)
Copy constructor - copies the content of the given set into this one.
Definition: StlSet.h:147
virtual void clear()
Removes all of the elements from this collection (optional operation).The collection will be empty af...
Definition: StlSet.h:203
StlSet()
Default constructor - does nothing.
Definition: StlSet.h:140
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:175
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 void copy(const Collection< E > &collection)
Renders this Collection as a Copy of the given Collection.
Definition: AbstractCollection.h:184
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: AprPool.h:25