diff options
author | crupest <crupest@outlook.com> | 2022-02-08 16:53:51 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-02-08 16:53:51 +0800 |
commit | 74bb9cd27242b9320f99ff4d2b50c3051576cc14 (patch) | |
tree | 744bac5799c593d1d6f81e7b09581bea626f2cde /include/cru/common/SelfResolvable.h | |
parent | b90c398de829d1ba5329651d75bae82f5e4085fe (diff) | |
download | cru-74bb9cd27242b9320f99ff4d2b50c3051576cc14.tar.gz cru-74bb9cd27242b9320f99ff4d2b50c3051576cc14.tar.bz2 cru-74bb9cd27242b9320f99ff4d2b50c3051576cc14.zip |
...
Diffstat (limited to 'include/cru/common/SelfResolvable.h')
-rw-r--r-- | include/cru/common/SelfResolvable.h | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/include/cru/common/SelfResolvable.h b/include/cru/common/SelfResolvable.h new file mode 100644 index 00000000..c8acd4b9 --- /dev/null +++ b/include/cru/common/SelfResolvable.h @@ -0,0 +1,69 @@ +#pragma once +#include "PreConfig.h" + +#include <memory> +#include <type_traits> + +namespace cru { +template <typename T> +class SelfResolvable; + +template <typename T> +class ObjectResolver { + friend SelfResolvable<T>; + + private: + ObjectResolver(const std::shared_ptr<T*>& resolver) : resolver_(resolver) {} + + public: + ObjectResolver(const ObjectResolver&) = default; + ObjectResolver& operator=(const ObjectResolver&) = default; + ObjectResolver(ObjectResolver&&) = default; + ObjectResolver& operator=(ObjectResolver&&) = default; + ~ObjectResolver() = default; + + T* Resolve() const { + // resolver_ is null only when this has been moved. + // You shouldn't resolve a moved resolver. So assert it. + Expects(resolver_); + return *resolver_; + } + + private: + std::shared_ptr<T*> resolver_; +}; + +template <typename T> +class SelfResolvable { + public: + SelfResolvable() : resolver_(new T*(static_cast<T*>(this))) {} + SelfResolvable(const SelfResolvable&) = delete; + SelfResolvable& operator=(const SelfResolvable&) = delete; + + // Resolvers to old object will resolve to new object. + SelfResolvable(SelfResolvable&& other) + : resolver_(std::move(other.resolver_)) { + (*resolver_) = static_cast<T*>(this); + } + + // Old resolvers for this object will resolve to nullptr. + // Other's resolvers will now resolve to this. + SelfResolvable& operator=(SelfResolvable&& other) { + if (this != &other) { + (*resolver_) = nullptr; + resolver_ = std::move(other.resolver_); + (*resolver_) = static_cast<T*>(this); + } + return *this; + } + + virtual ~SelfResolvable() { + if (resolver_ != nullptr) (*resolver_) = nullptr; + } + + ObjectResolver<T> CreateResolver() { return ObjectResolver<T>(resolver_); } + + private: + std::shared_ptr<T*> resolver_; +}; +} // namespace cru |