xtd 0.2.0
binary_reader.h
Go to the documentation of this file.
1 #pragma once
5 #include "../optional.h"
6 #include "../object.h"
7 #include "../ustring.h"
8 #include <iostream>
9 #include <fstream>
10 #include <vector>
11 #include <stack>
12 
14 namespace xtd {
16  namespace io {
38  class core_export_ binary_reader : public xtd::object {
39  public:
41 
47  explicit binary_reader(const xtd::ustring& path);
54  explicit binary_reader(std::istream& stream);
57  binary_reader(const binary_reader&) = delete;
58  binary_reader& operator =(const binary_reader&) = delete;
59  ~binary_reader();
61 
63 
69  std::optional<std::reference_wrapper<std::istream>> base_stream() const;
70 
73  bool end_of_stream() const;
75 
77 
80  void close();
81 
86  int32 peek_char() const;
87 
89  std::streampos pop();
90 
93  void push(std::streampos pos = 0);
94 
100  virtual int32 read();
101 
111  virtual size_t read(std::vector<xtd::byte>& buffer, size_t index, size_t count);
112 
121  virtual size_t read(std::vector<char>& buffer, size_t index, size_t count);
122 
129  virtual bool read_boolean();
130 
137  virtual xtd::byte read_byte();
138 
146  virtual std::vector<xtd::byte> read_bytes(size_t count);
147 
154  virtual char read_char();
155 
163  virtual std::vector<char> read_chars(size_t count);
164 
171  virtual double read_double();
172 
179  virtual int16 read_int16();
180 
187  virtual int32 read_int32();
188 
195  virtual int64 read_int64();
196 
203  virtual sbyte read_sbyte();
204 
211  virtual float read_single();
212 
219  virtual ustring read_string();
220 
227  virtual uint16 read_uint16();
228 
235  virtual uint32 read_uint32();
236 
243  virtual uint64 read_uint64();
244 
246  void rewind();
247 
251  void seekg(std::streamoff off, std::ios_base::seekdir dir = std::ios_base::cur);
252 
255  std::streampos tellg();
257 
259  binary_reader& operator >>(bool& value) {value = read_boolean(); return *this;}
260  binary_reader& operator >>(xtd::byte& value) {value = read_byte(); return *this;}
261  binary_reader& operator >>(char& value) {value = read_char(); return *this;}
262  binary_reader& operator >>(double& value) {value = read_double(); return *this;}
263  binary_reader& operator >>(int16& value) {value = read_int16(); return *this;}
264  binary_reader& operator >>(int32& value) {value = read_int32(); return *this;}
265  binary_reader& operator >>(int64& value) {value = read_int64(); return *this;}
266  binary_reader& operator >>(sbyte& value) {value = read_sbyte(); return *this;}
267  binary_reader& operator >>(float& value) {value = read_single(); return *this;}
268  binary_reader& operator >>(ustring& value) {value = read_string(); return *this;}
269  binary_reader& operator >>(uint16& value) {value = read_uint16(); return *this;}
270  binary_reader& operator >>(uint32& value) {value = read_uint32(); return *this;}
271  binary_reader& operator >>(uint64& value) {value = read_uint64(); return *this;}
273 
274  private:
275  std::istream* stream_ = nullptr;
276  std::stack<std::streampos> pos_stack_;
277  bool delete_when_destroy_ = false;
278  };
279  }
280 }
int_least8_t sbyte
Represents a 8-bit signed integer.
Definition: types.h:173
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
uint_least16_t uint16
Represents a 16-bit unsigned integer.
Definition: types.h:228
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:46
Performs operations on std::basic_string instances that contain file or directory path information...
Definition: path.h:34
int_least16_t int16
Represents a 16-bit signed integer.
Definition: types.h:118
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes...
Definition: object.h:32
int_least32_t int32
Represents a 32-bit signed integer.
Definition: types.h:129
Read access to the file. Data can be read from the file. Combine with Write for read/write access...
uint_least64_t uint64
Represents a 64-bit unsigned integer.
Definition: types.h:250
int_least64_t int64
Represents a 64-bit signed integer.
Definition: types.h:140
uint_least8_t byte
Represents a 8-bit unsigned integer.
Definition: types.h:39
uint_least32_t uint32
Represents a 32-bit unsigned integer.
Definition: types.h:239
Reads primitive data types as binary values in a specific encoding.
Definition: binary_reader.h:38