From 48210e6925658163952e458a4d13629ffdb0cea3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20G=C3=BCndling?= Date: Thu, 10 Oct 2024 13:05:34 +0200 Subject: [PATCH] I/O helpers --- include/cista/io.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 include/cista/io.h diff --git a/include/cista/io.h b/include/cista/io.h new file mode 100644 index 00000000..36dba58e --- /dev/null +++ b/include/cista/io.h @@ -0,0 +1,44 @@ +#pragma once + +#include + +#include "cista/memory_holder.h" +#include "cista/serialization.h" + +namespace cista { + +template +void write(std::filesystem::path const& p, T const& w) { + auto mmap = + cista::mmap{p.generic_string().c_str(), cista::mmap::protection::WRITE}; + auto writer = cista::buf(std::move(mmap)); + cista::serialize(writer, w); +} + +template +void write(std::filesystem::path const& p, wrapped const& w) { + write(p, *w); +} + +template +cista::wrapped read(std::filesystem::path const& p) { + auto b = cista::file{p.generic_string().c_str(), "r"}.content(); + auto const ptr = cista::deserialize(b); + auto mem = cista::memory_holder{std::move(b)}; + return cista::wrapped{std::move(mem), ptr}; +} + +template +cista::wrapped read_mmap(std::filesystem::path const& p) { + auto mmap = + cista::mmap{p.generic_string().c_str(), cista::mmap::protection::READ}; + auto const ptr = cista::deserialize(mmap); + auto mem = cista::memory_holder{buf{std::move(mmap)}}; + return cista::wrapped{std::move(mem), ptr}; +} + +} // namespace cista \ No newline at end of file