iodevice.h
1 /*
2 ** UICore
3 ** Copyright (c) 1997-2015 The UICore Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries UICore may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 */
28 
29 #pragma once
30 
31 #include "../System/exception.h"
32 #include "endian.h"
33 #include <memory>
34 
35 namespace uicore
36 {
37  class IODevice
38  {
39  public:
40  virtual ~IODevice() { }
41 
42  virtual long long size() const = 0;
43  long long position() const { return const_cast<IODevice*>(this)->seek_from_current(0); }
44 
45  virtual long long seek(long long position) = 0;
46  virtual long long seek_from_current(long long offset) = 0;
47  virtual long long seek_from_end(long long offset) = 0;
48 
49  virtual int try_read(void *data, int size) = 0;
50  void read(void *data, int size) { int bytes = try_read(data, size); if (bytes != size) throw Exception("Could not read all bytes"); }
51  virtual void write(const void *data, int size) = 0;
52 
53  virtual void close() { }
54 
55  bool is_big_endian_mode() const { return swap_bytes; }
56  void set_big_endian_mode(bool value = true) { swap_bytes = value; }
57 
58  uint8_t read_uint8() { return read_type<uint8_t>(); }
59  int8_t read_int8() { return read_type<int8_t>(); }
60  uint16_t read_uint16() { return read_type<uint16_t>(); }
61  int16_t read_int16() { return read_type<int16_t>(); }
62  uint32_t read_uint32() { return read_type<uint32_t>(); }
63  int32_t read_int32() { return read_type<int32_t>(); }
64  float read_float() { return read_type<float>(); }
65  double read_double() { return read_type<double>(); }
66 
67  void write_uint8(uint8_t v) { write_type(v); }
68  void write_int8(int8_t v) { write_type(v); }
69  void write_uint16(uint16_t v) { write_type(v); }
70  void write_int16(int16_t v) { write_type(v); }
71  void write_uint32(uint32_t v) { write_type(v); }
72  void write_int32(int32_t v) { write_type(v); }
73  void write_uint64(uint64_t v) { write_type(v); }
74  void write_int64(int64_t v) { write_type(v); }
75  void write_float(float v) { write_type(v); }
76  void write_double(double v) { write_type(v); }
77 
78  template<typename T> T read_type() { T v; read(&v, sizeof(T)); return swap_bytes ? Endian::swap(v) : v; }
79  template<typename T> void write_type(T v) { if (swap_bytes) v = Endian::swap(v); write(&v, sizeof(T)); }
80 
81  private:
82  bool swap_bytes = false;
83  };
84 
85  typedef std::shared_ptr<IODevice> IODevicePtr;
86 }
int16_t read_int16()
Definition: iodevice.h:61
virtual void close()
Definition: iodevice.h:53
void write_double(double v)
Definition: iodevice.h:76
T read_type()
Definition: iodevice.h:78
Definition: iodevice.h:37
static uint8_t swap(uint8_t val)
Definition: endian.h:103
virtual long long seek_from_end(long long offset)=0
float read_float()
Definition: iodevice.h:64
void write_type(T v)
Definition: iodevice.h:79
long long position() const
Definition: iodevice.h:43
uint16_t read_uint16()
Definition: iodevice.h:60
double read_double()
Definition: iodevice.h:65
void write_float(float v)
Definition: iodevice.h:75
virtual long long size() const =0
virtual long long seek_from_current(long long offset)=0
std::shared_ptr< IODevice > IODevicePtr
Definition: iodevice.h:85
void write_uint32(uint32_t v)
Definition: iodevice.h:71
void write_int32(int32_t v)
Definition: iodevice.h:72
void write_int16(int16_t v)
Definition: iodevice.h:70
void write_int64(int64_t v)
Definition: iodevice.h:74
void write_uint8(uint8_t v)
Definition: iodevice.h:67
void write_int8(int8_t v)
Definition: iodevice.h:68
Top-level exception class.
Definition: exception.h:38
void write_uint16(uint16_t v)
Definition: iodevice.h:69
uint8_t read_uint8()
Definition: iodevice.h:58
int32_t read_int32()
Definition: iodevice.h:63
void set_big_endian_mode(bool value=true)
Definition: iodevice.h:56
uint32_t read_uint32()
Definition: iodevice.h:62
virtual ~IODevice()
Definition: iodevice.h:40
bool is_big_endian_mode() const
Definition: iodevice.h:55
virtual void write(const void *data, int size)=0
void read(void *data, int size)
Definition: iodevice.h:50
virtual int try_read(void *data, int size)=0
virtual long long seek(long long position)=0
int8_t read_int8()
Definition: iodevice.h:59
void write_uint64(uint64_t v)
Definition: iodevice.h:73
Definition: Application/application.h:35