| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-FileCopyrightText: 2023 Carl Zeiss Microscopy GmbH | ||
| 2 | // | ||
| 3 | // SPDX-License-Identifier: MIT | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <cstring> | ||
| 8 | #include <cstdint> | ||
| 9 | #include <memory> | ||
| 10 | #include <exception> | ||
| 11 | #include "../IBlobOutput.h" | ||
| 12 | |||
| 13 | namespace imgdoc2 | ||
| 14 | { | ||
| 15 | /// \brief implementation of the IBlobOutput interface | ||
| 16 | /// | ||
| 17 | /// An implementation of the IBlobOutput interface which allocates the data being delivered | ||
| 18 | /// on the heap. The memory allocated is owned by the instance. | ||
| 19 | class BlobOutputOnHeap : public imgdoc2::IBlobOutput | ||
| 20 | { | ||
| 21 | private: | ||
| 22 | std::uint8_t* buffer_{ nullptr }; | ||
| 23 | size_t buffer_size_{ 0 }; | ||
| 24 | bool is_reserved_{ false }; | ||
| 25 | public: | ||
| 26 | /// Destructor which releases the allocated memory. | ||
| 27 | 28 | ~BlobOutputOnHeap() override | |
| 28 | 28 | { | |
| 29 | 28 | free(this->buffer_); | |
| 30 | 28 | } | |
| 31 | |||
| 32 | //! @copydoc imgdoc2::IBlobOutput::Reserve(size_t) | ||
| 33 | 10 | [[nodiscard]] bool Reserve(size_t s) override | |
| 34 | { | ||
| 35 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
|
10 | if (this->is_reserved_) |
| 36 | { | ||
| 37 |
1/2✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | throw std::logic_error("This instance has already been initialized."); |
| 38 | } | ||
| 39 | |||
| 40 | 8 | this->buffer_ = static_cast<uint8_t*>(malloc(s)); | |
| 41 | 8 | this->buffer_size_ = s; | |
| 42 | 8 | this->is_reserved_ = true; | |
| 43 | 8 | return true; | |
| 44 | } | ||
| 45 | |||
| 46 | //! @copydoc imgdoc2::IBlobOutput::SetData(size_t, size_t, const void*) | ||
| 47 | 8 | [[nodiscard]] bool SetData(size_t offset, size_t size, const void* data) override | |
| 48 | { | ||
| 49 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
|
8 | if (!this->is_reserved_) |
| 50 | { | ||
| 51 |
1/2✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | throw std::logic_error("'Reserve' was not called before."); |
| 52 | } | ||
| 53 | |||
| 54 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
|
6 | if (offset + size > this->buffer_size_) |
| 55 | { | ||
| 56 |
1/2✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | throw std::invalid_argument("out-of-bounds"); |
| 57 | } | ||
| 58 | |||
| 59 | 4 | memcpy(this->buffer_ + offset, data, size); | |
| 60 | 4 | return true; | |
| 61 | } | ||
| 62 | public: | ||
| 63 | /// Gets a boolean indicating whether output data has been reserved, in other words - | ||
| 64 | /// whether "Reserve" was called. | ||
| 65 | /// \returns True if output memory was reserved; false otherwise. | ||
| 66 | 4 | [[nodiscard]] bool GetHasData() const { return this->is_reserved_; } | |
| 67 | |||
| 68 | /// Gets a const pointer to the data. The size of this buffer is given by "GetSizeOfData". If | ||
| 69 | /// this instance has not been initialized, nullptr is returned. | ||
| 70 | /// \returns If the instance is initialized, then a pointer to the buffer is returned; nullptr otherwise. | ||
| 71 | 4 | [[nodiscard]] const std::uint8_t* GetDataC() const { return this->buffer_; } | |
| 72 | |||
| 73 | /// Gets a pointer to the data. The size of this buffer is given by "GetSizeOfData". If | ||
| 74 | /// this instance has not been initialized, nullptr is returned. | ||
| 75 | /// \returns If the instance is initialized, then a pointer to the buffer is returned; nullptr otherwise. | ||
| 76 | [[nodiscard]] std::uint8_t* GetData() { return this->buffer_; } | ||
| 77 | |||
| 78 | /// Gets size of data in bytes. It is usually advised to check "GetHasData()" before. | ||
| 79 | /// \returns The size of the data in bytes. | ||
| 80 | 4 | [[nodiscard]] size_t GetSizeOfData() const { return this->buffer_size_; } | |
| 81 | public: | ||
| 82 | // no copy and no move | ||
| 83 | BlobOutputOnHeap() = default; | ||
| 84 | BlobOutputOnHeap(const BlobOutputOnHeap&) = delete; // copy constructor | ||
| 85 | BlobOutputOnHeap& operator=(const BlobOutputOnHeap&) = delete; // copy assignment | ||
| 86 | BlobOutputOnHeap(BlobOutputOnHeap&&) = delete; // move constructor | ||
| 87 | BlobOutputOnHeap& operator=(BlobOutputOnHeap&&) = delete; // move assignment | ||
| 88 | }; | ||
| 89 | } | ||
| 90 |