vgl 1.0.0
Virtual Globe Library: API reference
Loading...
Searching...
No Matches
base.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2026 Jeremiah van Oosten
3//
4// C++ wrapper for vgl/base.h: result codes, expected, versioned structure initialization and
5// version. See docs/c-api.md, section 16.
6
7#ifndef VGL_BASE_HPP
8#define VGL_BASE_HPP
9
10#include <vgl/base.h>
11
12#include <cstdint>
13#include <type_traits>
14#include <utility>
15#include <version>
16
17#if defined(__cpp_lib_expected) && __cpp_lib_expected >= 202202L
18# include <expected>
19#endif
20
21namespace vgl
22{
23
41
42[[nodiscard]] constexpr bool succeeded(result r) noexcept
43{
44 return static_cast<std::int32_t>(r) >= 0;
45}
46
47[[nodiscard]] constexpr bool failed(result r) noexcept
48{
49 return static_cast<std::int32_t>(r) < 0;
50}
51
52// Returns the name of a result code, such as "VGL_RESULT_OUT_OF_MEMORY", or
53// "VGL_RESULT_UNKNOWN" for an unknown code.
54[[nodiscard]] inline const char* result_name_get(result r) noexcept
55{
56 const char* name = nullptr;
57 static_cast<void>(vgl_result_name_get(static_cast<vgl_result>(r), &name));
58 return name;
59}
60
61#if defined(__cpp_lib_expected) && __cpp_lib_expected >= 202202L
62
63template <typename T>
64using expected = std::expected<T, result>;
65
66using unexpected = std::unexpected<result>;
67
68#else
69
70// Error wrapper for constructing a failed expected, mirroring std::unexpected.
72{
73public:
74 constexpr explicit unexpected(result error) noexcept : error_(error) {}
75
76 [[nodiscard]] constexpr result error() const noexcept { return error_; }
77
78private:
79 result error_;
80};
81
82// Minimal subset of std::expected<T, vgl::result>, for standard libraries without
83// <expected>. T must be default constructible. It never throws: calling value() on a
84// failed expected is undefined behaviour, as with operator* on std::expected.
85template <typename T>
87{
88 static_assert(std::is_default_constructible_v<T>,
89 "vgl::expected requires a default-constructible type");
90
91public:
92 constexpr expected(T value) noexcept(std::is_nothrow_move_constructible_v<T>)
93 : value_(std::move(value))
94 {
95 }
96
97 constexpr expected(unexpected error) noexcept : error_(error.error()) {}
98
99 [[nodiscard]] constexpr bool has_value() const noexcept { return succeeded(error_); }
100 [[nodiscard]] constexpr explicit operator bool() const noexcept { return has_value(); }
101
102 [[nodiscard]] constexpr T& value() & noexcept { return value_; }
103 [[nodiscard]] constexpr const T& value() const& noexcept { return value_; }
104 [[nodiscard]] constexpr T&& value() && noexcept { return std::move(value_); }
105
106 [[nodiscard]] constexpr T& operator*() & noexcept { return value_; }
107 [[nodiscard]] constexpr const T& operator*() const& noexcept { return value_; }
108 [[nodiscard]] constexpr T* operator->() noexcept { return &value_; }
109 [[nodiscard]] constexpr const T* operator->() const noexcept { return &value_; }
110
111 [[nodiscard]] constexpr result error() const noexcept { return error_; }
112
113private:
114 T value_{};
115 result error_ = result::success;
116};
117
118#endif
119
120// Returns a zero-initialized versioned structure with struct_size set
121// (docs/c-api.md, section 8.1).
122template <typename T>
123[[nodiscard]] constexpr T init() noexcept
124{
125 T value{};
126 value.struct_size = static_cast<std::uint32_t>(sizeof(T));
127 return value;
128}
129
131{
132 std::uint32_t major_version = 0;
133 std::uint32_t minor_version = 0;
134 std::uint32_t patch_version = 0;
135};
136
137// Returns the version of the vgl binary.
139{
140 version v;
141 static_cast<void>(vgl_version_get(&v.major_version, &v.minor_version, &v.patch_version));
142 return v;
143}
144
145} // namespace vgl
146
147#endif // VGL_BASE_HPP
vgl_result
Definition base.h:98
@ VGL_RESULT_IO_ERROR
Definition base.h:117
@ VGL_RESULT_SUCCESS
Definition base.h:100
@ VGL_RESULT_VERSION_MISMATCH
Definition base.h:121
@ VGL_RESULT_CORRUPT_DATA
Definition base.h:119
@ VGL_RESULT_OUT_OF_MEMORY
Definition base.h:109
@ VGL_RESULT_NOT_READY
Definition base.h:105
@ VGL_RESULT_NOT_FOUND
Definition base.h:115
@ VGL_RESULT_INVALID_OPERATION
Definition base.h:113
@ VGL_RESULT_INVALID_ARGUMENT
Definition base.h:107
@ VGL_RESULT_CANCELLED
Definition base.h:123
@ VGL_RESULT_INTERNAL_ERROR
Definition base.h:127
@ VGL_RESULT_NOT_SUPPORTED
Definition base.h:111
@ VGL_RESULT_INCOMPLETE
Definition base.h:102
@ VGL_RESULT_BACKEND_ERROR
Definition base.h:125
vgl_result vgl_version_get(uint32_t *out_major, uint32_t *out_minor, uint32_t *out_patch)
vgl_result vgl_result_name_get(vgl_result result, const char **out_name)
Definition base.hpp:87
constexpr const T & operator*() const &noexcept
Definition base.hpp:107
constexpr const T & value() const &noexcept
Definition base.hpp:103
constexpr T & operator*() &noexcept
Definition base.hpp:106
constexpr expected(unexpected error) noexcept
Definition base.hpp:97
constexpr const T * operator->() const noexcept
Definition base.hpp:109
constexpr T && value() &&noexcept
Definition base.hpp:104
constexpr bool has_value() const noexcept
Definition base.hpp:99
constexpr result error() const noexcept
Definition base.hpp:111
constexpr T & value() &noexcept
Definition base.hpp:102
constexpr expected(T value) noexcept(std::is_nothrow_move_constructible_v< T >)
Definition base.hpp:92
constexpr T * operator->() noexcept
Definition base.hpp:108
Definition base.hpp:72
constexpr result error() const noexcept
Definition base.hpp:76
constexpr unexpected(result error) noexcept
Definition base.hpp:74
Definition base.hpp:22
result
Definition base.hpp:25
constexpr T init() noexcept
Definition base.hpp:123
const char * result_name_get(result r) noexcept
Definition base.hpp:54
version version_get() noexcept
Definition base.hpp:138
constexpr bool succeeded(result r) noexcept
Definition base.hpp:42
Definition base.hpp:131
std::uint32_t patch_version
Definition base.hpp:134
std::uint32_t major_version
Definition base.hpp:132
std::uint32_t minor_version
Definition base.hpp:133