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 "../IDataObj.h" | ||
9 | |||
10 | namespace imgdoc2 | ||
11 | { | ||
12 | /// An implementation of the IDataObjBase interface. This class allocates and owns memory on the heap. | ||
13 | class DataObjectOnHeap : public imgdoc2::IDataObjBase | ||
14 | { | ||
15 | private: | ||
16 | std::uint8_t* buffer_{ nullptr }; | ||
17 | size_t buffer_size_{ 0 }; | ||
18 | public: | ||
19 | /// Constructor which allocates the specified amount of data on the heap. | ||
20 | /// \param size The size of the buffer to allocate (in bytes). | ||
21 | 8 | explicit DataObjectOnHeap(size_t size) | |
22 | 8 | { | |
23 | 8 | this->buffer_ = static_cast<std::uint8_t*>(malloc(size)); | |
24 | 8 | this->buffer_size_ = size; | |
25 | 8 | } | |
26 | |||
27 | //! @copydoc imgdoc2::IDataObjBase::GetData(const void**, size_t*) const | ||
28 | 8 | void GetData(const void** p, size_t* s) const override | |
29 | { | ||
30 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | if (p != nullptr) |
31 | { | ||
32 | 8 | *p = this->buffer_; | |
33 | } | ||
34 | |||
35 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | if (s != nullptr) |
36 | { | ||
37 | 8 | *s = this->buffer_size_; | |
38 | } | ||
39 | 8 | } | |
40 | |||
41 | 16 | ~DataObjectOnHeap() override | |
42 | 16 | { | |
43 | 16 | free(this->buffer_); | |
44 | 16 | } | |
45 | |||
46 | public: | ||
47 | /// Gets a const pointer to the data. The size of this buffer is given by "GetSizeOfData". | ||
48 | 4 | [[nodiscard]] const std::uint8_t* GetDataC() const { return this->buffer_; } | |
49 | |||
50 | /// Gets a pointer to the data. The size of this buffer is given by "GetSizeOfData". | ||
51 | 800 | [[nodiscard]] std::uint8_t* GetData() { return this->buffer_; } | |
52 | |||
53 | /// Gets size of data in bytes. | ||
54 | /// \returns The size of the data in bytes. | ||
55 | 808 | [[nodiscard]] size_t GetSizeOfData() const { return this->buffer_size_; } | |
56 | public: | ||
57 | // no copy and no move | ||
58 | DataObjectOnHeap() = delete; | ||
59 | DataObjectOnHeap(const DataObjectOnHeap&) = delete; // copy constructor | ||
60 | DataObjectOnHeap& operator=(const DataObjectOnHeap&) = delete; // copy assignment | ||
61 | DataObjectOnHeap(DataObjectOnHeap&&) = delete; // move constructor | ||
62 | DataObjectOnHeap& operator=(DataObjectOnHeap&&) = delete; // move assignment | ||
63 | }; | ||
64 | } | ||
65 |