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
14template<class T>
15class WvBufBase;
16
35template<class T>
37{
38protected:
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
48protected:
58 store(store) { }
59
60public:
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
585template<class T>
587{
588public:
589 explicit WvBufBase(WvBufStore *store) :
590 WvBufBaseCommonImpl<T>(store) { }
591};
592
593
594
602template<class T>
604{
605protected:
606 typedef T Elem;
607
608 WvInPlaceBufStore mystore;
609
610public:
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
726template<class T>
728{
729protected:
730 typedef T Elem;
731
733
734public:
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
812template<class T>
814{
815protected:
816 typedef T Elem;
817
818 WvCircularBufStore mystore;
819
820public:
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
939 {
940 mystore.normalize();
941 }
942};
943
944
945
952template<class T>
953class WvDynBufBase : public WvBufBase<T>
954{
955protected:
956 typedef T Elem;
957
958 WvDynBufStore mystore;
959
960public:
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
988template<class T>
989class WvNullBufBase : public WvBufBase<T>
990{
991protected:
992 typedef T Elem;
993
994 WvNullBufStore mystore;
995
996public:
999 WvBufBase<T>(& mystore),
1000 mystore(sizeof(Elem)) { }
1001};
1002
1003
1004
1013template<class T>
1015{
1016protected:
1017 typedef T Elem;
1018
1019 WvBufCursorStore mystore;
1020
1021public:
1033 size_t _length) :
1034 WvBufBase<T>(& mystore),
1035 mystore(sizeof(Elem), _buf.getstore(),
1036 _start * sizeof(Elem), _length * sizeof(Elem)) { }
1037};
1038
1039
1051template<class T>
1052class WvBufViewBase : public WvBufBase<T>
1053{
1054public:
1063 template<typename S>
1065 WvBufBase<T>(_buf.getstore()) { }
1066};
1067
1068#endif // __WVBUFFERBASE_H
An abstract generic buffer template.
Definition wvbufbase.h:37
void merge(Buffer &inbuf, size_t count)
Efficiently moves count bytes from the specified buffer into this one.
Definition wvbufbase.h:558
size_t optgettable() const
Returns the optimal maximum number of elements in the buffer currently available for reading without ...
Definition wvbufbase.h:154
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
WvBufBaseCommonImpl(WvBufStore *store)
Initializes the buffer.
Definition wvbufbase.h:57
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
T peek(int offset=0)
Returns the element at the specified offset in the buffer.
Definition wvbufbase.h:286
size_t ungettable() const
Returns the maximum number of elements that may be ungotten at this time.
Definition wvbufbase.h:188
virtual ~WvBufBaseCommonImpl()
Destroys the buffer.
Definition wvbufbase.h:62
WvBufStore * getstore()
Returns a pointer to the underlying storage class object.
Definition wvbufbase.h:69
bool iswritable() const
Returns true if the buffer supports writing.
Definition wvbufbase.h:342
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
void merge(Buffer &inbuf)
Efficiently merges the entire contents of a buffer into this one.
Definition wvbufbase.h:568
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
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
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
T get()
Reads the next element from the buffer.
Definition wvbufbase.h:272
size_t unallocable() const
Returns the maximum number of elements that may be unallocated at this time.
Definition wvbufbase.h:443
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
size_t free() const
Returns the number of elements that the buffer can currently accept for writing.
Definition wvbufbase.h:353
T * alloc(size_t count)
Allocates exactly the specified number of elements and returns a pointer to an UNINITIALIZED storage ...
Definition wvbufbase.h:379
void skip(size_t count)
Skips exactly the specified number of elements.
Definition wvbufbase.h:136
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
size_t optallocable() const
Returns the optimal maximum number of elements that the buffer can currently accept for writing witho...
Definition wvbufbase.h:397
void put(T &value)
Writes the element into the buffer at its tail.
Definition wvbufbase.h:519
bool isreadable() const
Returns true if the buffer supports reading.
Definition wvbufbase.h:81
void zap()
Clears the buffer.
Definition wvbufbase.h:257
size_t used() const
Returns the number of elements in the buffer currently available for reading.
Definition wvbufbase.h:92
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
void poke(T &value, int offset)
Writes the element into the buffer at the specified offset.
Definition wvbufbase.h:535
The generic buffer base type.
Definition wvbufbase.h:587
A buffer that acts like a cursor over a portion of another buffer.
Definition wvbufbase.h:1015
WvBufCursorBase(WvBufBase< T > &_buf, int _start, size_t _length)
Creates a new buffer.
Definition wvbufbase.h:1032
The WvBufCursor storage class.
Definition wvbufstore.h:513
The abstract buffer storage base class.
Definition wvbufstore.h:27
A buffer that provides a read-write view over another buffer with a different datatype.
Definition wvbufbase.h:1053
WvBufViewBase(WvBufBase< S > &_buf)
Creates a new buffer.
Definition wvbufbase.h:1064
A buffer that wraps a pre-allocated array and provides read-write access to its elements using a circ...
Definition wvbufbase.h:814
WvCircularBufBase(size_t _size)
Creates a new empty circular buffer backed by a new array.
Definition wvbufbase.h:841
void reset(T *_data, size_t _avail, size_t _size, bool _autofree=false)
Resets the underlying buffer pointer and properties.
Definition wvbufbase.h:910
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
WvCircularBufBase()
Creates a new empty buffer with no backing array.
Definition wvbufbase.h:846
void normalize()
Normalizes the arrangement of the data such that the contents of the buffer are stored at the beginni...
Definition wvbufbase.h:938
bool get_autofree() const
Returns the autofree flag.
Definition wvbufbase.h:883
size_t size() const
Returns the total size of the buffer.
Definition wvbufbase.h:873
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
T * ptr() const
Returns the underlying array pointer.
Definition wvbufbase.h:863
virtual ~WvCircularBufBase()
Destroys the buffer.
Definition wvbufbase.h:856
void set_autofree(bool _autofree)
Sets or clears the autofree flag.
Definition wvbufbase.h:893
The WvCircularBuf storage class.
Definition wvbufstore.h:321
A buffer that wraps a pre-allocated array and provides read-only access to its elements.
Definition wvbufbase.h:728
virtual ~WvConstInPlaceBufBase()
Destroys the buffer.
Definition wvbufbase.h:756
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
const T * ptr() const
Returns the underlying array pointer.
Definition wvbufbase.h:763
void reset(const T *_data, size_t _avail)
Resets the underlying buffer pointer and properties.
Definition wvbufbase.h:777
WvConstInPlaceBufBase()
Creates a new empty buffer with no backing array.
Definition wvbufbase.h:746
WvConstInPlaceBufBase(const T *_data, size_t _avail)
Creates a new buffer backed by the supplied array.
Definition wvbufbase.h:741
The WvConstInPlaceBuf storage class.
Definition wvbufstore.h:294
A buffer that dynamically grows and shrinks based on demand.
Definition wvbufbase.h:954
WvDynBufBase(size_t _minalloc=1024, size_t _maxalloc=1048576)
Creates a new buffer.
Definition wvbufbase.h:973
The WvDynBuf storage class.
Definition wvbufstore.h:481
A buffer that wraps a pre-allocated array and provides read-write access to its elements.
Definition wvbufbase.h:604
bool get_autofree() const
Returns the autofree flag.
Definition wvbufbase.h:672
void set_autofree(bool _autofree)
Sets or clears the autofree flag.
Definition wvbufbase.h:682
void reset(T *_data, size_t _avail, size_t _size, bool _autofree=false)
Resets the underlying buffer pointer and properties.
Definition wvbufbase.h:698
WvInPlaceBufBase(size_t _size)
Creates a new empty buffer backed by a new array.
Definition wvbufbase.h:630
virtual ~WvInPlaceBufBase()
Destroys the buffer.
Definition wvbufbase.h:645
T * ptr() const
Returns the underlying array pointer.
Definition wvbufbase.h:652
WvInPlaceBufBase()
Creates a new empty buffer with no backing array.
Definition wvbufbase.h:635
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
size_t size() const
Returns the total size of the buffer.
Definition wvbufbase.h:662
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
The WvInPlaceBuf storage class.
Definition wvbufstore.h:252
A buffer that is always empty.
Definition wvbufbase.h:990
WvNullBufBase()
Creates a new buffer.
Definition wvbufbase.h:998
The WvNullBuf storage class.
Definition wvbufstore.h:503