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>
8#include <mutex>
10
71 template <typename T>
72 class Proxy final {
73 private:
74 struct Private {};
75
76 public:
80 explicit Proxy([[maybe_unused]] Private, T* toWrap) : m_wrapped(toWrap) {
81 }
82
87 [[nodiscard]] static std::shared_ptr<Proxy<T>> create(T* toWrap) noexcept {
88 return std::make_shared<Proxy<T>>(Private{}, toWrap);
89 }
90
95 [[nodiscard]] T* getWrapped() noexcept {
96 std::scoped_lock<std::mutex> sl{ m_mutex };
97 return m_wrapped;
98 }
99
103 void null() noexcept {
104 std::scoped_lock<std::mutex> sl{ m_mutex };
105 m_wrapped = nullptr;
106 }
107
112 [[nodiscard]] bool isValid() noexcept {
113 std::scoped_lock<std::mutex> sl{ m_mutex };
114 return m_wrapped;
115 }
116
117 private:
118 std::mutex m_mutex;
119 T* m_wrapped{ nullptr };
120 };
121
122} // namespace mostly_harmless::utils
123#endif // MOSTLYHARMLESS_PROXY_H
Proxy(Private, T *toWrap)
Definition mostlyharmless_Proxy.h:80
static std::shared_ptr< Proxy< T > > create(T *toWrap) noexcept
Definition mostlyharmless_Proxy.h:87
T * getWrapped() noexcept
Definition mostlyharmless_Proxy.h:95
bool isValid() noexcept
Definition mostlyharmless_Proxy.h:112
void null() noexcept
Definition mostlyharmless_Proxy.h:103
Contains general purpose utility classes & functions.
Definition mostlyharmless_TaskThread.h:12