ContiguousDataStorageInputRef

Материал из CAMaaS preliminary wiki
Перейти к навигации Перейти к поиску

Ссылочный адаптер, определенный над типом const IContiguousDataStorageInput.

struct ContiguousDataStorageInputRef;
Открытые типы
ТипОписание
interface_typeПсевдоним типа const IContiguousDataStorageInput.
Открытые методы
МетодОписание
(конструкторы)Конструкторы, определенные требованиями ссылочного адаптера.
get_interfaceВозвращает адрес ассоциированной с адаптером реализации накопителя данных.
share_ownershipРазделяет владение ассоциированным накопителем данных, возвращая владеющий адаптер.
readСоздает поток чтения InputStreamOwn, связанный с источником данных.
element_countВозвращает число элементов в источнике данных.
byte_sizeВозвращает объем данных в накопителе в байтах.
max_byte_sizeВозвращает максимально допустимый объем данных в накопителе.
operator ConsequentDataStorageInputOwnОператор преобразования типа адаптера.
operator ArbitrarilyAccessedDataStorageInputOwnОператор преобразования типа адаптера.
Защищенные методы
МетодОписание
reset_interfaceЗадает адаптеру новую реализацию IContiguousDataStorageInput.
Наследует
КлассОписание
Ссылочный адаптер, определенный над типом const IDataStorage.
Пример: Использование ContiguousDataStorageInputRef и ContiguousDataStorageOutputRef для работы с памятью и файлами.
#include <camaas/idatastorage.h>
#include <algorithm>
#include <utility>
#include <vector>
#include <string>
#include <iostream>
#include <iomanip>

using namespace CAMaaS;

std::string operator""_s(const char* psz, std::size_t cch) {return std::string(psz, cch);}

void TestContiguousDataSource(ContiguousDataStorageOutputRef output_side, ContiguousDataStorageInputRef input_side)
{
	auto osWrite = represent_as<OutputByteStreamOwn>(output_side.write());
	osWrite.write(std::vector<std::uint8_t>({0x30, 0x31, 0x32, 0x33, 0x34}));
	osWrite.write(std::vector<std::uint8_t>({0x35, 0x36, 0x37, 0x38, 0x39}));
	auto osWrite2 = represent_as<OutputByteStreamOwn>(output_side.write(20));
	osWrite2.write(std::vector<std::uint8_t>({'a', 'b', 'c', 'd', 'e'}));
	osWrite2.write(std::vector<std::uint8_t>({'e', 'f', 'g', 'h', 'i'}));
	represent_as<ContiguousDataStorageOwn>(output_side).erase(10, 10);
	auto osRead1 = represent_as<InputByteStreamOwn>(input_side.read());
	auto v1 = osRead1.read_as<std::vector<std::uint8_t>>(10);
	auto vout10 = osRead1.read_as<std::vector<std::uint8_t>>(5);
	auto osRead2 = represent_as<InputByteStreamOwn>(input_side.read(15));
	auto vout11 = osRead2.read_as<std::vector<std::uint8_t>>(std::size_t(input_side.byte_size() - 15));
	std::move(vout10.begin(), vout10.end(), std::back_inserter(v1));
	std::move(vout11.begin(), vout11.end(), std::back_inserter(v1));
	auto v2 = represent_as<InputByteStreamOwn>(input_side.read()).read_all_as<std::vector<std::uint8_t>>();
	std::cout << "Vectors are equal: " << std::boolalpha << (v1 == v2) << "\n";
	std::cout << "Read contents: ";
	for (auto b:v2)
		std::cout << std::hex << b;
	std::cout << "\n";
}

int main(int, char**)
{
	std::cout << "===File test===\n";
	auto strFile = u8"Test file.txt"_s;
	auto file = make_file_based_data_storage<FileReadWrite, FileCreateAlways>(strFile);
	TestContiguousDataSource(file, file);
	std::cout << "===Preallocated buffer test===\n";
	std::uint8_t pBuffer[100];
	auto buff = make_inmemory_preallocated_data_storage(pBuffer, sizeof(pBuffer));
	TestContiguousDataSource(buff, buff);
	std::cout << "===Preallocated buffer test with owning===\n";
	auto alloc = std::allocator<std::uint8_t>();
	auto p2 = std::allocator_traits<std::allocator<std::uint8_t>>::allocate(alloc, sizeof(pBuffer));
	auto buff2 = make_inmemory_preallocated_data_storage(own_buffer(p2, sizeof(pBuffer), alloc));
	TestContiguousDataSource(buff2, buff2);
	std::cout << "===Reading from existing buffer===\n";
	auto pRead = make_inmemory_input_data_source(pBuffer, std::size_t(buff.byte_size()));
	auto vRead = represent_as<InputByteStreamOwn>(pRead.read()).read_all_as<std::vector<std::uint8_t>>();
	std::cout << "Read contents from the input buffer: ";
	for (auto b:vRead)
		std::cout << std::hex << b;
	std::cout << "\n";
	try
	{
		represent_as<ConsequentDataStorageOwn>(pRead);
		std::cout << "Conversion did NOT cause an expected error\n";
	}catch (Chusov::Exceptions::ChsvCodeException& ex)
	{
		std::cout << "Conversion caused expected error: " << ex.what() << "\n";
	}
	std::cout << "===Cached buffer test===\n";
	auto buff_cached = make_inmemory_data_storage();
	TestContiguousDataSource(buff_cached, buff_cached);

	return 0;
	
}
См. также
ContiguousDataStorageInputOwnВладеющий адаптер, определенный над интерфейсом типом const IContiguousDataStorageInput.
ContiguousDataStorageOutputRefСсылочный адаптер, определенный над интерфейсом IContiguousDataStorageOutput.
ContiguousDataStorageRefСсылочный адаптер, определенный над интерфейсом IContiguousDataStorage.