forked from cculianu/Fulcrum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVersion.h
59 lines (51 loc) · 2.96 KB
/
Version.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//
// Fulcrum - A fast & nimble SPV Server for Bitcoin Cash
// Copyright (C) 2019-2020 Calin A. Culianu <calin.culianu@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program (see LICENSE.txt). If not, see
// <https://www.gnu.org/licenses/>.
//
#pragma once
#include "Common.h"
#include <QString>
#include <tuple> // for std::tie
/// A class that encapsulates a version, be it a protocol version or a bitcoind version. Basically something that
/// can compare "1.0" and "1.2.1" and come up with a lexical comparison (which is what the tuple can do, but if you
/// inherit from tuple you lose structured binding!) -- this is like a tuple basically, with added niceties like
/// defining an "invalid" version (0.0.0), and a .toString(). Can be used with C++17 structured binding, eg:
/// auto [major, minor, rev] = version;
struct Version
{
unsigned major = 0, minor = 0, revision = 0;
/// constructs an invalid version (0,0,0)
Version() = default;
Version(unsigned maj, unsigned min, unsigned rev);
enum CompactType { BitcoinD };
/// To explicitly construct an instance from the version number returned by bitcoind's getnetworkinfo RPC call
/// e.g.: 200600 becomes -> 0.20.6
explicit Version(unsigned compactVersion, CompactType);
/// Accepts e.g. "1.0" or "v1.7". Note that strings like "3.1.1.1" or "3.1.1CS" become "3,1,1"
/// (extra stuff at the end is ignored). An initial 'v' or 'V' character is also ok and simply ignored.
/// Also note: If the string contains a '/' character, everything *after* the first '/' is parsed!
Version(const QString & versionString);
/// back to e.g. "1.2.3". Note that 1.0.0 is returned as simply "1.0" even if originally parsed from "1.0.0",
/// unless alwaysIncludeRevEvenIfZero = true.
QString toString(bool alwaysIncludeRevEvenIfZero = false) const;
constexpr bool isValid() const { return major || minor || revision; }
constexpr bool operator==(const Version & o) const { return std::tie(major, minor, revision) == std::tie(o.major, o.minor, o.revision); }
constexpr bool operator<(const Version & o) const { return std::tie(major, minor, revision) < std::tie(o.major, o.minor, o.revision); }
constexpr bool operator<=(const Version & o) const { return *this < o || *this == o; }
constexpr bool operator>(const Version & o) const { return !(*this <= o); }
constexpr bool operator>=(const Version & o) const { return !(*this < o); }
};