Fire Core  8.0.0.alpha
GIS framework per tutti gli usi
 Tutto Classi Namespace Funzioni Variabili Ridefinizioni di tipo (typedef) Tipi enumerati (enum) Valori del tipo enumerato Friend
Group.hpp
1 // Copyright 2015 GEOSYSTEMS SRL
2 // All Rights Reserved.
3 
4 #ifndef FIRE_GROUP_HPP_INCLUDED
5 #define FIRE_GROUP_HPP_INCLUDED
6 
7 #include <boost/iterator/filter_iterator.hpp>
8 
9 #include <exception>
10 #include <set>
11 #include <cstddef>
12 #include <algorithm>
13 #include <vector>
14 
15 #include "fire/Object.hpp"
16 #include "fire/Filter.hpp"
17 #include "fire/Predicate.hpp"
18 #include "fire/exception/Exception.hpp"
19 
24 namespace fire {
32 template < class T >
33 class Group : public fire::Object {
34  public:
39  typedef typename std::set< T >::iterator iterator;
40  typedef typename std::set< T >::const_iterator const_iterator;
45  typedef typename std::set< T >::reverse_iterator reverse_iterator;
46  typedef typename std::set< T >::const_reverse_iterator const_reverse_iterator;
47 
48  private:
53  bool objectsIteratorInitialized;
58  std::size_t index;
63  iterator objectsIterator;
68  fire::Predicate< T >* predicate;
73  boost::filter_iterator< fire::Predicate< T >, iterator >* firstFilteredObject;
78  boost::filter_iterator< fire::Predicate< T >, iterator >* lastFilteredObject;
83  boost::filter_iterator< fire::Predicate< T >, iterator >* filteredObjectsIterator;
84 
85  protected:
90  std::set< T > objects;
91 
92  public:
97  Group() {
98  try {
99  this->objectsIteratorInitialized = false;
100  this->predicate = NULL;
101  this->firstFilteredObject = NULL;
102  this->lastFilteredObject = NULL;
103  this->filteredObjectsIterator = NULL;
104  } catch (const std::exception& exception) {
105  throw;
106  }
107  }
112  virtual ~Group() {
113  try {
114  if (this->predicate != NULL) {
115  delete this->predicate;
116  }
117  if (this->firstFilteredObject != NULL) {
118  delete this->firstFilteredObject;
119  }
120  if (this->lastFilteredObject != NULL) {
121  delete this->lastFilteredObject;
122  }
123  if (this->filteredObjectsIterator != NULL) {
124  delete this->filteredObjectsIterator;
125  }
126  } catch (const std::exception& exception) {
127  throw;
128  }
129  }
137  virtual void include(T object) {
138  try {
139  this->objects.insert(object);
140  } catch (const std::exception& exception) {
141  throw;
142  }
143  }
151  void include(const std::set< T >& objects) {
152  try {
153  for (typename std::set< T >::iterator objectsIterator = objects.begin() ; objectsIterator != objects.end() ; ++objectsIterator) {
154  this->include(*objectsIterator);
155  }
156  } catch (const std::exception& exception) {
157  throw;
158  }
159  }
167  void include(const fire::Group< T >& group) {
168  try {
169  this->include(group.objects);
170  } catch (const std::exception& exception) {
171  throw;
172  }
173  }
181  virtual void exclude(T object) {
182  try {
183  this->objects.erase(object);
184  } catch (const std::exception& exception) {
185  throw;
186  }
187  }
195  void exclude(const std::set< T >& objects) {
196  try {
197  for (typename std::set< T >::iterator objectsIterator = objects.begin() ; objectsIterator != objects.end() ; ++objectsIterator) {
198  this->exclude(*objectsIterator);
199  }
200  } catch (const std::exception& exception) {
201  throw;
202  }
203  }
211  void exclude(const fire::Group< T >& group) {
212  try {
213  this->exclude(group.objects);
214  } catch (const std::exception& exception) {
215  throw;
216  }
217  }
222  virtual void clear() {
223  try {
224  this->objects.clear();
225  } catch (const std::exception& exception) {
226  throw;
227  }
228  }
236  bool empty() const {
237  bool value = false;
238  try {
239  value = this->objects.empty();
240  } catch (const std::exception& exception) {
241  throw;
242  }
243  return value;
244  }
252  std::size_t size() const {
253  std::size_t value = 0;
254  try {
255  value = this->objects.size();
256  } catch (const std::exception& exception) {
257  throw;
258  }
259  return value;
260  }
271  virtual bool contains(T object) const {
272  bool value = true;
273  try {
274  if (this->objects.count(object) == 0) {
275  value = false;
276  }
277  } catch (const std::exception& exception) {
278  throw;
279  }
280  return value;
281  }
292  bool contains(const std::set< T >& objects) const {
293  bool value = true;
294  try {
295  for (typename std::set< T >::iterator objectsIterator = objects.begin() ; objectsIterator != objects.end() ; ++objectsIterator) {
296  if (!this->contains(*objectsIterator)) {
297  value = false;
298  }
299  }
300  } catch (const std::exception& exception) {
301  throw;
302  }
303  return value;
304  }
315  bool contains(const fire::Group< T >& group) const {
316  bool value = true;
317  try {
318  if (!this->contains(group.objects)) {
319  value = false;
320  }
321  } catch (const std::exception& exception) {
322  throw;
323  }
324  return value;
325  }
337  fire::Group< T > value;
338  try {
339  std::auto_ptr< fire::Predicate< T > > predicate(new Predicate< T >(filter));
340  boost::filter_iterator< fire::Predicate< T >, typename std::set< T >::iterator > firstObject(*predicate, this->objects.begin(), this->objects.end());
341  boost::filter_iterator< fire::Predicate< T >, typename std::set< T >::iterator > lastObject(*predicate, this->objects.end(), this->objects.end());
342  std::set< T > objects;
343  std::copy(firstObject, lastObject, std::inserter(objects, objects.begin()));
344  value.include(objects);
345  } catch (const std::exception& exception) {
346  throw;
347  }
348  return value;
349  }
361  fire::Group< T >& value = *this;
362  try {
363  this->objects.clear();
364  this->include(object);
365  } catch (const std::exception& exception) {
366  throw;
367  }
368  return value;
369  }
380  fire::Group< T >& operator=(const std::set< T >& objects) {
381  fire::Group< T >& value = *this;
382  try {
383  this->objects.clear();
384  this->include(objects);
385  } catch (const std::exception& exception) {
386  throw;
387  }
388  return value;
389  }
401  fire::Group< T >& value = *this;
402  try {
403  if (this != &group) {
404  this->objects.clear();
405  this->include(group);
406  }
407  } catch (const std::exception& exception) {
408  throw;
409  }
410  return value;
411  }
422  fire::Group< T >& operator+=(const T& object) {
423  fire::Group< T >& value = *this;
424  try {
425  this->include(object);
426  } catch (const std::exception& exception) {
427  throw;
428  }
429  return value;
430  }
441  fire::Group< T >& operator+=(const std::set< T >& objects) {
442  fire::Group< T >& value = *this;
443  try {
444  this->include(objects);
445  } catch (const std::exception& exception) {
446  throw;
447  }
448  return value;
449  }
461  fire::Group< T >& value = *this;
462  try {
463  this->include(group);
464  } catch (const std::exception& exception) {
465  throw;
466  }
467  return value;
468  }
479  fire::Group< T >& operator-=(const T& object) {
480  fire::Group< T >& value = *this;
481  try {
482  this->exclude(object);
483  } catch (const std::exception& exception) {
484  throw;
485  }
486  return value;
487  }
498  fire::Group< T >& operator-=(const std::set< T >& objects) {
499  fire::Group< T >& value = *this;
500  try {
501  this->exclude(objects);
502  } catch (const std::exception& exception) {
503  throw;
504  }
505  return value;
506  }
518  fire::Group< T >& value = *this;
519  try {
520  this->exclude(group);
521  } catch (const std::exception& exception) {
522  throw;
523  }
524  return value;
525  }
536  fire::Group< T > operator+(const T& object) const {
537  fire::Group< T > value;
538  try {
539  value = *this;
540  value += object;
541  } catch (const std::exception& exception) {
542  throw;
543  }
544  return value;
545  }
556  fire::Group< T > operator+(const std::set< T >& objects) const {
557  fire::Group< T > value;
558  try {
559  value = *this;
560  value += objects;
561  } catch (const std::exception& exception) {
562  throw;
563  }
564  return value;
565  }
577  fire::Group< T > value;
578  try {
579  value = *this;
580  value += group;
581  } catch (const std::exception& exception) {
582  throw;
583  }
584  return value;
585  }
596  fire::Group< T > operator-(const T& object) const {
597  fire::Group< T > value;
598  try {
599  value = *this;
600  value -= object;
601  } catch (const std::exception& exception) {
602  throw;
603  }
604  return value;
605  }
616  fire::Group< T > operator-(const std::set< T >& objects) const {
617  fire::Group< T > value;
618  try {
619  value = *this;
620  value -= objects;
621  } catch (const std::exception& exception) {
622  throw;
623  }
624  return value;
625  }
637  fire::Group< T > value;
638  try {
639  value = *this;
640  value -= group;
641  } catch (const std::exception& exception) {
642  throw;
643  }
644  return value;
645  }
657  fire::Group< T > value;
658  try {
659  value = this->filter(filter);
660  } catch (const std::exception& exception) {
661  throw;
662  }
663  return value;
664  }
672  std::vector< T > toVector() const {
673  std::vector< T > value;
674  try {
675  for (typename std::set< T >::iterator objectsIterator = this->objects.begin() ; objectsIterator != this->objects.end() ; ++objectsIterator) {
676  value.push_back(*objectsIterator);
677  }
678  } catch (const std::exception& exception) {
679  throw;
680  }
681  return value;
682  }
691  iterator value;
692  try {
693  value = this->objects.begin();
694  } catch (const std::exception& exception) {
695  throw;
696  }
697  return value;
698  }
699  const_iterator begin() const {
700  iterator value;
701  try {
702  value = this->objects.begin();
703  } catch (const std::exception& exception) {
704  throw;
705  }
706  return value;
707  }
716  iterator value;
717  try {
718  value = this->objects.end();
719  } catch (const std::exception& exception) {
720  throw;
721  }
722  return value;
723  }
724  const_iterator end() const {
725  iterator value;
726  try {
727  value = this->objects.end();
728  } catch (const std::exception& exception) {
729  throw;
730  }
731  return value;
732  }
741  reverse_iterator value;
742  try {
743  value = this->objects.rbegin();
744  } catch (const std::exception& exception) {
745  throw;
746  }
747  return value;
748  }
749  const_reverse_iterator rbegin() const {
750  reverse_iterator value;
751  try {
752  value = this->objects.rbegin();
753  } catch (const std::exception& exception) {
754  throw;
755  }
756  return value;
757  }
766  reverse_iterator value;
767  try {
768  value = this->objects.rend();
769  } catch (const std::exception& exception) {
770  throw;
771  }
772  return value;
773  }
774  const_reverse_iterator rend() const {
775  reverse_iterator value;
776  try {
777  value = this->objects.rend();
778  } catch (const std::exception& exception) {
779  throw;
780  }
781  return value;
782  }
791  try {
792  this->predicate = new Predicate< T >(filter);
793  this->firstFilteredObject = new boost::filter_iterator< fire::Predicate< T >, iterator >(*this->predicate, this->objects.begin(), this->objects.end());
794  this->lastFilteredObject = new boost::filter_iterator< fire::Predicate< T >, iterator >(*this->predicate, this->objects.end(), this->objects.end());
795  this->objectsIteratorInitialized = false;
796  } catch (const std::exception& exception) {
797  throw;
798  }
799  }
805  try {
806  if (this->predicate != NULL) {
807  delete this->predicate;
808  this->predicate = NULL;
809  }
810  if (this->firstFilteredObject != NULL) {
811  delete this->firstFilteredObject;
812  this->firstFilteredObject = NULL;
813  }
814  if (this->lastFilteredObject != NULL) {
815  delete this->lastFilteredObject;
816  this->lastFilteredObject = NULL;
817  }
818  if (this->filteredObjectsIterator != NULL) {
819  delete this->filteredObjectsIterator;
820  this->filteredObjectsIterator = NULL;
821  }
822  this->objectsIteratorInitialized = false;
823  } catch (const std::exception& exception) {
824  throw;
825  }
826  }
831  void rewind() {
832  try {
833  if ((this->firstFilteredObject != NULL) && (this->lastFilteredObject != NULL)) {
834  this->filteredObjectsIterator = new boost::filter_iterator< fire::Predicate< T >, iterator >(*this->predicate, this->objects.begin(), this->objects.end());
835  } else {
836  this->objectsIterator = this->objects.begin();
837  }
838  this->objectsIteratorInitialized = true;
839  this->index = -1;
840  } catch (const std::exception& exception) {
841  throw;
842  }
843  }
852  T next() {
853  T value;
854  try {
855  if (this->objectsIteratorInitialized == false) {
856  this->rewind();
857  }
858  if (this->filteredObjectsIterator != NULL) {
859  if (this->index == -1) {
860  if (*this->filteredObjectsIterator != *this->lastFilteredObject) {
861  value = **this->filteredObjectsIterator;
862  ++this->index;
863  } else {
864  throw fire::exception::Exception("iterator is out of bounds");
865  }
866  } else {
867  if (*this->filteredObjectsIterator != --*this->lastFilteredObject) {
868  delete this->lastFilteredObject;
869  this->lastFilteredObject = new boost::filter_iterator< fire::Predicate< T >, iterator >(*this->predicate, this->objects.end(), this->objects.end());
870  ++*this->filteredObjectsIterator;
871  value = **this->filteredObjectsIterator;
872  } else {
873  throw fire::exception::Exception("iterator is out of bounds");
874  }
875  }
876  } else {
877  if (this->index == -1) {
878  if (this->objectsIterator != this->objects.end()) {
879  value = *this->objectsIterator;
880  ++this->index;
881  } else {
882  throw fire::exception::Exception("iterator is out of bounds");
883  }
884  } else {
885  if (this->objectsIterator != --this->objects.end()) {
886  ++this->objectsIterator;
887  value = *this->objectsIterator;
888  ++this->index;
889  } else {
890  throw fire::exception::Exception("iterator is out of bounds");
891  }
892  }
893  }
894  } catch (const std::exception& exception) {
895  throw;
896  }
897  return value;
898  }
906  bool hasNext() {
907  bool value = false;
908  try {
909  if (this->objectsIteratorInitialized == false) {
910  this->rewind();
911  }
912  if (this->filteredObjectsIterator != NULL) {
913  if (this->index == -1) {
914  if (*this->filteredObjectsIterator != *this->lastFilteredObject) {
915  value = true;
916  }
917  } else {
918  if (*this->filteredObjectsIterator != --*this->lastFilteredObject) {
919  delete this->lastFilteredObject;
920  this->lastFilteredObject = new boost::filter_iterator< fire::Predicate< T >, iterator >(*this->predicate, this->objects.end(), this->objects.end());
921  value = true;
922  }
923  }
924  } else {
925  if (this->index == -1) {
926  if (this->objectsIterator != this->objects.end()) {
927  value = true;
928  }
929  } else {
930  if (this->objectsIterator != --this->objects.end()) {
931  value = true;
932  }
933  }
934  }
935  } catch (const std::exception& exception) {
936  throw;
937  }
938  return value;
939  }
944  void forward() {
945  try {
946  if ((this->firstFilteredObject != NULL) && (this->lastFilteredObject != NULL)) {
947  this->filteredObjectsIterator = new boost::filter_iterator< fire::Predicate< T >, iterator >(*this->predicate, this->objects.end(), this->objects.end());
948  } else {
949  this->objectsIterator = this->objects.end();
950  }
951  this->objectsIteratorInitialized = true;
952  } catch (const std::exception& exception) {
953  throw;
954  }
955  }
964  T previous() {
965  T value;
966  try {
967  if (this->objectsIteratorInitialized == false) {
968  this->forward();
969  }
970  if (this->filteredObjectsIterator != NULL) {
971  if (*this->filteredObjectsIterator != *this->firstFilteredObject) {
972  --*this->filteredObjectsIterator;
973  value = **this->filteredObjectsIterator;
974  } else {
975  throw fire::exception::Exception("iterator is out of bounds");
976  }
977  } else {
978  if (this->objectsIterator != this->objects.begin()) {
979  --this->objectsIterator;
980  value = *this->objectsIterator;
981  } else {
982  throw fire::exception::Exception("iterator is out of bounds");
983  }
984  }
985  } catch (const std::exception& exception) {
986  throw;
987  }
988  return value;
989  }
997  bool hasPrevious() {
998  bool value = false;
999  try {
1000  if (this->objectsIteratorInitialized == false) {
1001  this->forward();
1002  }
1003  if (this->filteredObjectsIterator != NULL) {
1004  if (*this->filteredObjectsIterator != *this->firstFilteredObject) {
1005  value = true;
1006  }
1007  } else {
1008  if (this->objectsIterator != this->objects.begin()) {
1009  value = true;
1010  }
1011  }
1012  } catch (const std::exception& exception) {
1013  throw;
1014  }
1015  return value;
1016  }
1017 };
1018 } // namespace fire
1019 
1020 #endif
fire::Group< T > & operator-=(const fire::Group< T > &group)
Operatore di sottrazione e assegnamento con un gruppo di oggetti generici.
Definition: Group.hpp:517
std::vector< T > toVector() const
Restituisce un vettore degli oggetti generici del gruppo.
Definition: Group.hpp:672
void exclude(const fire::Group< T > &group)
Esclude un gruppo di oggetti generici dal gruppo.
Definition: Group.hpp:211
virtual void exclude(T object)
Esclude un oggetto generico dal gruppo.
Definition: Group.hpp:181
fire::Group< T > & operator+=(const T &object)
Operatore di somma e assegnamento con un oggetto generico.
Definition: Group.hpp:422
void exclude(const std::set< T > &objects)
Esclude un insieme di oggetti generici dal gruppo.
Definition: Group.hpp:195
Group()
Costruttore.
Definition: Group.hpp:97
void include(const std::set< T > &objects)
Include un insieme di oggetti generici nel gruppo.
Definition: Group.hpp:151
fire::Group< T > & operator-=(const T &object)
Operatore di sottrazione e assegnamento con un oggetto generico.
Definition: Group.hpp:479
bool contains(const fire::Group< T > &group) const
Restituisce l'indicazione se il gruppo contiene un gruppo di oggetti generici.
Definition: Group.hpp:315
fire::Group< T > operator%(const fire::Filter< T > &filter) const
Operatore di filtro.
Definition: Group.hpp:656
T previous()
Restituisce l'oggetto generico precedente del gruppo iterator is out of bounds.
Definition: Group.hpp:964
virtual bool contains(T object) const
Restituisce l'indicazione se il gruppo contiene un oggetto generico.
Definition: Group.hpp:271
fire::Group< T > & operator=(const fire::Group< T > &group)
Operatore di assegnamento con un gruppo di oggetti generici.
Definition: Group.hpp:400
fire::Group< T > & operator+=(const fire::Group< T > &group)
Operatore di somma e assgnamento con un gruppo di oggetti generici.
Definition: Group.hpp:460
virtual void clear()
Svuota il gruppo.
Definition: Group.hpp:222
Classe per la gestione di un oggetto.
Definition: Object.hpp:29
void forward()
Sposta l'iteratore alla fine.
Definition: Group.hpp:944
Classe per la gestione di un gruppo di oggetti generici.
Definition: Group.hpp:33
std::set< T > objects
Insieme degli oggetti generici.
Definition: Group.hpp:90
Classe per la gestione di un'eccezione generica.
Definition: Exception.hpp:26
bool contains(const std::set< T > &objects) const
Restituisce l'indicazione se il gruppo contiene un insieme di oggetti generici.
Definition: Group.hpp:292
virtual fire::Group< T > filter(const fire::Filter< T > &filter) const
Restituisce un gruppo filtrato.
Definition: Group.hpp:336
fire::Group< T > operator-(const T &object) const
Operatore di sottrazione con un oggetto generico.
Definition: Group.hpp:596
virtual void include(T object)
Include un oggetto generico nel gruppo.
Definition: Group.hpp:137
reverse_iterator rbegin()
Restituisce l'inizio dell'iteratore inverso sugli oggetti generici del gruppo.
Definition: Group.hpp:740
bool hasNext()
Restituisce l'indicazione se il gruppo ha un oggetto generico successivo.
Definition: Group.hpp:906
fire::Group< T > & operator=(const std::set< T > &objects)
Operatore di assegnamento con un insieme di oggetti generici.
Definition: Group.hpp:380
std::size_t size() const
Restituisce la dimensione del gruppo.
Definition: Group.hpp:252
fire::Group< T > & operator-=(const std::set< T > &objects)
Operatore di sottrazione e assegnamento con un insieme di oggetti generici.
Definition: Group.hpp:498
virtual ~Group()
Distruttore.
Definition: Group.hpp:112
Classe per la gestione di un filtro.
Definition: Filter.hpp:25
void setIteratorFilter(const fire::Filter< T > &filter)
Imposta un filtro per l'iteratore sugli oggetti generici.
Definition: Group.hpp:790
Classe per la gestione di un predicato.
Definition: Predicate.hpp:35
bool hasPrevious()
Restituisce l'indicazione se il gruppo ha un oggetto generico precedente.
Definition: Group.hpp:997
fire::Group< T > operator+(const T &object) const
Operatore di somma con un oggetto generico.
Definition: Group.hpp:536
void include(const fire::Group< T > &group)
Include un gruppo di oggetti generici nel gruppo.
Definition: Group.hpp:167
fire::Group< T > operator-(const std::set< T > &objects) const
Operatore di sottrazione con un insieme di oggetti generici.
Definition: Group.hpp:616
void unsetIteratorFilter()
Disimposta il filtro per l'iteratore sugli oggetti generici.
Definition: Group.hpp:804
std::set< T >::reverse_iterator reverse_iterator
Definizione dell'iteratore inverso sugli oggetti generici.
Definition: Group.hpp:45
fire::Group< T > & operator+=(const std::set< T > &objects)
Operatore di somma e assegnamento con un insieme di oggetti generici.
Definition: Group.hpp:441
T next()
Restituisce il successivo oggetto generico del gruppo iterator is out of bounds.
Definition: Group.hpp:852
fire::Group< T > operator+(const fire::Group< T > &group) const
Operatore di somma con un gruppo di oggetti generici.
Definition: Group.hpp:576
void rewind()
Sposta l'iteratore all'inizio.
Definition: Group.hpp:831
bool empty() const
Restituisce l'indicazione se il gruppo รจ vuoto.
Definition: Group.hpp:236
fire::Group< T > operator+(const std::set< T > &objects) const
Operatore di somma con un insieme di oggetti generici.
Definition: Group.hpp:556
fire::Group< T > operator-(const fire::Group< T > &group) const
Operatore di sottrazione con un gruppo di oggetti generici.
Definition: Group.hpp:636
fire::Group< T > & operator=(T object)
Operatore di assegnamento con un oggetto generico.
Definition: Group.hpp:360
iterator end()
Restituisce la fine dell'iteratore sugli oggetti generici del gruppo.
Definition: Group.hpp:715
std::set< T >::iterator iterator
Definizione dell'iteratore sugli oggetti generici.
Definition: Group.hpp:39
reverse_iterator rend()
Restituisce la fine dell'iteratore inverso sugli oggetti generici del gruppo.
Definition: Group.hpp:765
iterator begin()
Restituisce l'inizio dell'iteratore sugli oggetti generici del gruppo.
Definition: Group.hpp:690