xtd 0.2.0
Loading...
Searching...
No Matches
binary_writer.hpp
Go to the documentation of this file.
1
4#pragma once
5#define __XTD_STD_INTERNAL__
7#undef __XTD_STD_INTERNAL__
8#include "../array.hpp"
9#include "../fixed_array.hpp"
10#include "../object.hpp"
11#include "../optional.hpp"
12#include "../read_only_span"
13#include "../string.hpp"
14#include <fstream>
15#include <array>
16#include <vector>
17
19namespace xtd {
21 namespace io {
48 public:
50
56 explicit binary_writer(const xtd::string& path);
59 explicit binary_writer(std::ostream& stream);
62 binary_writer(const binary_writer&) = delete;
63 auto operator =(const binary_writer&) -> binary_writer& = delete;
66
68
73 [[nodiscard]] auto base_stream() const -> std::optional<xtd::ref<std::ostream>>;
75
77
80 virtual auto close() -> void;
81
84 virtual auto flush() -> void;
85
92 virtual auto seek(xtd::size offset, std::ios::seekdir origin) -> xtd::size;
93
96 [[nodiscard]] virtual auto tell() -> std::streampos;
97
102 virtual auto write(bool value) -> void;
103
108 virtual auto write(xtd::byte value) -> void;
109
114 virtual auto write(char value) -> void;
115
120 template<xtd::size size>
121 auto write(const std::array<xtd::byte, size>& buffer) -> void {
122 for (auto b : buffer)
123 write(b);
124 }
125
130 template<xtd::size size>
131 auto write(const std::array<char, size>& buffer) -> void {
132 for (auto c : buffer)
133 write(c);
134 }
135
140 virtual auto write(const xtd::read_only_span<xtd::byte>& buffer) -> void;
141
149 virtual auto write(const xtd::array<xtd::byte>& buffer, xtd::size index, xtd::size count) -> void;
150
155 virtual auto write(const xtd::read_only_span<char>& buffer) -> void;
156
164 virtual auto write(const xtd::array<char>& buffer, xtd::size index, xtd::size count) -> void;
165
170 virtual auto write(double value) -> void;
171
176 virtual auto write(int16 value) -> void;
177
182 virtual auto write(xtd::int32 value) -> void;
183
188 virtual auto write(xtd::int64 value) -> void;
189
194 virtual auto write(xtd::sbyte value) -> void;
195
200 virtual auto write(float value) -> void;
201
206 virtual auto write(const xtd::string& value) -> void;
208 virtual auto write(const std::string& value) -> void;
209 virtual auto write(const std::u8string& value) -> void;
210 virtual auto write(const std::u16string& value) -> void;
211 virtual auto write(const std::u32string& value) -> void;
212 virtual auto write(const std::wstring& value) -> void;
213 virtual auto write(const char* value) -> void;
214 virtual auto write(const xtd::char8* value) -> void;
215 virtual auto write(const xtd::char16* value) -> void;
216 virtual auto write(const xtd::char32* value) -> void;
217 virtual auto write(const xtd::wchar* value) -> void;
219
224 virtual auto write(xtd::uint16 value) -> void;
225
230 virtual auto write(xtd::uint32 value) -> void;
231
236 virtual auto write(xtd::uint64 value) -> void;
237
239 virtual auto write(xtd::slong value) -> void;
240 virtual auto write(xtd::ulong value) -> void;
242
244
246 auto operator <<(bool value) -> binary_writer& {write(value); return *this;}
247 auto operator <<(byte value) -> binary_writer& {write(value); return *this;}
248 auto operator <<(char value) -> binary_writer& {write(value); return *this;}
249 template<xtd::size size>
250 auto operator <<(const xtd::fixed_array<xtd::byte, size>& value) -> binary_writer& {write(value); return *this;}
251 template<xtd::size size>
252 auto operator <<(const xtd::fixed_array<char, size>& value) -> binary_writer& {write(value); return *this;}
253 auto operator <<(const xtd::array<xtd::byte>& value) -> binary_writer& {write(value, 0, value.length()); return *this;}
254 auto operator <<(const xtd::array<char>& value) -> binary_writer& {write(value, 0, value.length()); return *this;}
255 auto operator <<(double value) -> binary_writer& {write(value); return *this;}
256 auto operator <<(int16 value) -> binary_writer& {write(value); return *this;}
257 auto operator <<(int32 value) -> binary_writer& {write(value); return *this;}
258 auto operator <<(int64 value) -> binary_writer& {write(value); return *this;}
259 auto operator <<(sbyte value) -> binary_writer& {write(value); return *this;}
260 auto operator <<(float value) -> binary_writer& {write(value); return *this;}
261 auto operator <<(const string& value) -> binary_writer& {write(value); return *this;}
262 auto operator <<(const char* value) -> binary_writer& {write(value); return *this;}
263 auto operator <<(const char8* value) -> binary_writer& {write(value); return *this;}
264 auto operator <<(const char16* value) -> binary_writer& {write(value); return *this;}
265 auto operator <<(const char32* value) -> binary_writer& {write(value); return *this;}
266 auto operator <<(const wchar* value) -> binary_writer& {write(value); return *this;}
267 auto operator <<(uint16 value) -> binary_writer& {write(value); return *this;}
268 auto operator <<(uint32 value) -> binary_writer& {write(value); return *this;}
269 auto operator <<(uint64 value) -> binary_writer& {write(value); return *this;}
271
272 private:
273 auto write_7bit_encoded_int(int32 value) -> void;
274 std::ostream* stream_ = nullptr;
275 bool delete_when_destroy_ = false;
276 };
277 }
278}
Contains __xtd_std_version definitions.
Contains xtd::array class.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
Represents a fixed array class.
Definition fixed_array.hpp:23
Writes primitive types in binary to a stream and supports writing strings.
Definition binary_writer.hpp:47
virtual auto write(const xtd::read_only_span< xtd::byte > &buffer) -> void
Writes a byte array to the underlying stream.
virtual auto close() -> void
Closes the xtd::io::binary_writer object and the underlying stream, and releases any system resources...
virtual auto tell() -> std::streampos
Tell the current seek position of the readers stream;.
virtual auto write(double value) -> void
Writes an eight-byte floating-point value to the current stream and advances the stream position by e...
virtual auto write(const xtd::array< char > &buffer, xtd::size index, xtd::size count) -> void
Writes a region of a character array to the current stream.
virtual auto write(float value) -> void
Writes an four-byte floating-point value to the current stream and advances the stream position by fo...
binary_writer(const xtd::string &path)
Initializes a new instance of the binary_writer class for the specified file name.
virtual auto write(xtd::sbyte value) -> void
Writes an signed byte to the current stream and advances the stream position by one byte.
virtual auto seek(xtd::size offset, std::ios::seekdir origin) -> xtd::size
Sets the position within the current stream.
virtual auto write(xtd::uint32 value) -> void
Writes a four-byte unsigned integer to the current stream and advances the stream position by four by...
virtual auto write(bool value) -> void
Writes a one-byte boolean value to the current stream, with 0 representing false and 1 representing t...
virtual auto write(const xtd::array< xtd::byte > &buffer, xtd::size index, xtd::size count) -> void
Writes a region of a byte array to the current stream.
virtual auto write(xtd::uint64 value) -> void
Writes a eight-byte unsigned integer to the current stream and advances the stream position by eight ...
virtual auto write(const xtd::string &value) -> void
Writes a length-prefixed string to this stream, and advances the current position of the stream and t...
virtual auto write(xtd::int32 value) -> void
Writes a four-byte signed integer to the current stream and advances the stream position by four byte...
auto base_stream() const -> std::optional< xtd::ref< std::ostream > >
Returns the underlying stream.
virtual auto write(const xtd::read_only_span< char > &buffer) -> void
Writes a character array to the underlying stream.
auto write(const std::array< char, size > &buffer) -> void
Writes a character array to the underlying stream.
Definition binary_writer.hpp:131
binary_writer(std::ostream &stream)
Initializes a new instance of the binary_writer class for the specified stream.
virtual auto flush() -> void
Clears all buffers for the current writer and causes any buffered data to be written to the underlyin...
virtual auto write(int16 value) -> void
Writes a two-byte signed integer to the current stream and advances the stream position by two bytes.
virtual auto write(xtd::int64 value) -> void
Writes a eight-byte signed integer to the current stream and advances the stream position by eight by...
virtual auto write(xtd::uint16 value) -> void
Writes a two-byte unsigned integer to the current stream and advances the stream position by two byte...
Performs operations on std::basic_string instances that contain file or directory path information....
Definition path.hpp:37
Provides a generic view of a sequence of bytes. This is an abstract class.
Definition stream.hpp:40
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:45
Contains xtd::fixed_array class.
#define core_export_
Define shared library export.
Definition core_export.hpp:13
xtd::basic_string< char > string
Represents text as a sequence of UTF-8 code units.
Definition __string_definitions.hpp:43
std::int64_t int64
Represents a 64-bit signed integer.
Definition int64.hpp:23
std::uint32_t uint32
Represents a 32-bit unsigned integer.
Definition uint32.hpp:23
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
char8_t char8
Represents a 8-bit unicode character.
Definition char8.hpp:26
__slong__ slong
Represents a 32-bit or 64-bit signed integer.
Definition slong.hpp:27
std::int8_t sbyte
Represents a 8-bit signed integer.
Definition sbyte.hpp:23
wchar_t wchar
Represents a wide character.
Definition wchar.hpp:24
__ulong__ ulong
Represents a 32-bit or 64-bit unsigned integer.
Definition ulong.hpp:27
std::uint16_t uint16
Represents a 16-bit unsigned integer.
Definition uint16.hpp:23
char16_t char16
Represents a 16-bit unicode character.
Definition char16.hpp:26
std::int32_t int32
Represents a 32-bit signed integer.
Definition int32.hpp:23
xtd::reference_wrapper_object< type_t > ref
The xtd::ref object is a reference wrapper.
Definition ref.hpp:25
std::int16_t int16
Represents a 16-bit signed integer.
Definition int16.hpp:23
std::optional< type_t > optional
Represents the optional alias on std::optional.
Definition optional.hpp:26
std::uint64_t uint64
Represents a 64-bit unsigned integer.
Definition uint64.hpp:23
char32_t char32
Represents a 32-bit unicode character.
Definition char32.hpp:23
@ write
Write access to the file. Data can be written to the file. Combine with Read for read/write access.
Definition file_access.hpp:19
@ ios
The operating system is iOS Apple.
Definition platform_id.hpp:36
@ c
The C key.
Definition console_key.hpp:92
@ b
The B key.
Definition console_key.hpp:90
The xtd::io namespace contains types that allow reading and writing to files and data streams,...
Definition binary_reader.hpp:17
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
constexpr read_only_span(const element_type(&array)[len]) noexcept
Creates an xtd::read_only_span with specified native array.
Definition read_only_span.hpp:112
Contains xtd::object class.
Contains xtd::optional type.
Contains xtd::string alias.