wxMaxima
Loading...
Searching...
No Matches
Compat.h
Go to the documentation of this file.
1// -*- mode: c++; c-file-style: "linux"; c-basic-offset: 2; indent-tabs-mode: nil -*-
2//
3// Copyright (C) 2026 Gunter Königsmann <wxMaxima@physikbuch.de>
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 2 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//
16// You should have received a copy of the GNU General Public License
17// along with this program; if not, write to the Free Software
18// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19//
20// SPDX-License-Identifier: GPL-2.0+
21
37#ifndef WXM_COMPAT_H
38#define WXM_COMPAT_H
39
40#include <thread>
41#include <version>
42#ifdef __cpp_lib_jthread
43 using jthread = std::jthread;
44 using stop_token = std::stop_token;
45 using stop_source = std::stop_source;
46#else
47 struct stop_token {
48 bool stop_requested() const { return false; }
49 };
50 // A minimal stop_source shim for compilers without std::jthread: on those
51 // compilers cancellation is a no-op (as it always was here).
52 struct stop_source {
53 stop_token get_token() const { return {}; }
54 bool request_stop() noexcept { return false; }
55 bool stop_requested() const noexcept { return false; }
56 };
57 // A minimal jthread shim for compilers without std::jthread
58 class jthread : public std::thread {
59 public:
60 jthread() noexcept = default;
61 template<typename F, typename... Args>
62 explicit jthread(F&& f, Args&&... args)
63 : std::thread([](std::decay_t<F> f, std::decay_t<Args>... args) {
64 if constexpr (std::is_invocable_v<std::decay_t<F>, stop_token, std::decay_t<Args>...>) {
65 f(stop_token{}, std::move(args)...);
66 } else {
67 f(std::move(args)...);
68 }
69 }, std::forward<F>(f), std::forward<Args>(args)...) {}
70
71 jthread& operator=(jthread&& other) noexcept {
72 if (joinable()) join();
73 std::thread::operator=(std::move(other));
74 return *this;
75 }
76 ~jthread() {
77 if (joinable()) join();
78 }
79 void request_stop() noexcept {}
80 };
81#endif
82
83#include <algorithm>
84#include <ranges>
85namespace ranges {
86#if defined(__cpp_lib_ranges_contains) && __cpp_lib_ranges_contains >= 202207L
87 using std::ranges::contains;
88#else
89 template<typename R, typename T>
90 constexpr bool contains(R&& r, const T& value) {
91 return std::ranges::find(r, value) != std::ranges::end(r);
92 }
93#endif
94}
95
114#include <wx/defs.h>
115#ifndef wxWARN_UNUSED
116#define wxWARN_UNUSED
117#endif
118
119#endif // WXM_COMPAT_H
Definition: Compat.h:58
Definition: Compat.h:52
Definition: Compat.h:47