xtd 0.2.0
binary_writer.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 <fstream>
9 #include <array>
10 #include <vector>
11 
13 namespace xtd {
15  namespace io {
37  class core_export_ binary_writer : public xtd::object {
38  public:
40 
46  explicit binary_writer(const xtd::ustring& path);
49  explicit binary_writer(std::ostream& stream);
52  binary_writer(const binary_writer&) = delete;
53  binary_writer& operator =(const binary_writer&) = delete;
54  ~binary_writer();
56 
58 
63  std::optional<std::reference_wrapper<std::ostream>> base_stream() const;
65 
67 
70  virtual void close();
71 
74  virtual void flush();
75 
82  virtual size_t seek(size_t offset, std::ios::seekdir origin);
83 
88  virtual void write(bool value);
89 
94  virtual void write(xtd::byte value);
95 
100  virtual void write(char value);
101 
106  template<size_t size>
107  void write(const std::array<xtd::byte, size>& buffer) {
108  for (auto b : buffer)
109  write(b);
110  }
111 
116  template<size_t size>
117  void write(const std::array<char, size>& buffer) {
118  for (auto c : buffer)
119  write(c);
120  }
121 
126  virtual void write(const std::vector<xtd::byte>& buffer);
127 
135  virtual void write(const std::vector<xtd::byte>& buffer, size_t index, size_t count);
136 
141  virtual void write(const std::vector<char>& buffer);
142 
150  virtual void write(const std::vector<char>& buffer, size_t index, size_t count);
151 
156  virtual void write(double value);
157 
162  virtual void write(int16 value);
163 
168  virtual void write(int32 value);
169 
174  virtual void write(int64 value);
175 
180  virtual void write(sbyte value);
181 
186  virtual void write(float value);
187 
192  virtual void write(const ustring& value);
194  virtual void write(const std::string& value);
195 #if defined(__cpp_lib_char8_t)
196  virtual void write(const std::u8string& value);
197 #endif
198  virtual void write(const std::u16string& value);
199  virtual void write(const std::u32string& value);
200  virtual void write(const std::wstring& value);
201  virtual void write(const char* value);
202 #if defined(__cpp_lib_char8_t)
203  virtual void write(const char8* value);
204 #endif
205  virtual void write(const char16* value);
206  virtual void write(const char32* value);
207  virtual void write(const wchar* value);
209 
214  virtual void write(uint16 value);
215 
220  virtual void write(uint32 value);
221 
226  virtual void write(uint64 value);
228 
230  binary_writer& operator <<(bool value) {write(value); return *this;}
231  binary_writer& operator <<(byte value) {write(value); return *this;}
232  binary_writer& operator <<(char value) {write(value); return *this;}
233  template<size_t size>
234  binary_writer& operator <<(const std::array<xtd::byte, size>& value) {write(value); return *this;}
235  template<size_t size>
236  binary_writer& operator <<(const std::array<char, size>& value) {write(value); return *this;}
237  binary_writer& operator <<(const std::vector<xtd::byte>& value) {write(value); return *this;}
238  binary_writer& operator <<(const std::vector<char>& value) {write(value); return *this;}
239  binary_writer& operator <<(double value) {write(value); return *this;}
240  binary_writer& operator <<(int16 value) {write(value); return *this;}
241  binary_writer& operator <<(int32 value) {write(value); return *this;}
242  binary_writer& operator <<(int64 value) {write(value); return *this;}
243  binary_writer& operator <<(sbyte value) {write(value); return *this;}
244  binary_writer& operator <<(float value) {write(value); return *this;}
245  binary_writer& operator <<(const ustring& value) {write(value); return *this;}
246  binary_writer& operator <<(const std::string& value) {write(value); return *this;}
247 #if defined(__cpp_lib_char8_t)
248  binary_writer& operator <<(const std::u8string& value) {write(value); return *this;}
249 #endif
250  binary_writer& operator <<(const std::u16string& value) {write(value); return *this;}
251  binary_writer& operator <<(const std::u32string& value) {write(value); return *this;}
252  binary_writer& operator <<(const std::wstring& value) {write(value); return *this;}
253  binary_writer& operator <<(const char* value) {write(value); return *this;}
254 #if defined(__cpp_lib_char8_t)
255  binary_writer& operator <<(const char8* value) {write(value); return *this;}
256 #endif
257  binary_writer& operator <<(const char16* value) {write(value); return *this;}
258  binary_writer& operator <<(const char32* value) {write(value); return *this;}
259  binary_writer& operator <<(const wchar* value) {write(value); return *this;}
260  binary_writer& operator <<(uint16 value) {write(value); return *this;}
261  binary_writer& operator <<(uint32 value) {write(value); return *this;}
262  binary_writer& operator <<(uint64 value) {write(value); return *this;}
264 
265  private:
266  std::ostream* stream_ = nullptr;
267  bool delete_when_destroy_ = false;
268  };
269  }
270 }
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
void write(const std::array< xtd::byte, size > &buffer)
Writes a byte array to the underlying stream.
Definition: binary_writer.h:107
void write(const std::array< char, size > &buffer)
Writes a character array to the underlying stream.
Definition: binary_writer.h:117
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
char32_t char32
Represents a 32-bit unicode character.
Definition: types.h:85
uint_least64_t uint64
Represents a 64-bit unsigned integer.
Definition: types.h:250
char8_t char8
Represents a 8-bit unicode character.
Definition: types.h:62
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
char16_t char16
Represents a 16-bit unicode character.
Definition: types.h:74
Write access to the file. Data can be written to the file. Combine with Read for read/write access...
wchar_t wchar
Represents a wide character.
Definition: types.h:284
Writes primitive types in binary to a stream and supports writing strings.
Definition: binary_writer.h:37