github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/common/version.h (about) 1 // Copyright (C) 2018 go-nebulas authors 2 // 3 // This file is part of the go-nebulas library. 4 // 5 // the go-nebulas library is free software: you can redistribute it and/or 6 // modify 7 // it under the terms of the GNU 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 // the go-nebulas library 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 General Public License for more details. 15 // 16 // You should have received a copy of the GNU General Public License 17 // along with the go-nebulas library. If not, see 18 // <http://www.gnu.org/licenses/>. 19 // 20 #pragma once 21 #include "common/common.h" 22 23 namespace neb { 24 class version { 25 public: 26 inline version() { m_data.m_data = 0; } 27 version(uint64_t data) { m_data.m_data = data; }; 28 version(uint32_t major_version, uint16_t minor_version, 29 uint16_t patch_version) { 30 m_data.m_detail.m_major_version = major_version; 31 m_data.m_detail.m_minor_version = minor_version; 32 m_data.m_detail.m_patch_version = patch_version; 33 } 34 35 version(const version &) = default; 36 version &operator=(const version &) = default; 37 38 inline uint32_t major_version() const { 39 return m_data.m_detail.m_major_version; 40 } 41 inline uint16_t minor_version() const { 42 return m_data.m_detail.m_minor_version; 43 } 44 inline uint16_t patch_version() const { 45 return m_data.m_detail.m_patch_version; 46 } 47 inline uint32_t &major_version() { return m_data.m_detail.m_major_version; } 48 inline uint16_t &minor_version() { return m_data.m_detail.m_minor_version; } 49 inline uint16_t &patch_version() { return m_data.m_detail.m_patch_version; } 50 51 inline uint64_t data() const { return m_data.m_data; } 52 53 inline version &from_string(const std::string &str) { 54 std::vector<int> vs; 55 std::istringstream f(str); 56 std::string s; 57 while (std::getline(f, s, '.')) { 58 vs.push_back(std::atoi(s.c_str())); 59 } 60 if (vs.size() != 3) { 61 throw std::invalid_argument("invalid version string"); 62 } 63 m_data.m_detail.m_major_version = vs[0]; 64 m_data.m_detail.m_minor_version = vs[1]; 65 m_data.m_detail.m_patch_version = vs[2]; 66 return *this; 67 } 68 69 friend inline std::ostream &operator<<(std::ostream &stream, 70 const version &v) { 71 stream << v.m_data.m_detail.m_major_version << ',' 72 << v.m_data.m_detail.m_minor_version << ',' 73 << v.m_data.m_detail.m_patch_version; 74 return stream; 75 } 76 77 friend inline bool operator<(const version &v1, const version &v2) { 78 if (v1.m_data.m_detail.m_major_version < 79 v2.m_data.m_detail.m_major_version) { 80 return true; 81 } else if (v1.m_data.m_detail.m_minor_version < 82 v2.m_data.m_detail.m_minor_version) { 83 return true; 84 } else if (v1.m_data.m_detail.m_patch_version < 85 v2.m_data.m_detail.m_patch_version) { 86 return true; 87 } 88 return false; 89 } 90 91 friend inline bool operator>(const version &v1, const version &v2) { 92 return v2 < v1; 93 } 94 95 friend inline bool operator>=(const version &v1, const version &v2) { 96 return !(v1 < v2); 97 } 98 99 friend inline bool operator<=(const version &v1, const version &v2) { 100 return !(v1 > v2); 101 } 102 103 friend inline bool operator==(const version &v1, const version &v2) { 104 return v1 >= v2 && v2 >= v1; 105 } 106 107 friend inline bool operator!=(const version &v1, const version &v2) { 108 return !(v1 == v2); 109 } 110 111 112 protected: 113 union _version_data { 114 uint64_t m_data; 115 struct _version_detail { 116 uint32_t m_major_version; 117 uint16_t m_minor_version; 118 uint16_t m_patch_version; 119 }; 120 _version_detail m_detail; 121 }; 122 123 _version_data m_data; 124 }; // end class version 125 }