Comments improvements; minor header files cleanup.
This commit is contained in:
+50
-43
@@ -30,7 +30,7 @@ SOFTWARE.
|
||||
|
||||
namespace version {
|
||||
|
||||
/// Parse_error is thrown on all parsing and validation errors.
|
||||
/// Any error in parsing or validation of version string will result in Parse_error exception being thrown.
|
||||
class Parse_error : public std::runtime_error {
|
||||
using std::runtime_error::runtime_error;
|
||||
};
|
||||
@@ -40,7 +40,7 @@ namespace version {
|
||||
Type of identifier affects comparison: alphanumeric identifiers are compared as ASCII strings, while
|
||||
numeric identifiers are compared as numbers.
|
||||
*/
|
||||
enum class Identifier_type {
|
||||
enum class Id_type {
|
||||
alnum, ///< Identifier is alphanumerical
|
||||
num ///< Identifier is numeric
|
||||
};
|
||||
@@ -51,22 +51,22 @@ namespace version {
|
||||
These identifiers can be either numerical or alphanumerical.
|
||||
This structure describes one such identifier.
|
||||
*/
|
||||
using Prerelease_identifier = std::pair<std::string, Identifier_type>;
|
||||
using Prerelease_identifier = std::pair<std::string, Id_type>;
|
||||
|
||||
/// Container for all prerelease identifiers for a given version string.
|
||||
using Prerelease_identifiers = std::vector<Prerelease_identifier>;
|
||||
|
||||
/// Build identifier is just a string and has no meaning for the purpose of version precedence.
|
||||
/// Build identifier is arbitrary string with no special meaning with regards to version precedence.
|
||||
using Build_identifier = std::string;
|
||||
|
||||
/// Container for all build identifiers for a given version string.
|
||||
/// Container for all build identifiers of a given version string.
|
||||
using Build_identifiers = std::vector<Build_identifier>;
|
||||
|
||||
/// Description of version broken into parts, as per semantic versioning specification.
|
||||
struct Version_data {
|
||||
int major; ///< Major version, change only on incompatible API modifications
|
||||
int minor; ///< Minor version, change on compatible API modifications
|
||||
int patch; ///< Patch version, change only on bugfixes
|
||||
int major; ///< Major version, change only on incompatible API modifications.
|
||||
int minor; ///< Minor version, change on backwards-compatible API modifications.
|
||||
int patch; ///< Patch version, change only on bugfixes.
|
||||
|
||||
/// Optional series of prerelease identifiers.
|
||||
Prerelease_identifiers prerelease_ids;
|
||||
@@ -75,40 +75,47 @@ namespace version {
|
||||
Build_identifiers build_ids;
|
||||
};
|
||||
|
||||
/// Forward declaration required for operators' template declarations.
|
||||
template<typename P, typename C>
|
||||
// Forward declaration required for operators' template declarations.
|
||||
template<typename Parser, typename Comparator>
|
||||
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>&);
|
||||
/// Test if left-hand version operand is of lower precedence than the right-hand version.
|
||||
template<typename Parser, typename Comparator>
|
||||
bool operator<(const Basic_version<Parser, Comparator>&,
|
||||
const Basic_version<Parser, Comparator>&);
|
||||
|
||||
/// 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);
|
||||
/// Test if left-hand version operand if of equal precedence as the right-hand version.
|
||||
template<typename Parser, typename Comparator>
|
||||
bool operator==(const Basic_version<Parser, Comparator>&,
|
||||
const Basic_version<Parser, Comparator>&);
|
||||
|
||||
/// 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);
|
||||
template<typename Parser, typename Comparator>
|
||||
std::ostream& operator<<(std::ostream&,
|
||||
const Basic_version<Parser, Comparator>&);
|
||||
|
||||
/// Compare if two version objects are different.
|
||||
template<typename P, typename C>
|
||||
bool operator!=(const version::Basic_version<P, C>& l, const version::Basic_version<P, C>& r);
|
||||
/// Test if left-hand version and right-hand version are of different precedence.
|
||||
template<typename Parser, typename Comparator>
|
||||
bool operator!=(const Basic_version<Parser, Comparator>&,
|
||||
const Basic_version<Parser, Comparator>&);
|
||||
|
||||
/// Compare if left version object is greater than the right.
|
||||
template<typename P, typename C>
|
||||
bool operator>(const version::Basic_version<P, C>& l, const version::Basic_version<P, C>& r);
|
||||
/// Test if left-hand version operand is of higher precedence than the right-hand version.
|
||||
template<typename Parser, typename Comparator>
|
||||
bool operator>(const Basic_version<Parser, Comparator>&,
|
||||
const Basic_version<Parser, Comparator>&);
|
||||
|
||||
/// Compare if left version object is greater than or equal the right.
|
||||
template<typename P, typename C>
|
||||
bool operator>=(const version::Basic_version<P, C>& l, const version::Basic_version<P, C>& r);
|
||||
/// Test if left-hand version operand is of higher or equal precedence as the right-hand version.
|
||||
template<typename Parser, typename Comparator>
|
||||
bool operator>=(const Basic_version<Parser, Comparator>&,
|
||||
const Basic_version<Parser, Comparator>&);
|
||||
|
||||
/// Compare if left version object is less than or equal the right.
|
||||
template<typename P, typename C>
|
||||
bool operator<=(const version::Basic_version<P, C>& l, const version::Basic_version<P, C>& r);
|
||||
/// Test if left-hand version operand is of lower or equal precedence as the right-hand version.
|
||||
template<typename Parser, typename Comparator>
|
||||
bool operator<=(const Basic_version<Parser, Comparator>&,
|
||||
const Basic_version<Parser, Comparator>&);
|
||||
|
||||
|
||||
/// Generic version description and comparison class.
|
||||
/// Base class for various version parsing and precedence ordering schemes.
|
||||
/**
|
||||
Basic_version class describes general version object without prescribing parsing,
|
||||
validation and comparison rules. These rules are implemented by supplied Parser and
|
||||
@@ -117,11 +124,11 @@ namespace version {
|
||||
template<typename Parser, typename Comparator>
|
||||
class Basic_version {
|
||||
public:
|
||||
/// Construct Basic_version object using P to parse default ("0.0.0") version string and C for comparison.
|
||||
Basic_version(Parser p, Comparator c);
|
||||
/// Construct Basic_version object using Parser object to parse default ("0.0.0") version string and Comparator for comparison.
|
||||
Basic_version(Parser, Comparator);
|
||||
|
||||
/// Construct Basic_version object using P to parse version string and C for comparison.
|
||||
Basic_version(const std::string& v, Parser p, Comparator c);
|
||||
/// Construct Basic_version object using Parser to parse supplied version string and Comparator for comparison.
|
||||
Basic_version(const std::string&, Parser, Comparator);
|
||||
|
||||
/// Construct Basic_version by copying data from another one.
|
||||
Basic_version(const Basic_version&);
|
||||
@@ -129,15 +136,15 @@ namespace version {
|
||||
/// Copy version data from another Basic_version to this one.
|
||||
Basic_version& operator=(const Basic_version&);
|
||||
|
||||
const int major() const;
|
||||
const int minor() const;
|
||||
const int patch() const;
|
||||
const std::string prerelease() const;
|
||||
const std::string build() const;
|
||||
const int major() const; ///< Get major version.
|
||||
const int minor() const; ///< Get minor version.
|
||||
const int patch() const; ///< Get patch version.
|
||||
const std::string prerelease() const; ///< Get prerelease version string.
|
||||
const std::string build() const; ///< Get build version string.
|
||||
|
||||
friend bool operator< <>(const Basic_version& l, const Basic_version& r);
|
||||
friend bool operator== <>(const Basic_version& l, const Basic_version& r);
|
||||
friend std::ostream& operator<< <>(std::ostream& os, const Basic_version& v);
|
||||
friend bool operator< <>(const Basic_version&, const Basic_version&);
|
||||
friend bool operator== <>(const Basic_version&, const Basic_version&);
|
||||
friend std::ostream& operator<< <>(std::ostream&s, const Basic_version&);
|
||||
|
||||
private:
|
||||
Parser parser_;
|
||||
|
||||
+44
-40
@@ -33,73 +33,77 @@ namespace version {
|
||||
/// Utility function to splice all vector elements to output stream, using designated separator
|
||||
/// between elements and function object for getting values from vector elements.
|
||||
template<typename T, typename F>
|
||||
std::ostream& splice(std::ostream& os, const std::vector<T>& v, const std::string& sep, F read) {
|
||||
if (!v.empty()) {
|
||||
for (auto it = v.cbegin(); it < v.cend() - 1; ++it) {
|
||||
std::ostream& splice(std::ostream& os, const std::vector<T>& vec, const std::string& sep, F read) {
|
||||
if (!vec.empty()) {
|
||||
for (auto it = vec.cbegin(); it < vec.cend() - 1; ++it) {
|
||||
os << read(*it) << sep;
|
||||
}
|
||||
os << read(*v.crbegin());
|
||||
os << read(*vec.crbegin());
|
||||
}
|
||||
return os;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename P, typename C>
|
||||
Basic_version<P, C>::Basic_version(P p, C c)
|
||||
template<typename Parser, typename Comparator>
|
||||
Basic_version<Parser, Comparator>::Basic_version(Parser p, Comparator c)
|
||||
: parser_(p), comparator_(c), ver_(parser_.parse("0.0.0")) {};
|
||||
|
||||
template<typename P, typename C>
|
||||
Basic_version<P, C>::Basic_version(const std::string& v, P p, C c)
|
||||
template<typename Parser, typename Comparator>
|
||||
Basic_version<Parser, Comparator>::Basic_version(const std::string& v, Parser p, Comparator c)
|
||||
: parser_(p), comparator_(c), ver_(parser_.parse(v)) {};
|
||||
|
||||
template<typename P, typename C>
|
||||
Basic_version<P, C>::Basic_version(const Basic_version<P, C>&) = default;
|
||||
template<typename Parser, typename Comparator>
|
||||
Basic_version<Parser, Comparator>::Basic_version(const Basic_version<Parser, Comparator>&) = default;
|
||||
|
||||
template<typename P, typename C>
|
||||
Basic_version<P, C>& Basic_version<P, C>::operator=(const Basic_version<P, C>&) = default;
|
||||
template<typename Parser, typename Comparator>
|
||||
Basic_version<Parser, Comparator>& Basic_version<Parser, Comparator>::operator=(
|
||||
const Basic_version<Parser, Comparator>&) = default;
|
||||
|
||||
template<typename P, typename C>
|
||||
const int Basic_version<P, C>::major() const {
|
||||
template<typename Parser, typename Comparator>
|
||||
const int Basic_version<Parser, Comparator>::major() const {
|
||||
return ver_.major;
|
||||
}
|
||||
|
||||
template<typename P, typename C>
|
||||
const int Basic_version<P, C>::minor() const {
|
||||
template<typename Parser, typename Comparator>
|
||||
const int Basic_version<Parser, Comparator>::minor() const {
|
||||
return ver_.minor;
|
||||
}
|
||||
|
||||
template<typename P, typename C>
|
||||
const int Basic_version<P, C>::patch() const {
|
||||
template<typename Parser, typename Comparator>
|
||||
const int Basic_version<Parser, Comparator>::patch() const {
|
||||
return ver_.patch;
|
||||
}
|
||||
|
||||
template<typename P, typename C>
|
||||
const std::string Basic_version<P, C>::prerelease() const {
|
||||
template<typename Parser, typename Comparator>
|
||||
const std::string Basic_version<Parser, Comparator>::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 {
|
||||
template<typename Parser, typename Comparator>
|
||||
const std::string Basic_version<Parser, Comparator>::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) {
|
||||
template<typename Parser, typename Comparator>
|
||||
bool operator<(const Basic_version<Parser, Comparator>& l,
|
||||
const Basic_version<Parser, Comparator>& 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) {
|
||||
template<typename Parser, typename Comparator>
|
||||
bool operator==(const Basic_version<Parser, Comparator>& l,
|
||||
const Basic_version<Parser, Comparator>& 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) {
|
||||
template<typename Parser, typename Comparator>
|
||||
std::ostream& operator<<(std::ostream& os,
|
||||
const Basic_version<Parser, Comparator>& v) {
|
||||
os << v.ver_.major << "." << v.ver_.minor << "." << v.ver_.patch;
|
||||
std::string prl = v.prerelease();
|
||||
if (!prl.empty()) {
|
||||
@@ -112,27 +116,27 @@ namespace version {
|
||||
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) {
|
||||
template<typename Parser, typename Comparator>
|
||||
inline bool operator!=(const Basic_version<Parser, Comparator>& l,
|
||||
const Basic_version<Parser, Comparator>& 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) {
|
||||
template<typename Parser, typename Comparator>
|
||||
inline bool operator>(const Basic_version<Parser, Comparator>& l,
|
||||
const Basic_version<Parser, Comparator>& 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) {
|
||||
template<typename Parser, typename Comparator>
|
||||
inline bool operator>=(const Basic_version<Parser, Comparator>& l,
|
||||
const Basic_version<Parser, Comparator>& 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) {
|
||||
template<typename Parser, typename Comparator>
|
||||
inline bool operator<=(const Basic_version<Parser, Comparator>& l,
|
||||
const Basic_version<Parser, Comparator>& r) {
|
||||
return !(l > r);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user