Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
version.h
1// SPDX-FileCopyrightText: © 2026 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include <optional>
7#include <string_view>
8
9#include <nlohmann/json.hpp>
10
11namespace zrythm::utils
12{
13
17namespace version_keys
18{
19using namespace std::literals;
20inline constexpr auto kMajor = "major"sv;
21inline constexpr auto kMinor = "minor"sv;
22inline constexpr auto kPatch = "patch"sv;
23}
24
28struct Version
29{
30 int major;
31 int minor;
32 std::optional<int> patch;
33
34 [[nodiscard]] constexpr bool operator== (const Version &other) const
35 {
36 return major == other.major && minor == other.minor
37 && patch.value_or (0) == other.patch.value_or (0);
38 }
39
40 [[nodiscard]] constexpr bool operator!= (const Version &other) const
41 {
42 return !(*this == other);
43 }
44
45 [[nodiscard]] constexpr bool operator< (const Version &other) const
46 {
47 if (major != other.major)
48 return major < other.major;
49 if (minor != other.minor)
50 return minor < other.minor;
51 auto this_patch = patch.value_or (0);
52 auto other_patch = other.patch.value_or (0);
53 return this_patch < other_patch;
54 }
55
56 [[nodiscard]] constexpr bool operator> (const Version &other) const
57 {
58 return other < *this;
59 }
60
61 [[nodiscard]] constexpr bool operator<= (const Version &other) const
62 {
63 return !(other < *this);
64 }
65
66 [[nodiscard]] constexpr bool operator>= (const Version &other) const
67 {
68 return !(*this < other);
69 }
70};
71
72inline void
73to_json (nlohmann::json &j, const Version &v)
74{
75 using namespace version_keys;
76 j = nlohmann::json::object ();
77 j[kMajor] = v.major;
78 j[kMinor] = v.minor;
79 if (v.patch.has_value ())
80 {
81 j[kPatch] = *v.patch;
82 }
83}
84
85inline void
86from_json (const nlohmann::json &j, Version &v)
87{
88 using namespace version_keys;
89 v.major = j.at (kMajor).get<int> ();
90 v.minor = j.at (kMinor).get<int> ();
91 if (j.contains (kPatch))
92 {
93 v.patch = j[kPatch].get<int> ();
94 }
95 else
96 {
97 v.patch = std::nullopt;
98 }
99}
100
101} // namespace zrythm::utils
Key constants for version JSON serialization.
Definition version.h:18
String utilities.
Definition algorithms.h:12
Represents a semantic version with major, minor, and optional patch.
Definition version.h:29