Implement Version class
This commit is contained in:
parent
da136cd52f
commit
3124548314
3 changed files with 196 additions and 0 deletions
101
include/duckhandy/version_bt.hpp
Normal file
101
include/duckhandy/version_bt.hpp
Normal file
|
@ -0,0 +1,101 @@
|
|||
/* Copyright 2016-2021 Michele Santullo
|
||||
* This file is part of "duckhandy".
|
||||
*
|
||||
* "duckhandy" is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* "duckhandy" is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with "duckhandy". If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef id5594416333AA4792BFEC94E863E03B08
|
||||
#define id5594416333AA4792BFEC94E863E03B08
|
||||
|
||||
#include "int_conv.hpp"
|
||||
#include <cstddef>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace dhandy {
|
||||
|
||||
template <std::size_t N>
|
||||
class Version {
|
||||
public:
|
||||
constexpr static std::size_t Count = N;
|
||||
|
||||
constexpr Version (const char* const ver, bool accept_less=false);
|
||||
|
||||
constexpr unsigned int operator[] (std::size_t idx) const;
|
||||
constexpr unsigned int major() const;
|
||||
constexpr unsigned int minor() const;
|
||||
constexpr unsigned int revision() const;
|
||||
|
||||
private:
|
||||
unsigned int m_versions[N];
|
||||
};
|
||||
|
||||
template <std::size_t N>
|
||||
inline constexpr Version<N>::Version (const char* const ver, bool accept_less) :
|
||||
m_versions{0}
|
||||
{
|
||||
std::size_t z = 0;
|
||||
std::size_t s = 0;
|
||||
std::size_t idx = 0;
|
||||
bool commit = false;
|
||||
|
||||
do {
|
||||
if (ver[z] >= '0' and ver[z] <= '9') {
|
||||
commit = true;
|
||||
}
|
||||
else if (commit) {
|
||||
commit = false;
|
||||
m_versions[idx++] = dhandy::ary_to_int<unsigned int>(ver + s, ver + z);
|
||||
s = z + 1;
|
||||
}
|
||||
else {
|
||||
s = z + 1;
|
||||
}
|
||||
} while(ver[z++] and idx < N);
|
||||
|
||||
if (not accept_less and idx < N)
|
||||
throw std::invalid_argument("Input version doesn't contain enough version numbers");
|
||||
}
|
||||
|
||||
template <std::size_t N>
|
||||
inline constexpr unsigned int Version<N>::operator[] (std::size_t idx) const {
|
||||
if (idx < N)
|
||||
return m_versions[idx];
|
||||
else
|
||||
throw std::out_of_range("Index is out of range");
|
||||
}
|
||||
|
||||
template <std::size_t N>
|
||||
inline constexpr unsigned int Version<N>::major() const {
|
||||
return m_versions[0];
|
||||
}
|
||||
|
||||
template <std::size_t N>
|
||||
inline constexpr unsigned int Version<N>::minor() const {
|
||||
if constexpr (N > 1)
|
||||
return m_versions[1];
|
||||
else
|
||||
throw std::out_of_range("Not enough version entries, there is no minor number");
|
||||
}
|
||||
|
||||
template <std::size_t N>
|
||||
inline constexpr unsigned int Version<N>::revision() const {
|
||||
if constexpr (N > 2)
|
||||
return m_versions[2];
|
||||
else
|
||||
throw std::out_of_range("Not enough version entries, there is no revision number");
|
||||
}
|
||||
|
||||
} //namespace dhandy
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue