#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2021 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License #ifndef NUCLEX_STORAGE_BINARYBLOBWRITER_H #define NUCLEX_STORAGE_BINARYBLOBWRITER_H #include "Nuclex/Storage/Config.h" #include "Nuclex/Storage/Binary/BinaryWriter.h" #include namespace Nuclex { namespace Storage { // ------------------------------------------------------------------------------------------- // class Blob; // ------------------------------------------------------------------------------------------- // }} // namespace Nuclex::Storage namespace Nuclex { namespace Storage { namespace Binary { // ------------------------------------------------------------------------------------------- // /// Writes binary data into a blob /// /// /// Each BinaryBlobWriter maintains its own cursor, so when writing complex /// data structures, you should use the same writer throughout the process instead /// of creating creating writers on the fly, otherwise you'd start writing from /// the beginning of the blob over and over again. /// /// /// A new BinaryBlobWriter starts with the endianness that is native to the system /// for optimal performance. If you want to write data in a portable way, simply switch /// the binary writer into little endian mode (AMD/Intel x86, x64) or big endian mode /// (ARM, PowerPC) right after creating it using the SetLittleEndian() method. /// /// class BinaryBlobWriter : public BinaryWriter { /// Initializes a new binary writer for the specified blob /// Blob the binary writer will write into public: NUCLEX_STORAGE_API BinaryBlobWriter(const std::shared_ptr &blob); /// Destroys the binary writer public: NUCLEX_STORAGE_API virtual ~BinaryBlobWriter() override = default; /// Retrieves the current position of the cursor /// The cursor's absolute position within the blob public: NUCLEX_STORAGE_API std::uint64_t GetPosition() const { return this->position; } /// Changes the position of the blob cursor /// New absolute position of the cursor public: NUCLEX_STORAGE_API void SetPosition(std::uint64_t newPosition) { this->position = newPosition; } /// Whether data should be written in little endian (x86) format /// True if data is written in little endian (x86) format, otherwise false public: NUCLEX_STORAGE_API bool IsLittleEndian() const override; /// Sets whether data should be read in little endian format /// True if data should be read in little endian public: NUCLEX_STORAGE_API void SetLittleEndian(bool useLittleEndian = true) override; /// Writes a boolean into the stream /// Boolean that will be written public: NUCLEX_STORAGE_API void Write(bool value) override; /// Writes an unsigned 8 bit integer into the stream /// 8 bit integer that will be written public: NUCLEX_STORAGE_API void Write(std::uint8_t value) override; /// Writes a signed 8 bit integer into the stream /// 8 bit integer that will be written public: NUCLEX_STORAGE_API void Write(std::int8_t value) override; /// Writes an unsigned 16 bit integer into the stream /// 16 bit integer that will be written public: NUCLEX_STORAGE_API void Write(std::uint16_t value) override; /// Writes a signed 16 bit integer into the stream /// 16 bit integer that will be written public: NUCLEX_STORAGE_API void Write(std::int16_t value) override; /// Writes an unsigned 32 bit integer into the stream /// 32 bit integer that will be written public: NUCLEX_STORAGE_API void Write(std::uint32_t value) override; /// Writes a signed 32 bit integer into the stream /// 32 bit integer that will be written public: NUCLEX_STORAGE_API void Write(std::int32_t value) override; /// Writes an unsigned 64 bit integer into the stream /// 64 bit integer that will be written public: NUCLEX_STORAGE_API void Write(std::uint64_t value) override; /// Writes a signed 64 bit integer into the stream /// 64 bit integer that will be written public: NUCLEX_STORAGE_API void Write(std::int64_t value) override; /// Writes a floating point value into the stream /// Floating point value that will be written public: NUCLEX_STORAGE_API void Write(float value) override; /// Writes a double precision floating point value into the stream /// Double precision floating point value that will be written public: NUCLEX_STORAGE_API void Write(double value) override; /// Writes a string into the stream /// String that will be written public: NUCLEX_STORAGE_API void Write(const std::string &value) override; /// Writes a wide character string into the stream /// Wide character string that will be written /// /// Avoid using this. Wide characters are 16 bit on Windows, 32 bit on Linux and /// you'll be stuck with an unportable mess of halved UTF-32 characters or /// UTF-16 characters stuck in a UTF-32 wide character string. Only use UTF-8 /// in your public APIs. /// public: NUCLEX_STORAGE_API void Write(const std::wstring &value) override; /// Writes a chunk of bytes into the stream /// Buffer the bytes that will be written /// Number of bytes to write public: NUCLEX_STORAGE_API void Write(const void *buffer, std::size_t byteCount) override; /// Blob the binary reader writes into private: std::shared_ptr blob; /// Current position of the binary writer's blob pointer private: std::uint64_t position; /// Whether the bytes will be flipped to convert endianness private: bool flipBytes; }; // ------------------------------------------------------------------------------------------- // }}} // namespace Nuclex::Storage::Binary #endif // NUCLEX_STORAGE_BINARY_BINARYBLOBWRITER_H