Wheefun I/O Library  0.0.5
Useful I/O Primitives.
peer.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 
22 #ifndef WFIO_PEER_H
23 #define WFIO_PEER_H
24 
25 #include <wfio/type.h>
26 #include <wfio/device.h>
27 
37 
44 typedef struct wfio_peer_t {
52  union {
54  void* const any;
56  struct wfio_device_t* const device;
58  struct wfio_binstream_t* const bin;
59  } impl;
60 } wfio_peer_t;
61 
73  struct wfio_device_t* device, int ioshare, int* error);
74 
85  struct wfio_device_t* device, int* error) {
86  return wfio_peer_fromDeviceEx(peer, device,
87  WFIO_IOSHARE_O_ACQUIRE, error);
88 }
89 
102  struct wfio_binstream_t* bin, int ioshare, int* error);
103 
114  struct wfio_binstream_t* bin, int* error) {
115  return wfio_peer_fromBinstreamEx(peer, bin,
116  WFIO_IOSHARE_O_ACQUIRE, error);
117 }
118 
135  wfio_ioshare_t ioshare, int* error);
136 
145 WFIO_DLL int wfio_peer_acquire(wfio_peer_t* peer, int* error);
146 
147 
154 
155 /* Basic I/O functions */
156 
168 WFIO_DLL size_t wfio_peer_read(wfio_peer_t* peer, void* buf,
169  size_t sz, size_t elem, int* error);
170 
182  size_t len, int* error) {
183  return wfio_peer_read(peer, buf, 1, len, error);
184 }
185 
195  unsigned char c;
196  if (wfio_peer_read(peer, &c, 1, 1, error) == WFIO_NPOS) {
197  return -1;
198  }
199  return c;
200 }
201 
213 WFIO_DLL size_t wfio_peer_write(wfio_peer_t* peer, const void* buf,
214  size_t sz, size_t elem, int* error);
215 
227  const void* buf, size_t len, int* error) {
228  return wfio_peer_write(peer, buf, 1, len, error);
229 }
230 
241  int* error) {
242  return wfio_peer_write(peer, &c, 1, 1, error) == 1;
243 }
244 
255  wfio_dcntl_t cmd, int* error, va_list va);
256 
267  wfio_dcntl_t cmd, int*error, ...) {
268  va_list va;
269  register int ret;
270  va_start(va, cmd);
271  ret = wfio_peer_vdcntl(peer, cmd, error, va);
272  va_end(va);
273  return ret;
274 }
275 
285 WFIO_DLL int wfio_peer_vioctl(wfio_peer_t* peer, int cmd, int* error,
286  va_list va);
287 
298  int cmd, ...) {
299  va_list va;
300  register int ret;
301  va_start(va, cmd);
302  ret = wfio_peer_vioctl(peer, cmd, error, va);
303  va_end(va);
304  return ret;
305 }
306 
320 WFIO_DLL int wfio_peer_seek(wfio_peer_t* peer, wfio_off_t offset,
321  wfio_whence_t whence, int* error);
322 
331 
342 
354 WFIO_DLL_FASTCALL int wfio_peer_flush(wfio_peer_t* peer, int* error);
355 
366 WFIO_DLL_FASTCALL size_t wfio_peer_getbufsz(wfio_peer_t* peer, int* error);
367 
389 WFIO_DLL size_t wfio_peer_setbufsz(wfio_peer_t* peer, size_t sz,
390  int* error);
391 
393 
394 #ifdef __cplusplus
395 namespace wfio {
396  class Peer {
397  public:
398 
403 
407  enum Type {
414  };
415 
416  private:
417 
418  Type m_type;
419  void* m_impl;
420 
421  public:
422 
423  /* Constructors */
424 
430  inline Peer() : m_type(UNKNOWN), m_impl(NULL) {}
431 
438  inline Peer(CType& ctype, int* error = 0) :
439  m_type((Type) ctype.type),
440  m_impl(ctype.impl.any) {
441  wfio_peer_acquire(&ctype, error);
442  }
443 
452  inline Peer(CType& ctype, IOShare share, int* error = 0) {
453  CType ret = wfio_peer_ioshare(&ctype, share,
454  error);
455  this->m_type = (Type) ret.type;
456  this->m_impl = ret.impl.any;
457  }
458 
465  inline Peer(CType* ctype, int* error = 0) {
466  CType ret = wfio_peer_ioshare(ctype,
468  error);
469  this->m_type = (Type) ret.type;
470  this->m_impl = ret.impl.any;
471  }
472 
481  inline Peer(CType* ctype, IOShare share, int* error = 0) {
482  CType ret = wfio_peer_ioshare(ctype, share,
483  error);
484  this->m_type = (Type) ret.type;
485  this->m_impl = ret.impl.any;
486  }
487 
494  inline Peer(Device& device, int* error = 0) {
495  ::wfio_peer_fromDevice((CType*) this,
496  *((::wfio_device_t**) &device),
497  error);
498  }
499 
508  inline Peer(Device& device, IOShare share,
509  int* error = 0) {
510  ::wfio_peer_fromDeviceEx((CType*) this,
511  *((::wfio_device_t**) &device),
512  share, error);
513  }
514 
521  Peer(BinStream& bin, int* error = 0) {
522  ::wfio_peer_fromBinstream((CType*) this,
523  *((::wfio_binstream_t**) &bin),
524  error);
525  }
526 
535  Peer(BinStream& bin, IOShare share, int* error = 0) {
536  ::wfio_peer_fromBinstreamEx((CType*) this,
537  *((::wfio_binstream_t**) &bin),
538  share, error);
539  }
540 
546  inline ~Peer() {
548  reinterpret_cast<CType*>(this));
549  }
550 
551  /* Fields */
552 
556  inline Type type() const throw() {
557  return m_type;
558  }
559 
560  /* Operations */
561 
573  inline size_t read(void* buf, size_t sz, size_t elem,
574  int* error = 0) throw() {
576  reinterpret_cast<CType*>(this), buf,
577  sz, elem, error);
578  }
579 
590  size_t read(void* buf, size_t len, int* error) throw() {
591  return read(buf, 1, len, error);
592  }
593 
601  int read(int* error = 0) {
602  unsigned char c;
603  if (!read(&c, 1, 1, error) == WFIO_NPOS) {
604  return -1;
605  }
606  return c;
607  }
608 
620  size_t write(const void* buf, size_t sz, size_t elem,
621  int* error = 0) {
623  reinterpret_cast<CType*>(this), buf, sz,
624  elem, error);
625  }
626 
627 
638  size_t write(const void* buf, size_t len, int* error = 0) {
639  return write(buf, 1, len, error);
640  }
641 
650  int write(char c, int* error = 0) {
651  return write(&c, 1, 1, error) == 1;
652  }
653 
654  /* Seek */
655 
669  bool seek(off_t offset, wfio_whence_t whence,
670  int* error = 0) {
671  return ::wfio_peer_seek((CType*) this, offset,
672  whence, error) != 0;
673  }
674 
680  bool canSeek() const throw() {
681  return ::wfio_peer_canSeek((CType*) this);
682  }
683 
694  off_t tell(int* error = 0) throw() {
695  return ::wfio_peer_tell((CType*) this, error);
696  }
697 
698  /* Buffering */
699 
707  bool flush(int* error = 0) {
708  return ::wfio_peer_flush((CType*) this, error)
709  != 0;
710  }
711 
712 
722  size_t bufsz(int* error = 0) const throw() {
723  return ::wfio_peer_getbufsz((CType*) this, error);
724  }
725 
740  size_t bufsz(size_t sz, int* error = 0) throw() {
741  return ::wfio_peer_setbufsz((CType*) this, sz,
742  error);
743  }
744 
745  /* I/O Control */
746 
756  inline int vdcntl(DCntl cmd, va_list va) {
758  reinterpret_cast<CType*>(this),
759  cmd, NULL, va);
760  }
761 
772  inline int vdcntlEx(int* error, DCntl cmd,
773  va_list va) {
775  reinterpret_cast<CType*>(this),
776  cmd, error, va);
777  }
778 
787  inline int dcntl(DCntl cmd, ...) {
788  va_list va;
789  register int ret;
790  va_start(va, cmd);
791  ret = this->vdcntl(cmd, va);
792  va_end(va);
793  return ret;
794  }
795 
806  inline int dcntl(int* error, DCntl cmd, ...) {
807  va_list va;
808  register int ret;
809  va_start(va, cmd);
810  ret = this->vdcntlEx(error, cmd, va);
811  va_end(va);
812  return ret;
813  }
814 
824  inline int vioctl(int cmd, va_list va) {
826  reinterpret_cast<CType*>(this),
827  cmd, NULL, va);
828  }
829 
840  inline int vioctlEx(int* error, int cmd,
841  va_list va) {
843  reinterpret_cast<CType*>(this),
844  cmd, error, va);
845  }
846 
855  inline int ioctl(int cmd, ...) {
856  va_list va;
857  register int ret;
858  va_start(va, cmd);
859  ret = this->vioctl(cmd, va);
860  va_end(va);
861  return ret;
862  }
863 
874  inline int ioctlExt(int* error, int cmd, ...) {
875  va_list va;
876  register int ret;
877  va_start(va, cmd);
878  ret = this->vioctlEx(error, cmd, va);
879  va_end(va);
880  return ret;
881  }
882 
883  /* Conversion Operations */
884 
889  return reinterpret_cast<CType*>(this);
890  }
891 
904  WFIO_DLL_FASTCALL Device toDevice(int* error = 0);
905 
911  inline operator Device() {
912  return this->toDevice(NULL);
913  }
914 
927  WFIO_DLL_FASTCALL BinStream toBinStream(int* error = 0);
928 
934  inline operator BinStream() {
935  return this->toBinStream(NULL);
936  }
937  };
938 }
939 #endif
940 
941 #endif
The root namespace for WFIO.
Definition: bin.h:879
int vdcntl(DCntl cmd, va_list va)
Perform device-independent I/O on this peer using a va_list.
Definition: peer.h:756
Peer(CType &ctype, int *error=0)
Construct a peer from a CType.
Definition: peer.h:438
~Peer()
Definition: peer.h:546
bool flush(int *error=0)
Attempt to flush this Peer.
Definition: peer.h:707
A command to be supplied to a device or similar stream.
Definition: iotype.h:334
Peer(Device &device, IOShare share, int *error=0)
Construct a Peer from a Device using the specified IO sharing mode.
Definition: peer.h:508
WFIO_ALWAYS_INLINE int wfio_peer_readb(wfio_peer_t *peer, int *error)
Read a single byte from this peer.
Definition: peer.h:194
Wrap a wfio_binstream_t.
Definition: bin.h:883
#define WFIO_DLL
Mark the symbol as an element of the library.
Definition: host.h:397
WFIO_C_BEGIN typedef long int wfio_off_t
An integer corresponding to a device offset.
Definition: iotype.h:48
#define WFIO_C_BEGIN
Definition: host.h:477
#define WFIO_DLL_FASTCALL
Shorthand for WFIO_DLL WFIO_FASTCALL.
Definition: host.h:425
Definition: iotype.h:129
WFIO_DLL_FASTCALL int wfio_peer_canSeek(wfio_peer_t *peer)
Determine whether or not this peer can seek.
A generic stream encapsulation.
Definition: peer.h:44
Peer(CType *ctype, int *error=0)
Construct a peer from a CType pointer.
Definition: peer.h:465
Peer(BinStream &bin, int *error=0)
Construct a Peer from a BinsStream.
Definition: peer.h:521
void *const any
Definition: peer.h:54
Type type() const
Obtain the type of this Peer.
Definition: peer.h:556
WFIO_DLL int wfio_peer_vioctl(wfio_peer_t *peer, int cmd, int *error, va_list va)
Perform device-dependent I/O on this peer using a va_list.
int dcntl(int *error, DCntl cmd,...)
Perform device-independent I/O on this Peer, capturing any errors in error.
Definition: peer.h:806
int write(char c, int *error=0)
Write a single byte to this peer.
Definition: peer.h:650
WFIO_DLL_FASTCALL wfio_peer_t wfio_peer_ioshare(wfio_peer_t *peer, wfio_ioshare_t ioshare, int *error)
Share this peer with the supplied ioshare mode.
size_t read(void *buf, size_t len, int *error)
Read bytes from this Peer.
Definition: peer.h:590
int ioctl(int cmd,...)
Perform device-dependent I/O on this Peer.
Definition: peer.h:855
WFIO_DLL_FASTCALL wfio_off_t wfio_peer_tell(wfio_peer_t *peer, int *error)
Determine the current stream position of this peer.
const wfio_peer_type_t type
The type of this peer.
Definition: peer.h:48
#define WFIO_ALWAYS_INLINE
Mark this symbol to be forced inline.
Definition: host.h:447
bool seek(off_t offset, wfio_whence_t whence, int *error=0)
Seek to a position in the stream.
Definition: peer.h:669
Definition: iotype.h:131
wfio_whence_t
The type corresponding to the point of reference in a seek operation.
Definition: iotype.h:60
wfio_peer_type_t
The type associated with a peer.
Definition: iotype.h:127
Peer(Device &device, int *error=0)
Construct a peer from a Device.
Definition: peer.h:494
size_t read(void *buf, size_t sz, size_t elem, int *error=0)
Read elements from this Peer.
Definition: peer.h:573
WFIO_ALWAYS_INLINE size_t wfio_peer_writebV(wfio_peer_t *peer, const void *buf, size_t len, int *error)
Write bytes to this peer.
Definition: peer.h:226
An IO sharing mode.
Definition: iotype.h:253
struct wfio_binstream_t *const bin
Definition: peer.h:58
WFIO_DLL int wfio_peer_acquire(wfio_peer_t *peer, int *error)
Share this peer.
Type
A type specification for a peer.
Definition: peer.h:407
WFIO_DLL size_t wfio_peer_setbufsz(wfio_peer_t *peer, size_t sz, int *error)
Set the buffer size.
WFIO_DLL int wfio_peer_seek(wfio_peer_t *peer, wfio_off_t offset, wfio_whence_t whence, int *error)
Seek to a position in the stream.
WFIO_ALWAYS_INLINE int wfio_peer_fromBinstream(wfio_peer_t *peer, struct wfio_binstream_t *bin, int *error)
Initialize a peer using a binstream.
Definition: peer.h:113
Peer(CType *ctype, IOShare share, int *error=0)
Construct a peer from a CType pointer using the specified IO sharing mode.
Definition: peer.h:481
wfio_dcntl_t
A command to be supplied to a device or similar stream.
Definition: iotype.h:175
Peer(CType &ctype, IOShare share, int *error=0)
Construct a peer from a CType using the specified IO sharing mode.
Definition: peer.h:452
Definition: iotype.h:133
A C++ wrapper for a wfio_device_t.
Definition: device.h:783
size_t bufsz(size_t sz, int *error=0)
Set the buffer size for this Peer.
Definition: peer.h:740
int vioctlEx(int *error, int cmd, va_list va)
Perform device-dependent I/O on this peer using a va_list, capturing any errors in error...
Definition: peer.h:840
union wfio_peer_t::@20 impl
The implementaton for this peer.
Peer(BinStream &bin, IOShare share, int *error=0)
Construct a Peer from a BinsStream using the specified IO sharing mode.
Definition: peer.h:535
A stream intended for binary output.
Definition: bin.h:54
struct wfio_device_t *const device
Definition: peer.h:56
A constant indicating no position.
Definition: iotype.h:226
Definition: peer.h:396
int dcntl(DCntl cmd,...)
Perform device-independent I/O on this Peer.
Definition: peer.h:787
int ioctlExt(int *error, int cmd,...)
Perform device-dependent I/O on this Peer, capturing any errors in error.
Definition: peer.h:874
size_t write(const void *buf, size_t sz, size_t elem, int *error=0)
Write elements to this Peer.
Definition: peer.h:620
#define WFIO_C_END
Definition: host.h:485
A simple, byte-oriented channel.
Definition: device.h:164
int wfio_ioshare_t
An I/O sharing mode.
Definition: iotype.h:167
WFIO_DLL size_t wfio_peer_read(wfio_peer_t *peer, void *buf, size_t sz, size_t elem, int *error)
Read elements from this peer.
An IOShare mode indicating that the stream is acquired.
Definition: iotype.h:148
WFIO_ALWAYS_INLINE int wfio_peer_writeb(wfio_peer_t *peer, char c, int *error)
Write a single byte to this peer.
Definition: peer.h:240
WFIO_DLL_FASTCALL size_t wfio_peer_getbufsz(wfio_peer_t *peer, int *error)
Obtain the buffer size.
WFIO_DLL int wfio_peer_fromBinstreamEx(wfio_peer_t *peer, struct wfio_binstream_t *bin, int ioshare, int *error)
Initialize a peer using a binstream with the specified ioshare mode.
WFIO_ALWAYS_INLINE int wfio_peer_ioctl(wfio_peer_t *peer, int *error, int cmd,...)
Perform device-dependent I/O on this peer.
Definition: peer.h:297
::wfio_peer_t CType
The C type from which a Peer is derived.
Definition: peer.h:402
WFIO_DLL int wfio_peer_vdcntl(wfio_peer_t *peer, wfio_dcntl_t cmd, int *error, va_list va)
Perform device-independent I/O on this peer using a va_list.
off_t tell(int *error=0)
Determine the current stream position of this peer.
Definition: peer.h:694
int vioctl(int cmd, va_list va)
Perform device-dependent I/O on this peer using a va_list.
Definition: peer.h:824
Peer()
Construct the default Peer.
Definition: peer.h:430
WFIO_ALWAYS_INLINE int wfio_peer_fromDevice(wfio_peer_t *peer, struct wfio_device_t *device, int *error)
Initialize a peer using a device.
Definition: peer.h:84
WFIO_C_BEGIN struct wfio_peer_t wfio_peer_t
A generic stream encapsulation.
Byte-oriented devices.
WFIO_DLL void wfio_peer_close(wfio_peer_t *peer)
Release this peer.
bool canSeek() const
Determine whether or not this Peer can seek.
Definition: peer.h:680
int read(int *error=0)
Read a single byte from this peer.
Definition: peer.h:601
size_t bufsz(int *error=0) const
Obtain the buffer size for this Peer.
Definition: peer.h:722
::wfio_off_t off_t
An integer corresponding to a device offset.
Definition: iotype.h:234
WFIO_ALWAYS_INLINE size_t wfio_peer_readbV(wfio_peer_t *peer, void *buf, size_t len, int *error)
Read bytes from this peer.
Definition: peer.h:181
WFIO_DLL_FASTCALL int wfio_peer_flush(wfio_peer_t *peer, int *error)
Determine the current stream position of this peer.
WFIO_DLL int wfio_peer_fromDeviceEx(wfio_peer_t *peer, struct wfio_device_t *device, int ioshare, int *error)
Initialize a peer using a device with the specified ioshare mode.
size_t write(const void *buf, size_t len, int *error=0)
Write bytes to this peer.
Definition: peer.h:638
WFIO_ALWAYS_INLINE int wfio_peer_dcntl(wfio_peer_t *peer, wfio_dcntl_t cmd, int *error,...)
Perform device-independent I/O on this peer.
Definition: peer.h:266
WFIO_DLL size_t wfio_peer_write(wfio_peer_t *peer, const void *buf, size_t sz, size_t elem, int *error)
Write elements to this peer.
int vdcntlEx(int *error, DCntl cmd, va_list va)
Perform device-independent I/O on this peer using a va_list, capturing any errors in error...
Definition: peer.h:772
Shared type definitions.