netgen/libsrc/core/version.hpp

91 lines
3.0 KiB
C++
Raw Normal View History

#ifndef NETGEN_CORE_VERSION_HPP
#define NETGEN_CORE_VERSION_HPP
#include <string>
#include <tuple>
#include "ngcore_api.hpp"
namespace ngcore
{
class Archive;
class VersionInfo
{
private:
size_t mayor_, minor_, release, patch;
std::string git_hash;
public:
VersionInfo() : mayor_(0), minor_(0), release(0), patch(0), git_hash("") {}
VersionInfo(std::string vstring)
{
minor_ = release = patch = 0;
git_hash = "";
if(vstring.substr(0,1) == "v")
vstring = vstring.substr(1,vstring.size()-1);
auto dot = vstring.find(".");
mayor_ = std::stoi(vstring.substr(0,dot));
if(dot == size_t(-1)) vstring = "";
else vstring = vstring.substr(dot+1, vstring.size()-dot-1);
if(vstring.size())
{
dot = vstring.find(".");
minor_ = std::stoi(vstring.substr(0,dot));
if (dot == size_t(-1)) vstring = "";
else vstring = vstring.substr(dot+1, vstring.size()-dot-1);
if(vstring.size())
{
dot = vstring.find("-");
2018-12-06 21:53:44 +05:00
release = std::stoi(vstring.substr(0,dot));
if(dot == size_t(-1)) vstring = "";
else vstring = vstring.substr(dot+1,vstring.size()-dot-1);
if(vstring.size())
{
dot = vstring.find("-");
2018-12-06 21:53:44 +05:00
patch = std::stoi(vstring.substr(0,dot));
if(dot == size_t(-1)) vstring = "";
else vstring = vstring.substr(dot+1, vstring.size()-dot-1);
if(vstring.size())
git_hash = vstring;
}
}
}
}
VersionInfo(const char* cstr) : VersionInfo(std::string(cstr)) { }
std::string to_string() const
{ std::string vstring = "v" + std::to_string(mayor_);
2018-12-10 14:52:00 +05:00
if(minor_ || release || patch || !git_hash.empty())
{
vstring += "." + std::to_string(minor_);
2018-12-10 14:52:00 +05:00
if(release || patch || !git_hash.empty())
{
2018-12-06 21:53:44 +05:00
vstring += "." + std::to_string(release);
2018-12-10 14:52:00 +05:00
if(patch || !git_hash.empty())
{
2018-12-06 21:53:44 +05:00
vstring += "-" + std::to_string(patch);
2018-12-10 14:52:00 +05:00
if(!git_hash.empty())
vstring += "-" + git_hash;
}
}
}
return vstring;
}
bool operator <(const VersionInfo& other) const
{
return std::tie(mayor_, minor_, release, patch) <
std::tie(other.mayor_, other.minor_, other.release, other.patch);
}
bool operator ==(const VersionInfo& other) const
{
return mayor_ == other.mayor_ && minor_ == other.minor_ && release == other.release
2018-12-06 21:53:44 +05:00
&& patch == other.patch;
}
bool operator >(const VersionInfo& other) const { return other < (*this); }
bool operator <=(const VersionInfo& other) const { return !((*this) > other); }
bool operator >=(const VersionInfo& other) const { return !((*this) < other); }
void DoArchive(Archive& ar);
};
} // namespace ngcore
#endif // NETGEN_CORE_VERSION_HPP