Loading...
Searching...
No Matches
ReadconDbAbi.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4#pragma once
5
6// ABI contract with readcon-db. The header must carry the major gpridge is
7// written for and a minor at or above the lowest minor that declares the
8// archive and trajectory calls gpridge makes; the loaded library must
9// accept the stamp built from that header.
10
11#include <readcon-db.h>
12
13#include <cstdint>
14#include <mutex>
15#include <sstream>
16#include <stdexcept>
17#include <string>
18
20#define GPR_READCON_DB_ABI_MAJOR 1
23#define GPR_READCON_DB_ABI_MIN_MINOR 0
24
25static_assert(RKRDB_ABI_VERSION_MAJOR == GPR_READCON_DB_ABI_MAJOR,
26 "gpridge is written against readcon-db C ABI major "
27 "GPR_READCON_DB_ABI_MAJOR; the readcon-db.h found declares "
28 "another major");
29static_assert(RKRDB_ABI_VERSION_MINOR >= GPR_READCON_DB_ABI_MIN_MINOR,
30 "readcon-db C ABI is older than gpridge needs "
31 "(see subprojects/readcon-db.wrap)");
32
34
35struct Stamp {
36 std::uint32_t major = 0;
37 std::uint32_t minor = 0;
38 std::uint32_t layout = 0;
39};
40
41[[nodiscard]] inline std::string describe(const Stamp& stamp)
42{
43 std::ostringstream out;
44 out << stamp.major << '.' << stamp.minor << " layout " << stamp.layout;
45 return out.str();
46}
47
52[[nodiscard]] inline std::string mismatch(const Stamp& header,
53 const Stamp& library,
54 bool library_accepts)
55{
56 if (library_accepts && header.major == library.major &&
57 header.layout == library.layout)
58 return {};
59 return "readcon-db ABI mismatch: gpridge was built against "
60 "readcon-db.h " +
61 describe(header) + " and the loaded library reports " +
62 describe(library) +
63 (library_accepts ? "" : " and rejects the header's stamp");
64}
65
66[[nodiscard]] inline Stamp headerStamp() noexcept
67{
68 return {RKRDB_ABI_VERSION_MAJOR, RKRDB_ABI_VERSION_MINOR,
69 RKRDB_ABI_LAYOUT_REVISION};
70}
71
72[[nodiscard]] inline Stamp libraryStamp()
73{
74 return {rkrdb_abi_version_major(), rkrdb_abi_version_minor(),
75 rkrdb_abi_layout_revision()};
76}
77
79[[nodiscard]] inline std::string loadedLibraryMismatch()
80{
81 const Stamp header = headerStamp();
82 return mismatch(
83 header, libraryStamp(),
84 rkrdb_abi_compatible(header.major, header.minor, header.layout) != 0);
85}
86
90{
91 static std::once_flag checked;
92 static std::string failure;
93 std::call_once(checked, [] { failure = loadedLibraryMismatch(); });
94 if (!failure.empty()) throw std::runtime_error(failure);
95}
96
97} // namespace gpr::readcon_db_abi
#define GPR_READCON_DB_ABI_MAJOR
Major of the readcon-db C ABI gpridge is written against.
#define GPR_READCON_DB_ABI_MIN_MINOR
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
std::string loadedLibraryMismatch()
The mismatch message for the loaded library; empty when compatible.
std::string mismatch(const Stamp &header, const Stamp &library, bool library_accepts)
Stamp headerStamp() noexcept
std::string describe(const Stamp &stamp)
void requireCompatibleLibrary()