|
| 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 | +#ifndef LIBBITCOIN_SYSTEM_CONFIG_VERSION_HPP |
| 20 | +#define LIBBITCOIN_SYSTEM_CONFIG_VERSION_HPP |
| 21 | + |
| 22 | +#include <memory> |
| 23 | +#include <bitcoin/system/define.hpp> |
| 24 | + |
| 25 | +namespace libbitcoin { |
| 26 | +namespace system { |
| 27 | +namespace config { |
| 28 | + |
| 29 | +/// Container for a protocol version tuple with up to four segments. |
| 30 | +/// Segments are non-negative integers, padded with zeros for fewer than four. |
| 31 | +/// The format is major.minor[.subminor[.patch]] with at least one dot. |
| 32 | +class BC_API version |
| 33 | +{ |
| 34 | +public: |
| 35 | + typedef std::shared_ptr<version> ptr; |
| 36 | + |
| 37 | + DEFAULT_COPY_MOVE_DESTRUCT(version); |
| 38 | + |
| 39 | + version() NOEXCEPT; |
| 40 | + |
| 41 | + /// Deserialize from dotted string (throws on invalid format). |
| 42 | + version(const std::string& version_str) THROWS; |
| 43 | + |
| 44 | + /// Construct from individual segments (2 to 4, pads with zeros). |
| 45 | + version(uint32_t major, uint32_t minor, uint32_t subminor={}, |
| 46 | + uint32_t patch={}) NOEXCEPT; |
| 47 | + |
| 48 | + /// Properties. |
| 49 | + /// ----------------------------------------------------------------------- |
| 50 | + |
| 51 | + /// Access to the internal segments array. |
| 52 | + const std::array<uint32_t, 4>& segments() const NOEXCEPT; |
| 53 | + |
| 54 | + /// Methods. |
| 55 | + /// ----------------------------------------------------------------------- |
| 56 | + |
| 57 | + /// The version is 0.0.0.0. |
| 58 | + bool is_default() const NOEXCEPT; |
| 59 | + |
| 60 | + /// Serialize to dot string, omitting trailing zero segments (minimum 2). |
| 61 | + std::string to_string() const NOEXCEPT; |
| 62 | + |
| 63 | + /// Operators. |
| 64 | + /// ----------------------------------------------------------------------- |
| 65 | + |
| 66 | + /// Deserialize from input stream (throws on invalid format). |
| 67 | + friend std::istream& operator>>(std::istream& input, version& argument) THROWS; |
| 68 | + |
| 69 | + /// Serialize to output stream. |
| 70 | + friend std::ostream& operator<<(std::ostream& output, |
| 71 | + const version& argument) NOEXCEPT; |
| 72 | + |
| 73 | +private: |
| 74 | + // This is not thread safe. |
| 75 | + std::array<uint32_t, 4> segments_; |
| 76 | +}; |
| 77 | + |
| 78 | +/// Lexicographical comparison. |
| 79 | +BC_API bool operator==(const version& left, const version& right) NOEXCEPT; |
| 80 | +BC_API bool operator!=(const version& left, const version& right) NOEXCEPT; |
| 81 | +BC_API bool operator<(const version& left, const version& right) NOEXCEPT; |
| 82 | +BC_API bool operator<=(const version& left, const version& right) NOEXCEPT; |
| 83 | +BC_API bool operator>(const version& left, const version& right) NOEXCEPT; |
| 84 | +BC_API bool operator>=(const version& left, const version& right) NOEXCEPT; |
| 85 | + |
| 86 | +typedef std::vector<version> versions; |
| 87 | + |
| 88 | +} // namespace config |
| 89 | +} // namespace system |
| 90 | +} // namespace libbitcoin |
| 91 | + |
| 92 | +namespace std |
| 93 | +{ |
| 94 | +template<> |
| 95 | +struct hash<bc::system::config::version> |
| 96 | +{ |
| 97 | + size_t operator()(const bc::system::config::version& value) const NOEXCEPT |
| 98 | + { |
| 99 | + return std::hash<std::string>{}(value.to_string()); |
| 100 | + } |
| 101 | +}; |
| 102 | +} // namespace std |
| 103 | + |
| 104 | +#endif |
0 commit comments