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 Version(int major = 0, int minor = 0) noexcept // NOLINT(hicpp-explicit-conversions)
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
68TOOLBOX_API std::ostream& operator<<(std::ostream& os, Version ver);
69
70template <>
72 static auto from_string(std::string_view sv) noexcept
73 {
74 const auto [major, minor] = split_pair(sv, '.');
76 }
77 static auto from_string(const std::string& s) noexcept
78 {
79 return from_string(std::string_view{s});
80 }
81};
82
83inline std::size_t hash_value(toolbox::Version ver)
84{
85 std::size_t h{0};
86 boost::hash_combine(h, ver.major);
87 boost::hash_combine(h, ver.minor);
88 return h;
89}
90
91} // namespace util
92} // namespace toolbox
93
94namespace std {
95template <>
96struct hash<toolbox::Version> {
97 inline std::size_t operator()(toolbox::Version ver) const { return hash_value(ver); }
98};
99} // namespace std
100
101#endif // TOOLBOX_UTIL_VERSION_HPP
#define TOOLBOX_API
Definition Config.h:39
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, char delim) noexcept
Definition String.cpp:51
std::size_t hash_value(IntWrapper< PolicyT > wrapper)
Definition IntTypes.hpp:260
constexpr int dec_digits(std::int64_t i) noexcept
Definition Utility.hpp:49
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:97
static auto from_string(std::string_view sv) noexcept
Definition Version.hpp:72
static auto from_string(const std::string &s) noexcept
Definition Version.hpp:77
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