MetaImGUI 1.0.0
ImGui Application Template for C++20
Loading...
Searching...
No Matches
UpdateChecker.h
Go to the documentation of this file.
1/*
2 MetaImGUI
3 Copyright (C) 2026 A P Nicholson
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
17*/
18
19#pragma once
20
21#include <atomic>
22#include <functional>
23#include <memory>
24#include <mutex>
25#include <stop_token>
26#include <string>
27#include <thread>
28
29namespace MetaImGUI {
30
31// C++20: Using designated initializers for clear, safe initialization
41
42struct UpdateInfo {
43 bool updateAvailable = false;
44 std::string latestVersion;
45 std::string currentVersion;
46 std::string releaseUrl;
47 std::string releaseNotes;
48 std::string downloadUrl;
50};
51
53public:
54 UpdateChecker(std::string repoOwner, std::string repoName);
56
57 // Delete copy and move
58 UpdateChecker(const UpdateChecker&) = delete;
62
63 // Check for updates asynchronously
64 void CheckForUpdatesAsync(std::function<void(const UpdateInfo&)> callback);
65
66 // Check for updates synchronously (blocking)
68
69 // Cancel ongoing check
70 void Cancel();
71
72 // Check if a check is in progress
73 [[nodiscard]] bool IsChecking() const;
74
75 // Compare versions (returns: -1 if v1 < v2, 0 if equal, 1 if v1 > v2)
76 static int CompareVersions(const std::string& v1, const std::string& v2);
77
78private:
79 std::string m_repoOwner;
80 std::string m_repoName;
81
82 // m_checking is the single source of truth for "a check is running".
83 // Atomic compare_exchange on this guards entry; the worker resets it on exit.
84 std::atomic<bool> m_checking;
85
86 // C++20: Using std::jthread for automatic thread management
87 std::jthread m_checkThread;
88 std::mutex m_threadMutex; // Serialises thread (re)creation against itself
89
90 // Internal implementation
91 UpdateInfo CheckForUpdatesImpl(const std::stop_token& stopToken);
92 UpdateInfo ParseReleaseInfo(const std::string& jsonResponse);
93};
94
95} // namespace MetaImGUI
UpdateChecker(UpdateChecker &&)=delete
UpdateChecker & operator=(UpdateChecker &&)=delete
static int CompareVersions(const std::string &v1, const std::string &v2)
void CheckForUpdatesAsync(std::function< void(const UpdateInfo &)> callback)
UpdateChecker & operator=(const UpdateChecker &)=delete
UpdateChecker(const UpdateChecker &)=delete
@ NetworkError
curl/transport failure, or non-2xx that isn't rate-limit
@ RateLimited
403 with X-RateLimit-Remaining: 0 (GitHub-style)
@ Cancelled
stop_token::stop_requested() fired during transfer
@ UpdateFound
A newer release is available.
@ UpToDate
Current version is the latest.
@ Unknown
No check has run, or result not yet available.
@ ParseError
Response did not contain a parseable version.
UpdateCheckStatus status
std::string latestVersion
std::string currentVersion