AppVersion
user_6919294
c_cpp
7 months ago
9.4 kB
10
Indexable
// CODE BY IRENEUSZ A.
// AppVersion.h
#ifndef APP_VERSION_H
#define APP_VERSION_H
#include <string>
#include <ctime>
#include <sstream>
#include <vector>
#include <algorithm>
// Typ wyliczeniowy dla określenia etapu wydania
enum class ReleaseType {
Alpha,
Beta,
RC,
RTM
};
// Klasa reprezentująca wersję aplikacji
class AppVersion {
private:
int m_major;
int m_minor;
int m_build;
int m_revision;
ReleaseType m_releaseType;
public:
// Konstruktor domyślny
AppVersion() : m_major(0), m_minor(0), m_build(0), m_revision(0), m_releaseType(ReleaseType::RTM) {}
/**
* Konstruktor z parametrami
*
* @param major Główny numer wersji, zwiększany przy znaczących zmianach
* @param minor Podrzędny numer wersji, zwiększany przy mniejszych zmianach
* @param build Numer kompilacji, zazwyczaj zwiększany przy każdej kompilacji
* @param revision Numer rewizji, zazwyczaj używany dla poprawek lub małych zmian
* @param releaseType Typ wydania (Alpha, Beta, RC, RTM)
*/
AppVersion(int major, int minor, int build = 0, int revision = 0, ReleaseType releaseType = ReleaseType::RTM)
: m_major(major), m_minor(minor), m_build(build), m_revision(revision), m_releaseType(releaseType) {}
// Gettery i settery
int GetMajor() const { return m_major; }
void SetMajor(int major) { m_major = major; }
int GetMinor() const { return m_minor; }
void SetMinor(int minor) { m_minor = minor; }
int GetBuild() const { return m_build; }
void SetBuild(int build) { m_build = build; }
int GetRevision() const { return m_revision; }
void SetRevision(int revision) { m_revision = revision; }
ReleaseType GetReleaseType() const { return m_releaseType; }
void SetReleaseType(ReleaseType releaseType) { m_releaseType = releaseType; }
/**
* Konwertuje wersję do pełnego formatu tekstowego (np. "1.0.0.1 Beta")
*
* @return Pełny format tekstowy wersji
*/
std::string ToString() const;
/**
* Konwertuje wersję do krótkiego formatu (np. "1.0")
*
* @return Krótki format tekstowy wersji
*/
std::string ToShortString() const;
/**
* Konwertuje wersję do formatu zgodnego z Windows (32-bitowa liczba)
*
* @return 32-bitowa reprezentacja wersji
*/
unsigned int ToWindowsVersion() const;
/**
* Sprawdza czy bieżąca wersja jest nowsza od podanej
*
* @param other Wersja do porównania
* @return true jeśli bieżąca wersja jest nowsza, false w przeciwnym wypadku
*/
bool IsNewerThan(const AppVersion& other) const;
/**
* Sprawdza czy bieżąca wersja jest równa podanej
*
* @param other Wersja do porównania
* @return true jeśli wersje są identyczne, false w przeciwnym wypadku
*/
bool Equals(const AppVersion& other) const;
/**
* Sprawdza czy bieżąca wersja jest kompatybilna z podaną
*
* @param other Wersja do porównania
* @return true jeśli wersje są kompatybilne, false w przeciwnym wypadku
*/
bool IsCompatibleWith(const AppVersion& other) const;
/**
* Tworzy obiekt wersji z podanego ciągu znaków
*
* @param versionStr Ciąg znaków reprezentujący wersję
* @return Obiekt AppVersion
*/
static AppVersion FromString(const std::string& versionStr);
/**
* Automatycznie aktualizuje numery Build i Revision na podstawie aktualnej daty i czasu
*/
void AutoUpdateBuildInfo();
/**
* Przeciążone operatory porównania
*/
bool operator==(const AppVersion& other) const { return Equals(other); }
bool operator!=(const AppVersion& other) const { return !Equals(other); }
bool operator<(const AppVersion& other) const { return other.IsNewerThan(*this); }
bool operator>(const AppVersion& other) const { return IsNewerThan(other); }
bool operator<=(const AppVersion& other) const { return !IsNewerThan(other); }
bool operator>=(const AppVersion& other) const { return !other.IsNewerThan(*this); }
};
/**
* Stała reprezentująca aktualną wersję aplikacji
*/
const AppVersion APP_VERSION(1, 0, 0, 0, ReleaseType::RTM);
#endif // APP_VERSION_H
// AppVersion.cpp
#include "AppVersion.h"
std::string AppVersion::ToString() const {
static const std::string ReleaseNames[] = { "Alpha", "Beta", "RC", "RTM" };
std::stringstream ss;
ss << m_major << "." << m_minor << "." << m_build << "." << m_revision;
if (m_releaseType != ReleaseType::RTM) {
ss << " " << ReleaseNames[static_cast<int>(m_releaseType)];
}
return ss.str();
}
std::string AppVersion::ToShortString() const {
std::stringstream ss;
ss << m_major << "." << m_minor;
return ss.str();
}
unsigned int AppVersion::ToWindowsVersion() const {
// Format używany przez Windows:
// starsze 16 bitów to Major i Minor, młodsze 16 bitów to Build i Revision
return (static_cast<unsigned int>(m_major) << 24) |
(static_cast<unsigned int>(m_minor) << 16) |
(static_cast<unsigned int>(m_build) << 8) |
static_cast<unsigned int>(m_revision);
}
bool AppVersion::IsNewerThan(const AppVersion& other) const {
// Sprawdzamy hierarchicznie każdy element wersji
if (m_major > other.m_major) return true;
if (m_major < other.m_major) return false;
if (m_minor > other.m_minor) return true;
if (m_minor < other.m_minor) return false;
if (m_build > other.m_build) return true;
if (m_build < other.m_build) return false;
return m_revision > other.m_revision;
}
bool AppVersion::Equals(const AppVersion& other) const {
return m_major == other.m_major &&
m_minor == other.m_minor &&
m_build == other.m_build &&
m_revision == other.m_revision &&
m_releaseType == other.m_releaseType;
}
bool AppVersion::IsCompatibleWith(const AppVersion& other) const {
// Przykładowa implementacja kompatybilności:
// Wersje są kompatybilne, jeśli mają ten sam numer Major
return m_major == other.m_major;
// Można rozbudować to o więcej reguł, np.:
// return (m_major == other.m_major) && (m_minor >= other.m_minor);
}
AppVersion AppVersion::FromString(const std::string& versionStr) {
AppVersion result;
// Rozdzielamy wersję i typ wydania
std::string versionPart = versionStr;
std::string releasePart;
size_t spacePos = versionStr.find(' ');
if (spacePos != std::string::npos) {
versionPart = versionStr.substr(0, spacePos);
releasePart = versionStr.substr(spacePos + 1);
// Określamy typ wydania
if (releasePart == "Alpha") {
result.m_releaseType = ReleaseType::Alpha;
} else if (releasePart == "Beta") {
result.m_releaseType = ReleaseType::Beta;
} else if (releasePart == "RC") {
result.m_releaseType = ReleaseType::RC;
} else {
result.m_releaseType = ReleaseType::RTM;
}
}
// Dzielimy ciąg na części oddzielone kropkami
std::vector<int> parts;
std::stringstream ss(versionPart);
std::string item;
while (std::getline(ss, item, '.')) {
try {
parts.push_back(std::stoi(item));
} catch (...) {
parts.push_back(0);
}
}
// Przypisujemy wartości, jeśli są dostępne
if (parts.size() > 0) result.m_major = parts[0];
if (parts.size() > 1) result.m_minor = parts[1];
if (parts.size() > 2) result.m_build = parts[2];
if (parts.size() > 3) result.m_revision = parts[3];
return result;
}
void AppVersion::AutoUpdateBuildInfo() {
std::time_t t = std::time(nullptr);
std::tm* now = std::localtime(&t);
// Obliczanie dnia roku (1-366)
int dayOfYear = now->tm_yday + 1;
// Używamy numeru dnia w roku jako numer kompilacji
m_build = dayOfYear;
// Używamy połączenia godziny i minuty jako numer rewizji
m_revision = now->tm_hour * 100 + now->tm_min;
}
// Przykładowe użycie:
/*
int main() {
// Tworzenie wersji
AppVersion version(1, 2, 3, 4, ReleaseType::Beta);
// Wyświetlanie wersji
std::cout << "Wersja: " << version.ToString() << std::endl;
std::cout << "Krótka wersja: " << version.ToShortString() << std::endl;
// Parsowanie wersji z tekstu
AppVersion parsedVersion = AppVersion::FromString("2.3.4.5 Alpha");
std::cout << "Parsowana wersja: " << parsedVersion.ToString() << std::endl;
// Automatyczne aktualizowanie informacji o kompilacji
AppVersion autoVersion(1, 0);
autoVersion.AutoUpdateBuildInfo();
std::cout << "Auto wersja: " << autoVersion.ToString() << std::endl;
// Porównywanie wersji
if (version.IsNewerThan(autoVersion)) {
std::cout << "Wersja " << version.ToString() << " jest nowsza niż " << autoVersion.ToString() << std::endl;
}
return 0;
}
*/Editor is loading...
Leave a Comment