MostlyHarmless 0.0.1
 
Loading...
Searching...
No Matches
mostlyharmless_Proxy.h
Go to the documentation of this file.
1//
2// Created by Syl Morrison on 08/02/2025.
3//
4
5#ifndef MOSTLYHARMLESS_PROXY_H
6#define MOSTLYHARMLESS_PROXY_H
7#include <memory>
9
70 template <typename T>
71 class Proxy final {
72 private:
73 struct Private {};
74
75 public:
79 explicit Proxy([[maybe_unused]] Private, T* toWrap) : m_wrapped(toWrap) {
80 }
81
86 [[nodiscard]] static std::shared_ptr<Proxy<T>> create(T* toWrap) noexcept {
87 return std::make_shared<Proxy<T>>(Private{}, toWrap);
88 }
89
94 [[nodiscard]] T* getWrapped() noexcept {
95 std::scoped_lock<std::mutex> sl{ m_mutex };
96 return m_wrapped;
97 }
98
102 void null() noexcept {
103 std::scoped_lock<std::mutex> sl{ m_mutex };
104 m_wrapped = nullptr;
105 }
106
111 [[nodiscard]] bool isValid() noexcept {
112 std::scoped_lock<std::mutex> sl{ m_mutex };
113 return m_wrapped;
114 }
115
116 private:
117 std::mutex m_mutex;
118 T* m_wrapped{ nullptr };
119 };
120
121} // namespace mostly_harmless::utils
122#endif // MOSTLYHARMLESS_PROXY_H
Proxy(Private, T *toWrap)
Definition mostlyharmless_Proxy.h:79
static std::shared_ptr< Proxy< T > > create(T *toWrap) noexcept
Definition mostlyharmless_Proxy.h:86
T * getWrapped() noexcept
Definition mostlyharmless_Proxy.h:94
bool isValid() noexcept
Definition mostlyharmless_Proxy.h:111
void null() noexcept
Definition mostlyharmless_Proxy.h:102
Contains general purpose utility classes & functions.
Definition mostlyharmless_TaskThread.h:12