ucommon
containers.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
2 //
3 // This file is part of GNU uCommon C++.
4 //
5 // GNU uCommon C++ is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // GNU uCommon C++ is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
17 
29 #ifndef _UCOMMON_CONTAINERS_H_
30 #define _UCOMMON_CONTAINERS_H_
31 
32 #ifndef _UCOMMON_CONFIG_H_
33 #include <ucommon/platform.h>
34 #endif
35 
36 #ifndef _UCOMMON_PROTOCOLS_H_
37 #include <ucommon/protocols.h>
38 #endif
39 
40 #ifndef _UCOMMON_LINKED_H_
41 #include <ucommon/linked.h>
42 #endif
43 
44 #ifndef _UCOMMON_MEMORY_H_
45 #include <ucommon/memory.h>
46 #endif
47 
48 #ifndef _UCOMMON_THREAD_H_
49 #include <ucommon/thread.h>
50 #endif
51 
52 NAMESPACE_UCOMMON
53 
60 class __EXPORT LinkedAllocator : private Conditional
61 {
62 protected:
63  LinkedObject *freelist;
64 
66 
67  LinkedObject *get(void);
68 
69  LinkedObject *get(timeout_t timeout);
70 
71  void release(LinkedObject *node);
72 
73 public:
78  operator bool();
79 
84  bool operator!();
85 };
86 
96 class __EXPORT Buffer : protected Conditional
97 {
98 private:
99  size_t bufsize, objsize;
100  caddr_t buf, head, tail;
101  unsigned objcount, limit;
102 
103 protected:
109  Buffer(size_t typesize, size_t count);
110 
114  virtual ~Buffer();
115 
121  void *get(timeout_t timeout);
122 
128  void *get(void);
129 
135  void put(void *data);
136 
143  bool put(void *data, timeout_t timeout);
144 
151  void release(void);
152 
158  void copy(void *data);
159 
166  bool copy(void *data, timeout_t timeout);
167 
175  void *peek(unsigned item);
176 
177  virtual void *invalid(void) const;
178 
179 public:
184  unsigned size(void);
185 
190  unsigned count(void);
191 
196  operator bool();
197 
202  bool operator!();
203 };
204 
215 class __EXPORT Queue : protected OrderedIndex, protected Conditional
216 {
217 private:
218  mempager *pager;
219  LinkedObject *freelist;
220  size_t used;
221 
222  class __LOCAL member : public OrderedObject
223  {
224  public:
225  member(Queue *q, ObjectProtocol *obj);
226  ObjectProtocol *object;
227  };
228 
229  friend class member;
230 
231 protected:
232  size_t limit;
233 
234  virtual ObjectProtocol *invalid(void) const;
235 
236 public:
244  Queue(mempager *pager = NULL, size_t number = 0);
245 
249  ~Queue();
250 
258  bool remove(ObjectProtocol *object);
259 
268  bool post(ObjectProtocol *object, timeout_t timeout = 0);
269 
275  ObjectProtocol *get(unsigned offset = 0);
276 
284  ObjectProtocol *fifo(timeout_t timeout = 0);
285 
293  ObjectProtocol *lifo(timeout_t timeout = 0);
294 
299  size_t count(void);
300 };
301 
310 class __EXPORT Stack : protected Conditional
311 {
312 private:
313  LinkedObject *freelist, *usedlist;
314  mempager *pager;
315  size_t used;
316 
317  class __LOCAL member : public LinkedObject
318  {
319  public:
320  member(Stack *s, ObjectProtocol *obj);
321  ObjectProtocol *object;
322  };
323 
324  friend class member;
325 
326 protected:
327  size_t limit;
328 
329  virtual ObjectProtocol *invalid(void) const;
330 
331 public:
338  Stack(mempager *pager = NULL, size_t number = 0);
339 
343  virtual ~Stack();
344 
352  bool remove(ObjectProtocol *object);
353 
362  bool push(ObjectProtocol *object, timeout_t timeout = 0);
363 
371  ObjectProtocol *pull(timeout_t timeout = 0);
372 
378  ObjectProtocol *get(unsigned offset = 0);
379 
384  size_t count(void);
385 
386  const ObjectProtocol *peek(timeout_t timeout = 0);
387 };
388 
396 template <class T>
398 {
399 private:
400  T* array;
401 
402 public:
403  inline linked_allocator(size_t size) : LinkedAllocator() {
404  array = new T[size];
405  for(unsigned i = 0; i < size; ++i)
406  array[i].enlist(&freelist);
407  }
408 
410  {delete[] array;};
411 
412  inline T *get(void)
413  {return static_cast<T *>(LinkedAllocator::get());};
414 
415  inline T *get(timeout_t timeout)
416  {return static_cast<T *>(LinkedAllocator::get(timeout));};
417 
418  inline void release(T *node)
419  {LinkedAllocator::release(node);};
420 };
421 
433 template<class T>
434 class bufferof : public Buffer
435 {
436 public:
441  inline bufferof(unsigned capacity) :
442  Buffer(sizeof(T), capacity) {};
443 
449  inline T *get(void)
450  {return static_cast<T*>(get());};
451 
457  inline T *get(timeout_t timeout)
458  {return static_cast<T*>(get(timeout));};
459 
465  inline void put(T *object)
466  {put(object);};
467 
474  inline bool put(T *object, timeout_t timeout)
475  {return put(object, timeout);};
476 
482  inline void copy(T *object)
483  {copy(object);};
484 
491  inline bool get(T *object, timeout_t timeout)
492  {return copy(object, timeout);};
493 
500  inline const T& at(unsigned item)
501  {return static_cast<const T&>(Buffer::peek(item));};
502 
509  inline T&operator[](unsigned item)
510  {return static_cast<T&>(Buffer::peek(item));};
511 
512  inline T* operator()(unsigned offset = 0)
513  {return static_cast<T*>(Buffer::peek(offset));}
514 };
515 
523 template<class T>
524 class stackof : public Stack
525 {
526 public:
532  inline stackof(mempager *memory, size_t size = 0) : Stack(memory, size) {};
533 
541  inline bool remove(T *object)
542  {return Stack::remove(object);};
543 
552  inline bool push(T *object, timeout_t timeout = 0)
553  {return Stack::push(object);};
554 
562  inline T *pull(timeout_t timeout = 0)
563  {return static_cast<T *>(Stack::pull(timeout));};
564 
571  inline const T *peek(timeout_t timeout = 0)
572  {return static_cast<const T *>(Stack::peek(timeout));};
573 
574  inline T* operator()(unsigned offset = 0)
575  {return static_cast<T*>(Stack::get(offset));}
576 
583  inline const T& at(unsigned offset = 0)
584  {return static_cast<const T&>(Stack::get(offset));};
585 
592  inline const T& operator[](unsigned offset)
593  {return static_cast<T&>(Stack::get(offset));};
594 
595 };
596 
604 template<class T>
605 class queueof : public Queue
606 {
607 public:
613  inline queueof(mempager *memory, size_t size = 0) : Queue(memory, size) {};
614 
622  inline bool remove(T *object)
623  {return Queue::remove(object);};
624 
633  inline bool post(T *object, timeout_t timeout = 0)
634  {return Queue::post(object);};
635 
643  inline T *fifo(timeout_t timeout = 0)
644  {return static_cast<T *>(Queue::fifo(timeout));};
645 
653  inline T *lifo(timeout_t timeout = 0)
654  {return static_cast<T *>(Queue::lifo(timeout));};
655 
662  inline const T& at(unsigned offset = 0)
663  {return static_cast<const T&>(Queue::get(offset));};
664 
671  inline T& operator[](unsigned offset)
672  {return static_cast<T&>(Queue::get(offset));};
673 
674  inline T* operator()(unsigned offset = 0)
675  {return static_cast<T*>(Queue::get(offset));}
676 };
677 
681 typedef Stack stack_t;
682 
686 typedef Queue fifo_t;
687 
688 END_NAMESPACE
689 
690 #endif