Move inline definitions out of the main header file; Add accessors for version field.
This commit is contained in:
@@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2015 Marko Zivanovic
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "version.h"
|
||||||
|
|
||||||
|
namespace version {
|
||||||
|
|
||||||
|
/// Parse string into Version_data structure according to semantic versioning 2.0.0 rules.
|
||||||
|
struct Semver200_parser {
|
||||||
|
Version_data parse(const std::string&) const;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Compare Version_data to another using semantic versioning 2.0.0 rules.
|
||||||
|
struct Semver200_comparator {
|
||||||
|
int compare(const Version_data&, const Version_data&) const;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Concrete version class that binds all semver 2.0.0 functionality together.
|
||||||
|
class Semver200_version : public Basic_version<Semver200_parser, Semver200_comparator> {
|
||||||
|
public:
|
||||||
|
Semver200_version(const std::string& v)
|
||||||
|
: Basic_version{ v, Semver200_parser(), Semver200_comparator() } {}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
+39
-63
@@ -75,46 +75,45 @@ namespace version {
|
|||||||
Build_identifiers build_ids;
|
Build_identifiers build_ids;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// Forward declaration required for operators' template declarations.
|
||||||
|
template<typename P, typename C>
|
||||||
|
class Basic_version;
|
||||||
|
|
||||||
|
/// Compare if left version object is less than the right.
|
||||||
|
template<typename P, typename C>
|
||||||
|
bool operator<(const Basic_version<P, C>&, const Basic_version<P, C>&);
|
||||||
|
|
||||||
|
/// Compare if two version objects are equal.
|
||||||
|
template<typename P, typename C>
|
||||||
|
bool operator==(const Basic_version<P, C>& l, const Basic_version<P, C>& r);
|
||||||
|
|
||||||
|
/// Output version object to stream using standard semver format (X.Y.Z-PR+B).
|
||||||
|
template<typename P, typename C>
|
||||||
|
std::ostream& operator<<(std::ostream& os, const Basic_version<P, C>& v);
|
||||||
|
|
||||||
|
|
||||||
/// Generic version description and comparison class.
|
/// Generic version description and comparison class.
|
||||||
/**
|
/**
|
||||||
Basic_version class describes general version object without prescribing parsing,
|
Basic_version class describes general version object without prescribing parsing,
|
||||||
validation and comparison rules. These rules are implemented by supplied Parser and
|
validation and comparison rules. These rules are implemented by supplied P and
|
||||||
Comparator parameters.
|
C parameters.
|
||||||
*/
|
*/
|
||||||
template<typename Parser, typename Comparator>
|
template<typename Parser, typename Comparator>
|
||||||
class Basic_version {
|
class Basic_version {
|
||||||
public:
|
public:
|
||||||
/// Construct Basic_version object using Parser to parse version string and Comparator for comparison.
|
/// Construct Basic_version object using P to parse version string and C for comparison.
|
||||||
Basic_version(const std::string& v, Parser p, Comparator c)
|
Basic_version(const std::string& v, Parser p, Comparator c)
|
||||||
: parser_(p), comparator_(c), ver_(parser_.parse(v)) {};
|
: parser_(p), comparator_(c), ver_(parser_.parse(v)) {};
|
||||||
|
|
||||||
/// Compare if left version object is less than the right.
|
const int major() const;
|
||||||
friend bool operator<(const Basic_version& l, const Basic_version& r) {
|
const int minor() const;
|
||||||
return l.comparator_.compare(l.ver_, r.ver_) == -1;
|
const int patch() const;
|
||||||
}
|
const std::string prerelease() const;
|
||||||
/// Compare if two version objects are equal.
|
const std::string build() const;
|
||||||
friend bool operator==(const Basic_version& l, const Basic_version& r) {
|
|
||||||
return l.comparator_.compare(l.ver_, r.ver_) == 0;
|
friend bool operator< <>(const Basic_version& l, const Basic_version& r);
|
||||||
}
|
friend bool operator== <>(const Basic_version& l, const Basic_version& r);
|
||||||
/// Output version object to stream using standard semver format (X.Y.Z-PR+B).
|
friend std::ostream& operator<< <>(std::ostream& os, const Basic_version& v);
|
||||||
friend std::ostream& operator<<(std::ostream& os, const Basic_version& v) {
|
|
||||||
os << v.ver_.major << "." << v.ver_.minor << "." << v.ver_.patch;
|
|
||||||
if (!v.ver_.prerelease_ids.empty()) {
|
|
||||||
os << "-";
|
|
||||||
for (auto it = v.ver_.prerelease_ids.cbegin(); it < v.ver_.prerelease_ids.cend() - 1; ++it) {
|
|
||||||
os << it->first << ".";
|
|
||||||
}
|
|
||||||
os << v.ver_.prerelease_ids.crbegin()->first;
|
|
||||||
}
|
|
||||||
if (!v.ver_.build_ids.empty()) {
|
|
||||||
os << "+";
|
|
||||||
for (auto it = v.ver_.build_ids.cbegin(); it < v.ver_.build_ids.cend() - 1; ++it) {
|
|
||||||
os << *it << ".";
|
|
||||||
}
|
|
||||||
os << *v.ver_.build_ids.crbegin();
|
|
||||||
}
|
|
||||||
return os;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Parser parser_;
|
Parser parser_;
|
||||||
@@ -122,45 +121,22 @@ namespace version {
|
|||||||
const Version_data ver_;
|
const Version_data ver_;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Parse string into Version_data structure according to semantic versioning 2.0.0 rules.
|
|
||||||
struct Semver200_parser {
|
|
||||||
Version_data parse(const std::string&) const;
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Compare Version_data to another using semantic versioning 2.0.0 rules.
|
|
||||||
struct Semver200_comparator {
|
|
||||||
int compare(const Version_data&, const Version_data&) const;
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Concrete version class that binds all semver 2.0.0 functionality together.
|
|
||||||
class Semver200_version : public Basic_version<Semver200_parser, Semver200_comparator> {
|
|
||||||
public:
|
|
||||||
Semver200_version(const std::string& v)
|
|
||||||
: Basic_version{ v, Semver200_parser(), Semver200_comparator() } {}
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Compare if two version objects are different.
|
/// Compare if two version objects are different.
|
||||||
template<typename P, typename C>
|
template<typename Parser, typename Comparator>
|
||||||
inline bool operator!=(const Basic_version<P, C>& l, const Basic_version<P, C>& r) {
|
bool operator!=(const version::Basic_version<Parser, Comparator>& l, const version::Basic_version<Parser, Comparator>& r);
|
||||||
return !(l == r);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Compare if left version object is greater than the right.
|
/// Compare if left version object is greater than the right.
|
||||||
template<typename P, typename C>
|
template<typename Parser, typename Comparator>
|
||||||
inline bool operator>(const Basic_version<P, C>& l, const Basic_version<P, C>& r) {
|
bool operator>(const version::Basic_version<Parser, Comparator>& l, const version::Basic_version<Parser, Comparator>& r);
|
||||||
return r < l;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Compare if left version object is greater than or equal the right.
|
/// Compare if left version object is greater than or equal the right.
|
||||||
template<typename P, typename C>
|
template<typename Parser, typename Comparator>
|
||||||
inline bool operator>=(const Basic_version<P, C>& l, const Basic_version<P, C>& r) {
|
bool operator>=(const version::Basic_version<Parser, Comparator>& l, const version::Basic_version<Parser, Comparator>& r);
|
||||||
return !(l < r);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Compare if left version object is less than or equal the right.
|
/// Compare if left version object is less than or equal the right.
|
||||||
template<typename P, typename C>
|
template<typename Parser, typename Comparator>
|
||||||
inline bool operator<=(const Basic_version<P, C>& l, const Basic_version<P, C>& r) {
|
bool operator<=(const version::Basic_version<Parser, Comparator>& l, const version::Basic_version<Parser, Comparator>& r);
|
||||||
return !(l > r);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include "version.inl"
|
||||||
|
|||||||
@@ -0,0 +1,122 @@
|
|||||||
|
/*
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2015 Marko Zivanovic
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "version.h"
|
||||||
|
|
||||||
|
namespace version {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
/// Utility function to splice all vector elements to output stream, using designated separator and
|
||||||
|
/// function object for getting value from vector element.
|
||||||
|
template<typename T, typename F>
|
||||||
|
std::ostream& splice(std::ostream& os, const std::vector<T>& v, const std::string& sep, F read) {
|
||||||
|
for (auto it = v.cbegin(); it < v.cend() - 1; ++it) {
|
||||||
|
os << read(*it) << sep;
|
||||||
|
}
|
||||||
|
os << read(*v.crbegin());
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename P, typename C>
|
||||||
|
const int Basic_version<P, C>::major() const {
|
||||||
|
return ver_.major;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename P, typename C>
|
||||||
|
const int Basic_version<P, C>::minor() const {
|
||||||
|
return ver_.minor;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename P, typename C>
|
||||||
|
const int Basic_version<P, C>::patch() const {
|
||||||
|
return ver_.patch;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename P, typename C>
|
||||||
|
const std::string Basic_version<P, C>::prerelease() const {
|
||||||
|
std::stringstream ss;
|
||||||
|
splice(ss, ver_.prerelease_ids, ".", [](const auto& id) { return id.first;});
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename P, typename C>
|
||||||
|
const std::string Basic_version<P, C>::build() const {
|
||||||
|
std::stringstream ss;
|
||||||
|
splice(ss, ver_.build_ids, ".", [](const auto& id) { return id;});
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<typename P, typename C>
|
||||||
|
bool operator<(const Basic_version<P, C>& l, const Basic_version<P, C>& r) {
|
||||||
|
return l.comparator_.compare(l.ver_, r.ver_) == -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename P, typename C>
|
||||||
|
bool operator==(const version::Basic_version<P, C>& l, const version::Basic_version<P, C>& r) {
|
||||||
|
return l.comparator_.compare(l.ver_, r.ver_) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename P, typename C>
|
||||||
|
std::ostream& operator<<(std::ostream& os, const version::Basic_version<P, C>& v) {
|
||||||
|
os << v.ver_.major << "." << v.ver_.minor << "." << v.ver_.patch;
|
||||||
|
if (!v.ver_.prerelease_ids.empty()) {
|
||||||
|
os << "-";
|
||||||
|
splice(os, v.ver_.prerelease_ids, ".", [](const auto& id) { return id.first;});
|
||||||
|
}
|
||||||
|
if (!v.ver_.build_ids.empty()) {
|
||||||
|
os << "+";
|
||||||
|
splice(os, v.ver_.build_ids, ".", [](const auto& id) { return id;});
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Compare if two version objects are different.
|
||||||
|
template<typename P, typename C>
|
||||||
|
inline bool operator!=(const version::Basic_version<P, C>& l, const version::Basic_version<P, C>& r) {
|
||||||
|
return !(l == r);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Compare if left version object is greater than the right.
|
||||||
|
template<typename P, typename C>
|
||||||
|
inline bool operator>(const version::Basic_version<P, C>& l, const version::Basic_version<P, C>& r) {
|
||||||
|
return r < l;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Compare if left version object is greater than or equal the right.
|
||||||
|
template<typename P, typename C>
|
||||||
|
inline bool operator>=(const version::Basic_version<P, C>& l, const version::Basic_version<P, C>& r) {
|
||||||
|
return !(l < r);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Compare if left version object is less than or equal the right.
|
||||||
|
template<typename P, typename C>
|
||||||
|
inline bool operator<=(const version::Basic_version<P, C>& l, const version::Basic_version<P, C>& r) {
|
||||||
|
return !(l > r);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -25,7 +25,7 @@ SOFTWARE.
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include "version.h"
|
#include "semver200.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ SOFTWARE.
|
|||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include "version.h"
|
#include "semver200.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ SOFTWARE.
|
|||||||
#define BOOST_TEST_MODULE semver200_comparator_tests
|
#define BOOST_TEST_MODULE semver200_comparator_tests
|
||||||
|
|
||||||
#include <boost/test/unit_test.hpp>
|
#include <boost/test/unit_test.hpp>
|
||||||
#include "version.h"
|
#include "semver200.h"
|
||||||
|
|
||||||
using namespace version;
|
using namespace version;
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ SOFTWARE.
|
|||||||
|
|
||||||
#include <boost/test/unit_test.hpp>
|
#include <boost/test/unit_test.hpp>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
#include "version.h"
|
#include "semver200.h"
|
||||||
|
|
||||||
version::Semver200_parser p;
|
version::Semver200_parser p;
|
||||||
const version::Prerelease_identifiers no_rel_ids;
|
const version::Prerelease_identifiers no_rel_ids;
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ SOFTWARE.
|
|||||||
#define BOOST_TEST_MODULE semver200_version_tests
|
#define BOOST_TEST_MODULE semver200_version_tests
|
||||||
|
|
||||||
#include <boost/test/unit_test.hpp>
|
#include <boost/test/unit_test.hpp>
|
||||||
#include "version.h"
|
#include "semver200.h"
|
||||||
|
|
||||||
using namespace version;
|
using namespace version;
|
||||||
|
|
||||||
@@ -54,6 +54,8 @@ BOOST_AUTO_TEST_CASE(test_relational_operators) {
|
|||||||
|
|
||||||
BOOST_CHECK(v("1.0.0") >= v("1.0.0"));
|
BOOST_CHECK(v("1.0.0") >= v("1.0.0"));
|
||||||
BOOST_CHECK(v("1.0.0") >= v("0.0.9"));
|
BOOST_CHECK(v("1.0.0") >= v("0.0.9"));
|
||||||
|
|
||||||
|
BOOST_CHECK(v("2.0.0") > v("1.9.9"));
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(test_ostream_output) {
|
BOOST_AUTO_TEST_CASE(test_ostream_output) {
|
||||||
@@ -64,4 +66,13 @@ BOOST_AUTO_TEST_CASE(test_ostream_output) {
|
|||||||
CHECK_RT("1.2.3-alpha+build.314");
|
CHECK_RT("1.2.3-alpha+build.314");
|
||||||
CHECK_RT("1.2.3-alpha.1+build.314");
|
CHECK_RT("1.2.3-alpha.1+build.314");
|
||||||
CHECK_RT("1.2.3-alpha.1.2.3+build.314");
|
CHECK_RT("1.2.3-alpha.1.2.3+build.314");
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(test_accessors) {
|
||||||
|
auto p = v("1.2.3-pre.rel.1+test.build.321");
|
||||||
|
BOOST_CHECK_EQUAL(p.major(), 1);
|
||||||
|
BOOST_CHECK_EQUAL(p.minor(), 2);
|
||||||
|
BOOST_CHECK_EQUAL(p.patch(), 3);
|
||||||
|
BOOST_CHECK_EQUAL(p.prerelease(), "pre.rel.1");
|
||||||
|
BOOST_CHECK_EQUAL(p.build(), "test.build.321");
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user