version: add more constructors, comparators
This commit is contained in:
parent
af699c5443
commit
1f5e3dc903
129
version.cpp.rl
129
version.cpp.rl
@ -37,14 +37,40 @@ version::version ():
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
version::version (unsigned int _major,
|
||||
unsigned int _minor):
|
||||
size (0),
|
||||
components { _major, _minor },
|
||||
release (PRODUCTION)
|
||||
version::version (unsigned _major,
|
||||
unsigned _minor,
|
||||
unsigned _point,
|
||||
unsigned _build):
|
||||
components { _major, _minor, _point, _build }
|
||||
{
|
||||
components[MAJOR] = _major;
|
||||
components[MINOR] = _minor;
|
||||
size = 4u;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
version::version (unsigned _major,
|
||||
unsigned _minor,
|
||||
unsigned _point):
|
||||
version (_major, _minor, _point, 0)
|
||||
{
|
||||
size = 3u;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
version::version (unsigned _major,
|
||||
unsigned _minor):
|
||||
version (_major, _minor, 0)
|
||||
{
|
||||
size = 2u;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
version::version (unsigned _major):
|
||||
version (_major, 0)
|
||||
{
|
||||
size = 1u;
|
||||
}
|
||||
|
||||
|
||||
@ -95,19 +121,52 @@ unsigned version::build (void) const { return components[BUILD]; }
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
const unsigned*
|
||||
version::begin (void) const
|
||||
version::begin (void) const noexcept
|
||||
{
|
||||
return components.begin ();
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
const unsigned*
|
||||
version::end (void) const
|
||||
version::end (void) const noexcept
|
||||
{
|
||||
return components.begin () + size;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
const unsigned*
|
||||
version::cbegin (void) const noexcept
|
||||
{
|
||||
return begin ();
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
const unsigned*
|
||||
version::cend (void) const noexcept
|
||||
{
|
||||
return end ();
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
const unsigned&
|
||||
version::operator[] (size_t i) const
|
||||
{
|
||||
return components[i];
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
unsigned&
|
||||
version::operator[] (size_t i)
|
||||
{
|
||||
return components[i];
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
%%{
|
||||
machine version;
|
||||
@ -141,7 +200,7 @@ version::end (void) const
|
||||
//-----------------------------------------------------------------------------
|
||||
util::version
|
||||
util::version::parse (const std::string& str) {
|
||||
unsigned int accum = 0;
|
||||
unsigned accum = 0;
|
||||
|
||||
int cs;
|
||||
const char *p = str.data (),
|
||||
@ -166,30 +225,56 @@ util::version::parse (const char *str) {
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
bool
|
||||
version::operator> (const version &rhs) const
|
||||
version::operator< (const version &rhs) const noexcept
|
||||
{
|
||||
auto count = util::min (size, rhs.size);
|
||||
|
||||
for (decltype(count) i = 0; i < count; ++i)
|
||||
if (components[i] < rhs.components[i])
|
||||
return false;
|
||||
// make sure each element we have in common is LT
|
||||
for (decltype (count) i = 0; i < count; ++i)
|
||||
if ((*this)[i] < rhs[i])
|
||||
return true;
|
||||
|
||||
// if they have extra elements and they're not zeros then we must be LT
|
||||
if (size < rhs.size)
|
||||
return false;
|
||||
if (std::none_of (rhs.cbegin () + count, rhs.cend (), [] (auto i) { return i == 0; }))
|
||||
return true;
|
||||
|
||||
if (release <= rhs.release)
|
||||
return false;
|
||||
// test if we have an earlier release schedule
|
||||
if (release < rhs.release)
|
||||
return true;
|
||||
|
||||
return true;
|
||||
// we're equal or greater to rhs
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
bool
|
||||
version::operator== (const version &rhs) const {
|
||||
return components == rhs.components &&
|
||||
size == rhs.size &&
|
||||
release == rhs.release;
|
||||
version::operator== (const version &rhs) const noexcept
|
||||
{
|
||||
auto count = util::min (size, rhs.size);
|
||||
|
||||
bool front = std::equal (cbegin (), cbegin () + count, rhs.cbegin ());
|
||||
bool left = std::all_of (this->cbegin () + count, this->cend (), [] (auto i) { return i == 0; });
|
||||
bool right = std::all_of (rhs.cbegin () + count, rhs.cend (), [] (auto i) { return i == 0; });
|
||||
|
||||
return front && (size > rhs.size ? left : right) && release == rhs.release;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
bool
|
||||
version::operator<= (const version &rhs) const noexcept
|
||||
{
|
||||
return *this < rhs || *this == rhs;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
bool
|
||||
version::operator> (const version &rhs) const noexcept
|
||||
{
|
||||
return !(*this <= rhs);
|
||||
}
|
||||
|
||||
|
||||
|
26
version.hpp
26
version.hpp
@ -42,7 +42,10 @@ namespace util {
|
||||
};
|
||||
|
||||
version ();
|
||||
version (unsigned int _major, unsigned int _minor);
|
||||
version (unsigned _major);
|
||||
version (unsigned _major, unsigned _minor);
|
||||
version (unsigned _major, unsigned _minor, unsigned _point);
|
||||
version (unsigned _major, unsigned _minor, unsigned _point, unsigned _build);
|
||||
explicit version (const std::string& str);
|
||||
explicit version (const char *str);
|
||||
|
||||
@ -71,21 +74,26 @@ namespace util {
|
||||
unsigned point (void) const;
|
||||
unsigned build (void) const;
|
||||
|
||||
const unsigned* begin (void) const;
|
||||
const unsigned* end (void) const;
|
||||
const unsigned* begin (void) const noexcept;
|
||||
const unsigned* end (void) const noexcept;
|
||||
const unsigned* cbegin (void) const noexcept;
|
||||
const unsigned* cend (void) const noexcept;
|
||||
|
||||
const unsigned& operator[] (size_t) const;
|
||||
unsigned& operator[] (size_t);
|
||||
|
||||
size_t size;
|
||||
std::array<unsigned,4u> components;
|
||||
release_t release;
|
||||
release_t release = PRODUCTION;
|
||||
|
||||
static version parse (const std::string&);
|
||||
static version parse (const char*);
|
||||
|
||||
bool operator< (const version& rhs) const;
|
||||
bool operator> (const version& rhs) const;
|
||||
bool operator>= (const version& rhs) const;
|
||||
bool operator<= (const version& rhs) const;
|
||||
bool operator== (const version& rhs) const;
|
||||
bool operator< (const version& rhs) const noexcept;
|
||||
bool operator> (const version& rhs) const noexcept;
|
||||
bool operator>= (const version& rhs) const noexcept;
|
||||
bool operator<= (const version& rhs) const noexcept;
|
||||
bool operator== (const version& rhs) const noexcept;
|
||||
};
|
||||
|
||||
std::ostream& operator<< (std::ostream& os, const util::version& rhs);
|
||||
|
Loading…
Reference in New Issue
Block a user