Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Version.hpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2021 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#ifndef TOOLBOX_UTIL_VERSION_HPP
18#define TOOLBOX_UTIL_VERSION_HPP
19
21
22#include <boost/functional/hash.hpp>
23
24namespace toolbox {
25inline namespace util {
26
27struct Version {
28 constexpr explicit Version(int major = 0, int minor = 0) noexcept
29 : major{major}
30 , minor{minor}
31 {
32 }
33 ~Version() = default;
34
35 // Copy.
36 constexpr Version(const Version&) noexcept = default;
37 constexpr Version& operator=(const Version&) noexcept = default;
38
39 // Move.
42
43 constexpr bool empty() const noexcept { return major == 0 && minor == 0; }
45 constexpr std::size_t size() const noexcept
46 {
47 return dec_digits(major) + dec_digits(minor) + 1;
48 }
49 constexpr explicit operator bool() const noexcept { return !empty(); }
50
51 void clear() noexcept { major = minor = 0; }
52 void swap(Version& rhs) noexcept
53 {
54 std::swap(major, rhs.major);
55 std::swap(minor, rhs.minor);
56 }
57
58 constexpr auto operator<=>(const Version&) const noexcept = default;
59 constexpr bool operator==(const Version&) const noexcept = default;
60
61 int major{0}, minor{0};
62};
63
64static_assert(Version{1, 2} == Version{1, 2});
65static_assert(Version{1, 2} < Version{1, 3});
66static_assert(Version{1, 2} < Version{2, 2});
67
68template <typename StreamT>
69 requires Streamable<StreamT>
71{
72 os << ver.major << '.' << ver.minor;
73 return os;
74}
75
76template <>
78 static auto from_string(std::string_view sv) noexcept
79 {
80 const auto [major, minor] = split_pair(sv, '.');
82 }
83 static auto from_string(const std::string& s) noexcept
84 {
85 return from_string(std::string_view{s});
86 }
87};
88
89inline std::size_t hash_value(toolbox::Version ver)
90{
91 std::size_t h{0};
92 boost::hash_combine(h, ver.major);
93 boost::hash_combine(h, ver.minor);
94 return h;
95}
96
97} // namespace util
98} // namespace toolbox
99
100namespace std {
101template <>
102struct hash<toolbox::Version> {
103 inline std::size_t operator()(toolbox::Version ver) const { return hash_value(ver); }
104};
105} // namespace std
106
107#endif // TOOLBOX_UTIL_VERSION_HPP
STL namespace.
ostream & operator<<(ostream &os, const pair< T, U > &p)
Definition Parser.ut.cpp:29
pair< string_view, string_view > split_pair(string_view s, string_view delim) noexcept
Definition String.cpp:52
std::size_t hash_value(IntWrapper< PolicyT > wrapper)
Definition IntTypes.hpp:264
constexpr int dec_digits(ValueT i) noexcept
Definition Utility.hpp:52
std::string_view sv
Definition Tokeniser.hpp:26
constexpr auto bind() noexcept
Definition Slot.hpp:92
std::size_t operator()(toolbox::Version ver) const
Definition Version.hpp:103
static auto from_string(std::string_view sv) noexcept
Definition Version.hpp:78
static auto from_string(const std::string &s) noexcept
Definition Version.hpp:83
static constexpr auto from_string(StringT &&s) noexcept(noexcept(ValueT{s}))
constexpr bool empty() const noexcept
Definition Version.hpp:43
constexpr Version(Version &&) noexcept=default
constexpr Version & operator=(const Version &) noexcept=default
constexpr Version(const Version &) noexcept=default
constexpr bool operator==(const Version &) const noexcept=default
constexpr Version(int major=0, int minor=0) noexcept
Definition Version.hpp:28
constexpr auto operator<=>(const Version &) const noexcept=default
void clear() noexcept
Definition Version.hpp:51
void swap(Version &rhs) noexcept
Definition Version.hpp:52
constexpr std::size_t size() const noexcept
Returns the length of the equivalent string representation.
Definition Version.hpp:45