From 35cf05e3bc9c3ac65b7f5a0063a5cb5a455c073b Mon Sep 17 00:00:00 2001 From: Lynix Date: Sun, 29 Dec 2019 16:25:40 +0100 Subject: [PATCH] Core: Add EmptyStream class --- ChangeLog.md | 1 + include/Nazara/Core/EmptyStream.hpp | 46 ++++++++++++++ include/Nazara/Core/EmptyStream.inl | 22 +++++++ src/Nazara/Core/EmptyStream.cpp | 99 +++++++++++++++++++++++++++++ 4 files changed, 168 insertions(+) create mode 100644 include/Nazara/Core/EmptyStream.hpp create mode 100644 include/Nazara/Core/EmptyStream.inl create mode 100644 src/Nazara/Core/EmptyStream.cpp diff --git a/ChangeLog.md b/ChangeLog.md index 0aa103f4f..656dc699c 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -212,6 +212,7 @@ Nazara Engine: - Added ENetPeer::GetTotalByte[Received|Sent] - Added ENetPeer::GetTotalPacketSent - ⚠ ENetHost::GetTotalReceivedPackets now returns the number of commands received (instead of the number of UDP packets received) +- Added EmptyStream class, useful to measure how many bytes some writing operations will take Nazara Development Kit: - Added ImageWidget (#139) diff --git a/include/Nazara/Core/EmptyStream.hpp b/include/Nazara/Core/EmptyStream.hpp new file mode 100644 index 000000000..da66ca329 --- /dev/null +++ b/include/Nazara/Core/EmptyStream.hpp @@ -0,0 +1,46 @@ +// Copyright (C) 2019 Jérôme Leclercq +// This file is part of the "Nazara Engine - Core module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef NAZARA_EMPTYSTREAM_HPP +#define NAZARA_EMPTYSTREAM_HPP + +#include +#include + +namespace Nz +{ + class NAZARA_CORE_API EmptyStream : public Stream + { + public: + inline EmptyStream(); + EmptyStream(const EmptyStream&) = default; + EmptyStream(EmptyStream&&) noexcept = default; + ~EmptyStream() = default; + + void Clear(); + + bool EndOfStream() const override; + + UInt64 GetCursorPos() const override; + UInt64 GetSize() const override; + + bool SetCursorPos(UInt64 offset) override; + + EmptyStream& operator=(const EmptyStream&) = default; + EmptyStream& operator=(EmptyStream&&) noexcept = default; + + private: + void FlushStream() override; + std::size_t ReadBlock(void* buffer, std::size_t size) override; + std::size_t WriteBlock(const void* buffer, std::size_t size) override; + + UInt64 m_size; + }; +} + +#include + +#endif // NAZARA_EMPTYSTREAM_HPP diff --git a/include/Nazara/Core/EmptyStream.inl b/include/Nazara/Core/EmptyStream.inl new file mode 100644 index 000000000..9691760a7 --- /dev/null +++ b/include/Nazara/Core/EmptyStream.inl @@ -0,0 +1,22 @@ +// Copyright (C) 2019 Jérôme Leclercq +// This file is part of the "Nazara Engine - Core module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include + +namespace Nz +{ + /*! + * \ingroup core + * \class Nz::EmptyStream + * \brief Constructs an EmptyStream object by default + */ + inline EmptyStream::EmptyStream() : + m_size(0) + { + } + +} + +#include diff --git a/src/Nazara/Core/EmptyStream.cpp b/src/Nazara/Core/EmptyStream.cpp new file mode 100644 index 000000000..5cba8af35 --- /dev/null +++ b/src/Nazara/Core/EmptyStream.cpp @@ -0,0 +1,99 @@ +// Copyright (C) 2019 Jérôme Leclercq +// This file is part of the "Nazara Engine - Core module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include +#include +#include + +namespace Nz +{ + /*! + * \ingroup core + * \class Nz::EmptyStream + * \brief Core class that simulate an empty buffer (discarding all content) which can be used to measure size + */ + + /*! + * \brief Resets the stream size to zero + */ + void EmptyStream::Clear() + { + m_size = 0; + } + + /*! + * \brief Checks whether the stream reached the end of the stream + * \return Always false + */ + bool EmptyStream::EndOfStream() const + { + return false; + } + + /*! + * \brief Gets the position of the cursor (which is always zero) + * \return Always zero + */ + UInt64 EmptyStream::GetCursorPos() const + { + return 0; + } + + /*! + * \brief Gets the size of the raw memory (how many bytes would have been written on a regular stream) + * \return Size occupied until now + */ + UInt64 EmptyStream::GetSize() const + { + return m_size; + } + + /*! + * \brief Does nothing + * \return true + * + * \param offset Offset according to the beginning of the stream + */ + bool EmptyStream::SetCursorPos(UInt64 offset) + { + return true; + } + + /*! + * \brief Flushes the stream (does nothing) + */ + void EmptyStream::FlushStream() + { + // Nothing to flush + } + + /*! + * \brief Reads data + * \return Number of byte read (always zero) + * + * Reading from an empty stream does nothing and will always returns zero + * + * \param buffer Preallocated buffer to contain information read + * \param size Size of the read and thus of the buffer + */ + std::size_t EmptyStream::ReadBlock(void* buffer, std::size_t size) + { + return 0; + } + + /*! + * \brief Writes data + * \return size + * + * \param buffer Dummy parameter + * \param size How many bytes will be "written" + */ + std::size_t EmptyStream::WriteBlock(const void* /*buffer*/, std::size_t size) + { + m_size += size; + + return size; + } +}