rogue
Loading...
Searching...
No Matches
Version.cpp
Go to the documentation of this file.
1
17#include "rogue/Directives.h"
18
19#include "rogue/Version.h"
20
21#include <RogueConfig.h>
22#include <inttypes.h>
23#include <unistd.h>
24
25#include <cstdio>
26#include <mutex>
27#include <sstream>
28#include <string>
29
30#include "rogue/GeneralError.h"
31#include "rogue/GilRelease.h"
32
33#ifndef NO_PYTHON
34 #include <boost/python.hpp>
35namespace bp = boost::python;
36#endif
37
38const char rogue::Version::_version[] = ROGUE_VERSION;
39std::atomic<uint32_t> rogue::Version::_major{0};
40std::atomic<uint32_t> rogue::Version::_minor{0};
41std::atomic<uint32_t> rogue::Version::_maint{0};
42std::atomic<uint32_t> rogue::Version::_devel{0};
43
44void rogue::Version::init() {
45 static std::once_flag flag_;
46
47 std::call_once(flag_, []() {
48 char dump[100];
49 char lead;
50 uint32_t maj = 0, min = 0, mnt = 0, dev = 0;
51 int32_t ret = sscanf(_version,
52 "%c%" SCNu32 ".%" SCNu32 ".%" SCNu32 "-%" SCNu32 "-%99s",
53 &lead,
54 &maj,
55 &min,
56 &mnt,
57 &dev,
58 dump);
59
60 if ((ret != 4 && ret != 6) || (lead != 'v' && lead != 'V'))
61 throw(rogue::GeneralError("Version:init", "Invalid compiled version string"));
62
63 _major.store(maj);
64 _minor.store(min);
65 _maint.store(mnt);
66 _devel.store(dev);
67 });
68}
69
70void rogue::Version::extract(const std::string& compare, uint32_t* major, uint32_t* minor, uint32_t* maint) {
71 if (sscanf(compare.c_str(), "%" PRIu32 ".%" PRIu32 ".%" PRIu32, major, minor, maint) != 3)
72 throw(rogue::GeneralError("Version:extract", "Invalid version string"));
73}
74
76 std::string ret = _version;
77 return ret;
78}
79
80bool rogue::Version::greaterThanEqual(const std::string& compare) {
81 uint32_t cmajor, cminor, cmaint;
82 init();
83 extract(compare, &cmajor, &cminor, &cmaint);
84 if (cmajor != _major) return (_major > cmajor);
85 if (cminor != _minor) return (_minor > cminor);
86 if (cmaint != _maint) return (_maint > cmaint);
87 return (true);
88}
89
90bool rogue::Version::greaterThan(const std::string& compare) {
91 uint32_t cmajor, cminor, cmaint;
92 init();
93 extract(compare, &cmajor, &cminor, &cmaint);
94 if (cmajor != _major) return (_major > cmajor);
95 if (cminor != _minor) return (_minor > cminor);
96 if (cmaint != _maint) return (_maint > cmaint);
97 return (false);
98}
99
100bool rogue::Version::lessThanEqual(const std::string& compare) {
101 uint32_t cmajor, cminor, cmaint;
102 init();
103 extract(compare, &cmajor, &cminor, &cmaint);
104 if (cmajor != _major) return (_major < cmajor);
105 if (cminor != _minor) return (_minor < cminor);
106 if (cmaint != _maint) return (_maint < cmaint);
107 return (true);
108}
109
110bool rogue::Version::lessThan(const std::string& compare) {
111 uint32_t cmajor, cminor, cmaint;
112 init();
113 extract(compare, &cmajor, &cminor, &cmaint);
114 if (cmajor != _major) return (_major < cmajor);
115 if (cminor != _minor) return (_minor < cminor);
116 if (cmaint != _maint) return (_maint < cmaint);
117 return (false);
118}
119
120void rogue::Version::minVersion(const std::string& compare) {
121 if (lessThan(compare))
122 throw(rogue::GeneralError("Version:minVersion", "Installed rogue is less than minimum version"));
123}
124
125void rogue::Version::maxVersion(const std::string& compare) {
126 if (greaterThan(compare))
127 throw(rogue::GeneralError("Version:maxVersion", "Installed rogue is greater than maximum version"));
128}
129
130void rogue::Version::exactVersion(const std::string& compare) {
131 if (lessThan(compare) || greaterThan(compare))
132 throw(rogue::GeneralError("Version:exactVersion", "Installed rogue is not exact version"));
133}
134
136 init();
137 return _major;
138}
139
141 init();
142 return _minor;
143}
144
146 init();
147 return _maint;
148}
149
151 init();
152 return _devel;
153}
154
156 init();
157 std::stringstream ret;
158
159 ret << std::dec << _major;
160 ret << "." << std::dec << _minor;
161 ret << "." << std::dec << _maint;
162
163 if (_devel > 0) ret << ".dev" << std::dec << _devel;
164 return ret.str();
165}
166
167void rogue::Version::sleep(uint32_t seconds) {
168 rogue::GilRelease noGil;
169 ::sleep(seconds);
170}
171
172void rogue::Version::usleep(uint32_t useconds) {
173 rogue::GilRelease noGil;
174 ::usleep(useconds);
175}
176
178#ifndef NO_PYTHON
179 bp::class_<rogue::Version, boost::noncopyable>("Version", bp::no_init)
180 .def("current", &rogue::Version::current)
181 .staticmethod("current")
182 .def("greaterThanEqual", &rogue::Version::greaterThanEqual)
183 .staticmethod("greaterThanEqual")
184 .def("greaterThan", &rogue::Version::greaterThan)
185 .staticmethod("greaterThan")
186 .def("lessThanEqual", &rogue::Version::lessThanEqual)
187 .staticmethod("lessThanEqual")
188 .def("lessThan", &rogue::Version::lessThan)
189 .staticmethod("lessThan")
190 .def("minVersion", &rogue::Version::minVersion)
191 .staticmethod("minVersion")
192 .def("maxVersion", &rogue::Version::maxVersion)
193 .staticmethod("maxVersion")
194 .def("exactVersion", &rogue::Version::exactVersion)
195 .staticmethod("exactVersion")
196 .def("major", &rogue::Version::getMajor)
197 .staticmethod("major")
198 .def("minor", &rogue::Version::getMinor)
199 .staticmethod("minor")
200 .def("maint", &rogue::Version::getMaint)
201 .staticmethod("maint")
202 .def("devel", &rogue::Version::getDevel)
203 .staticmethod("devel")
204 .def("pythonVersion", &rogue::Version::pythonVersion)
205 .staticmethod("pythonVersion")
206 .def("sleep", &rogue::Version::sleep)
207 .staticmethod("sleep")
208 .def("usleep", &rogue::Version::usleep)
209 .staticmethod("usleep");
210#endif
211}
Generic Rogue exception type.
RAII helper that releases the Python GIL for a scope.
Definition GilRelease.h:36
static bool lessThanEqual(const std::string &compare)
Returns whether current version is less than or equal to compare.
Definition Version.cpp:100
static void maxVersion(const std::string &compare)
Throws if current version is above allowed maximum.
Definition Version.cpp:125
static void minVersion(const std::string &compare)
Throws if current version is below required minimum.
Definition Version.cpp:120
static uint32_t getMinor()
Returns minor version component.
Definition Version.cpp:140
static std::string current()
Returns current Rogue version string.
Definition Version.cpp:75
static void setup_python()
Registers Python bindings for version helpers.
Definition Version.cpp:177
static bool greaterThanEqual(const std::string &compare)
Returns whether current version is greater than or equal to compare.
Definition Version.cpp:80
static std::string pythonVersion()
Returns Python runtime version string.
Definition Version.cpp:155
static uint32_t getMajor()
Returns major version component.
Definition Version.cpp:135
static uint32_t getDevel()
Returns development/build component.
Definition Version.cpp:150
static bool lessThan(const std::string &compare)
Returns whether current version is less than compare.
Definition Version.cpp:110
static bool greaterThan(const std::string &compare)
Returns whether current version is greater than compare.
Definition Version.cpp:90
static void exactVersion(const std::string &compare)
Throws unless current version exactly matches compare.
Definition Version.cpp:130
static uint32_t getMaint()
Returns maintenance/patch version component.
Definition Version.cpp:145
static void usleep(uint32_t useconds)
Microsecond sleep helper for testing/debug timing.
Definition Version.cpp:172
static void sleep(uint32_t seconds)
Sleep helper for testing/debug timing.
Definition Version.cpp:167