xtd - Reference Guide  0.2.0
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
binary_writer.h
Go to the documentation of this file.
1 #pragma once
5 
6 #include <fstream>
7 #include <optional>
8 #include <array>
9 #include <vector>
10 #include "../object.h"
11 #include "../ustring.h"
12 
14 namespace xtd {
16  namespace io {
36  class core_export_ binary_writer : public xtd::object {
37  public:
39 
45  explicit binary_writer(const xtd::ustring& path);
48  explicit binary_writer(std::ostream& stream);
51  binary_writer(const binary_writer&) = delete;
52  binary_writer& operator=(const binary_writer&) = delete;
53  ~binary_writer();
55 
57 
62  std::optional<std::reference_wrapper<std::ostream>> base_stream() const;
64 
66 
69  virtual void close();
70 
73  virtual void flush();
74 
81  virtual size_t seek(size_t offset, std::ios::seekdir origin);
82 
87  virtual void write(bool value);
88 
93  virtual void write(byte_t value);
94 
99  virtual void write(char value);
100 
105  template<size_t size>
106  void write(const std::array<byte_t, size>& buffer) {
107  for (auto b : buffer)
108  write(b);
109  }
110 
115  template<size_t size>
116  void write(const std::array<char, size>& buffer) {
117  for (auto c : buffer)
118  write(c);
119  }
120 
125  virtual void write(const std::vector<byte_t>& buffer);
126 
134  virtual void write(const std::vector<byte_t>& buffer, size_t index, size_t count);
135 
140  virtual void write(const std::vector<char>& buffer);
141 
149  virtual void write(const std::vector<char>& buffer, size_t index, size_t count);
150 
155  virtual void write(double value);
156 
161  virtual void write(int16_t value);
162 
167  virtual void write(int32_t value);
168 
173  virtual void write(int64_t value);
174 
179  virtual void write(sbyte_t value);
180 
185  virtual void write(float value);
186 
191  virtual void write(const ustring& value);
193  virtual void write(const std::string& value);
194  virtual void write(const std::u8string& value);
195  virtual void write(const std::u16string& value);
196  virtual void write(const std::u32string& value);
197  virtual void write(const std::wstring& value);
198  virtual void write(const char* value);
199  virtual void write(const char8_t* value);
200  virtual void write(const char16_t* value);
201  virtual void write(const char32_t* value);
202  virtual void write(const wchar_t* value);
204 
209  virtual void write(uint16_t value);
210 
215  virtual void write(uint32_t value);
216 
221  virtual void write(uint64_t value);
223 
225  binary_writer& operator<<(bool value) {write(value); return *this;}
226  binary_writer& operator<<(byte value) {write(value); return *this;}
227  binary_writer& operator<<(char value) {write(value); return *this;}
228  template<size_t size>
229  binary_writer& operator<<(const std::array<byte_t, size>& value) {write(value); return *this;}
230  template<size_t size>
231  binary_writer& operator<<(const std::array<char, size>& value) {write(value); return *this;}
232  binary_writer& operator<<(const std::vector<byte_t>& value) {write(value); return *this;}
233  binary_writer& operator<<(const std::vector<char>& value) {write(value); return *this;}
234  binary_writer& operator<<(double value) {write(value); return *this;}
235  binary_writer& operator<<(int16_t value) {write(value); return *this;}
236  binary_writer& operator<<(int32_t value) {write(value); return *this;}
237  binary_writer& operator<<(int64_t value) {write(value); return *this;}
238  binary_writer& operator<<(sbyte value) {write(value); return *this;}
239  binary_writer& operator<<(float value) {write(value); return *this;}
240  binary_writer& operator<<(const ustring& value) {write(value); return *this;}
241  binary_writer& operator<<(const std::string& value) {write(value); return *this;}
242  binary_writer& operator<<(const std::u8string& value) {write(value); return *this;}
243  binary_writer& operator<<(const std::u16string& value) {write(value); return *this;}
244  binary_writer& operator<<(const std::u32string& value) {write(value); return *this;}
245  binary_writer& operator<<(const std::wstring& value) {write(value); return *this;}
246  binary_writer& operator<<(const char* value) {write(value); return *this;}
247  binary_writer& operator<<(const char8_t* value) {write(value); return *this;}
248  binary_writer& operator<<(const char16_t* value) {write(value); return *this;}
249  binary_writer& operator<<(const char32_t* value) {write(value); return *this;}
250  binary_writer& operator<<(const wchar_t* value) {write(value); return *this;}
251  binary_writer& operator<<(uint16_t value) {write(value); return *this;}
252  binary_writer& operator<<(uint32_t value) {write(value); return *this;}
253  binary_writer& operator<<(uint64_t value) {write(value); return *this;}
255 
256  private:
257  std::ostream* stream_ = nullptr;
258  bool delete_when_destroy_ = false;
259  };
260  }
261 }
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:50
void write(const std::array< char, size > &buffer)
Writes a character array to the underlying stream.
Definition: binary_writer.h:116
Performs operations on std::basic_string instances that contain file or directory path information...
Definition: path.h:32
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes...
Definition: object.h:30
void write(const std::array< byte_t, size > &buffer)
Writes a byte array to the underlying stream.
Definition: binary_writer.h:106
Write access to the file. Data can be written to the file. Combine with Read for read/write access...
uint8_t byte
Represents a 8-bit unsigned integer.
Definition: types.h:81
int8_t sbyte
Represents a 8-bit signed integer.
Definition: types.h:163
Writes primitive types in binary to a stream and supports writing strings.
Definition: binary_writer.h:36