xtd 0.2.0
Loading...
Searching...
No Matches
binary_writer.h
Go to the documentation of this file.
1
4#pragma once
5#include "../optional.h"
6#include "../object.h"
7#include "../ustring.h"
8#include <fstream>
9#include <array>
10#include <vector>
11
13namespace xtd {
15 namespace io {
38 public:
40
49 explicit binary_writer(std::ostream& stream);
52 binary_writer(const binary_writer&) = delete;
53 binary_writer& operator =(const binary_writer&) = delete;
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}
Writes primitive types in binary to a stream and supports writing strings.
Definition binary_writer.h:37
virtual void flush()
Clears all buffers for the current writer and causes any buffered data to be written to the underlyin...
virtual void write(int64 value)
Writes a eight-byte signed integer to the current stream and advances the stream position by eight by...
virtual void write(const std::vector< char > &buffer)
Writes a character array to the underlying stream.
void write(const std::array< xtd::byte, size > &buffer)
Writes a byte array to the underlying stream.
Definition binary_writer.h:107
virtual void write(const std::vector< xtd::byte > &buffer)
Writes a byte array to the underlying stream.
virtual void write(int16 value)
Writes a two-byte signed integer to the current stream and advances the stream position by two bytes.
virtual void write(double value)
Writes an eight-byte floating-point value to the current stream and advances the stream position by e...
virtual void write(const std::vector< char > &buffer, size_t index, size_t count)
Writes a region of a character array to the current stream.
void write(const std::array< char, size > &buffer)
Writes a character array to the underlying stream.
Definition binary_writer.h:117
virtual size_t seek(size_t offset, std::ios::seekdir origin)
Sets the position within the current stream.
virtual void write(uint64 value)
Writes a eight-byte unsigned integer to the current stream and advances the stream position by eight ...
virtual void write(uint32 value)
Writes a four-byte unsigned integer to the current stream and advances the stream position by four by...
virtual void write(char value)
Writes a character to the current stream and advances the current position by one byte.
std::optional< std::reference_wrapper< std::ostream > > base_stream() const
Returns the underlying stream.
virtual void close()
Closes the xtd::io::binary_writer object and the underlying stream, and releases any system resources...
virtual void write(const std::vector< xtd::byte > &buffer, size_t index, size_t count)
Writes a region of a byte array to the current stream.
virtual void write(const ustring &value)
Writes a length-prefixed string to this stream, and advances the current position of the stream and t...
virtual void write(bool value)
Writes a one-byte boolean value to the current stream, with 0 representing false and 1 representing t...
virtual void write(xtd::byte value)
Writes an unsigned byte to the current stream and advances the stream position by one byte.
virtual void write(uint16 value)
Writes a two-byte unsigned integer to the current stream and advances the stream position by two byte...
binary_writer(const xtd::ustring &path)
Initializes a new instance of the binary_writer class for the specified file name.
binary_writer(std::ostream &stream)
Initializes a new instance of the binary_writer class for the specified stream.
virtual void write(int32 value)
Writes a four-byte signed integer to the current stream and advances the stream position by four byte...
virtual void write(sbyte value)
Writes an signed byte to the current stream and advances the stream position by one byte.
virtual void write(float value)
Writes an four-byte floating-point value to the current stream and advances the stream position by fo...
Performs operations on std::basic_string instances that contain file or directory path information....
Definition path.h:34
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.h:32
Represents text as a sequence of UTF-8 code units.
Definition ustring.h:47
#define core_export_
Define shared library export.
Definition core_export.h:13
char8_t char8
Represents a 8-bit unicode character.
Definition types.h:64
wchar_t wchar
Represents a wide character.
Definition types.h:286
int_least16_t int16
Represents a 16-bit signed integer.
Definition types.h:120
char16_t char16
Represents a 16-bit unicode character.
Definition types.h:76
int_least8_t sbyte
Represents a 8-bit signed integer.
Definition types.h:175
int_least64_t int64
Represents a 64-bit signed integer.
Definition types.h:142
uint_least16_t uint16
Represents a 16-bit unsigned integer.
Definition types.h:230
int_least32_t int32
Represents a 32-bit signed integer.
Definition types.h:131
char32_t char32
Represents a 32-bit unicode character.
Definition types.h:87
uint_least64_t uint64
Represents a 64-bit unsigned integer.
Definition types.h:252
uint_least32_t uint32
Represents a 32-bit unsigned integer.
Definition types.h:241
uint_least8_t byte
Represents a 8-bit unsigned integer.
Definition types.h:41
@ write
Write access to the file. Data can be written to the file. Combine with Read for read/write access.
@ c
The C key.
@ b
The B key.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10