DISPLACE  1.0
A spatial model of fisheries to help sustainable fishing and maritime spatial planning
mapobjectcontainer.h
Go to the documentation of this file.
1 #ifndef MAPOBJECTCONTAINER_H
2 #define MAPOBJECTCONTAINER_H
3 
4 #include <QList>
5 #include <QMap>
6 #include <idtypes.h>
7 
11 template <typename Obj, typename Idx = int>
13 {
14  class ObjC {
15  public:
16  QList<Obj *> mList;
17  };
18 
19  QMap<Idx, ObjC *> mMap;
20 
21 public:
23  : mMap() {
24  }
25 
27  }
28 
32  Idx add (Idx id, Obj *object, int role) {
33  if (types::isIdInvalid(id))
34  id = Idx(mMap.size());
35 
36  typename QMap<Idx,ObjC *>::iterator it = mMap.find(id);
37  ObjC *cont;
38  if (it == mMap.end()) {
39  // not found: add it
40  cont = new ObjC;
41  mMap.insert(id, cont);
42  } else {
43  cont = *it;
44  }
45 
46  while (cont->mList.size() <= role)
47  cont->mList.push_back(0);
48  cont->mList[role] = object;
49 
50  return id;
51  }
52 
53  Obj *get(Idx id, int role) {
54  auto it = mMap.find(id);
55  if (it == mMap.end()) {
56  return nullptr;
57  }
58 
59  ObjC *obj = *it;
60  if (obj->mList.size() <= role)
61  return nullptr;
62  return obj->mList.at(role);
63  }
64 
65  void clear() {
66  mMap.clear();
67  }
68 
69  typedef typename QMap<Idx,ObjC *>::const_iterator Iterator;
70 
72  return mMap.constBegin();
73  }
74 
75  bool atEnd(Iterator it) {
76  return it == mMap.constEnd();
77  }
78 
79  Obj *get (Iterator it, int role) {
80  if (atEnd(it))
81  return nullptr;
82 
83  ObjC *obj = *it;
84  if (obj->mList.size() <= role)
85  return nullptr;
86 
87  return obj->mList.at(role);
88  }
89 
95  Obj *remove (Idx id, int role) {
96  auto it = mMap.find(id);
97  if (it == mMap.end()) {
98  return nullptr;
99  }
100 
101  Obj *r = 0;
102  ObjC *obj = *it;
103  swap (obj->mList[role], r);
104 
105  return r;
106  }
107 
113  QList<Obj *> remove (Idx id) {
114  auto it = mMap.find(id);
115  if (it == mMap.end()) {
116  return QList<Obj *>();
117  }
118 
119  ObjC *obj = *it;
120  QList<Obj *> r = obj->mList;
121  mMap.remove(id);
122 
123  return r;
124  }
125 };
126 
127 #endif // MAPOBJECTCONTAINER_H
MapObjectContainer()
Definition: mapobjectcontainer.h:22
Obj * remove(Idx id, int role)
remove the object specified by the id and role. The collection identified by id is not removed....
Definition: mapobjectcontainer.h:95
bool atEnd(Iterator it)
Definition: mapobjectcontainer.h:75
bool isIdInvalid(const X &)=delete
A generic Container class form Map Objects.
Definition: mapobjectcontainer.h:12
Idx add(Idx id, Obj *object, int role)
add an object with the specified id and role
Definition: mapobjectcontainer.h:32
QMap< Idx, ObjC * >::const_iterator Iterator
Definition: mapobjectcontainer.h:69
Obj * get(Idx id, int role)
Definition: mapobjectcontainer.h:53
~MapObjectContainer()
Definition: mapobjectcontainer.h:26
Obj * get(Iterator it, int role)
Definition: mapobjectcontainer.h:79
QList< Obj * > remove(Idx id)
Remove all the objects identified by the id, with all roles. The collection identified by id is remov...
Definition: mapobjectcontainer.h:113
Iterator begin()
Definition: mapobjectcontainer.h:71
void clear()
Definition: mapobjectcontainer.h:65