WvStreams
wvbufbase.h
1 /* -*- Mode: C++ -*-
2  * Worldvisions Weaver Software:
3  * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4  *
5  * A generic buffering API.
6  * Please declare specializations in a separate header file,
7  * See "wvbuf.h".
8  */
9 #ifndef __WVBUFFERBASE_H
10 #define __WVBUFFERBASE_H
11 
12 #include "wvbufstore.h"
13 
14 template<class T>
15 class WvBufBase;
16 
35 template<class T>
37 {
38 protected:
39  typedef T Elem;
40  typedef WvBufBase<T> Buffer;
41 
42  WvBufStore *store;
43 
44  // discourage copying
45  explicit WvBufBaseCommonImpl(
46  const WvBufBaseCommonImpl &other) { }
47 
48 protected:
57  explicit WvBufBaseCommonImpl(WvBufStore *store) :
58  store(store) { }
59 
60 public:
62  virtual ~WvBufBaseCommonImpl() { }
63 
70  {
71  return store;
72  }
73 
74  /*** Buffer Reading ***/
75 
81  bool isreadable() const
82  {
83  return store->isreadable();
84  }
85 
92  size_t used() const
93  {
94  return store->used() / sizeof(Elem);
95  }
96 
114  const T *get(size_t count)
115  {
116  if (count > used())
117  return NULL;
118 
119  return static_cast<const T*>(
120  store->get(count * sizeof(Elem)));
121  }
122 
136  void skip(size_t count)
137  {
138  store->skip(count * sizeof(Elem));
139  }
140 
154  size_t optgettable() const
155  {
156  size_t avail = store->optgettable();
157  size_t elems = avail / sizeof(Elem);
158  if (elems != 0) return elems;
159  return avail != 0 && store->used() >= sizeof(Elem) ? 1 : 0;
160  }
161 
177  void unget(size_t count)
178  {
179  store->unget(count * sizeof(Elem));
180  }
181 
188  size_t ungettable() const
189  {
190  return store->ungettable() / sizeof(Elem);
191  }
192 
225  const T *peek(int offset, size_t count)
226  {
227  return static_cast<const T*>(store->peek(
228  offset * sizeof(Elem), count * sizeof(Elem)));
229  }
230 
231  size_t peekable(int offset)
232  {
233  return store->peekable(offset * sizeof(Elem)) / sizeof(Elem);
234  }
235 
236  size_t optpeekable(int offset)
237  {
238  offset *= sizeof(Elem);
239  size_t avail = store->optpeekable(offset);
240  size_t elems = avail / sizeof(Elem);
241  if (elems != 0) return elems;
242  return avail != 0 &&
243  store->peekable(offset) >= sizeof(Elem) ? 1 : 0;
244  }
245 
257  void zap()
258  {
259  store->zap();
260  }
261 
272  T get()
273  {
274  return *get(1);
275  }
276 
286  T peek(int offset = 0)
287  {
288  return *peek(offset * sizeof(Elem), sizeof(Elem));
289  }
290 
309  void move(T *buf, size_t count)
310  {
311  store->move(buf, count * sizeof(Elem));
312  }
313 
330  void copy(T *buf, int offset, size_t count)
331  {
332  store->copy(buf, offset * sizeof(Elem), count * sizeof(Elem));
333  }
334 
335  /*** Buffer Writing ***/
336 
342  bool iswritable() const
343  {
344  return true;
345  }
346 
353  size_t free() const
354  {
355  return store->free() / sizeof(Elem);
356  }
357 
379  T *alloc(size_t count)
380  {
381  return static_cast<T*>(store->alloc(count * sizeof(Elem)));
382  }
383 
397  size_t optallocable() const
398  {
399  size_t avail = store->optallocable();
400  size_t elems = avail / sizeof(Elem);
401  if (elems != 0) return elems;
402  return avail != 0 && store->free() >= sizeof(Elem) ? 1 : 0;
403  }
404 
421  void unalloc(size_t count)
422  {
423  return store->unalloc(count * sizeof(Elem));
424  }
425 
443  size_t unallocable() const
444  {
445  return store->unallocable() / sizeof(Elem);
446  }
447 
461  T *mutablepeek(int offset, size_t count)
462  {
463  return static_cast<T*>(store->mutablepeek(
464  offset * sizeof(Elem), count * sizeof(Elem)));
465  }
466 
483  void put(const T *data, size_t count)
484  {
485  store->put(data, count * sizeof(Elem));
486  }
487 
504  void poke(const T *data, int offset, size_t count)
505  {
506  store->poke(data, offset * sizeof(Elem), count * sizeof(Elem));
507  }
508 
519  void put(T &value)
520  {
521  store->fastput(& value, sizeof(Elem));
522  }
523 
535  void poke(T &value, int offset)
536  {
537  poke(& value, offset, 1);
538  }
539 
540 
541  /*** Buffer to Buffer Transfers ***/
542 
558  void merge(Buffer &inbuf, size_t count)
559  {
560  store->merge(*inbuf.store, count * sizeof(Elem));
561  }
562 
568  void merge(Buffer &inbuf)
569  {
570  merge(inbuf, inbuf.used());
571  }
572 };
573 
574 
575 
585 template<class T>
586 class WvBufBase : public WvBufBaseCommonImpl<T>
587 {
588 public:
589  explicit WvBufBase(WvBufStore *store) :
590  WvBufBaseCommonImpl<T>(store) { }
591 };
592 
593 
594 
602 template<class T>
603 class WvInPlaceBufBase : public WvBufBase<T>
604 {
605 protected:
606  typedef T Elem;
607 
608  WvInPlaceBufStore mystore;
609 
610 public:
619  WvInPlaceBufBase(T *_data, size_t _avail, size_t _size,
620  bool _autofree = false) :
621  WvBufBase<T>(& mystore),
622  mystore(sizeof(Elem), _data, _avail * sizeof(Elem),
623  _size * sizeof(Elem), _autofree) { }
624 
630  explicit WvInPlaceBufBase(size_t _size) :
631  WvBufBase<T>(& mystore),
632  mystore(sizeof(Elem), _size * sizeof(Elem)) { }
633 
636  WvBufBase<T>(& mystore),
637  mystore(sizeof(Elem), NULL, 0, 0, false) { }
638 
645  virtual ~WvInPlaceBufBase() { }
646 
652  T *ptr() const
653  {
654  return static_cast<T*>(mystore.ptr());
655  }
656 
662  size_t size() const
663  {
664  return mystore.size() / sizeof(Elem);
665  }
666 
672  bool get_autofree() const
673  {
674  return mystore.get_autofree();
675  }
676 
682  void set_autofree(bool _autofree)
683  {
684  mystore.set_autofree(_autofree);
685  }
686 
698  void reset(T *_data, size_t _avail, size_t _size,
699  bool _autofree = false)
700  {
701  mystore.reset(_data, _avail * sizeof(Elem),
702  _size * sizeof(Elem), _autofree);
703  }
704 
711  void setavail(size_t _avail)
712  {
713  mystore.setavail(_avail * sizeof(Elem));
714  }
715 };
716 
717 
718 
726 template<class T>
728 {
729 protected:
730  typedef T Elem;
731 
732  WvConstInPlaceBufStore mystore;
733 
734 public:
741  WvConstInPlaceBufBase(const T *_data, size_t _avail) :
742  WvBufBase<T>(& mystore),
743  mystore(sizeof(Elem), _data, _avail * sizeof(Elem)) { }
744 
747  WvBufBase<T>(& mystore),
748  mystore(sizeof(Elem), NULL, 0) { }
749 
757 
763  const T *ptr() const
764  {
765  return static_cast<const T*>(mystore.ptr());
766  }
767 
777  void reset(const T *_data, size_t _avail)
778  {
779  mystore.reset(_data, _avail * sizeof(Elem));
780  }
781 
788  void setavail(size_t _avail)
789  {
790  mystore.setavail(_avail * sizeof(Elem));
791  }
792 };
793 
794 
795 
812 template<class T>
813 class WvCircularBufBase : public WvBufBase<T>
814 {
815 protected:
816  typedef T Elem;
817 
818  WvCircularBufStore mystore;
819 
820 public:
830  WvCircularBufBase(T *_data, size_t _avail, size_t _size,
831  bool _autofree = false) :
832  WvBufBase<T>(& mystore),
833  mystore(sizeof(Elem), _data, _avail * sizeof(Elem),
834  _size * sizeof(Elem), _autofree) { }
835 
841  explicit WvCircularBufBase(size_t _size) :
842  WvBufBase<T>(& mystore),
843  mystore(sizeof(Elem), _size * sizeof(Elem)) { }
844 
847  WvBufBase<T>(& mystore),
848  mystore(sizeof(Elem), NULL, 0, 0, false) { }
849 
856  virtual ~WvCircularBufBase() { }
857 
863  T *ptr() const
864  {
865  return static_cast<T*>(mystore.ptr());
866  }
867 
873  size_t size() const
874  {
875  return mystore.size() / sizeof(Elem);
876  }
877 
883  bool get_autofree() const
884  {
885  return mystore.get_autofree();
886  }
887 
893  void set_autofree(bool _autofree)
894  {
895  mystore.set_autofree(_autofree);
896  }
897 
910  void reset(T *_data, size_t _avail, size_t _size,
911  bool _autofree = false)
912  {
913  mystore.reset(_data, _avail * sizeof(Elem),
914  _size * sizeof(Elem), _autofree);
915  }
916 
924  void setavail(size_t _avail)
925  {
926  mystore.setavail(_avail * sizeof(Elem));
927  }
928 
938  void normalize()
939  {
940  mystore.normalize();
941  }
942 };
943 
944 
945 
952 template<class T>
953 class WvDynBufBase : public WvBufBase<T>
954 {
955 protected:
956  typedef T Elem;
957 
958  WvDynBufStore mystore;
959 
960 public:
973  explicit WvDynBufBase(size_t _minalloc = 1024,
974  size_t _maxalloc = 1048576) :
975  WvBufBase<T>(& mystore),
976  mystore(sizeof(Elem), _minalloc * sizeof(Elem),
977  _maxalloc * sizeof(Elem)) { }
978 };
979 
980 
981 
988 template<class T>
989 class WvNullBufBase : public WvBufBase<T>
990 {
991 protected:
992  typedef T Elem;
993 
994  WvNullBufStore mystore;
995 
996 public:
999  WvBufBase<T>(& mystore),
1000  mystore(sizeof(Elem)) { }
1001 };
1002 
1003 
1004 
1013 template<class T>
1014 class WvBufCursorBase : public WvBufBase<T>
1015 {
1016 protected:
1017  typedef T Elem;
1018 
1019  WvBufCursorStore mystore;
1020 
1021 public:
1032  WvBufCursorBase(WvBufBase<T> &_buf, int _start,
1033  size_t _length) :
1034  WvBufBase<T>(& mystore),
1035  mystore(sizeof(Elem), _buf.getstore(),
1036  _start * sizeof(Elem), _length * sizeof(Elem)) { }
1037 };
1038 
1039 
1051 template<class T>
1052 class WvBufViewBase : public WvBufBase<T>
1053 {
1054 public:
1063  template<typename S>
1065  WvBufBase<T>(_buf.getstore()) { }
1066 };
1067 
1068 #endif // __WVBUFFERBASE_H
WvBufBaseCommonImpl::iswritable
bool iswritable() const
Returns true if the buffer supports writing.
Definition: wvbufbase.h:342
WvBufBaseCommonImpl::put
void put(T &value)
Writes the element into the buffer at its tail.
Definition: wvbufbase.h:519
WvBufBaseCommonImpl::~WvBufBaseCommonImpl
virtual ~WvBufBaseCommonImpl()
Destroys the buffer.
Definition: wvbufbase.h:62
WvBufBaseCommonImpl::get
const T * get(size_t count)
Reads exactly the specified number of elements and returns a pointer to a storage location owned by t...
Definition: wvbufbase.h:114
WvCircularBufBase::setavail
void setavail(size_t _avail)
Sets the amount of available data using the current buffer and resets the read index to the beginning...
Definition: wvbufbase.h:924
WvConstInPlaceBufBase::setavail
void setavail(size_t _avail)
Sets the amount of available data using the current buffer and resets the read index to the beginning...
Definition: wvbufbase.h:788
WvConstInPlaceBufStore
The WvConstInPlaceBuf storage class.
Definition: wvbufstore.h:292
WvInPlaceBufBase::WvInPlaceBufBase
WvInPlaceBufBase(size_t _size)
Creates a new empty buffer backed by a new array.
Definition: wvbufbase.h:630
WvBufBaseCommonImpl::poke
void poke(const T *data, int offset, size_t count)
Efficiently copies the specified number of elements from the specified storage location into the buff...
Definition: wvbufbase.h:504
WvBufBaseCommonImpl::unget
void unget(size_t count)
Ungets exactly the specified number of elements by returning them to the buffer for subsequent reads.
Definition: wvbufbase.h:177
WvCircularBufBase::get_autofree
bool get_autofree() const
Returns the autofree flag.
Definition: wvbufbase.h:883
WvBufBaseCommonImpl::alloc
T * alloc(size_t count)
Allocates exactly the specified number of elements and returns a pointer to an UNINITIALIZED storage ...
Definition: wvbufbase.h:379
WvDynBufStore
The WvDynBuf storage class.
Definition: wvbufstore.h:480
WvBufBaseCommonImpl::unalloc
void unalloc(size_t count)
Unallocates exactly the specified number of elements by removing them from the buffer and releasing t...
Definition: wvbufbase.h:421
WvBufCursorBase::WvBufCursorBase
WvBufCursorBase(WvBufBase< T > &_buf, int _start, size_t _length)
Creates a new buffer.
Definition: wvbufbase.h:1032
WvBufBaseCommonImpl::free
size_t free() const
Returns the number of elements that the buffer can currently accept for writing.
Definition: wvbufbase.h:353
WvBufBaseCommonImpl::optgettable
size_t optgettable() const
Returns the optimal maximum number of elements in the buffer currently available for reading without ...
Definition: wvbufbase.h:154
WvBufBaseCommonImpl::ungettable
size_t ungettable() const
Returns the maximum number of elements that may be ungotten at this time.
Definition: wvbufbase.h:188
WvBufBaseCommonImpl::getstore
WvBufStore * getstore()
Returns a pointer to the underlying storage class object.
Definition: wvbufbase.h:69
WvBufBaseCommonImpl::get
T get()
Reads the next element from the buffer.
Definition: wvbufbase.h:272
WvBufBaseCommonImpl::WvBufBaseCommonImpl
WvBufBaseCommonImpl(WvBufStore *store)
Initializes the buffer.
Definition: wvbufbase.h:57
WvBufBase
The generic buffer base type.
Definition: wvbufbase.h:15
WvBufBaseCommonImpl::isreadable
bool isreadable() const
Returns true if the buffer supports reading.
Definition: wvbufbase.h:81
WvInPlaceBufBase::set_autofree
void set_autofree(bool _autofree)
Sets or clears the autofree flag.
Definition: wvbufbase.h:682
WvBufBaseCommonImpl::move
void move(T *buf, size_t count)
Efficiently copies the specified number of elements from the buffer to the specified UNINITIALIZED st...
Definition: wvbufbase.h:309
WvConstInPlaceBufBase
A buffer that wraps a pre-allocated array and provides read-only access to its elements.
Definition: wvbufbase.h:727
WvBufViewBase
A buffer that provides a read-write view over another buffer with a different datatype.
Definition: wvbufbase.h:1052
WvBufBaseCommonImpl::copy
void copy(T *buf, int offset, size_t count)
Efficiently copies the specified number of elements from the buffer to the specified UNINITIALIZED st...
Definition: wvbufbase.h:330
WvInPlaceBufBase::WvInPlaceBufBase
WvInPlaceBufBase(T *_data, size_t _avail, size_t _size, bool _autofree=false)
Creates a new buffer backed by the supplied array.
Definition: wvbufbase.h:619
WvInPlaceBufBase
A buffer that wraps a pre-allocated array and provides read-write access to its elements.
Definition: wvbufbase.h:603
WvConstInPlaceBufBase::reset
void reset(const T *_data, size_t _avail)
Resets the underlying buffer pointer and properties.
Definition: wvbufbase.h:777
WvCircularBufBase
A buffer that wraps a pre-allocated array and provides read-write access to its elements using a circ...
Definition: wvbufbase.h:813
WvConstInPlaceBufBase::WvConstInPlaceBufBase
WvConstInPlaceBufBase()
Creates a new empty buffer with no backing array.
Definition: wvbufbase.h:746
WvCircularBufBase::ptr
T * ptr() const
Returns the underlying array pointer.
Definition: wvbufbase.h:863
WvCircularBufBase::reset
void reset(T *_data, size_t _avail, size_t _size, bool _autofree=false)
Resets the underlying buffer pointer and properties.
Definition: wvbufbase.h:910
WvCircularBufStore
The WvCircularBuf storage class.
Definition: wvbufstore.h:320
WvCircularBufBase::set_autofree
void set_autofree(bool _autofree)
Sets or clears the autofree flag.
Definition: wvbufbase.h:893
WvBufBaseCommonImpl::merge
void merge(Buffer &inbuf)
Efficiently merges the entire contents of a buffer into this one.
Definition: wvbufbase.h:568
WvCircularBufBase::size
size_t size() const
Returns the total size of the buffer.
Definition: wvbufbase.h:873
WvBufBase< unsigned char >
Specialization of WvBufBase for unsigned char type buffers intended for use with raw memory buffers.
Definition: wvbuf.h:22
WvBufBaseCommonImpl::zap
void zap()
Clears the buffer.
Definition: wvbufbase.h:257
WvBufBaseCommonImpl::put
void put(const T *data, size_t count)
Writes the specified number of elements from the specified storage location into the buffer at its ta...
Definition: wvbufbase.h:483
WvBufStore
The abstract buffer storage base class.
Definition: wvbufstore.h:26
WvCircularBufBase::WvCircularBufBase
WvCircularBufBase(size_t _size)
Creates a new empty circular buffer backed by a new array.
Definition: wvbufbase.h:841
WvInPlaceBufStore
The WvInPlaceBuf storage class.
Definition: wvbufstore.h:251
WvConstInPlaceBufBase::~WvConstInPlaceBufBase
virtual ~WvConstInPlaceBufBase()
Destroys the buffer.
Definition: wvbufbase.h:756
WvBufBaseCommonImpl::peek
T peek(int offset=0)
Returns the element at the specified offset in the buffer.
Definition: wvbufbase.h:286
WvConstInPlaceBufBase::WvConstInPlaceBufBase
WvConstInPlaceBufBase(const T *_data, size_t _avail)
Creates a new buffer backed by the supplied array.
Definition: wvbufbase.h:741
WvInPlaceBufBase::reset
void reset(T *_data, size_t _avail, size_t _size, bool _autofree=false)
Resets the underlying buffer pointer and properties.
Definition: wvbufbase.h:698
WvNullBufBase
A buffer that is always empty.
Definition: wvbufbase.h:989
WvBufCursorBase
A buffer that acts like a cursor over a portion of another buffer.
Definition: wvbufbase.h:1014
WvDynBufBase
A buffer that dynamically grows and shrinks based on demand.
Definition: wvbufbase.h:953
WvInPlaceBufBase::size
size_t size() const
Returns the total size of the buffer.
Definition: wvbufbase.h:662
WvBufBaseCommonImpl::skip
void skip(size_t count)
Skips exactly the specified number of elements.
Definition: wvbufbase.h:136
WvInPlaceBufBase::~WvInPlaceBufBase
virtual ~WvInPlaceBufBase()
Destroys the buffer.
Definition: wvbufbase.h:645
WvInPlaceBufBase::get_autofree
bool get_autofree() const
Returns the autofree flag.
Definition: wvbufbase.h:672
WvBufBaseCommonImpl::used
size_t used() const
Returns the number of elements in the buffer currently available for reading.
Definition: wvbufbase.h:92
WvBufBaseCommonImpl::unallocable
size_t unallocable() const
Returns the maximum number of elements that may be unallocated at this time.
Definition: wvbufbase.h:443
WvNullBufBase::WvNullBufBase
WvNullBufBase()
Creates a new buffer.
Definition: wvbufbase.h:998
WvCircularBufBase::~WvCircularBufBase
virtual ~WvCircularBufBase()
Destroys the buffer.
Definition: wvbufbase.h:856
WvBufBaseCommonImpl::peek
const T * peek(int offset, size_t count)
Returns a const pointer into the buffer at the specified offset to the specified number of elements w...
Definition: wvbufbase.h:225
WvBufBaseCommonImpl
An abstract generic buffer template.
Definition: wvbufbase.h:36
WvBufBaseCommonImpl::mutablepeek
T * mutablepeek(int offset, size_t count)
Returns a non-const pointer info the buffer at the specified offset to the specified number of elemen...
Definition: wvbufbase.h:461
WvCircularBufBase::WvCircularBufBase
WvCircularBufBase()
Creates a new empty buffer with no backing array.
Definition: wvbufbase.h:846
WvBufBaseCommonImpl::merge
void merge(Buffer &inbuf, size_t count)
Efficiently moves count bytes from the specified buffer into this one.
Definition: wvbufbase.h:558
WvBufViewBase::WvBufViewBase
WvBufViewBase(WvBufBase< S > &_buf)
Creates a new buffer.
Definition: wvbufbase.h:1064
WvBufBaseCommonImpl::poke
void poke(T &value, int offset)
Writes the element into the buffer at the specified offset.
Definition: wvbufbase.h:535
WvInPlaceBufBase::ptr
T * ptr() const
Returns the underlying array pointer.
Definition: wvbufbase.h:652
WvDynBufBase::WvDynBufBase
WvDynBufBase(size_t _minalloc=1024, size_t _maxalloc=1048576)
Creates a new buffer.
Definition: wvbufbase.h:973
WvNullBufStore
The WvNullBuf storage class.
Definition: wvbufstore.h:501
WvCircularBufBase::normalize
void normalize()
Normalizes the arrangement of the data such that the contents of the buffer are stored at the beginni...
Definition: wvbufbase.h:938
WvBufBaseCommonImpl::optallocable
size_t optallocable() const
Returns the optimal maximum number of elements that the buffer can currently accept for writing witho...
Definition: wvbufbase.h:397
WvConstInPlaceBufBase::ptr
const T * ptr() const
Returns the underlying array pointer.
Definition: wvbufbase.h:763
WvInPlaceBufBase::WvInPlaceBufBase
WvInPlaceBufBase()
Creates a new empty buffer with no backing array.
Definition: wvbufbase.h:635
WvCircularBufBase::WvCircularBufBase
WvCircularBufBase(T *_data, size_t _avail, size_t _size, bool _autofree=false)
Creates a new circular buffer backed by the supplied array.
Definition: wvbufbase.h:830
WvBufCursorStore
The WvBufCursor storage class.
Definition: wvbufstore.h:511
WvInPlaceBufBase::setavail
void setavail(size_t _avail)
Sets the amount of available data using the current buffer and resets the read index to the beginning...
Definition: wvbufbase.h:711