UCommon
containers.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This file is part of GNU uCommon C++.
5 //
6 // GNU uCommon C++ is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published
8 // by the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // GNU uCommon C++ is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18 
30 #ifndef _UCOMMON_CONTAINERS_H_
31 #define _UCOMMON_CONTAINERS_H_
32 
33 #ifndef _UCOMMON_CONFIG_H_
34 #include <ucommon/platform.h>
35 #endif
36 
37 #ifndef _UCOMMON_PROTOCOLS_H_
38 #include <ucommon/protocols.h>
39 #endif
40 
41 #ifndef _UCOMMON_LINKED_H_
42 #include <ucommon/linked.h>
43 #endif
44 
45 #ifndef _UCOMMON_MEMORY_H_
46 #include <ucommon/memory.h>
47 #endif
48 
49 #ifndef _UCOMMON_THREAD_H_
50 #include <ucommon/thread.h>
51 #endif
52 
53 namespace ucommon {
54 
61 class __EXPORT LinkedAllocator : private Conditional
62 {
63 protected:
64  LinkedObject *freelist;
65 
67 
68  LinkedObject *get(void);
69 
70  LinkedObject *get(timeout_t timeout);
71 
72  void release(LinkedObject *node);
73 
74 public:
79  operator bool() const;
80 
85  bool operator!() const;
86 };
87 
97 class __EXPORT Buffer : protected Conditional
98 {
99 private:
100  size_t bufsize, objsize;
101  caddr_t buf, head, tail;
102  unsigned objcount, limit;
103 
104 protected:
110  Buffer(size_t typesize, size_t count);
111 
115  virtual ~Buffer();
116 
122  void *get(timeout_t timeout);
123 
129  void *get(void);
130 
136  void put(void *data);
137 
144  bool put(void *data, timeout_t timeout);
145 
152  void release(void);
153 
159  void copy(void *data);
160 
167  bool copy(void *data, timeout_t timeout);
168 
176  void *peek(unsigned item);
177 
178  virtual void *invalid(void) const;
179 
180 public:
185  unsigned size(void) const;
186 
191  unsigned count(void) const;
192 
197  operator bool() const;
198 
203  bool operator!() const;
204 };
205 
216 class __EXPORT Queue : protected OrderedIndex, protected Conditional
217 {
218 private:
219  mempager *pager;
220  LinkedObject *freelist;
221  size_t used;
222 
223  class __LOCAL member : public OrderedObject
224  {
225  public:
226  member(Queue *q, ObjectProtocol *obj);
227  ObjectProtocol *object;
228  };
229 
230  friend class member;
231 
232 protected:
233  size_t limit;
234 
235  virtual ObjectProtocol *invalid(void) const;
236 
237 public:
245  Queue(mempager *pager = NULL, size_t number = 0);
246 
250  ~Queue();
251 
259  bool remove(ObjectProtocol *object);
260 
269  bool post(ObjectProtocol *object, timeout_t timeout = 0);
270 
276  ObjectProtocol *get(unsigned offset = 0);
277 
285  ObjectProtocol *fifo(timeout_t timeout = 0);
286 
294  ObjectProtocol *lifo(timeout_t timeout = 0);
295 
300  size_t count(void) const;
301 };
302 
311 class __EXPORT Stack : protected Conditional
312 {
313 private:
314  LinkedObject *freelist, *usedlist;
315  mempager *pager;
316  size_t used;
317 
318  class __LOCAL member : public LinkedObject
319  {
320  public:
321  member(Stack *s, ObjectProtocol *obj);
322  ObjectProtocol *object;
323  };
324 
325  friend class member;
326 
327 protected:
328  size_t limit;
329 
330  virtual ObjectProtocol *invalid(void) const;
331 
332 public:
339  Stack(mempager *pager = NULL, size_t number = 0);
340 
344  virtual ~Stack();
345 
353  bool remove(ObjectProtocol *object);
354 
363  bool push(ObjectProtocol *object, timeout_t timeout = 0);
364 
372  ObjectProtocol *pull(timeout_t timeout = 0);
373 
379  ObjectProtocol *get(unsigned offset = 0);
380 
385  size_t count(void) const;
386 
387  const ObjectProtocol *peek(timeout_t timeout = 0);
388 };
389 
397 template <class T>
399 {
400 private:
401  T* array;
402 
403 public:
404  inline linked_allocator(size_t size) : LinkedAllocator() {
405  array = new T[size];
406  for(unsigned i = 0; i < size; ++i)
407  array[i].enlist(&freelist);
408  }
409 
410  ~linked_allocator() {
411  delete[] array;
412  }
413 
414  inline T *get(void) {
415  return polypointer_cast<T *>(LinkedAllocator::get());
416  }
417 
418  inline T *get(timeout_t timeout) {
419  return polypointer_cast<T *>(LinkedAllocator::get(timeout));
420  }
421 
422  inline void release(T *node) {
423  LinkedAllocator::release(node);
424  }
425 };
426 
438 template<class T>
439 class bufferof : public Buffer
440 {
441 public:
446  inline bufferof(unsigned capacity) :
447  Buffer(sizeof(T), capacity) {}
448 
454  inline T *get(void) {
455  return static_cast<T*>(get());
456  }
457 
463  inline T *get(timeout_t timeout) {
464  return static_cast<T*>(get(timeout));
465  }
466 
472  inline void put(T *object) {
473  put(object);
474  }
475 
482  inline bool put(T *object, timeout_t timeout) {
483  return put(object, timeout);
484  }
485 
491  inline void copy(T *object) {
492  copy(object);
493  }
494 
501  inline bool get(T *object, timeout_t timeout) {
502  return copy(object, timeout);
503  }
504 
511  inline const T& at(unsigned item) {
512  return static_cast<const T&>(Buffer::peek(item));
513  }
514 
521  inline T&operator[](unsigned item) {
522  return static_cast<T&>(Buffer::peek(item));
523  }
524 
525  inline T* operator()(unsigned offset = 0) {
526  return static_cast<T*>(Buffer::peek(offset));
527  }
528 };
529 
537 template<class T>
538 class stackof : public Stack
539 {
540 public:
546  inline stackof(mempager *memory, size_t size = 0) : Stack(memory, size) {}
547 
555  inline bool remove(T *object) {
556  return Stack::remove(polypointer_cast<ObjectProtocol*>(object));
557  }
558 
567  inline bool push(T *object, timeout_t timeout = 0) {
568  return Stack::push(polypointer_cast<ObjectProtocol*>(object), timeout);
569  }
570 
578  inline T *pull(timeout_t timeout = 0) {
579  return polypointer_cast<T *>(Stack::pull(timeout));
580  }
581 
588  inline const T *peek(timeout_t timeout = 0) {
589  return polypointer_cast<const T *>(Stack::peek(timeout));
590  }
591 
592  inline T* operator()(unsigned offset = 0) {
593  return polypointer_cast<T*>(Stack::get(offset));
594  }
595 
602  inline const T& at(unsigned offset = 0) {
603  return polyreference_cast<const T&>(Stack::get(offset));
604  }
605 
612  inline const T& operator[](unsigned offset) {
613  return polyreference_cast<T&>(Stack::get(offset));
614  }
615 
616 };
617 
625 template<class T>
626 class queueof : public Queue
627 {
628 public:
634  inline queueof(mempager *memory, size_t size = 0) : Queue(memory, size) {}
635 
643  inline bool remove(T *object) {
644  return Queue::remove(polypointer_cast<ObjectProtocol *>(object));
645  }
646 
655  inline bool post(T *object, timeout_t timeout = 0) {
656  return Queue::post(polypointer_cast<ObjectProtocol*>(object), timeout);
657  }
658 
666  inline T *fifo(timeout_t timeout = 0) {
667  return polypointer_cast<T *>(Queue::fifo(timeout));
668  }
669 
677  inline T *lifo(timeout_t timeout = 0) {
678  return polypointer_cast<T *>(Queue::lifo(timeout));
679  }
680 
687  inline const T& at(unsigned offset = 0) {
688  return polyreference_cast<const T&>(Queue::get(offset));
689  }
690 
697  inline T& operator[](unsigned offset) {
698  return polyreference_cast<T&>(Queue::get(offset));
699  }
700 
701  inline T* operator()(unsigned offset = 0) {
702  return polypointer_cast<T*>(Queue::get(offset));
703  }
704 };
705 
709 typedef Stack stack_t;
710 
714 typedef Queue fifo_t;
715 
716 } // namespace ucommon
717 
718 #endif
The conditional is a common base for other thread synchronizing classes.
Definition: thread.h:87
bool push(ObjectProtocol *object, timeout_t timeout=0)
Push an object into the stack by it's pointer.
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
Definition: access.h:280
Linked allocator template to gather linked objects.
Definition: containers.h:398
Thread classes and sychronization objects.
Various miscellaneous platform specific headers and defines.
A thread-safe buffer for serializing and streaming class data.
Definition: containers.h:97
Queue fifo_t
Convenience type for using thread-safe object fifo (queue).
Definition: containers.h:714
queueof(mempager *memory, size_t size=0)
Create templated queue of typed objects.
Definition: containers.h:634
const T & at(unsigned offset=0)
Examine past item in the queue.
Definition: containers.h:687
const T & at(unsigned offset=0)
Examine past item in the stack.
Definition: containers.h:602
A managed private heap for small allocations.
Definition: memory.h:192
Common base class for all objects that can be formed into a linked list.
Definition: linked.h:59
A linked object base class for ordered objects.
Definition: linked.h:319
bufferof(unsigned capacity)
Create a buffer to hold a series of typed objects.
Definition: containers.h:446
void put(T *object)
Put (copy) a typed object into the buffer.
Definition: containers.h:472
ObjectProtocol * copy(ObjectProtocol *object)
Convenience function to access object copy.
Definition: object.h:510
bool push(T *object, timeout_t timeout=0)
Push a typed object into the stack by it's pointer.
Definition: containers.h:567
A templated typed class for thread-safe stack of object pointers.
Definition: containers.h:538
Linked objects, lists, templates, and containers.
void copy(T *object)
Copy the next typed object from the buffer.
Definition: containers.h:491
Private heaps, pools, and associations.
A common base class for all managed objects.
Definition: protocols.h:545
LinkedObject * get(void)
Get (pull) object off the list.
An index container for maintaining an ordered list of objects.
Definition: linked.h:182
stackof(mempager *memory, size_t size=0)
Create templated stack of typed objects.
Definition: containers.h:546
ObjectProtocol * lifo(timeout_t timeout=0)
Get and remove first object posted to the queue.
ObjectProtocol * fifo(timeout_t timeout=0)
Get and remove last object posted to the queue.
bool put(T *object, timeout_t timeout)
Put (copy) an object into the buffer.
Definition: containers.h:482
T * lifo(timeout_t timeout=0)
Get and remove last typed object posted to the queue.
Definition: containers.h:677
T &() limit(T &value, T &low, T &high)
Convenience macro to range restrict values.
Definition: generics.h:437
Common namespace for all ucommon objects.
Definition: access.h:47
T * pull(timeout_t timeout=0)
Get and remove last typed object posted to the stack.
Definition: containers.h:578
Manage a thread-safe stack of objects through reference pointers.
Definition: containers.h:311
bool remove(ObjectProtocol *object)
Remove a specific object pointer for the queue.
const T & at(unsigned item)
Examine past item in the buffer.
Definition: containers.h:511
Mempager managed type factory for pager pool objects.
Definition: memory.h:1345
ObjectProtocol * get(unsigned offset=0)
Examine an existing object on the stack.
bool remove(ObjectProtocol *object)
Remove a specific object pointer for the queue.
ObjectProtocol * pull(timeout_t timeout=0)
Get and remove last object pushed on the stack.
A templated typed class for thread-safe queue of object pointers.
Definition: containers.h:626
Linked allocator helper for linked_allocator template.
Definition: containers.h:61
T & operator[](unsigned offset)
Examine past item in the queue.
Definition: containers.h:697
void * peek(unsigned item)
Peek at pending data in buffer.
bool post(ObjectProtocol *object, timeout_t timeout=0)
Post an object into the queue by it's pointer.
Abstract interfaces and support.
A templated typed class for buffering of objects.
Definition: containers.h:439
T & operator[](unsigned item)
Examine past item in the buffer.
Definition: containers.h:521
const T * peek(timeout_t timeout=0)
Examine last typed object posted to the stack.
Definition: containers.h:588
bool post(T *object, timeout_t timeout=0)
Post a typed object into the queue by it's pointer.
Definition: containers.h:655
const T & operator[](unsigned offset)
Examine past item in the stack.
Definition: containers.h:612
Stack stack_t
Convenience type for using thread-safe object stacks.
Definition: containers.h:709
T * fifo(timeout_t timeout=0)
Get and remove first typed object posted to the queue.
Definition: containers.h:666
Manage a thread-safe queue of objects through reference pointers.
Definition: containers.h:216