20 #ifndef HURRICANE_COLLECTION
21 #define HURRICANE_COLLECTION
23 #include "hurricane/Locator.h"
24 #include "hurricane/Filter.h"
28 template<
class Type>
class GenericCollection;
29 template<
class Type,
class SubType>
class SubTypeCollection;
30 template<
class Type,
class SubType>
class NotSubTypeCollection;
31 template<
class Type>
class SubSetCollection;
84 while (locator.isValid()) {
110 public:
template<
class SubType>
114 return getSubSet<SubType>().getSubSet(filter);
120 return NotSubTypeCollection<Type, SubType>(
this);
126 public:
bool isEmpty() const
130 return !GenericLocator<Type>(
getLocator()).isValid();
136 public:
void fill(list<Type>& list)
const
140 while (locator.isValid()) {
141 list.push_back(locator.getElement());
146 public:
void fill(set<Type>& set)
const
150 while (locator.isValid()) {
151 set.insert(locator.getElement());
156 public:
template<
class Compare>
void fill(set<Type, Compare>& set)
const
160 while (locator.isValid()) {
161 set.insert(locator.getElement());
166 public:
void fill(vector<Type>& vector)
const
170 while (locator.isValid()) {
171 vector.push_back(locator.getElement());
179 public:
virtual string _getTypeName() const
182 return _TName(
"Collection<Type>");
185 public:
virtual string _getString()
const = 0;
187 public: Record* _getRecord() const
190 Record* record = NULL;
192 record =
new Record(getString(
this));
195 while (locator.isValid()) {
196 string slotName = getString(n++);
197 Type slotRecord = locator.getElement();
198 record->add(getSlot(slotName, slotRecord));
208 iterator ( Locator<Type>* l ) : _locator(l) {}
209 ~iterator () {
delete _locator; }
210 bool operator== (
const iterator& o)
const {
return not (*
this != o); }
211 iterator& operator++ () { _locator->progress();
return *
this; }
212 Type operator* () {
return _locator->getElement(); }
213 bool operator!= (
const iterator& o )
const
215 bool invalidA = ( _locator == NULL) or not ( _locator->isValid());
216 bool invalidB = (o._locator == NULL) or not (o._locator->isValid());
217 return invalidA != invalidB or (not invalidA and not invalidB and _locator != o._locator);
220 Locator<Type>* _locator;
224 iterator begin() {
return iterator(
getLocator()); }
225 iterator end() {
return iterator(NULL); }
226 bool empty() {
return begin() == end(); }
261 _collection(collection.getClone())
268 _collection(genericCollection.getClone())
277 _collection(collection)
287 if (_collection)
delete _collection;
293 public: GenericCollection& operator=(
const Collection<Type>& collection)
296 if (_collection)
delete _collection;
297 _collection = collection.
getClone();
301 public: GenericCollection& operator=(
const GenericCollection& genericCollection)
304 if (_collection)
delete _collection;
305 _collection = genericCollection.
getClone();
309 public: GenericCollection& operator=(Collection<Type>* collection)
314 if (_collection)
delete _collection;
315 _collection = collection;
322 public:
virtual Collection<Type>* getClone() const
325 return (_collection) ? _collection->
getClone() : NULL;
328 public:
virtual Locator<Type>* getLocator() const
331 return (_collection) ? _collection->
getLocator() : NULL;
334 public:
virtual unsigned getSize() const
337 return (_collection) ? _collection->
getSize() : 0;
343 public:
virtual string _getTypeName() const
346 return _TName(
"GenericCollection");
349 public:
virtual string _getString() const
353 return "<" + _getTypeName() +
" unbound>";
355 return "<" + _getTypeName()+
" " + getString(_collection) +
">";
366 template<
class Type>
class ElementCollection :
public Collection<Type> {
375 protected:
const ElType _element;
376 protected:
bool _done;
380 public: Locator (
const ElType _element ) : _element(_element), _done(false) {};
381 public: Locator (
const Locator &locator ) : _element(locator._element), _done(locator._done) {};
385 public:
virtual ElType getElement ()
const {
return const_cast<ElType
>(_element); };
386 public:
virtual Locator<ElType>* getClone ()
const {
return new Locator(*
this); };
387 public:
virtual bool isValid ()
const {
return !_done; };
388 public:
virtual void progress () { _done =
true; };
392 public:
virtual string _getString ()
const {
394 return "<" + _TName(
"ElementCollection::Locator") +
" unbound>";
396 return "<" + _TName(
"ElementCollection::Locator") +
" " + getString(_element) +
">";
404 public:
typedef Collection<Type> Inherit;
409 private: Type _element;
414 public: ElementCollection()
421 public: ElementCollection(
const Type element)
428 public: ElementCollection(
const ElementCollection<Type>& elementCollection)
431 _element(elementCollection._element)
438 public:
virtual Collection<Type>* getClone() const
441 return (
new ElementCollection (*
this) );
444 public:
virtual Locator<Type>* getLocator() const
447 return (
new Locator<Type> (_element) );
450 public:
virtual unsigned getSize() const
453 return (_element) ? 1 : 0;
459 public:
virtual string _getString() const
463 return "<" + _TName(
"ElementCollection") +
" unbound>";
465 return "<" + _TName(
"ElementCollection") +
" " + getString(_element) +
">";
503 _locator(collection.getLocator())
505 while (_locator.isValid() && !
dynamic_cast<SubType
>(_locator.getElement()))
512 _locator(genericLocator.getClone())
514 while (_locator.isValid() && !
dynamic_cast<SubType
>(_locator.getElement()))
521 public:
virtual SubType getElement()
const
524 return (_locator.isValid()) ? (SubType)_locator.getElement() : SubType();
530 return new Locator(_locator);
537 _locator.getLocator()->getLocator() );
544 public:
virtual bool isValid()
const
547 return _locator.isValid();
553 public:
virtual void progress()
556 if (_locator.isValid()) {
559 }
while (_locator.isValid() && !
dynamic_cast<SubType
>(_locator.getElement()));
583 _collection(collection->getClone())
590 _collection(collection)
597 _collection(subTypeCollection._collection)
607 _collection = subTypeCollection._collection;
614 public:
virtual Collection<SubType>* getClone() const
617 return new SubTypeCollection(_collection);
623 return new Locator(_collection);
629 public:
virtual string _getString() const
632 return "<" + _TName(
"SubTypeCollection") +
" " + getString(_collection) +
">";
642 template<
class Type,
class SubType>
class NotSubTypeCollection :
public Collection<SubType> {
648 public:
typedef Collection<SubType> Inherit;
661 private: GenericLocator<Type> _locator;
666 public: Locator(
const GenericCollection<Type>& collection)
669 _locator(collection.getLocator())
671 while (_locator.isValid() &&
dynamic_cast<SubType
>(_locator.getElement()))
675 public: Locator(
const GenericLocator<Type>& genericLocator)
678 _locator(genericLocator.getClone())
680 while (_locator.isValid() && !
dynamic_cast<SubType
>(_locator.getElement()))
687 public:
virtual SubType getElement() const
690 return (_locator.isValid()) ? (SubType)_locator.getElement() : SubType();
696 return new Locator(_locator);
703 _locator.getLocator()->getLocator() );
710 public:
virtual bool isValid() const
719 public:
virtual void progress()
722 if (_locator.isValid()) {
725 }
while (_locator.isValid() && !
dynamic_cast<SubType
>(_locator.getElement()));
734 private: GenericCollection<Type> _collection;
739 public: NotSubTypeCollection()
746 public: NotSubTypeCollection(
const Collection<Type>* collection)
749 _collection(collection->getClone())
753 public: NotSubTypeCollection(
const GenericCollection<Type>& collection)
756 _collection(collection)
760 public: NotSubTypeCollection(
const NotSubTypeCollection& subTypeCollection)
763 _collection(subTypeCollection._collection)
770 public: NotSubTypeCollection& operator=(
const NotSubTypeCollection& subTypeCollection)
773 _collection = subTypeCollection._collection;
780 public:
virtual Collection<SubType>* getClone() const
783 return new NotSubTypeCollection(_collection);
789 return new Locator(_collection);
795 public:
virtual string _getString() const
798 return "<" + _TName(
"NotSubTypeCollection") +
" " + getString(_collection) +
">";
836 _locator(collection.getLocator()),
839 while (_locator.isValid() && !_filter.accept(_locator.getElement()))
846 _locator(collection.getLocator()),
849 while (_locator.isValid() && !_filter.accept(_locator.getElement()))
856 _locator(genericCollection.getLocator()),
859 while (_locator.isValid() && !_filter.accept(_locator.getElement()))
866 _locator(genericLocator),
869 while (_locator.isValid() && !_filter.accept(_locator.getElement()))
876 public:
virtual Type getElement()
const
879 return (_locator.isValid()) ? _locator.getElement() : Type();
885 return new Locator(_locator, _filter);
891 return ( _locator.getLocator()->getLocator() );
897 public:
virtual bool isValid()
const
900 return _locator.isValid();
906 public:
virtual void progress()
909 if (_locator.isValid()) {
912 }
while (_locator.isValid() && !_filter.accept(_locator.getElement()));
938 _collection(collection),
946 _collection(subSetCollection._collection),
947 _filter(subSetCollection._filter)
957 _collection = subSetCollection._collection;
958 _filter = subSetCollection._filter;
965 public:
virtual Collection<Type>* getClone() const
968 return new SubSetCollection(_collection, _filter);
974 return new Locator(_collection, _filter);
980 public:
virtual string _getString() const
983 return "<" + _TName(
"SubSetCollection") +
" " + getString(_collection) +
">";
1005 #define for_each_object(Type, element, collection)\
1008 GenericLocator<Type> _locator = collection.getLocator();\
1009 while (_locator.isValid()) {\
1010 Type element = _locator.getElement();\
1011 _locator.progress();
1013 #define for_each_element(Type, element, collection)\
1016 ElementCollection<Type>::Locator<Type>* _locator = collection.getLocator();\
1017 while (_locator->isValid()) {\
1018 Type element = _locator->getElement();\
1019 _locator->progress();
1026 template<
typename Element>
1027 class ForEachIterator {
1029 inline ForEachIterator ( GenericCollection<Element> coll );
1030 inline bool isValid ();
1031 inline Element operator* ();
1032 inline Element operator-> ();
1033 inline ForEachIterator& operator++ (
int);
1035 GenericCollection<Element> collection;
1036 GenericLocator<Element> locator;
1041 template<
typename Element>
1042 inline ForEachIterator<Element>::ForEachIterator ( GenericCollection<Element> coll )
1044 , locator(collection.getLocator())
1047 if ( locator.isValid() ) element = locator.getElement();
1051 template<
typename Element >
1052 inline bool ForEachIterator<Element>::isValid ()
1054 if ( locator.isValid() ) element = locator.getElement();
1055 return locator.isValid();
1059 template<
typename Element >
1060 inline Element ForEachIterator<Element>::operator* ()
1066 template<
typename Element >
1067 inline Element ForEachIterator<Element>::operator-> ()
1073 template<
typename Element >
1074 inline ForEachIterator<Element>& ForEachIterator<Element>::operator++ (
int)
1076 locator.progress ();
1081 #define forEach(type,iterator,collection) \
1082 for ( ForEachIterator<type> iterator(collection); iterator.isValid() ; iterator++ )
1089 {
return collection._getString(); }
1092 {
return collection->_getString(); }
1095 {
return collection->_getString(); }
1098 {
return collection._getRecord(); }
1101 {
return collection->_getRecord(); }
1104 {
return collection->_getRecord(); }
1107 template<
typename Type>
1111 cdebug_log(19,0) <<
"jsonWrite< GenericCollection<" <<
Hurricane::demangle(
typeid(Type).name())
1112 <<
"> >(w,key,coll)" <<
" key:\"" << key <<
"\"" << std::endl;
1117 for ( Type element : collection ) jsonWrite( w, element );
1124 #include "hurricane/MultisetCollection.h"
1125 #include "hurricane/SetCollection.h"
1126 #include "hurricane/MapCollection.h"
1127 #include "hurricane/MultimapCollection.h"
1128 #include "hurricane/ListCollection.h"
1129 #include "hurricane/VectorCollection.h"
Collection description (API)
Definition: Collection.h:39
virtual unsigned getSize() const
Definition: Collection.h:78
virtual Locator< Type > * getLocator() const =0
GenericCollection< SubType > getSubSet(const Filter< SubType > &filter) const
Definition: Collection.h:111
Type getFirst() const
Definition: Collection.h:91
GenericCollection< SubType > getSubSet() const
Definition: Collection.h:104
GenericCollection< Type > getSubSet(const Filter< Type > &filter) const
Definition: Collection.h:98
virtual ~Collection()
Definition: Collection.h:58
virtual Collection< Type > * getClone() const =0
Filter description (API)
Definition: Filter.h:36
Generic Collection auto-pointer.
Definition: Collection.h:235
GenericCollection(const Collection< Type > &collection)
Definition: Collection.h:258
GenericCollection(Collection< Type > *collection)
Definition: Collection.h:272
GenericCollection(const GenericCollection< Type > &genericCollection)
Definition: Collection.h:265
Generic Filter auto-pointer.
Definition: Filter.h:86
Generic Locator auto-pointer.
Definition: Locator.h:113
Locator description (API)
Definition: Locator.h:33
virtual bool isValid() const =0
Applies a Filter to a Collection.
Definition: Collection.h:808
SubSetCollection(const Collection< Type > &collection, const Filter< Type > &filter)
Definition: Collection.h:935
SubSetCollection(const SubSetCollection &subSetCollection)
Definition: Collection.h:943
Applies a Type Filter to a Collection.
Definition: Collection.h:476
SubTypeCollection(const GenericCollection< Type > &collection)
Definition: Collection.h:587
SubTypeCollection(const Collection< Type > *collection)
Definition: Collection.h:580
SubTypeCollection(const SubTypeCollection &subTypeCollection)
Definition: Collection.h:594
bool enabled() const
Definition: Commons.h:1056
The namespace dedicated to Hurricane.
Definition: Generalities.dox:5
string demangle(const char *symbol)