make_inmemory_input_data_source
Перейти к навигации
Перейти к поиску
InMemoryDataStorageInputOwn make_inmemory_input_data_source(const void* pBuffer, std::size_t cbBuffer, bool fShareBuffer = true); //1
InMemoryDataStorageInputOwn make_inmemory_input_data_source(_In_z_ const char* pszData, bool fShareBuffer = true); //2
InMemoryDataStorageInputOwn make_inmemory_input_data_source(_In_z_ const wchar_t* pszData, bool fShareBuffer = true); //3
template <class char_traits, class allocator>
InMemoryDataStorageInputOwn make_inmemory_input_data_source(const std::basic_string<char, char_traits, allocator>& str, bool fShareBuffer = true); //4
template <class Container>
InMemoryDataStorageInputOwn make_inmemory_input_data_source(const Container& v, bool fShareBuffer = true); //5
Делегирует вызов функции CreateInMemoryBinaryInputDataSource с буфером, над которым определен C++ объект.
- Параметры
[in] pBuffer Буфер данных объемом cbBuffer байт. cbBuffer Объем данных, доступных для чтения из буфера pBuffer, в байтах. [in] pszData C-строка с терминальным нулем. str Строка std::basic_string, над которой определен входной буфер. v Контейнер с методами data и size, над которым определен входной буфер. fShareBuffer Флаг разделения владения буфером с клиентом функции. Его установка требует управления временем жизни буфера со стороны клиента, а также доступность буфера на протяжении времени жизни создаваемого источника данных. Если флаг сброшен, производится линейное копирование во внутренний буфер, управление жизнью которого осуществляется создаваемым источником данных. - Возвращаемое значение:
- Объект InMemoryDataStorageInputOwn.
- См. также
IInMemoryDataStorageInput Интерфейс, над которым определен возвращаемый объект.
Пример: Использование 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;
}