|
| 1 | +/** |
| 2 | + * Copyright (c) 2011-2025 libbitcoin developers (see AUTHORS) |
| 3 | + * |
| 4 | + * This file is part of libbitcoin. |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Affero General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Affero General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | +#include <bitcoin/system/config/version.hpp> |
| 20 | + |
| 21 | +#include <algorithm> |
| 22 | +#include <sstream> |
| 23 | +#include <bitcoin/system/data/data.hpp> |
| 24 | +#include <bitcoin/system/define.hpp> |
| 25 | +#include <bitcoin/system/math/math.hpp> |
| 26 | +#include <bitcoin/system/serial/serial.hpp> |
| 27 | + |
| 28 | +namespace libbitcoin { |
| 29 | +namespace system { |
| 30 | +namespace config { |
| 31 | + |
| 32 | +// Contructors. |
| 33 | +// ---------------------------------------------------------------------------- |
| 34 | + |
| 35 | +// Default version is 0.0.0.0 and serializes as "0.0". |
| 36 | +version::version() NOEXCEPT |
| 37 | + : version(0, 0, 0, 0) |
| 38 | +{ |
| 39 | +} |
| 40 | + |
| 41 | +version::version(const std::string& version) THROWS |
| 42 | + : version() |
| 43 | +{ |
| 44 | + std::istringstream(version) >> (*this); |
| 45 | +} |
| 46 | + |
| 47 | +version::version(uint32_t major, uint32_t minor, uint32_t subminor, |
| 48 | + uint32_t patch) NOEXCEPT |
| 49 | + : segments_{ major, minor, subminor, patch } |
| 50 | +{ |
| 51 | +} |
| 52 | + |
| 53 | +// Properties. |
| 54 | +// ---------------------------------------------------------------------------- |
| 55 | + |
| 56 | +const std::array<uint32_t, 4>& version::segments() const NOEXCEPT |
| 57 | +{ |
| 58 | + return segments_; |
| 59 | +} |
| 60 | + |
| 61 | +// Methods. |
| 62 | +// ---------------------------------------------------------------------------- |
| 63 | + |
| 64 | +bool version::is_default() const NOEXCEPT |
| 65 | +{ |
| 66 | + constexpr std::array<uint32_t, 4> default_{}; |
| 67 | + return segments_ == default_; |
| 68 | +} |
| 69 | + |
| 70 | +std::string version::to_string() const NOEXCEPT |
| 71 | +{ |
| 72 | + std::ostringstream value{}; |
| 73 | + value << (*this); |
| 74 | + BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT) |
| 75 | + return value.str(); |
| 76 | + BC_POP_WARNING() |
| 77 | +} |
| 78 | + |
| 79 | +// Operators. |
| 80 | +// ---------------------------------------------------------------------------- |
| 81 | + |
| 82 | +bool operator==(const version& left, const version& right) NOEXCEPT |
| 83 | +{ |
| 84 | + return left.segments() == right.segments(); |
| 85 | +} |
| 86 | + |
| 87 | +bool operator!=(const version& left, const version& right) NOEXCEPT |
| 88 | +{ |
| 89 | + return !(left == right); |
| 90 | +} |
| 91 | + |
| 92 | +bool operator<(const version& left, const version& right) NOEXCEPT |
| 93 | +{ |
| 94 | + return std::lexicographical_compare( |
| 95 | + left.segments().begin(), left.segments().end(), |
| 96 | + right.segments().begin(), right.segments().end()); |
| 97 | +} |
| 98 | + |
| 99 | +bool operator<=(const version& left, const version& right) NOEXCEPT |
| 100 | +{ |
| 101 | + return left < right || left == right; |
| 102 | +} |
| 103 | + |
| 104 | +bool operator>(const version& left, const version& right) NOEXCEPT |
| 105 | +{ |
| 106 | + return right < left; |
| 107 | +} |
| 108 | + |
| 109 | +bool operator>=(const version& left, const version& right) NOEXCEPT |
| 110 | +{ |
| 111 | + return right <= left; |
| 112 | +} |
| 113 | + |
| 114 | +std::istream& operator>>(std::istream& input, |
| 115 | + version& argument) THROWS |
| 116 | +{ |
| 117 | + std::string value; |
| 118 | + input >> value; |
| 119 | + |
| 120 | + using namespace system; |
| 121 | + const auto tokens = system::split(value, "."); |
| 122 | + const auto count = tokens.size(); |
| 123 | + if (count < 2u || count > 4u) |
| 124 | + throw istream_exception(value); |
| 125 | + |
| 126 | + argument = {}; |
| 127 | + for (size_t index{}; index < count; ++index) |
| 128 | + if (!deserialize(argument.segments_.at(index), tokens.at(index))) |
| 129 | + throw istream_exception(value); |
| 130 | + |
| 131 | + return input; |
| 132 | +} |
| 133 | + |
| 134 | +std::ostream& operator<<(std::ostream& output, |
| 135 | + const version& argument) NOEXCEPT |
| 136 | +{ |
| 137 | + size_t last{ 3 }; |
| 138 | + while (last > 1u && is_zero(argument.segments_.at(last))) |
| 139 | + --last; |
| 140 | + |
| 141 | + for (size_t index{}; index <= last; ++index) |
| 142 | + { |
| 143 | + BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT) |
| 144 | + if (!is_zero(index)) output << '.'; |
| 145 | + output << argument.segments_.at(index); |
| 146 | + BC_POP_WARNING() |
| 147 | + } |
| 148 | + |
| 149 | + return output; |
| 150 | +} |
| 151 | + |
| 152 | +} // namespace config |
| 153 | +} // namespace system |
| 154 | +} // namespace libbitcoin |
0 commit comments