Skip to content

Commit c44997a

Browse files
Barthelemyknopers8
authored andcommitted
A non-static Version (#288)
* A non-static Version * Forgot a file
1 parent 3c96e1a commit c44997a

5 files changed

Lines changed: 156 additions & 72 deletions

File tree

Framework/CMakeLists.txt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ set(TEST_SRCS
226226
test/testPostProcessingInterface.cxx
227227
test/testPostProcessingConfig.cxx
228228
test/testCheckWorkflow.cxx
229-
test/testWorkflow.cxx)
229+
test/testWorkflow.cxx
230+
test/testVersion.cxx
231+
)
230232

231233
set(TEST_ARGS
232234
""
@@ -249,7 +251,9 @@ set(TEST_ARGS
249251
""
250252
""
251253
"-b --run"
252-
"-b --run")
254+
"-b --run"
255+
""
256+
)
253257

254258
list(LENGTH TEST_SRCS count)
255259
math(EXPR count "${count}-1")
@@ -272,12 +276,15 @@ endforeach()
272276

273277
foreach(t testTaskInterface testWorkflow testTaskRunner testCheckWorkflow
274278
testInfrastructureGenerator testPostProcessingConfig testPostProcessingInterface
275-
testPostProcessingRunner testCheck testCheckRunner)
279+
testPostProcessingRunner testCheck testCheckRunner )
276280
target_sources(${t} PRIVATE
277281
${CMAKE_BINARY_DIR}/getTestDataDirectory.cxx)
278282
target_include_directories(${t} PRIVATE ${CMAKE_SOURCE_DIR})
279283
endforeach()
280284

285+
target_include_directories(testVersion PRIVATE
286+
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>)
287+
281288
set_property(TEST testWorkflow PROPERTY TIMEOUT 10)
282289
set_property(TEST testWorkflow PROPERTY LABELS slow)
283290
set_property(TEST testCheckWorkflow PROPERTY TIMEOUT 60)

Framework/include/QualityControl/Version.h.in

Lines changed: 81 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
///
1212
/// \file Version.h
13-
/// \brief Report the version for this package.
14-
/// \author bvonhall
13+
/// \author Barthelemy von Haller
1514
///
1615

1716
#ifndef QC_CORE_VERSION_H
@@ -20,71 +19,87 @@
2019
#include <string>
2120
#include <sstream>
2221

