WebCFace 2.9.0
Web-based Communication Framework & Dashboard-like UI
Loading...
Searching...
No Matches
safe_global.h
Go to the documentation of this file.
1#pragma once
2#include <cassert>
3#include <utility>
4#ifdef WEBCFACE_MESON
5#include "webcface-config.h"
6#else
7#include "webcface/common/webcface-config.h"
8#endif
9
11namespace internal {
12template <typename T>
14 T obj;
15 bool alive;
16
17 public:
22 template <typename... Args,
23 typename std::enable_if_t<
24 (!std::is_same_v<std::decay_t<Args>, SafeGlobal> && ...),
25 std::nullptr_t> = nullptr>
26 SafeGlobal(Args &&...args)
27 : obj(std::forward<Args>(args)...), alive(true) {}
28 SafeGlobal(const SafeGlobal &) = delete;
29 SafeGlobal &operator=(const SafeGlobal &) = delete;
30 SafeGlobal(SafeGlobal &&) = delete;
32 ~SafeGlobal() { alive = false; }
33 operator bool() const { return alive; }
34 T &operator*() {
35 assert(alive);
36 return obj;
37 }
39 assert(alive);
40 return &obj;
41 }
42};
43
44template <typename T>
46 return SafeGlobal<T>(std::move(obj));
47}
48
49} // namespace internal
Definition safe_global.h:13
SafeGlobal & operator=(const SafeGlobal &)=delete
SafeGlobal & operator=(SafeGlobal &&)=delete
T & operator*()
Definition safe_global.h:34
T * operator->()
Definition safe_global.h:38
SafeGlobal(const SafeGlobal &)=delete
SafeGlobal(Args &&...args)
Definition safe_global.h:26
SafeGlobal(SafeGlobal &&)=delete
~SafeGlobal()
Definition safe_global.h:32
SafeGlobal< T > safeGlobal(T obj)
Definition safe_global.h:45
#define WEBCFACE_NS_END
Definition webcface-config.h:118
#define WEBCFACE_NS_BEGIN
Definition webcface-config.h:117