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
33#ifndef WXM_COMPAT_H
34#define WXM_COMPAT_H
35
36#include <thread>
37#include <version>
38#ifdef __cpp_lib_jthread
39 using jthread = std::jthread;
40 using stop_token = std::stop_token;
41 using stop_source = std::stop_source;
42#else
43 struct stop_token {
44 bool stop_requested() const { return false; }
45 };
46 // A minimal stop_source shim for compilers without std::jthread: on those
47 // compilers cancellation is a no-op (as it always was here).
48 struct stop_source {
49 stop_token get_token() const { return {}; }
50 bool request_stop() noexcept { return false; }
51 bool stop_requested() const noexcept { return false; }
52 };
53 // A minimal jthread shim for compilers without std::jthread
54 class jthread : public std::thread {
55 public:
56 jthread() noexcept = default;
57 template<typename F, typename... Args>
58 explicit jthread(F&& f, Args&&... args)
59 : std::thread([](std::decay_t<F> f, std::decay_t<Args>... args) {
60 if constexpr (std::is_invocable_v<std::decay_t<F>, stop_token, std::decay_t<Args>...>) {
61 f(stop_token{}, std::move(args)...);
62 } else {
63 f(std::move(args)...);
64 }
65 }, std::forward<F>(f), std::forward<Args>(args)...) {}
66
67 jthread& operator=(jthread&& other) noexcept {
68 if (joinable()) join();
69 std::thread::operator=(std::move(other));
70 return *this;
71 }
72 ~jthread() {
73 if (joinable()) join();
74 }
75 void request_stop() noexcept {}
76 };
77#endif
78
79#include <algorithm>
80#include <ranges>
81namespace ranges {
82#if defined(__cpp_lib_ranges_contains) && __cpp_lib_ranges_contains >= 202207L
83 using std::ranges::contains;
84#else
85 template<typename R, typename T>
86 constexpr bool contains(R&& r, const T& value) {
87 return std::ranges::find(r, value) != std::ranges::end(r);
88 }
89#endif
90}
91#endif // WXM_COMPAT_H
Definition: Compat.h:54
Definition: Compat.h:48
Definition: Compat.h:43