23-
namespace o2::quality_control::core
24-
{
25-
26-
/// The current major version.
27-
#define QUALITYCONTROL_VERSION_MAJOR @PROJECT_VERSION_MAJOR@
28-
29-
/// The current minor version.
30-
#define QUALITYCONTROL_VERSION_MINOR @PROJECT_VERSION_MINOR@
31-
32-
/// The current patch level.
33-
#define QUALITYCONTROL_VERSION_PATCH @PROJECT_VERSION_PATCH@
34-
35-
/// True if the current version is newer than the given one.
36-
#define QUALITYCONTROL_VERSION_GT(MAJOR, MINOR, PATCH) \
37-
((QUALITYCONTROL_VERSION_MAJOR > MAJOR) || \
38-
(QUALITYCONTROL_VERSION_MAJOR == \
39-
MAJOR&&(QUALITYCONTROL_VERSION_MINOR > MINOR || (QUALITYCONTROL_VERSION_MINOR == MINOR&& QUALITYCONTROL_VERSION_PATCH > PATCH))))
40-
41-
/// True if the current version is equal or newer to the given.
42-
#define QUALITYCONTROL_VERSION_GE(MAJOR, MINOR, PATCH) \
43-
((QUALITYCONTROL_VERSION_MAJOR > MAJOR) || \
44-
(QUALITYCONTROL_VERSION_MAJOR == \
45-
MAJOR&&(QUALITYCONTROL_VERSION_MINOR > MINOR || (QUALITYCONTROL_VERSION_MINOR == MINOR&& QUALITYCONTROL_VERSION_PATCH >= PATCH))))
46-
47-
/// True if the current version is older than the given one.
48-
#define QUALITYCONTROL_VERSION_LT(MAJOR, MINOR, PATCH) \
49-
((QUALITYCONTROL_VERSION_MAJOR < MAJOR) || \
50-
(QUALITYCONTROL_VERSION_MAJOR == \
51-
MAJOR&&(QUALITYCONTROL_VERSION_MINOR < MINOR || (QUALITYCONTROL_VERSION_MINOR == MINOR&& QUALITYCONTROL_VERSION_PATCH < PATCH))))
52-
53-
/// True if the current version is older or equal to the given.
54-
#define QUALITYCONTROL_VERSION_LE(MAJOR, MINOR, PATCH) \
55-
((QUALITYCONTROL_VERSION_MAJOR < MAJOR) || \
56-
(QUALITYCONTROL_VERSION_MAJOR == \
57-
MAJOR&&(QUALITYCONTROL_VERSION_MINOR < MINOR || (QUALITYCONTROL_VERSION_MINOR == MINOR&& QUALITYCONTROL_VERSION_PATCH <= PATCH))))
58-
59-
/// Information about the current QualityControl version.
60-
class Version {
61-
public:
62-
/// @return the current major version of QualityControl.
63-
static int getMajor()
64-
{
65-
return QUALITYCONTROL_VERSION_MAJOR;
66-
}
67-
68-
/// @return the current minor version of QualityControl.
69-
static int getMinor()
70-
{
71-
return QUALITYCONTROL_VERSION_MINOR;
72-
}
73-
74-
/// @return the current patch level of QualityControl.
75-
static int getPatch()
76-
{
77-
return QUALITYCONTROL_VERSION_PATCH;
78-
}
79-
80-
/// @return the current QualityControl version (MM.mm.pp).
81-
static std::string getString()
82-
{
83-
std::ostringstream version;
84-
version << QUALITYCONTROL_VERSION_MAJOR << '.' << QUALITYCONTROL_VERSION_MINOR << '.' << QUALITYCONTROL_VERSION_PATCH;
85-
return version.str();
86-
}
22+
namespace o2::quality_control::core {
8723

24+
/// Represents a software package version.
25+
/// Inspired from https://sourcey.com/articles/comparing-version-strings-in-cpp
26+
class Version
27+
{
28+
public:
29+
30+
/// Create a version from a string.
31+
/// @param version The version in the form X.Y.Z. If minor or patch is missing, it is replaced by 0.
32+
Version(std::string version)
33+
{
34+
std::sscanf(version.c_str(), "%d.%d.%d", &mMajor, &mMinor, &mPatch);
35+
}
36+
37+
Version(int major, int minor, int patch): mMajor(major), mMinor(minor), mPatch(patch)
38+
{
39+
}
40+
41+
~Version() = default;
42+
43+
/// \brief Returns the version of the QC framework.
44+
/// Returns the version of the QC framework as found in CMake.
45+
static Version& GetQcVersion()
46+
{
47+
// Guaranteed to be destroyed. Instantiated on first use
48+
static Version qcVersion{@PROJECT_VERSION_MAJOR@, @PROJECT_VERSION_MINOR@, @PROJECT_VERSION_PATCH@};
49+
return qcVersion;
50+
}
51+
52+
int getMajor() const
53+
{
54+
return mMajor;
55+
}
56+
57+
int getMinor() const
58+
{
59+
return mMinor;
60+
}
61+
62+
int getPatch() const
63+
{
64+
return mPatch;
65+
}
66+
67+
bool operator<(const Version &other)
68+
{
69+
if (getMajor() < other.getMajor()) {
70+
return true;
71+
}
72+
if (getMinor() < other.getMinor()) {
73+
return true;
74+
}
75+
if (getPatch() < other.getPatch()) {
76+
return true;
77+
}
78+
return false;
79+
}
80+
81+
bool operator==(const Version &other)
82+
{
83+
return getMajor() == other.getMajor()
84+
&& getMinor() == other.getMinor()
85+
&& getPatch() == other.getPatch();
86+
}
87+
88+
friend std::ostream &operator<<(std::ostream &stream, const Version &ver)
89+
{
90+
stream << ver.getMajor() << '.' << ver.getMinor() << '.' << ver.getPatch();
91+
return stream;
92+
}
93+
94+
std::string getString()
95+
{
96+
std::ostringstream version;
97+
version << this;
98+
return version.str();
99+
}
100+
101+
private:
102+
int mMajor = 0, mMinor = 0, mPatch = 0;
88103
};
89104

90105
} // namespace o2::quality_control::core

Framework/src/CcdbDatabase.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ void CcdbDatabase::storeMO(std::shared_ptr<o2::quality_control::core::MonitorObj
109109
// metadata
110110
map<string, string> metadata;
111111
// QC metadata (prefix qc_)
112-
metadata["qc_version"] = Version::getString();
112+
metadata["qc_version"] = Version::GetQcVersion().getString();
113113
// user metadata
114114
map<string, string> userMetadata = mo->getMetadataMap();
115115
if (!userMetadata.empty()) {
@@ -165,7 +165,7 @@ void CcdbDatabase::storeQO(std::shared_ptr<QualityObject> qo)
165165
// metadata
166166
map<string, string> metadata;
167167
// QC metadata (prefix qc_)
168-
metadata["qc_version"] = Version::getString();
168+
metadata["qc_version"] = Version::GetQcVersion().getString();
169169
metadata["qc_quality"] = std::to_string(qo->getQuality().getLevel());
170170

171171
// other attributes

Framework/src/InfrastructureGenerator.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ void InfrastructureGenerator::customizeInfrastructure(std::vector<framework::Com
200200
void InfrastructureGenerator::printVersion()
201201
{
202202
// Log the version number
203-
QcInfoLogger::GetInstance() << "QC version " << o2::quality_control::core::Version::getString() << infologger::endm;
203+
QcInfoLogger::GetInstance() << "QC version " << o2::quality_control::core::Version::GetQcVersion().getString() << infologger::endm;
204204
}
205205

206206
} // namespace o2::quality_control::core

Framework/test/testVersion.cxx

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright CERN and copyright holders of ALICE O2. This software is
2+
// distributed under the terms of the GNU General Public License v3 (GPL
3+
// Version 3), copied verbatim in the file "COPYING".
4+
//
5+
// See http://alice-o2.web.cern.ch/license for full licensing information.
6+
//
7+
// In applying this license CERN does not waive the privileges and immunities
8+
// granted to it by virtue of its status as an Intergovernmental Organization
9+
// or submit itself to any jurisdiction.
10+
11+
///
12+
/// \file testQuality.cxx
13+
/// \author Barthelemy von Haller
14+
///
15+
16+
#include "QualityControl/Version.h"
17+
18+
#define BOOST_TEST_MODULE Quality test
19+
#define BOOST_TEST_MAIN
20+
#define BOOST_TEST_DYN_LINK
21+
#include <boost/test/unit_test.hpp>
22+
#include <boost/test/output_test_stream.hpp>
23+
24+
using namespace std;
25+
26+
namespace o2::quality_control::core
27+
{
28+
29+
BOOST_AUTO_TEST_CASE(test_original_version)
30+
{
31+
assert((Version("3.7.8.0") == Version("3.7.8.0")) == true);
32+
assert((Version("3.7.8.0") == Version("3.7.8")) == true);
33+
assert((Version("3.7.8.0") < Version("3.7.8")) == false);
34+
assert((Version("3.7.9") < Version("3.7.8")) == false);
35+
assert((Version("3") < Version("3.7.9")) == true);
36+
assert((Version("1.7.9") < Version("3.1")) == true);
37+
assert((Version("") == Version("0.0.0")) == true);
38+
assert((Version("0") == Version("0.0.0")) == true);
39+
assert((Version("") != Version("0.0.1")) == true);
40+
41+
std::cout << "Printing version (3.7.8): " << Version("3.7.8.0") << std::endl;
42+
}
43+
44+
BOOST_AUTO_TEST_CASE(test_version)
45+
{
46+
Version v("2.0.0");
47+
BOOST_CHECK(v == Version("2.0.0"));
48+
Version qc = Version::GetQcVersion();
49+
BOOST_CHECK(qc.getMajor() != 0 || qc.getMinor() != 0 || qc.getPatch() != 0);
50+
cout << qc << endl;
51+
}
52+
53+
BOOST_AUTO_TEST_CASE(test_output)
54+
{
55+
Version v("1.2.3");
56+
boost::test_tools::output_test_stream output;
57+
output << v;
58+
59+
BOOST_CHECK(output.is_equal("1.2.3"));
60+
}
61+
62+
} // namespace o2::quality_control::core

0 commit comments

Comments
 (0)