moddef-cpp
Idiomatic C++ runtime for ModDef
Loading...
Searching...
No Matches
document.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2
12#ifndef MODDEF_DOCUMENT_HPP
13#define MODDEF_DOCUMENT_HPP
14
15#include <cstddef>
16#include <cstdint>
17#include <span>
18#include <string_view>
19#include <utility>
20#include <vector>
21
22#include "moddef/error.hpp"
23#include "moddef/str.hpp"
24
25extern "C" {
26#include "moddef/doc.h"
27}
28
29namespace moddef {
30
33class Document {
34 public:
36 static Result<Document> view(std::span<const std::uint8_t> bytes) {
37 Document d;
38 if (auto e = md_doc_init(&d.doc_, bytes.data(), bytes.size()); e != MD_OK)
39 return std::unexpected(from_c(e));
40 return d;
41 }
42
44 static Result<Document> own(std::vector<std::uint8_t> bytes) {
45 Document d;
46 d.storage_ = std::move(bytes);
47 if (auto e = md_doc_init(&d.doc_, d.storage_.data(), d.storage_.size()); e != MD_OK)
48 return std::unexpected(from_c(e));
49 return d;
50 }
51
52 std::string_view doc_id() const noexcept { return from_c(md_doc_id(&doc_)); }
53
54 const md_doc_t* c_doc() const noexcept { return &doc_; }
55
56 // Movable: the owned buffer (if any) keeps its heap address across a move,
57 // so the C view stays valid; the view is repointed for clarity.
58 Document(Document&& o) noexcept : storage_(std::move(o.storage_)), doc_(o.doc_) {
59 if (!storage_.empty()) {
60 doc_.raw.p = storage_.data();
61 doc_.raw.len = static_cast<std::uint32_t>(storage_.size());
62 }
63 }
64 Document& operator=(Document&& o) noexcept {
65 storage_ = std::move(o.storage_);
66 doc_ = o.doc_;
67 if (!storage_.empty()) {
68 doc_.raw.p = storage_.data();
69 doc_.raw.len = static_cast<std::uint32_t>(storage_.size());
70 }
71 return *this;
72 }
73 Document(const Document&) = delete;
74 Document& operator=(const Document&) = delete;
75
76 private:
77 Document() = default;
78
79 std::vector<std::uint8_t> storage_; // empty in view mode
80 md_doc_t doc_{};
81};
82
83} // namespace moddef
84
85#endif // MODDEF_DOCUMENT_HPP
A parsed ModDef document.
Definition document.hpp:33
Document & operator=(Document &&o) noexcept
Definition document.hpp:64
static Result< Document > view(std::span< const std::uint8_t > bytes)
View over external bytes (no copy).
Definition document.hpp:36
static Result< Document > own(std::vector< std::uint8_t > bytes)
Take ownership of a byte buffer (host-side convenience).
Definition document.hpp:44
std::string_view doc_id() const noexcept
Definition document.hpp:52
Document(const Document &)=delete
Document(Document &&o) noexcept
Definition document.hpp:58
const md_doc_t * c_doc() const noexcept
Definition document.hpp:54
Document & operator=(const Document &)=delete
Error type and Result<T> (spec §26.3/§26.4, §32).
Definition device.hpp:28
std::expected< T, Error > Result
The canonical result type across the API; Result<void> for actions.
Definition error.hpp:53
Error from_c(md_err_t e) noexcept
Definition error.hpp:46
std::string_view <-> md_str_t bridging.