//! \~\ingroup Core
//! \~\file pimemoryblock.h
//! \brief
//! \~english Memory block helper struct for data storage and binary stream operations
//! \~russian Вспомогательная структура памяти для хранения данных и операций с двоичным потоком
//! \details
//! \~english The PIMemoryBlock struct provides a lightweight wrapper to store and restore custom blocks of data to/from PIBinaryStream. It
//! holds a pointer to data and its size in bytes.
//! \~russian Структура PIMemoryBlock предоставляет легковесный wrapper для сохранения и извлечения произвольных блоков данных в/из
//! PIBinaryStream. Она содержит указатель на данные и их размер в байтах.
/*
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 .
*/
#ifndef PIMEMORYBLOCK_H
#define PIMEMORYBLOCK_H
//! \~\brief
//! \~english Helper struct to store and restore custom blocks of data to/from PIBinaryStream
//! \~russian Вспомогательная структура для сохранения и извлечения произвольных блоков данных в/из PIBinaryStream
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(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
PIMemoryBlock createMemoryBlock(const T * ptr) {
return PIMemoryBlock(ptr, sizeof(T));
}
#endif // PIMEMORYBLOCK_H