MAK Data Logger API Documentation for DIS
collectors.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 1992-2010 VT MAK
3 ** All rights reserved.
4 ******************************************************************************/
5 
9 
10 #ifndef collectors_H_
11 #define collectors_H_
12 
13 #include <vector>
14 #include <deque>
15 #include <list>
16 #include <set>
17 #include <vlutil/vlSmartPointers.h>
18 
19 namespace MAKLogger
20 {
21 
22 
25  template <typename T>
27  {
28  public:
29  virtual ~DtCollectorImpl() {}
30 
31  virtual void collect(T&) = 0;
32  virtual void collect(const T&) = 0;
33 
34  virtual size_t size() const = 0;
35  virtual bool full() const = 0;
36  virtual bool empty() const = 0;
37  };
38 
45  template <typename T>
47  {
48  public:
49 
51  myImpl(DtBoost::shared_ptr<DtCollectorImpl<T> >(impl)){}
52 
53  void collect(T& t) { myImpl->collect(t);}
54  void collect(const T& t) { myImpl->collect(t);}
55  size_t size() const { return myImpl->size();}
56  bool full() const { return myImpl->full();}
57  bool empty() const { return myImpl->empty();}
58 
59  protected:
60  DtBoost::shared_ptr<DtCollectorImpl<T> > myImpl;
61  private:
65  static void *operator new(size_t size);
67  static void operator delete(void* ptr);
68  };
69 
73  template <typename T,typename Container>
75  {
76  public:
77 
78  DtPushBackCollector(Container& container) :
79  myContainer(container)
80  {
81  }
82 
83  virtual void collect(T& t)
84  {
85  myContainer.push_back(t);
86  }
87 
88  virtual void collect(const T& t)
89  {
90  myContainer.push_back(t);
91  }
92 
93  virtual size_t size()const
94  {
95  return myContainer.size();
96  }
97 
98  virtual bool full() const
99  {
100  return false;
101  }
102 
103  virtual bool empty() const
104  {
105  return myContainer.empty();
106  }
107 
108  protected:
109  Container& myContainer;
110  };
111 
113  template <typename T,typename Container>
115  {
116  public:
117 
118  DtInsertCollector(Container& container) : myContainer(container) {}
119 
120  virtual void collect(T& t)
121  {
122  myContainer.insert(t);
123  }
124 
125  virtual void collect(const T& t)
126  {
127  myContainer.insert(t);
128  }
129 
130  virtual size_t size()const
131  {
132  return myContainer.size();
133  }
134 
135  virtual bool full() const
136  {
137  return false;
138  }
139 
140  virtual bool empty() const
141  {
142  return myContainer.empty();
143  }
144 
145  protected:
146  Container& myContainer;
147  };
148 
150  template <typename T,typename Container>
152  {
153  public:
154 
155  DtFixedSizePushBackCollector(Container& container,int size) :
156  myContainer(container),myMaxSize(size) {}
157 
158  virtual void collect(T& t)
159  {
160  if(!full) myContainer.push_back(t);
161  }
162 
163  virtual void collect(const T& t)
164  {
165  if(!full) myContainer.push_back(t);
166  }
167 
168  virtual void size()const
169  {
170  myContainer.size();
171  }
172 
173  virtual bool full() const
174  {
175  return myMaxSize == myContainer.size();
176  }
177 
178  virtual bool empty() const
179  {
180  return myContainer.empty();
181  }
182 
183  protected:
184  Container& myContainer;
186  };
187 
189  template <typename T>
191  {
192  public:
193 
194  DtFixedSizeArrayCollector(T* container,int size) :
195  myContainer(container),
196  myMaxSize(size),
197  myCurrentIndex(0)
198  {}
199 
200  virtual void collect(T& t)
201  {
202  if(!full) myContainer[myCurrentIndex] = t;
203  ++myCurrentIndex;
204  }
205 
206  virtual void collect(const T& t)
207  {
208  if(!full) myContainer[myCurrentIndex] = t;
209  ++myCurrentIndex;
210  }
211 
212  virtual void size()const
213  {
214  return myCurrentIndex;
215  }
216 
217  virtual bool full() const
218  {
219  return myMaxSize <= myCurrentIndex;
220  }
221 
222  virtual bool empty() const
223  {
224  return myCurrentIndex == 0;
225  }
226 
227  protected:
231  };
232 
235  {
236  public:
237  template <typename T>
238  static DtCollector<T> create(std::vector<T>& collection)
239  {
240  return new DtPushBackCollector<T,std::vector<T> >(collection);
241  }
242 
243  template <typename T>
244  static DtCollector<T> create(std::deque<T>& collection)
245  {
246  return new DtPushBackCollector<T,std::deque<T> >(collection);
247  }
248 
249  template <typename T>
250  static DtCollector<T> create(std::list<T>& collection)
251  {
252  return new DtPushBackCollector<T,std::list<T> >(collection);
253  }
254 
255  template <typename T>
256  static DtCollector<T> create(std::set<T>& collection)
257  {
258  return new DtInsertCollector<T,std::set<T> >(collection);
259  }
260 
261  template <typename T>
262  static DtCollector<T> create(T* array,int size)
263  {
264  return new DtFixedSizeArrayCollector<T>(array,size);
265  }
266  };
267 
268  template <typename Key,typename Value>
270  {
271  public:
272  virtual void add(Key,Value) = 0;
273  virtual bool containes(Key) = 0;
274 
275  virtual int size() const = 0;
276  virtual bool full() const = 0;
277  virtual bool empty() const = 0;
278  };
279 
281  template <typename Key,typename Value,typename Container>
282  class DtInsertMapCollector : public DtMapCollector<Key,Value>
283  {
284  public:
285  DtInsertMapCollector(Container& container) :
286  DtMapCollector<Key,Value>(),
287  myContainer(container) {}
288 
289  virtual void add(Key key,Value value)
290  {
291  myContainer.insert(key,value);
292  }
293 
294  virtual bool containes(Key key)
295  {
296  return myContainer.find(key) != myContainer.end();
297  }
298 
299  virtual int size()const
300  {
301  return myContainer.size();
302  }
303 
304  virtual bool full() const
305  {
306  return false;
307  }
308 
309  virtual bool empty() const
310  {
311  return myContainer.empty();
312  }
313 
314  protected:
315  Container& myContainer;
316  };
317 
318 }
319 
320 #endif

Document ID: Generated on Sun Dec 15 18:04:30 EST 2013 from SVN revision 134418
Copyright © 2005-2012 VT MÄK. All Rights Reserved (www.mak.com)