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