Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Collection.h
1 /*
2  * $Id$
3  * Collection class interface
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2004-08, IRIT UPS.
7  *
8  * OTAWA is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * OTAWA is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with OTAWA; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #ifndef ELM_DYNDATA_COLLECTION_H
23 #define ELM_DYNDATA_COLLECTION_H
24 
25 #include "AbstractCollection.h"
26 
27 namespace elm { namespace dyndata {
28 
29 template <class T, class I>
31 public:
32  inline IterInst(const I& i): _i(i) { }
33  virtual ~IterInst(void) { }
34  virtual bool ended(void) const { return _i.ended(); }
35  virtual T item(void) const { return _i.item(); }
36  virtual void next(void) { _i.next(); }
37  inline const I& iter(void) const { return _i; }
38 private:
39  I _i;
40 };
41 template <class T, class I>
42 inline IterInst<T, I> *iter(const I& i) { return new IterInst<T, I>(i); }
43 
44 
45 // Collection class
46 template <class T, class C>
47 class Collection: public virtual AbstractCollection<T> {
48 public:
49  //inline C& collection(void) { return coll; }
50 
51  virtual ~Collection(void) { }
52  virtual int count(void) { return coll.count(); }
53  virtual bool contains(const T& item) const { return coll.contains(item); }
54  virtual bool isEmpty(void) const { return coll.isEmpty(); }
55  virtual Iter<T> items(void) const { return new IterInst<T, typename C::Iter>(coll.items()); }
56 
57 protected:
58  C coll;
59 };
60 
61 // MutableCollection class
62 template <class T, class C>
63 class MutableCollection: public Collection<T, C>, public MutableAbstractCollection<T> {
64 public:
65  virtual ~MutableCollection(void) { }
66  virtual void clear(void) { Collection<T, C>::coll.clear(); }
67  virtual void add(const T& item) { Collection<T, C>::coll.add(item); }
68  virtual void addAll(const AbstractCollection<T>& coll)
69  { for(auto iter = coll.items(); iter(); iter++) add(*iter); }
70  virtual void remove(const T& item) { Collection<T, C>::coll.remove(item); }
71  virtual void removeAll(const AbstractCollection<T>& coll)
72  { for(auto iter = coll.items(); iter(); iter++) remove(iter); }
73  virtual void remove(const Iter<T>& iter)
74  { Collection<T, C>::coll.remove(static_cast<const IterInst<T, typename C::Iter> *>(iter.instance())->iter()); }
75 };
76 
77 } } // elm::dyndata
78 
79 #endif // ELM_DYNDATA_COLLECTION_H
elm::iter
Definition: util_WAHVector.cpp:157
elm::dyndata::IterInst::item
virtual T item(void) const
Definition: Collection.h:35
elm::dyndata::MutableCollection::remove
virtual void remove(const T &item)
Definition: Collection.h:70
elm::dyndata::MutableCollection::remove
virtual void remove(const Iter< T > &iter)
Definition: Collection.h:73
elm::dyndata::IterInst::iter
const I & iter(void) const
Definition: Collection.h:37
elm::dyndata::MutableCollection::clear
virtual void clear(void)
Definition: Collection.h:66
elm::dyndata::Collection::isEmpty
virtual bool isEmpty(void) const
Definition: Collection.h:54
elm::dyndata::AbstractIter
Definition: AbstractCollection.h:32
elm::dyndata::IterInst
Definition: Collection.h:30
elm::dyndata::AbstractCollection
Definition: AbstractCollection.h:58
elm::dyndata::Collection::count
virtual int count(void)
Definition: Collection.h:52
elm::dyndata::Iter
Definition: AbstractCollection.h:43
elm::dyndata::MutableCollection::removeAll
virtual void removeAll(const AbstractCollection< T > &coll)
Definition: Collection.h:71
elm::dyndata::IterInst::IterInst
IterInst(const I &i)
Definition: Collection.h:32
elm::dyndata::IterInst::next
virtual void next(void)
Definition: Collection.h:36
elm
Definition: adapter.h:26
elm::dyndata::MutableCollection::add
virtual void add(const T &item)
Definition: Collection.h:67
elm::dyndata::MutableAbstractCollection
Definition: AbstractCollection.h:71
elm::dyndata::IterInst::ended
virtual bool ended(void) const
Definition: Collection.h:34
elm::dyndata::Collection::coll
C coll
Definition: Collection.h:58
elm::dyndata::MutableCollection::addAll
virtual void addAll(const AbstractCollection< T > &coll)
Definition: Collection.h:68
elm::dyndata::Collection::contains
virtual bool contains(const T &item) const
Definition: Collection.h:53
elm::dyndata::Collection
Definition: Collection.h:47
elm::dyndata::MutableCollection::~MutableCollection
virtual ~MutableCollection(void)
Definition: Collection.h:65
elm::dyndata::iter
IterInst< T, I > * iter(const I &i)
Definition: Collection.h:42
elm::dyndata::Collection::items
virtual Iter< T > items(void) const
Definition: Collection.h:55
elm::dyndata::Collection::~Collection
virtual ~Collection(void)
Definition: Collection.h:51
elm::dyndata::IterInst::~IterInst
virtual ~IterInst(void)
Definition: Collection.h:33
elm::dyndata::MutableCollection
Definition: Collection.h:63