version: add more constructors, comparators

This commit is contained in:
Danny Robson 2016-06-20 13:08:24 +10:00
parent af699c5443
commit 1f5e3dc903
2 changed files with 124 additions and 31 deletions

View File

@ -37,14 +37,40 @@ version::version ():
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
version::version (unsigned int _major, version::version (unsigned _major,
unsigned int _minor): unsigned _minor,
size (0), unsigned _point,
components { _major, _minor }, unsigned _build):
release (PRODUCTION) components { _major, _minor, _point, _build }
{ {
components[MAJOR] = _major; size = 4u;
components[MINOR] = _minor; }
//-----------------------------------------------------------------------------
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* const unsigned*
version::begin (void) const version::begin (void) const noexcept
{ {
return components.begin (); return components.begin ();
} }
//-----------------------------------------------------------------------------
const unsigned* const unsigned*
version::end (void) const version::end (void) const noexcept
{ {
return components.begin () + size; 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; machine version;
@ -141,7 +200,7 @@ version::end (void) const
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
util::version util::version
util::version::parse (const std::string& str) { util::version::parse (const std::string& str) {
unsigned int accum = 0; unsigned accum = 0;
int cs; int cs;
const char *p = str.data (), const char *p = str.data (),
@ -166,30 +225,56 @@ util::version::parse (const char *str) {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
bool bool
version::operator> (const version &rhs) const version::operator< (const version &rhs) const noexcept
{ {
auto count = util::min (size, rhs.size); auto count = util::min (size, rhs.size);
// make sure each element we have in common is LT
for (decltype (count) i = 0; i < count; ++i) for (decltype (count) i = 0; i < count; ++i)
if (components[i] < rhs.components[i]) if ((*this)[i] < rhs[i])
return false;
if (size < rhs.size)
return false;
if (release <= rhs.release)
return false;
return true; return true;
// if they have extra elements and they're not zeros then we must be LT
if (size < rhs.size)
if (std::none_of (rhs.cbegin () + count, rhs.cend (), [] (auto i) { return i == 0; }))
return true;
// test if we have an earlier release schedule
if (release < rhs.release)
return true;
// we're equal or greater to rhs
return false;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
bool bool
version::operator== (const version &rhs) const { version::operator== (const version &rhs) const noexcept
return components == rhs.components && {
size == rhs.size && auto count = util::min (size, rhs.size);
release == rhs.release;
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);
} }

View File

@ -42,7 +42,10 @@ namespace util {
}; };
version (); 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 std::string& str);
explicit version (const char *str); explicit version (const char *str);
@ -71,21 +74,26 @@ namespace util {
unsigned point (void) const; unsigned point (void) const;
unsigned build (void) const; unsigned build (void) const;
const unsigned* begin (void) const; const unsigned* begin (void) const noexcept;
const unsigned* end (void) const; 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; size_t size;
std::array<unsigned,4u> components; std::array<unsigned,4u> components;
release_t release; release_t release = PRODUCTION;
static version parse (const std::string&); static version parse (const std::string&);
static version parse (const char*); static version parse (const char*);
bool operator< (const version& rhs) const; bool operator< (const version& rhs) const noexcept;
bool operator> (const version& rhs) const; bool operator> (const version& rhs) const noexcept;
bool operator>= (const version& rhs) const; bool operator>= (const version& rhs) const noexcept;
bool operator<= (const version& rhs) const; bool operator<= (const version& rhs) const noexcept;
bool operator== (const version& rhs) const; bool operator== (const version& rhs) const noexcept;
}; };
std::ostream& operator<< (std::ostream& os, const util::version& rhs); std::ostream& operator<< (std::ostream& os, const util::version& rhs);