MiKTeX 26.5-next
A scalable TeX distribution
Loading...
Searching...
No Matches
VersionNumber.h
1/* miktex/Core/VersionNumber.h: -*- C++ -*-
2
3 Copyright (C) 1996-2020 Christian Schenk
4
5 This file is part of the MiKTeX Core Library.
6
7 The MiKTeX Core Library is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2, or
10 (at your option) any later version.
11
12 The MiKTeX Core Library is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with the MiKTeX Core Library; if not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22#pragma once
23
24#if !defined(EED99FD2335944DC974769575687D215)
25#define EED99FD2335944DC974769575687D215
26
27#include <miktex/Core/config.h>
28
29#include <ostream>
30#include <string>
31
32MIKTEX_CORE_BEGIN_NAMESPACE;
33
35struct VersionNumber
36{
37public:
38 VersionNumber() = default;
39
40public:
41 VersionNumber(const VersionNumber& other) = default;
42
43public:
44 VersionNumber& operator=(const VersionNumber& other) = default;
45
46public:
47 VersionNumber(VersionNumber&& other) = default;
48
49public:
50 VersionNumber& operator=(VersionNumber&& other) = default;
51
52public:
53 virtual ~VersionNumber() = default;
54
60public:
61 VersionNumber(unsigned n1, unsigned n2, unsigned n3, unsigned n4) :
62 n1(n1),
63 n2(n2),
64 n3(n3),
65 n4(n4)
66 {
67 }
68
69public:
70 VersionNumber(long highWord, long lowWord) :
71 n1((static_cast<unsigned long>(highWord) >> 16) & 0xffff),
72 n2(static_cast<unsigned long>(highWord) & 0xffff),
73 n3((static_cast<unsigned long>(lowWord) >> 16) & 0xffff),
74 n4(static_cast<unsigned long>(lowWord) & 0xffff)
75 {
76 }
77
80public:
81 explicit VersionNumber(const std::string& versionString)
82 {
83 *this = Parse(versionString);
84 }
85
88public:
89 explicit VersionNumber(const char* versionString)
90 {
91 *this = Parse(versionString);
92 }
93
95public:
96 unsigned n1 = 0;
97
99public:
100 unsigned n2 = 0;
101
103public:
104 unsigned n3 = 0;
105
107public:
108 unsigned n4 = 0;
109
112public:
113 long GetHighWord() const
114 {
115 unsigned long w = (n2 & 0xffff);
116 w |= static_cast<unsigned long>(n1 & 0xffff) << 16;
117 return static_cast<long>(w);
118 }
119
122public:
123 long GetLowWord() const
124 {
125 unsigned long w = (n4 & 0xffff);
126 w |= static_cast<unsigned long>(n3 & 0xffff) << 16;
127 return static_cast<long>(w);
128 }
129
135public:
136 int CompareTo(const VersionNumber& other) const
137 {
138 long cmp1 = GetHighWord() - other.GetHighWord();
139 long cmp2 = GetLowWord() - other.GetLowWord();
140 if (cmp1 < 0 || (cmp1 == 0 && cmp2 < 0))
141 {
142 return -1;
143 }
144 else if (cmp1 > 0 || (cmp1 == 0 && cmp2 > 0))
145 {
146 return 1;
147 }
148 else
149 {
150 return 0;
151 }
152 }
153
156public:
157 MIKTEXCORETHISAPI(std::string) ToString() const;
158
163public:
164 static MIKTEXCORECEEAPI(bool) TryParse(const std::string& versionString, VersionNumber& versionNumber);
165
169public:
170 static MIKTEXCORECEEAPI(VersionNumber) Parse(const std::string& versionString);
171};
172
177inline bool operator==(const VersionNumber& v1, const VersionNumber& v2)
178{
179 return v1.CompareTo(v2) == 0;
180}
181
187inline bool operator<(const VersionNumber& v1, const VersionNumber& v2)
188{
189 return v1.CompareTo(v2) < 0;
190}
191
192inline std::ostream& operator<<(std::ostream& os, const VersionNumber& v)
193{
194 return os << v.ToString();
195}
196
197MIKTEX_CORE_END_NAMESPACE;
198
199#endif
bool operator==(const VersionNumber &v1, const VersionNumber &v2)
Definition VersionNumber.h:177
An instance of this struct represents a version number.
Definition VersionNumber.h:36
unsigned n4
The fourth part of the version nimber. Usually 0.
Definition VersionNumber.h:108
unsigned n2
The second part of the version number. Usually the minor version.
Definition VersionNumber.h:100
unsigned n1
The first part of the version number. Usually the major version.
Definition VersionNumber.h:96
long GetHighWord() const
Definition VersionNumber.h:113
int CompareTo(const VersionNumber &other) const
Definition VersionNumber.h:136
VersionNumber(unsigned n1, unsigned n2, unsigned n3, unsigned n4)
Definition VersionNumber.h:61
VersionNumber(const char *versionString)
Definition VersionNumber.h:89
long GetLowWord() const
Definition VersionNumber.h:123
VersionNumber(const std::string &versionString)
Definition VersionNumber.h:81
static VersionNumber Parse(const std::string &versionString)
unsigned n3
The third part of the version number. Usually the j2000 number.
Definition VersionNumber.h:104
std::string ToString() const
static bool TryParse(const std::string &versionString, VersionNumber &versionNumber)