Files
pip/libs/main/core/pimemoryblock.h
2026-03-07 17:00:45 +03:00

94 lines
3.1 KiB
C++

/*! \file pimemoryblock.h
* \ingroup Core
* \~\brief
* \~english Non-owning memory block helper
* \~russian Вспомогательный невладеющий блок памяти
*/
/*
PIP - Platform Independent Primitives
Base types and functions
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIMEMORYBLOCK_H
#define PIMEMORYBLOCK_H
//! \ingroup Core
//! \include pimemoryblock.h
//! \~\brief
//! \~english Helper structure describing a non-owning memory block.
//! \~russian Вспомогательная структура, описывающая невладеющий блок памяти.
struct PIMemoryBlock {
public:
//! \~english Constructs empty memory block.
//! \~russian Создает пустой блок памяти.
PIMemoryBlock() {}
//! \~english Constructs memory block from pointer and size.
//! \~russian Создает блок памяти из указателя и размера.
PIMemoryBlock(const void * data_, const int size_) {
d = const_cast<void *>(data_);
s = size_;
}
//! \~english Copy constructor.
//! \~russian Конструктор копирования.
PIMemoryBlock(const PIMemoryBlock & o) {
d = o.d;
s = o.s;
}
//! \~english Copy assignment operator.
//! \~russian Оператор присваивания копированием.
PIMemoryBlock & operator=(const PIMemoryBlock & o) {
d = o.d;
s = o.s;
return *this;
}
//! \~english Returns pointer to block data.
//! \~russian Возвращает указатель на данные блока.
void * data() { return d; }
//! \~english Returns pointer to block data.
//! \~russian Возвращает указатель на данные блока.
const void * data() const { return d; }
//! \~english Returns block size in bytes.
//! \~russian Возвращает размер блока в байтах.
int size() const { return s; }
//! \~english Returns `true` when the block stores a non-null pointer.
//! \~russian Возвращает `true`, когда блок хранит ненулевой указатель.
bool isNull() const { return d; }
private:
void * d = nullptr;
int s = 0;
};
//! \ingroup Core
//! \~\brief
//! \~english Creates %PIMemoryBlock for object pointed by "ptr".
//! \~russian Создает %PIMemoryBlock для объекта, на который указывает "ptr".
template<typename T>
PIMemoryBlock createMemoryBlock(const T * ptr) {
return PIMemoryBlock(ptr, sizeof(T));
}
#endif // PIMEMORYBLOCK_H