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.
36 constexpr bool empty() const noexcept
37 {
38 return major == 0 && minor == 0;
39 }
40
41 /// Convert this instance into a \c uint64_t. The \c major version will be in the higher-order bits, while \c minor
42 /// will be in the lower-order bits.
43 explicit constexpr operator std::uint64_t() const
44 {
45 return static_cast<std::uint64_t>(major) << 32
46 | static_cast<std::uint64_t>(minor) << 0;
47 }
48
49 /// Test for equality with \a other.
50 constexpr bool operator==(const version& other) const
51 {
52 return static_cast<std::uint64_t>(*this) == static_cast<std::uint64_t>(other);
53 }
54
55 /// Test for inequality with \a other.
56 constexpr bool operator!=(const version& other) const
57 {
58 return static_cast<std::uint64_t>(*this) != static_cast<std::uint64_t>(other);
59 }
60
61 /// Check that this version is less than \a other. The comparison is done lexicographically.
62 constexpr bool operator<(const version& other) const
63 {
64 return static_cast<std::uint64_t>(*this) < static_cast<std::uint64_t>(other);
65 }
66
67 /// Check that this version is less than or equal to \a other. The comparison is done lexicographically.
68 constexpr bool operator<=(const version& other) const
69 {
70 return static_cast<std::uint64_t>(*this) <= static_cast<std::uint64_t>(other);
71 }
72
73 /// Check that this version is greater than \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 or equal to \a other. The comparison is done lexicographically.
80 constexpr bool operator>=(const version& other) const
81 {
82 return static_cast<std::uint64_t>(*this) >= static_cast<std::uint64_t>(other);
83 }
84
85public:
86 version_element major;
87 version_element minor;
88};
89
90JSONV_PUBLIC std::ostream& operator<<(std::ostream&, const version&);
91
92}
An adapter for enumeration types.
Copyright (c) 2014-2020 by Travis Gockel.
#define JSONV_PUBLIC
This function or class is part of the public API for JSON Voorhees.
Definition config.hpp:102
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:68
constexpr bool operator==(const version &other) const
Test for equality with other.
Definition version.hpp:50
constexpr bool operator<(const version &other) const
Check that this version is less than other. The comparison is done lexicographically.
Definition version.hpp:62
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:80
constexpr bool operator>(const version &other) const
Check that this version is greater than other. The comparison is done lexicographically.
Definition version.hpp:74
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:36
constexpr bool operator!=(const version &other) const
Test for inequality with other.
Definition version.hpp:56