-
Notifications
You must be signed in to change notification settings - Fork 1
/
version.hpp
91 lines (77 loc) · 1.9 KB
/
version.hpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#ifndef FLUKS_VERSION_HPP
#define FLUKS_VERSION_HPP
#include <algorithm>
#include <sstream>
#include <vector>
namespace fluks {
const unsigned VERSION_BUF[] = { 0, 2 };
class Version {
public:
/** Construct the current version of fluks. */
Version() :
_version(VERSION_BUF,
VERSION_BUF + sizeof VERSION_BUF / sizeof *VERSION_BUF)
{
init_str();
}
/** Create a Version from an iterator. */
template <typename In>
Version(In begin, In end) :
_version(begin, end)
{
init_str();
}
/** Create a Version from a buffer. */
Version(const unsigned *version, size_t size) :
_version(version, version + size)
{
init_str();
}
/** Get the version as a string. */
const std::string &str() const
{ return _str; }
/** Get the version as a vector. */
const std::vector<unsigned> &vec() const
{ return _version; }
bool operator<(const Version &rhs) const
{ return cmp(rhs) < 0; }
bool operator>(const Version &rhs) const
{ return cmp(rhs) > 0; }
bool operator<=(const Version &rhs) const
{ return cmp(rhs) <= 0; }
bool operator>=(const Version &rhs) const
{ return cmp(rhs) >= 0; }
bool operator==(const Version &rhs) const
{ return cmp(rhs) == 0; }
bool operator!=(const Version &rhs) const
{ return cmp(rhs) != 0; }
private:
int cmp(const Version &rhs) const
{
size_t min = _version.size() < rhs._version.size() ?
_version.size() : rhs._version.size();
for (size_t i = 0; i < min; i++) {
if (_version[i] < rhs._version[i]) return -1;
if (rhs._version[i] < _version[i]) return 1;
}
// if rhs._version is longer, *this < rhs
if (min != rhs._version.size()) return -1;
if (min != _version.size()) return 1;
return 0;
}
void init_str()
{
std::ostringstream out;
bool first = true;
for (unsigned num : _version) {
if (!first) out << '.';
first = false;
out << num;
}
_str = out.str();
}
std::string _str;
std::vector<unsigned> _version;
};
}
#endif