.. _program_listing_file_include_db_Capnp_CapnpBinary_CapnpBinary.hpp: Program Listing for File CapnpBinary.hpp ======================================== |exhale_lsh| :ref:`Return to documentation for file ` (``include/db/Capnp/CapnpBinary/CapnpBinary.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp #pragma once #include #include #include #include #include "MixBinary.capnp.h" #include "MusicBinary.capnp.h" #include "NoteBinary.capnp.h" #include "PDJE_EXPORT_SETTER.hpp" #include "PDJE_LOG_SETTER.hpp" template class CapReader { private: std::vector Origin; std::optional capreader; public: CapReader() = default; ~CapReader() = default; std::optional Rp; bool open(const std::vector &capnpBinary) { try { Origin.resize(capnpBinary.size()); memcpy(Origin.data(), capnpBinary.data(), capnpBinary.size()); kj::ArrayPtr arr(Origin.data(), Origin.size()); capreader.emplace(::kj::arrayPtr( reinterpret_cast(arr.begin()), arr.size() / sizeof(capnp::word))); Rp = capreader->getRoot(); return true; } catch (std::exception &e) { critlog("failed to open capnpBinary. from CapReader open. " "ExceptionLog: "); critlog(e.what()); return false; } } std::vector out() { return Origin; } }; template class CapWriter { private: std::optional capwriter; public: CapWriter() = default; ~CapWriter() = default; std::optional Wp; bool open(const std::vector &capnpBinary) { try { kj::ArrayPtr arr(capnpBinary.data(), capnpBinary.size()); capwriter.emplace(); Wp = capwriter->initRoot(); capnp::FlatArrayMessageReader readed(::kj::arrayPtr( reinterpret_cast(arr.begin()), arr.size() / sizeof(capnp::word))); auto readroot = readed.getRoot(); Wp->initDatas(readroot.getDatas().size()); Wp->setDatas(readroot.getDatas()); return true; } catch (std::exception &e) { critlog("failed to open capnpBinary. from CapWriter open. " "ExceptionLog: "); critlog(e.what()); return false; } } bool makeNew() { try { capwriter.emplace(); Wp = capwriter->initRoot(); return true; } catch (std::exception &e) { critlog("failed to make new capnpWriter. from CapWriter makeNew. " "ExceptionLog: "); critlog(e.what()); return false; } } std::vector out() { try { auto farr = capnp::messageToFlatArray(capwriter.value()); auto fbyte = farr.asBytes(); std::vector buffer(fbyte.begin(), fbyte.end()); return buffer; } catch (std::exception &e) { critlog("failed to return capnp binary datas. from CapWriter out. " "ExceptionLog: "); critlog(e.what()); return std::vector(); } } };