DiaDes  0.1
DIAgnosis of Discrete-Event System
Box.hh
Go to the documentation of this file.
1 #ifndef __DIADES__UTILS__BOX_HH_
2 #define __DIADES__UTILS__BOX_HH_
3 
12 #include <vector>
13 
14 namespace Diades
15 {
16  namespace Utils
17  {
26  template<typename Object>
27  class Box
28  {
29  public:
34  typedef std::reference_wrapper<Object> Reference;
35 
36  private:
42  std::vector<Reference> _box;
43 
44 
45  public:
50  Box():_box()
51  {
52  _box.reserve(1);
53  }
54 
55 
61  Box(Object & object):_box()
62  {
63  _box.reserve(1);
64  _box.push_back(object);
65  }
66 
67 
68 
73  Box(Box & box):_box()
74  {
75  _box.reserve(1);
76  if(!box.empty())
77  {
78  _box.push_back(box.obj());
79  }
80  }
81 
82 
83 
84 
91  Object & obj()
92  {
93  return _box[0];
94  }
95 
102  const Object & cObj() const
103  {
104  return _box[0];
105  }
106 
107 
114  bool empty() const
115  {
116  return _box.empty();
117  }
118 
119 
127  Object & operator=(Object & object)
128  {
129  _box.clear();
130  _box.push_back(object);
131  return object;
132  }
133 
134 
135 
144  bool operator < (const Box & box) const
145  {
146  if(empty())
147  {
148  return (!box.empty());
149  }
150  if(box.empty())
151  {
152  return false;
153  }
154  return cObj() < box.cObj();
155  }
156 
157 
158 
167  bool operator == (const Box & box) const
168  {
169  return (empty() && box.empty()) || (cObj() == box.cObj());
170  }
171 
172 
177  void clear()
178  {
179  _box.clear();
180  }
181  };
182 
183 
184 
185 
186 
187 
188 
197  template<typename Object>
198  class CBox
199  {
200  public:
205  typedef std::reference_wrapper<Object const> Reference;
206 
207  private:
213  std::vector<Reference> _box;
214 
215 
216  public:
221  CBox():_box()
222  {
223  _box.reserve(1);
224  }
225 
226 
232  CBox(const Object & object):_box()
233  {
234  _box.reserve(1);
235  _box.push_back(object);
236  }
237 
238 
239 
244  CBox(const CBox & box):_box()
245  {
246  _box.reserve(1);
247  if(!box.empty())
248  {
249  _box.push_back(box.obj());
250  }
251  }
252 
253 
254 
255 
262  const Object & obj() const
263  {
264  return _box[0];
265  }
266 
267 
274  bool empty() const
275  {
276  return _box.empty();
277  }
278 
279 
287  const Object & operator=(const Object & object)
288  {
289  _box.clear();
290  _box.push_back(object);
291  return object;
292  }
293 
294 
299  void clear()
300  {
301  _box.clear();
302  }
303 
304 
305 
306 
315  bool operator < (const CBox & box) const
316  {
317  if(empty())
318  {
319  return (!box.empty());
320  }
321  if(box.empty())
322  {
323  return false;
324  }
325  return obj() < box.obj();
326  }
327 
328 
329 
338  bool operator == (const CBox & box) const
339  {
340  return (empty() && box.empty()) || (obj() == box.obj());
341  }
342 
343 
344 
345 
346 
347  };
348 
349 
350  };
351 };
352 
353 
354 namespace std
355 {
363  template<typename Object> struct hash<Diades::Utils::Box<Object> >
364  {
365  size_t operator()(const Diades::Utils::Box<Object> & box) const
366  {
367  return hash<Object>()(box.cObj());
368  }
369  };
370 
371 
372 
380  template<typename Object> struct hash<Diades::Utils::CBox<Object> >
381  {
382  size_t operator()(const Diades::Utils::CBox<Object> & box) const
383  {
384  return hash<Object>()(box.obj());
385  }
386  };
387 };
388 
389 
390 #endif
std::reference_wrapper< Object const > Reference
Definition: Box.hh:205
std::reference_wrapper< Object > Reference
Definition: Box.hh:34
const Object & operator=(const Object &object)
Definition: Box.hh:287
Box(Object &object)
Definition: Box.hh:61
STL namespace.
const Object & obj() const
Definition: Box.hh:262
void clear()
Definition: Box.hh:177
Object & obj()
Definition: Box.hh:91
const Object & cObj() const
Definition: Box.hh:102
Namespace of the Diades project.
size_t operator()(const Diades::Utils::Box< Object > &box) const
Definition: Box.hh:365
size_t operator()(const Diades::Utils::CBox< Object > &box) const
Definition: Box.hh:382
bool empty() const
Definition: Box.hh:274
void clear()
Definition: Box.hh:299
A CBox is a container that contains at most a constant reference to an object.
Definition: Box.hh:198
Box(Box &box)
Definition: Box.hh:73
bool operator==(const Box &box) const
Definition: Box.hh:167
CBox(const Object &object)
Definition: Box.hh:232
Object & operator=(Object &object)
Definition: Box.hh:127
bool empty() const
Definition: Box.hh:114
bool operator<(const Box &box) const
Definition: Box.hh:144
CBox(const CBox &box)
Definition: Box.hh:244
A Box is a container that contains at most a reference to an object.
Definition: Box.hh:27
std::vector< Reference > _box
Definition: Box.hh:213
std::vector< Reference > _box
Definition: Box.hh:42