Wheefun I/O Library  0.0.5
Useful I/O Primitives.
iobuf.h
Go to the documentation of this file.
1 /* WFIO - the Wheefun IO Library
2  Copyright (C) 2018 Phillip Kilgore
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <https://www.gnu.org/licenses/>.
16 
17  Additionally, you should have recieved a copy of the GNU Classpath
18  exception, which amends the license to generally permit linking against
19  the software provided herein.
20 */
21 
30 #ifndef WFIO_IOBUF_H
31 #define WFIO_IOBUF_H
32 
33 #include <wfio/type.h>
34 
36 
40 typedef enum wfio_iobuf_flags_t {
45 
50 
51  /* Synthetic flags */
52 
61 
71 
83 typedef size_t (*wfio_iobuf_source_callback_t)(void* src, void* buf,
84  size_t len, int* error);
85 
97 typedef size_t (*wfio_iobuf_sink_callback_t)(void* dest, const void* buf,
98  size_t len, int* error);
99 
122 typedef struct wfio_iobuf_t {
126  const int flags;
127 
131  const size_t capacity;
132 
139  const size_t facet;
140 
145  const size_t avail;
146 
150  const size_t start;
154  const size_t end;
155 
159  void* const base;
160 } wfio_iobuf_t;
161 
172  return buf ? (buf->base) : NULL;
173 }
174 
183  return buf ? buf->capacity - buf->avail : 0;
184 }
185 
195  size_t sz) {
196  return buf ? (buf->facet - buf->avail)/sz : 0;
197 }
198 
207 WFIO_DLL int wfio_iobuf_clear(wfio_iobuf_t* buf, int* error);
208 
217 WFIO_DLL wfio_iobuf_t* wfio_iobuf_alloc(size_t capacity, int* error);
218 
231 WFIO_DLL wfio_iobuf_t* wfio_iobuf_allocSplit(size_t f0, size_t f1, int* error);
232 
247 
259 
266 
278 WFIO_DLL int wfio_iobuf_flip(wfio_iobuf_t* buf, int* error);
279 
289 }
290 
300 }
301 
311  if (wfio_iobuf_notRead(buf)) {
312  wfio_iobuf_flip(buf, error);
313  }
314  return 1;
315 }
316 
326  if (wfio_iobuf_notWrite(buf)) {
327  wfio_iobuf_flip(buf, error);
328  }
329  return 1;
330 }
331 
343 WFIO_DLL size_t wfio_iobuf_read(wfio_iobuf_t* buf, void* dest,
344  size_t sz, size_t elem, int* error);
345 
356 WFIO_DLL size_t wfio_iobuf_readbV(wfio_iobuf_t* buf, void* dest,
357  size_t len, int* error);
358 
368  unsigned char ret = 0;
369  if (wfio_iobuf_readbV(buf, &ret, 1, error) == WFIO_NPOS) {
370  return -1;
371  }
372  return ret;
373 }
374 
386 WFIO_DLL size_t wfio_iobuf_write(wfio_iobuf_t* buf, const void* src,
387  size_t sz, size_t elem, int* error);
388 
399 WFIO_DLL size_t wfio_iobuf_writebV(wfio_iobuf_t* buf, const void* src,
400  size_t len, int* error);
401 
410 WFIO_INLINE int wfio_iobuf_writeb(wfio_iobuf_t* buf, char c, int* error) {
411  return wfio_iobuf_writebV(buf, &c, 1, error) != WFIO_NPOS;
412 }
413 
422 WFIO_DLL int wfio_iobuf_forceReorganize(wfio_iobuf_t* buf, int* error);
423 
424 /* Source/Sink functions */
425 
445 WFIO_DLL size_t wfio_iobuf_source(wfio_iobuf_t* buf, size_t sz, size_t elem,
446  void* src, wfio_iobuf_source_callback_t cb, int* error);
447 
463 WFIO_DLL size_t wfio_iobuf_sourcebV(wfio_iobuf_t* buf, size_t len,
464  void* src, wfio_iobuf_source_callback_t cb, int* error);
465 
481  wfio_iobuf_source_callback_t cb, int* error) {
482  return wfio_iobuf_sourcebV(buf, WFIO_NPOS, dest, cb, error);
483 }
484 
504 WFIO_DLL size_t wfio_iobuf_sink(wfio_iobuf_t* buf, size_t sz, size_t elem,
505  void* dest, wfio_iobuf_sink_callback_t cb, int* error);
506 
522 WFIO_DLL size_t wfio_iobuf_sinkbV(wfio_iobuf_t* buf, size_t len,
523  void* dest, wfio_iobuf_sink_callback_t cb, int* error);
524 
540  wfio_iobuf_sink_callback_t cb, int* error) {
541  return wfio_iobuf_sinkbV(buf, WFIO_NPOS, dest, cb, error);
542 }
543 
545 
546 #ifdef __cplusplus
547 namespace wfio {
551  WFIO_CXX_DLL class IOBuf {
552  mutable ::wfio_iobuf_t* m_buf;
553 
554  public:
555 
560 
565 
570 
571  /* Constructors */
572 
578  inline IOBuf() : m_buf(NULL) {}
579 
591  inline IOBuf(::wfio_iobuf_t* buf, bool acquire=true)
592  : m_buf(buf) {
593  if (acquire)
594  wfio_iobuf_acquire(buf);
595  }
596 
606  inline IOBuf(const IOBuf& buf) : m_buf(buf.m_buf) {
607  ::wfio_iobuf_acquire(m_buf);
608  }
609 
618  inline IOBuf(size_t size, int* error = NULL) :
619  m_buf(::wfio_iobuf_alloc(size, error)) {
620  }
621 
631  inline IOBuf(size_t read, size_t write, int* error = NULL) :
632  m_buf(::wfio_iobuf_allocSplit(read, write, error))
633  {}
634 
641  inline ~IOBuf() {
642  ::wfio_iobuf_free(m_buf);
643  }
644 
645  /* Named Constructors */
646 
656  static IOBuf allocShared(size_t capacity,
657  int* error = NULL) {
658  return IOBuf(::wfio_iobuf_allocShared(capacity,
659  error), false);
660  }
661 
662  /* Fields */
663 
667  inline int flags() const throw() {
668  return m_buf ? m_buf->flags : 0;
669  }
670 
674  inline size_t capacity() const throw() {
675  return m_buf ? m_buf->capacity : 0;
676  }
677 
682  inline size_t facet() const throw() {
683  return m_buf ? m_buf->facet : 0;
684  }
685 
690  inline size_t avail() const throw() {
691  return m_buf ? m_buf->avail : 0;
692  }
693 
701  inline size_t remain() const throw() {
703  }
704 
714  inline size_t remainElem(size_t sz) const throw() {
716  }
717 
728  inline size_t start() const throw() {
729  return m_buf ? m_buf->start : 0;
730  }
731 
742  inline size_t end() const throw() {
743  return m_buf ? m_buf->end : 0;
744  }
745 
756  inline void* base() throw() {
757  return m_buf ? m_buf->base : 0;
758  }
759 
770  inline const void* base() const throw() {
771  return m_buf ? m_buf->base : 0;
772  }
773 
774  /* Read/Write (Todo: bytewise versions of this) */
775 
787  inline size_t read(void* dest, size_t sz, size_t elem,
788  int* error = NULL) throw() {
789  return ::wfio_iobuf_read(m_buf, dest, sz, elem,
790  error);
791  }
792 
803  inline size_t readbV(void* dest, size_t len,
804  int* error = NULL) throw() {
805  return ::wfio_iobuf_readbV(m_buf, dest, len,
806  error);
807  }
808 
818  inline int readb(int* error = NULL) throw() {
819  unsigned char c;
820  if (::wfio_iobuf_readbV(m_buf, &c, 1, error)
821  == WFIO_NPOS) {
822  return -1;
823  }
824  return c;
825  }
826 
838  inline size_t write(const void* src, size_t sz,
839  size_t elem, int* error = NULL) throw() {
840  return ::wfio_iobuf_write(m_buf, src, sz, elem,
841  error);
842  }
843 
854  inline size_t writebV(const void* src, size_t len,
855  int* error = NULL) throw() {
856  return ::wfio_iobuf_writebV(m_buf, src, len,
857  error);
858  }
859 
869  inline size_t writeb(char c, int* error = NULL) throw() {
870  return ::wfio_iobuf_writebV(m_buf, &c, 1,
871  error);
872  }
873 
874  /* Source/Sink */
875 
888  inline size_t source(size_t sz, size_t elem,
889  void* src, SourceCallback cb,
890  int* error = NULL) throw() {
891  return ::wfio_iobuf_source(m_buf, sz, elem, src,
892  cb, error);
893  }
894 
906  inline size_t fill(void* src, SourceCallback cb,
907  int* error = NULL) throw() {
908  return ::wfio_iobuf_fill(m_buf, src, cb, error);
909  }
910 
923  inline size_t sink(size_t sz, size_t elem,
924  void* src, SinkCallback cb,
925  int* error = NULL) throw() {
926  return ::wfio_iobuf_sink(m_buf, sz, elem, src,
927  cb, error);
928  }
929 
940  inline size_t empty(void* src, SinkCallback cb,
941  int* error = NULL) throw() {
942  return ::wfio_iobuf_empty(m_buf, src, cb, error);
943  }
944 
945 
946  /* Split buffer operations */
947 
959  inline bool flip(int* error = NULL) throw() {
960  return ::wfio_iobuf_flip(m_buf, error) != 0;
961  }
962 
970  inline bool notRead() const throw() {
971  return ::wfio_iobuf_notRead(m_buf) != 0;
972  }
973 
981  inline bool notWrite() const throw() {
982  return ::wfio_iobuf_notWrite(m_buf) != 0;
983  }
984 
993  inline bool ensureRead(int* error = NULL) throw() {
994  return ::wfio_iobuf_ensureRead(m_buf, error) != 0;
995  }
996 
1005  inline bool ensureWrite(int* error = NULL) throw() {
1006  return ::wfio_iobuf_ensureWrite(m_buf, error) != 0;
1007  }
1008 
1009  /* Other operations */
1010 
1018  inline bool forceReorganize(int* error = NULL) throw() {
1020  }
1021 
1027  inline int clear(int* error = NULL) throw() {
1028  return ::wfio_iobuf_clear(m_buf, error) != 0;
1029  }
1030 
1040  inline bool valid() const throw() {
1041  return m_buf != NULL;
1042  }
1043 
1049  inline operator bool () const {
1050  return m_buf != NULL;
1051  }
1052 
1058  return m_buf;
1059  }
1060 
1073  WFIO_CXX_DLL IOBuf& operator=(const IOBuf& rhs);
1074  };
1075 };
1076 #endif
1077 
1078 #endif
IOBuf(size_t size, int *error=NULL)
Construct a new exclusive buffer of length size.
Definition: iobuf.h:618
size_t empty(void *src, SinkCallback cb, int *error=NULL)
Empty this buffer into src.
Definition: iobuf.h:940
The root namespace for WFIO.
Definition: bin.h:879
wfio_iobuf_flags_t
A type representing a wfio_iobuf_t&#39;s flags.
Definition: iobuf.h:40
WFIO_DLL wfio_iobuf_t * wfio_iobuf_alloc(size_t capacity, int *error)
Create a new exclusive IO buffer.
const size_t start
The starting index of this buffer.
Definition: iobuf.h:150
size_t write(const void *src, size_t sz, size_t elem, int *error=NULL)
Emplace elements into this IOBuf.
Definition: iobuf.h:838
WFIO_INLINE void * wfio_iobuf_rawdata(wfio_iobuf_t *buf)
Obtain access to the raw data memory associated with this buffer.
Definition: iobuf.h:171
WFIO_DLL wfio_iobuf_t * wfio_iobuf_allocSplit(size_t f0, size_t f1, int *error)
Create a new shared IO buffer with split facet sizes.
const size_t end
The end index of this buffer.
Definition: iobuf.h:154
The IO buffer has been flipped.
Definition: iobuf.h:44
~IOBuf()
Release this IOBuf.
Definition: iobuf.h:641
#define WFIO_DLL
Mark the symbol as an element of the library.
Definition: host.h:397
#define WFIO_C_BEGIN
Definition: host.h:477
bool flip(int *error=NULL)
Flip this IOBuf.
Definition: iobuf.h:959
bool notRead() const
Whether or not this IOBuf is shared and not using the read facet.
Definition: iobuf.h:970
WFIO_INLINE int wfio_iobuf_readb(wfio_iobuf_t *buf, int *error)
Consume a single from the buffer buf.
Definition: iobuf.h:367
WFIO_DLL int wfio_iobuf_flip(wfio_iobuf_t *buf, int *error)
Flip the buffer buf.
size_t writeb(char c, int *error=NULL)
Emplace bytes into this IOBuf.
Definition: iobuf.h:869
WFIO_ALWAYS_INLINE int wfio_iobuf_notRead(const wfio_iobuf_t *buf)
Determine if the buf is shared and not using the read buffer.
Definition: iobuf.h:287
bool notWrite() const
Whether or not this IOBuf is shared and not using the write facet.
Definition: iobuf.h:981
IOBuf(::wfio_iobuf_t *buf, bool acquire=true)
Wrap a wfio_iobuf_t.
Definition: iobuf.h:591
WFIO_DLL int wfio_iobuf_acquire(wfio_iobuf_t *buf)
Acquire the buffer buf.
WFIO_ALWAYS_INLINE size_t wfio_iobuf_empty(wfio_iobuf_t *buf, void *dest, wfio_iobuf_sink_callback_t cb, int *error)
Empty this buffer into src.
Definition: iobuf.h:539
WFIO_DLL size_t wfio_iobuf_source(wfio_iobuf_t *buf, size_t sz, size_t elem, void *src, wfio_iobuf_source_callback_t cb, int *error)
Source new elements into buf from src.
const size_t facet
The capacity of this facet of the buffer.
Definition: iobuf.h:139
#define WFIO_ALWAYS_INLINE
Mark this symbol to be forced inline.
Definition: host.h:447
WFIO_ALWAYS_INLINE size_t wfio_iobuf_remain(wfio_iobuf_t *buf)
Obtain the number of bytes left in the buffer.
Definition: iobuf.h:182
WFIO_INLINE int wfio_iobuf_ensureWrite(wfio_iobuf_t *buf, int *error)
Ensure that buf is using the write buffer.
Definition: iobuf.h:325
size_t writebV(const void *src, size_t len, int *error=NULL)
Emplace bytes into this IOBuf.
Definition: iobuf.h:854
WFIO_DLL void wfio_iobuf_free(wfio_iobuf_t *buf)
Release the buffer buf.
bool ensureWrite(int *error=NULL)
Ensure that this IOBuf is using the write buffer.
Definition: iobuf.h:1005
int flags() const
Obtain this IOBuf&#39;s flags.
Definition: iobuf.h:667
int readb(int *error=NULL)
Consume bytes from this IOBuf.
Definition: iobuf.h:818
void * base()
Obtain the base address of this IOBuf&#39;s current facet.
Definition: iobuf.h:756
The IO buffer is shared.
Definition: iobuf.h:49
size_t remainElem(size_t sz) const
Obtain the space remaining in the current facet of this buffer for elements of size sz...
Definition: iobuf.h:714
size_t read(void *dest, size_t sz, size_t elem, int *error=NULL)
Consume elements from this IOBuf.
Definition: iobuf.h:787
WFIO_ALWAYS_INLINE size_t wfio_iobuf_fill(wfio_iobuf_t *buf, void *dest, wfio_iobuf_source_callback_t cb, int *error)
Fill this buffer from src.
Definition: iobuf.h:480
size_t(* wfio_iobuf_source_callback_t)(void *src, void *buf, size_t len, int *error)
A callback for wfio_iobuf_source().
Definition: iobuf.h:83
bool forceReorganize(int *error=NULL)
Force the reorganization of this IOBuf.
Definition: iobuf.h:1018
size_t sink(size_t sz, size_t elem, void *src, SinkCallback cb, int *error=NULL)
Sink new elements from this IOBuf into dest.
Definition: iobuf.h:923
size_t source(size_t sz, size_t elem, void *src, SourceCallback cb, int *error=NULL)
Source new elements into this IOBuf from src.
Definition: iobuf.h:888
size_t end() const
Obtain the end index for this IOBuf&#39;s current facet.
Definition: iobuf.h:742
WFIO_DLL size_t wfio_iobuf_sourcebV(wfio_iobuf_t *buf, size_t len, void *src, wfio_iobuf_source_callback_t cb, int *error)
Source new bytes into buf from src.
::wfio_iobuf_t * CType
The C type from which an IOBuf is derived.
Definition: iobuf.h:559
const int flags
Flags associated with this buffer.
Definition: iobuf.h:126
static IOBuf allocShared(size_t capacity, int *error=NULL)
Allocate a new shared IOBuf.
Definition: iobuf.h:656
The buffer is shared but not yet flipped.
Definition: iobuf.h:60
The buffer is shared and has been flipped.
Definition: iobuf.h:69
size_t(* wfio_iobuf_sink_callback_t)(void *dest, const void *buf, size_t len, int *error)
A callback for wfio_iobuf_sink().
Definition: iobuf.h:97
A C++ wrapper for a wfio_iobuf_t.
Definition: iobuf.h:551
WFIO_DLL size_t wfio_iobuf_sinkbV(wfio_iobuf_t *buf, size_t len, void *dest, wfio_iobuf_sink_callback_t cb, int *error)
Sink new bytes from buf into dest.
size_t remain() const
Obtain the space remaining in the current facet of this buffer.
Definition: iobuf.h:701
WFIO_DLL size_t wfio_iobuf_write(wfio_iobuf_t *buf, const void *src, size_t sz, size_t elem, int *error)
Emplace bytes into the buffer buf.
IOBuf(const IOBuf &buf)
Copy-construct an IOBuf.
Definition: iobuf.h:606
A constant indicating no position.
Definition: iotype.h:226
struct wfio_iobuf_t wfio_iobuf_t
A generic I/O buffer.
WFIO_ALWAYS_INLINE int wfio_iobuf_notWrite(const wfio_iobuf_t *buf)
Determine if the buf is shared and not using the write buffer.
Definition: iobuf.h:298
#define WFIO_INLINE
Mark this symbol for inlining.
Definition: host.h:377
size_t start() const
Obtain the starting index for this IOBuf&#39;s current facet.
Definition: iobuf.h:728
WFIO_DLL int wfio_iobuf_forceReorganize(wfio_iobuf_t *buf, int *error)
Force reoganization of this buffer.
#define WFIO_C_END
Definition: host.h:485
IOBuf()
Construct the default IOBuf.
Definition: iobuf.h:578
int clear(int *error=NULL)
Clear the contents of this IOBuf&#39;s current facet.
Definition: iobuf.h:1027
IOBuf(size_t read, size_t write, int *error=NULL)
Construct a new split buffer.
Definition: iobuf.h:631
WFIO_INLINE int wfio_iobuf_ensureRead(wfio_iobuf_t *buf, int *error)
Ensure that buf is using the read buffer.
Definition: iobuf.h:310
const size_t capacity
The maximum capacity of this buffer.
Definition: iobuf.h:131
size_t avail() const
The number of characters available for consumption in this IOBuf.
Definition: iobuf.h:690
size_t fill(void *src, SourceCallback cb, int *error=NULL)
Fill this IOBuf from src.
Definition: iobuf.h:906
size_t readbV(void *dest, size_t len, int *error=NULL)
Consume bytes from this IOBuf.
Definition: iobuf.h:803
WFIO_DLL size_t wfio_iobuf_sink(wfio_iobuf_t *buf, size_t sz, size_t elem, void *dest, wfio_iobuf_sink_callback_t cb, int *error)
Sink new elements from buf into dest.
WFIO_DLL size_t wfio_iobuf_writebV(wfio_iobuf_t *buf, const void *src, size_t len, int *error)
Emplace bytes into the buffer buf.
const void * base() const
Obtain the base address of this IOBuf&#39;s current facet in a constant context.
Definition: iobuf.h:770
WFIO_DLL size_t wfio_iobuf_readbV(wfio_iobuf_t *buf, void *dest, size_t len, int *error)
Consume bytes from the buffer buf.
void *const base
The base address of this buffer&#39;s data.
Definition: iobuf.h:159
bool valid() const
Determine if this IOBuf is valid.
Definition: iobuf.h:1040
WFIO_INLINE int wfio_iobuf_writeb(wfio_iobuf_t *buf, char c, int *error)
Consume a single from the buffer buf.
Definition: iobuf.h:410
size_t capacity() const
Obtain this IOBuf&#39;s total capacity.
Definition: iobuf.h:674
::wfio_iobuf_sink_callback_t SinkCallback
A callback used to implement a sink operation.
Definition: iobuf.h:569
A generic I/O buffer.
Definition: iobuf.h:122
const size_t avail
The number of available characters for consumption in this buffer.
Definition: iobuf.h:145
WFIO_DLL size_t wfio_iobuf_read(wfio_iobuf_t *buf, void *dest, size_t sz, size_t elem, int *error)
Consume elements from the buffer buf.
WFIO_DLL wfio_iobuf_t * wfio_iobuf_allocShared(size_t capacity, int *error)
Create a new shared IO buffer with evenly-split facet sizes.
WFIO_DLL int wfio_iobuf_clear(wfio_iobuf_t *buf, int *error)
Discard all data currently in current facet of this buffer.
bool ensureRead(int *error=NULL)
Ensure that this IOBuf is using the read buffer.
Definition: iobuf.h:993
WFIO_ALWAYS_INLINE size_t wfio_iobuf_remainElem(wfio_iobuf_t *buf, size_t sz)
Obtain the number of elements left in the buffer.
Definition: iobuf.h:194
Shared type definitions.
size_t facet() const
Obtain the capacity of this IOBuf&#39;s current facet.
Definition: iobuf.h:682
::wfio_iobuf_source_callback_t SourceCallback
A callback used to implement a source operation.
Definition: iobuf.h:564