Skip to content

Commit

Permalink
use cista i/o helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
felixguendling committed Oct 10, 2024
1 parent 6a37889 commit 97c736c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .pkg
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[cista]
url=git@github.com:felixguendling/cista.git
branch=master
commit=f1358310262c347a8b4a533e5dd6184ec97ba637
commit=48210e6925658163952e458a4d13629ffdb0cea3
[geo]
url=git@github.com:motis-project/geo.git
branch=master
Expand Down
2 changes: 1 addition & 1 deletion include/nigiri/timetable.h
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ struct timetable {

void write(cista::memory_holder&) const;
void write(std::filesystem::path const&) const;
static cista::wrapped<timetable> read(cista::memory_holder&&);
static cista::wrapped<timetable> read(std::filesystem::path const&);

// Schedule range.
interval<date::sys_days> date_range_;
Expand Down
48 changes: 4 additions & 44 deletions src/timetable.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "nigiri/timetable.h"

#include "cista/mmap.h"
#include "cista/serialization.h"
#include "cista/io.h"

#include "utl/overloaded.h"

Expand All @@ -10,9 +9,6 @@

namespace nigiri {

constexpr auto const kMode =
cista::mode::WITH_INTEGRITY | cista::mode::WITH_STATIC_VERSION;

std::string reverse(std::string s) {
std::reverse(s.begin(), s.end());
return s;
Expand Down Expand Up @@ -71,48 +67,12 @@ std::ostream& operator<<(std::ostream& out, timetable const& tt) {
return out;
}

cista::wrapped<timetable> timetable::read(cista::memory_holder&& mem) {
return std::visit(
utl::overloaded{
[&](cista::buf<cista::mmap>& b) {
auto const ptr =
reinterpret_cast<timetable*>(&b[cista::data_start(kMode)]);
return cista::wrapped{std::move(mem), ptr};
},
[&](cista::buffer& b) {
auto const ptr = cista::deserialize<timetable, kMode>(b);
return cista::wrapped{std::move(mem), ptr};
},
[&](cista::byte_buf& b) {
auto const ptr = cista::deserialize<timetable, kMode>(b);
return cista::wrapped{std::move(mem), ptr};
}},
mem);
cista::wrapped<timetable> timetable::read(std::filesystem::path const& p) {
return cista::read<timetable>(p);
}

void timetable::write(std::filesystem::path const& p) const {
auto mmap = cista::mmap{p.string().c_str(), cista::mmap::protection::WRITE};
auto writer = cista::buf<cista::mmap>(std::move(mmap));

{
auto const timer = scoped_timer{"timetable.write"};
cista::serialize<kMode>(writer, *this);
}
}

void timetable::write(cista::memory_holder& mem) const {
std::visit(utl::overloaded{[&](cista::buf<cista::mmap>& writer) {
cista::serialize<kMode>(writer, *this);
},
[&](cista::buffer&) {
throw std::runtime_error{"not supported"};
},
[&](cista::byte_buf& b) {
auto writer = cista::buf{std::move(b)};
cista::serialize<kMode>(writer, *this);
b = std::move(writer.buf_);
}},
mem);
return cista::write(p, *this);
}

} // namespace nigiri

0 comments on commit 97c736c

Please sign in to comment.