JSON Voorhees
Killer JSON for C++
Loading...
Searching...
No Matches
version.hpp
1/// \file jsonv/serialization.hpp
2/// Conversion between C++ types and JSON values.
3///
4/// Copyright (c) 2015-2020 by Travis Gockel. All rights reserved.
5///
6/// This program is free software: you can redistribute it and/or modify it under the terms of the Apache License
7/// as published by the Apache Software Foundation, either version 2 of the License, or (at your option) any later
8/// version.
9///
10/// \author Travis Gockel (travis@gockelhut.com)
11#pragma once
12
13#include <jsonv/config.hpp>
14
15#include <cstdint>
16#include <iosfwd>
17
18namespace jsonv
19{
20
21/// Represents a version used to extract and encode JSON objects from C++ classes. This is useful for API versioning: if
22/// you need certain fields to be serialized only after a certain version or only before a different one.
24{
25public:
26 using version_element = std::uint32_t;
27
28public:
29 /// Initialize an instance with the given \a major and \a minor version info.
30 constexpr version(version_element major = 0, version_element minor = 0) noexcept :
31 major{major},
32 minor{minor}
33 { }
34
35 /// Check if this version is an "empty" value -- meaning \c major and \c minor are both \c 0.
37 constexpr bool empty() const noexcept
38 {
39 return major == 0 && minor == 0;
40 }
41
42 /// Convert this instance into a \c uint64_t. The \c major version will be in the higher-order bits, while \c minor
43 /// will be in the lower-order bits.
45 explicit constexpr operator std::uint64_t() const
46 {
47 return static_cast<std::uint64_t>(major) << 32
48 | static_cast<std::uint64_t>(minor) << 0;
49 }
50
51 /// Test for equality with \a other.
53 constexpr bool operator==(const version& other) const
54 {
55 return static_cast<std::uint64_t>(*this) == static_cast<std::uint64_t>(other);
56 }
57
58 /// Test for inequality with \a other.
60 constexpr bool operator!=(const version& other) const
61 {
62 return static_cast<std::uint64_t>(*this) != static_cast<std::uint64_t>(other);
63 }
64
65 /// Check that this version is less than \a other. The comparison is done lexicographically.
67 constexpr bool operator<(const version& other) const
68 {
69 return static_cast<std::uint64_t>(*this) < static_cast<std::uint64_t>(other);
70 }
71
72 /// Check that this version is less than or equal to \a other. The comparison is done lexicographically.
74 constexpr bool operator<=(const version& other) const
75 {
76 return static_cast<std::uint64_t>(*this) <= static_cast<std::uint64_t>(other);
77 }
78
79 /// Check that this version is greater than \a other. The comparison is done lexicographically.
81 constexpr bool operator>(const version& other) const
82 {
83 return static_cast<std::uint64_t>(*this) > static_cast<std::uint64_t>(other);
84 }
85
86 /// Check that this version is greater than or equal to \a other. The comparison is done lexicographically.
88 constexpr bool operator>=(const version& other) const
89 {
90 return static_cast<std::uint64_t>(*this) >= static_cast<std::uint64_t>(other);
91 }
92
93public:
94 version_element major;
95 version_element minor;
96};
97
98JSONV_PUBLIC std::ostream& operator<<(std::ostream&, const version&);
99
100}
Copyright (c) 2014-2020 by Travis Gockel.
#define JSONV_NODISCARD
Warn if the caller discards the result of this function.
Definition config.hpp:132
#define JSONV_PUBLIC
This function or class is part of the public API for JSON Voorhees.
Definition config.hpp:113
Represents a version used to extract and encode JSON objects from C++ classes.
Definition version.hpp:24
constexpr bool operator<=(const version &other) const
Check that this version is less than or equal to other. The comparison is done lexicographically.
Definition version.hpp:74
constexpr bool operator==(const version &other) const
Test for equality with other.
Definition version.hpp:53
constexpr bool operator<(const version &other) const
Check that this version is less than other. The comparison is done lexicographically.
Definition version.hpp:67
constexpr bool operator>=(const version &other) const
Check that this version is greater than or equal to other. The comparison is done lexicographically.
Definition version.hpp:88
constexpr bool operator>(const version &other) const
Check that this version is greater than other. The comparison is done lexicographically.
Definition version.hpp:81
constexpr version(version_element major=0, version_element minor=0) noexcept
Initialize an instance with the given major and minor version info.
Definition version.hpp:30
constexpr bool empty() const noexcept
Check if this version is an "empty" value – meaning major and minor are both 0.
Definition version.hpp:37
constexpr bool operator!=(const version &other) const
Test for inequality with other.
Definition version.hpp:60