From bfed46e04629d9b628cfcd6f427724123e755598 Mon Sep 17 00:00:00 2001 From: 002_cap Date: Sun, 7 Feb 2021 19:01:52 +0300 Subject: [PATCH 1/3] =?UTF-8?q?(=D0=94=D0=BB=D1=8F=20=D0=BD=D0=B0=D1=87?= =?UTF-8?q?=D0=B0=D0=BB=D0=B0=20=D1=81=D1=82=D0=BE=D0=B8=D1=82=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BD=D0=B5=D1=87=D0=BD=D0=BE=20=D0=BF=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=B5=D1=80=D0=B8=D1=82=D1=8C=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BE=D1=81=D0=BF=D0=BE=D1=81=D0=BE=D0=B1=D0=BD=D0=BE=D1=81?= =?UTF-8?q?=D1=82=D1=8C,=20=D1=87=D1=82=D0=BE=20=D1=83=20=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D1=8F=20=D0=BD=D0=B5=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87?= =?UTF-8?q?=D0=B8=D0=BB=D0=BE=D1=81=D1=8C=20=D1=81=D0=B4=D0=B5=D0=BB=D0=B0?= =?UTF-8?q?=D1=82=D1=8C=20=D0=B8=D0=B7-=D0=B7=D0=B0=20=D0=B4=D0=BE=D0=B2?= =?UTF-8?q?=D0=BE=D0=BB=D1=8C=D0=BD=D0=BE=20=D1=81=D1=82=D1=80=D0=B0=D0=BD?= =?UTF-8?q?=D0=BD=D0=BE=D0=B9=20=D1=87=D0=B5=D1=80=D0=B5=D0=B4=D1=8B=20?= =?UTF-8?q?=D1=81=D0=BE=D0=B1=D1=8B=D1=82=D0=B8=D0=B9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cpp/Platform.Disposables/Disposable.h | 20 +++++--- cpp/Platform.Disposables/DisposableBase.h | 51 +++++++++++++------ .../Disposable[TPrimary, TAuxiliary].h | 22 ++++++-- cpp/Platform.Disposables/Disposable[T].h | 27 +++++++--- cpp/Platform.Disposables/Disposal.h | 7 ++- cpp/Platform.Disposables/EnsureExtensions.h | 15 +++++- .../GenericObjectExtensions.h | 16 ++++-- cpp/Platform.Disposables/IDisposable.h | 11 +++- .../IDisposableExtensions.h | 11 +++- .../Platform.Disposables.h | 13 +++-- 10 files changed, 144 insertions(+), 49 deletions(-) diff --git a/cpp/Platform.Disposables/Disposable.h b/cpp/Platform.Disposables/Disposable.h index 7d0e907..e813850 100644 --- a/cpp/Platform.Disposables/Disposable.h +++ b/cpp/Platform.Disposables/Disposable.h @@ -1,4 +1,12 @@ -namespace Platform::Disposables +#ifndef DISPOSABLES_DISPOSABLE_H +#define DISPOSABLES_DISPOSABLE_H + +#include + +#include "DisposableBase.h" +#include "Disposal.h" + +namespace Platform::Disposables { template class Disposable; template<> class Disposable<> : public DisposableBase @@ -22,9 +30,6 @@ public: Disposable() { OnDispose = _emptyDelegate; } - public: Disposable(std::function action) : Disposable(action) { } - - public: Disposable(std::function disposal) : Disposable(disposal) { } protected: void Dispose(bool manual, bool wasDisposed) override { this->RaiseOnDisposeEvent(manual, wasDisposed); } @@ -32,7 +37,7 @@ public: template static bool TryDisposeAndResetToDefault(T* object) { - auto result = object.TryDispose(); + auto result = object->TryDispose(); if (result) { object = 0; @@ -40,4 +45,7 @@ return result; } }; -} \ No newline at end of file +} + + +#endif \ No newline at end of file diff --git a/cpp/Platform.Disposables/DisposableBase.h b/cpp/Platform.Disposables/DisposableBase.h index 9e651eb..d78d2d8 100644 --- a/cpp/Platform.Disposables/DisposableBase.h +++ b/cpp/Platform.Disposables/DisposableBase.h @@ -1,10 +1,25 @@ -namespace Platform::Disposables +#ifndef DISPOSABLES_DISPOSABLE_BASE_H +#define DISPOSABLES_DISPOSABLE_BASE_H + +#include + + + + +#include "IDisposable.h" +#include "EnsureExtensions.h" +#include "../../../Exceptions/cpp/Platform.Exceptions/IgnoredExceptions.h" +#include "../../../Exceptions/cpp/Platform.Exceptions/ExceptionExtensions.h" +#include "../../../Exceptions/cpp/Platform.Exceptions/Ensure.h" + + +namespace Platform::Disposables { class DisposableBase : public IDisposable { - private: static readonly ConcurrentStack> _disposablesWeekReferencesStack = ConcurrentStack>(); + private: static std::stack> _disposablesWeekReferencesStack; - private: volatile std::int32_t _disposed; + private: volatile std::atomic _disposed; public: bool IsDisposed() { @@ -26,12 +41,12 @@ return false; } - static DisposableBase() { std::atexit(OnProcessExit); } protected: DisposableBase() { _disposed = 0; - _disposablesWeekReferencesStack.Push(WeakReference(this, false)); + _disposablesWeekReferencesStack.push(std::weak_ptr(std::shared_ptr(this))); + std::atexit(OnProcessExit); } ~DisposableBase() { Destruct(); } @@ -41,14 +56,13 @@ public: void Dispose() { this->Dispose(true); - GC.SuppressFinalize(this); } public: void Destruct() { try { - if (!IsDisposed) + if (!IsDisposed()) { this->Dispose(false); } @@ -61,13 +75,14 @@ protected: virtual void Dispose(bool manual) { - auto originalDisposedValue = Interlocked.CompareExchange(ref _disposed, 1, 0); + int compare_value = 1; + bool originalDisposedValue = _disposed.compare_exchange_weak(compare_value, 0); auto wasDisposed = originalDisposedValue > 0; - if (wasDisposed && !AllowMultipleDisposeCalls && manual) + if (wasDisposed && !AllowMultipleDisposeCalls() && manual) { - Platform::Disposables::EnsureExtensions::NotDisposed(Platform::Exceptions::Ensure::Always, this, ObjectName, "Multiple dispose calls are not allowed. Override AllowMultipleDisposeCalls property to modify behavior."); + Platform::Disposables::EnsureExtensions::NotDisposed(Platform::Exceptions::Ensure::Always, *this, ObjectName(), "Multiple dispose calls are not allowed. Override AllowMultipleDisposeCalls property to modify behavior."); } - if (AllowMultipleDisposeAttempts || !wasDisposed) + if (AllowMultipleDisposeAttempts() || !wasDisposed) { this->Dispose(manual, wasDisposed); } @@ -75,14 +90,18 @@ private: static void OnProcessExit() { - while (_disposablesWeekReferencesStack.TryPop(out WeakReference weakReference)) + while (!_disposablesWeekReferencesStack.empty()) { - if (weakReference.TryGetTarget(out DisposableBase disposable)) + auto weakReference = _disposablesWeekReferencesStack.top(); + _disposablesWeekReferencesStack.pop(); + + if (auto disposable = weakReference.lock()) { - GC.SuppressFinalize(disposable); - disposable.Destruct(); + disposable->Destruct(); } } } }; -} \ No newline at end of file +} + +#endif \ No newline at end of file diff --git a/cpp/Platform.Disposables/Disposable[TPrimary, TAuxiliary].h b/cpp/Platform.Disposables/Disposable[TPrimary, TAuxiliary].h index a8cb670..ab70099 100644 --- a/cpp/Platform.Disposables/Disposable[TPrimary, TAuxiliary].h +++ b/cpp/Platform.Disposables/Disposable[TPrimary, TAuxiliary].h @@ -1,9 +1,17 @@ -namespace Platform::Disposables +#ifndef DISPOSABLES_DISPOSABLE_T1_T2_H +#define DISPOSABLES_DISPOSABLE_T1_T2_H + +#include "Disposal.h" +#include "Disposable[T].h" + +namespace Platform::Disposables { - template class Disposable; + template class Disposable : public Disposable { public: const TAuxiliary AuxiliaryObject; + public: Platform::Delegates::MulticastDelegate OnDispose; + public: const TPrimary Object; public: Disposable(TPrimary object, TAuxiliary auxiliaryObject, std::function action) : Disposable(object) @@ -18,11 +26,11 @@ }; } - public: Disposable(TPrimary object, TAuxiliary auxiliaryObject, std::function action) : Disposable(object, action) { return AuxiliaryObject = auxiliaryObject; } + public: Disposable(TPrimary object, TAuxiliary auxiliaryObject, std::function action) : Disposable(object, action) { AuxiliaryObject = auxiliaryObject; } - public: Disposable(TPrimary object, TAuxiliary auxiliaryObject, Disposal disposal) : Disposable(object, disposal) { return AuxiliaryObject = auxiliaryObject; } + public: Disposable(TPrimary object, TAuxiliary auxiliaryObject, Disposal disposal) : Disposable(object, disposal) { AuxiliaryObject = auxiliaryObject; } - public: Disposable(TPrimary object, TAuxiliary auxiliaryObject) : Disposable(object) { return AuxiliaryObject = auxiliaryObject; } + public: Disposable(TPrimary object, TAuxiliary auxiliaryObject) : Disposable(object) { AuxiliaryObject = auxiliaryObject; } public: Disposable(TPrimary object) : Disposable(object) { } @@ -44,5 +52,9 @@ AuxiliaryObject.TryDispose(); Object.TryDispose(); } + + }; } + +#endif \ No newline at end of file diff --git a/cpp/Platform.Disposables/Disposable[T].h b/cpp/Platform.Disposables/Disposable[T].h index 246d557..db8430b 100644 --- a/cpp/Platform.Disposables/Disposable[T].h +++ b/cpp/Platform.Disposables/Disposable[T].h @@ -1,6 +1,14 @@ -namespace Platform::Disposables +#ifndef DISPOSABLES_DISPOSABLE_T_H +#define DISPOSABLES_DISPOSABLE_T_H + + +#include "Disposal.h" +#include "Disposable.h" + + +namespace Platform::Disposables { - template class Disposable; + template class Disposable : public Disposable<> { public: const T Object; @@ -17,9 +25,9 @@ }; } - public: Disposable(T object, std::function action) : Disposable<>(action) { return Object = object; } + public: Disposable(T object, std::function action) : Disposable<>(action) { Object = object; } - public: Disposable(T object, Disposal disposal) : Disposable<>(disposal) { return Object = object; } + public: Disposable(T object, Disposal disposal) : Disposable<>(disposal) { Object = object; } public: Disposable(T object) { Object = object; } @@ -33,8 +41,15 @@ protected: void Dispose(bool manual, bool wasDisposed) override { - base.Dispose(manual, wasDisposed); + // о нет так можно делать только в visual c++ + // __super::Dispose(manual, wasDisposed) + // ---------------------------------- + // base.Dispose(manual, wasDisposed); + + RaiseOnDisposeEvent(manual, wasDisposed); Object.TryDispose(); } }; -} \ No newline at end of file +} + +#endif \ No newline at end of file diff --git a/cpp/Platform.Disposables/Disposal.h b/cpp/Platform.Disposables/Disposal.h index c305842..62a8fc3 100644 --- a/cpp/Platform.Disposables/Disposal.h +++ b/cpp/Platform.Disposables/Disposal.h @@ -1,4 +1,9 @@ -namespace Platform::Disposables +#ifndef DISPOSABLES_DISPOSAl_H +#define DISPOSABLES_DISPOSAl_H + +namespace Platform::Disposables { using Disposal = void(bool, bool); } + +#endif \ No newline at end of file diff --git a/cpp/Platform.Disposables/EnsureExtensions.h b/cpp/Platform.Disposables/EnsureExtensions.h index 1126638..90fb695 100644 --- a/cpp/Platform.Disposables/EnsureExtensions.h +++ b/cpp/Platform.Disposables/EnsureExtensions.h @@ -1,10 +1,19 @@ -namespace Platform::Disposables +#ifndef DISPOSABLES_ENSURE_EXTENSION_H +#define DISPOSABLES_ENSURE_EXTENSION_H + +//TODO: this test includes +#include "../../../Exceptions/cpp/Platform.Exceptions/ExtensionRoots/EnsureAlwaysExtensionRoot.h" +#include "../../../Exceptions/cpp/Platform.Exceptions/ExtensionRoots/EnsureOnDebugExtensionRoot.h" +#include "../../../Exceptions/cpp/Platform.Exceptions/Ensure.h" + + +namespace Platform::Disposables { class EnsureExtensions { public: static void NotDisposed(Platform::Exceptions::ExtensionRoots::EnsureAlwaysExtensionRoot root, IDisposable &disposable, std::string objectName, std::string message) { - if (disposable.IsDisposed) + if (disposable.IsDisposed()) { throw std::runtime_error(std::string("Attempt to access disposed object [").append(objectName).append("]: ").append(message).append(1, '.')); } @@ -21,3 +30,5 @@ public: static void NotDisposed(Platform::Exceptions::ExtensionRoots::EnsureOnDebugExtensionRoot root, IDisposable &disposable) { Platform::Disposables::EnsureExtensions::NotDisposed(Platform::Exceptions::Ensure::Always, disposable, {}, {}); } }; } + +#endif \ No newline at end of file diff --git a/cpp/Platform.Disposables/GenericObjectExtensions.h b/cpp/Platform.Disposables/GenericObjectExtensions.h index cfb7680..38e1e35 100644 --- a/cpp/Platform.Disposables/GenericObjectExtensions.h +++ b/cpp/Platform.Disposables/GenericObjectExtensions.h @@ -1,4 +1,7 @@ -namespace Platform::Disposables +#ifndef DISPOSABLES_GENERIC_DISPOSABLE_H +#define DISPOSABLES_GENERIC_DISPOSABLE_H + +namespace Platform::Disposables { class GenericObjectExtensions { @@ -6,13 +9,14 @@ { try { - if (object is DisposableBase disposableBase) + if (DisposableBase* disposableBase = (DisposableBase*)object) { - disposableBase.DisposeIfNotDisposed(); + //TODO: add this method + //disposableBase->DisposeIfNotDisposed(); } - else if (object is System::IDisposable &disposable) + else if (System::IDisposable* disposable = (System::IDisposable*)object) { - disposable.Dispose(); + disposable->Dispose(); } return true; } @@ -26,3 +30,5 @@ public: template static void DisposeIfPossible(T object) { TryDispose(object); } }; } + +#endif \ No newline at end of file diff --git a/cpp/Platform.Disposables/IDisposable.h b/cpp/Platform.Disposables/IDisposable.h index 5f4315e..27f60f7 100644 --- a/cpp/Platform.Disposables/IDisposable.h +++ b/cpp/Platform.Disposables/IDisposable.h @@ -1,4 +1,9 @@ -namespace Platform::Disposables +#ifndef DISPOSABLES_IDISPOSABLE_H +#define DISPOSABLES_IDISPOSABLE_H + +#include "System.IDisposable.h" + +namespace Platform::Disposables { class IDisposable : public System::IDisposable { @@ -7,4 +12,6 @@ virtual void Destruct() = 0; }; -} \ No newline at end of file +} + +#endif \ No newline at end of file diff --git a/cpp/Platform.Disposables/IDisposableExtensions.h b/cpp/Platform.Disposables/IDisposableExtensions.h index 98c50cf..f5ae656 100644 --- a/cpp/Platform.Disposables/IDisposableExtensions.h +++ b/cpp/Platform.Disposables/IDisposableExtensions.h @@ -1,13 +1,20 @@ -namespace Platform::Disposables +#ifndef DISPOSABLES_IDISPOSABLE_EXTENSIONS_H +#define DISPOSABLES_IDISPOSABLE_EXTENSIONS_H + + + +namespace Platform::Disposables { class IDisposableExtensions { public: static void DisposeIfNotDisposed(IDisposable &disposable) { - if (!disposable.IsDisposed) + if (!disposable.IsDisposed()) { disposable.Dispose(); } } }; } + +#endif \ No newline at end of file diff --git a/cpp/Platform.Disposables/Platform.Disposables.h b/cpp/Platform.Disposables/Platform.Disposables.h index 2d77860..b6ae950 100644 --- a/cpp/Platform.Disposables/Platform.Disposables.h +++ b/cpp/Platform.Disposables/Platform.Disposables.h @@ -3,11 +3,16 @@ #ifndef PLATFORM_DISPOSABLES #define PLATFORM_DISPOSABLES -//#include -//#include -#include -#include +//TODO: this real includes + //#include + //#include +// + +//TODO: this test includes + #include "../../../Delegates/cpp/Platform.Delegates/Platform.Delegates.h" + #include "../../../Exceptions/cpp/Platform.Exceptions/Platform.Exceptions.h" +// #include "System.IDisposable.h" From e5bc1706830b97995a3434aab0e6ea0ac0cb6657 Mon Sep 17 00:00:00 2001 From: 002_cap Date: Sun, 7 Feb 2021 19:03:19 +0300 Subject: [PATCH 2/3] =?UTF-8?q?(=D0=94=D0=BB=D1=8F=20=D0=BD=D0=B0=D1=87?= =?UTF-8?q?=D0=B0=D0=BB=D0=B0=20=D1=81=D1=82=D0=BE=D0=B8=D1=82=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BD=D0=B5=D1=87=D0=BD=D0=BE=20=D0=BF=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=B5=D1=80=D0=B8=D1=82=D1=8C=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BE=D1=81=D0=BF=D0=BE=D1=81=D0=BE=D0=B1=D0=BD=D0=BE=D1=81?= =?UTF-8?q?=D1=82=D1=8C,=20=D1=87=D1=82=D0=BE=20=D1=83=20=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D1=8F=20=D0=BD=D0=B5=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87?= =?UTF-8?q?=D0=B8=D0=BB=D0=BE=D1=81=D1=8C=20=D1=81=D0=B4=D0=B5=D0=BB=D0=B0?= =?UTF-8?q?=D1=82=D1=8C=20=D0=B8=D0=B7-=D0=B7=D0=B0=20=D0=B4=D0=BE=D0=B2?= =?UTF-8?q?=D0=BE=D0=BB=D1=8C=D0=BD=D0=BE=20=D1=81=D1=82=D1=80=D0=B0=D0=BD?= =?UTF-8?q?=D0=BD=D0=BE=D0=B9=20=D1=87=D0=B5=D1=80=D0=B5=D0=B4=D1=8B=20?= =?UTF-8?q?=D1=81=D0=BE=D0=B1=D1=8B=D1=82=D0=B8=D0=B9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (См. TODO comment) --- cpp/Platform.Disposables/IDisposable.h | 1 + 1 file changed, 1 insertion(+) diff --git a/cpp/Platform.Disposables/IDisposable.h b/cpp/Platform.Disposables/IDisposable.h index 27f60f7..534697b 100644 --- a/cpp/Platform.Disposables/IDisposable.h +++ b/cpp/Platform.Disposables/IDisposable.h @@ -3,6 +3,7 @@ #include "System.IDisposable.h" + namespace Platform::Disposables { class IDisposable : public System::IDisposable From ca2585f8ed8316353c779626e4aa85e4b71b6e79 Mon Sep 17 00:00:00 2001 From: 002_cap Date: Sun, 14 Feb 2021 16:52:05 +0300 Subject: [PATCH 3/3] =?UTF-8?q?=D0=92=20=D0=BF=D1=80=D0=B8=D0=BD=D1=86?= =?UTF-8?q?=D0=B8=D0=BF=D0=B5=20=D0=B2=D1=81=D1=91=20(=D1=81=D1=82=D0=B5?= =?UTF-8?q?=D0=BA=20=D0=BD=D0=B0=20=D0=BC=D1=8C=D1=8E=D1=82=D0=B5=D0=BA?= =?UTF-8?q?=D1=81=D0=B0=D1=85=3F=20=D0=BA=D0=BE=D0=BC=D1=83=20=D0=BE=D0=BD?= =?UTF-8?q?=20=D0=BD=D1=83=D0=B6=D0=B5=D0=BD=3F=20=D1=8D=D1=82=D0=BE=20?= =?UTF-8?q?=D0=B2=D0=B5=D0=B4=D1=8C=20=D0=B4=D0=B5=D1=82=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8?= =?UTF-8?q?=20=D0=B2=D1=81=D1=91-=D1=82=D0=B0=D0=BA=D0=B8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ((все странности помечены знаменитыми TODO comments)) --- cpp/Platform.Disposables/DisposableBase.h | 28 ++++++++++++------- cpp/Platform.Disposables/EnsureExtensions.h | 19 +++++-------- .../GenericObjectExtensions.h | 7 +++-- .../Platform.Disposables.h | 11 ++------ 4 files changed, 32 insertions(+), 33 deletions(-) diff --git a/cpp/Platform.Disposables/DisposableBase.h b/cpp/Platform.Disposables/DisposableBase.h index d78d2d8..7206191 100644 --- a/cpp/Platform.Disposables/DisposableBase.h +++ b/cpp/Platform.Disposables/DisposableBase.h @@ -19,9 +19,9 @@ namespace Platform::Disposables { private: static std::stack> _disposablesWeekReferencesStack; - private: volatile std::atomic _disposed; + private: volatile std::atomic _disposed; - public: bool IsDisposed() + public: bool IsDisposed() override { return _disposed > 0; } @@ -42,23 +42,29 @@ namespace Platform::Disposables } - protected: DisposableBase() + public: DisposableBase() { + std::shared_ptr this_ptr = std::shared_ptr(this); + std::weak_ptr this_weak = std::weak_ptr(this_ptr); + + _disposed = 0; - _disposablesWeekReferencesStack.push(std::weak_ptr(std::shared_ptr(this))); + _disposablesWeekReferencesStack.push(this_weak); std::atexit(OnProcessExit); } - ~DisposableBase() { Destruct(); } + //TODO: завязывайте с вызовом всякой виртуальщины в конструкторе/деструкторе + //~DisposableBase() { Destruct(); } - protected: virtual void Dispose(bool manual, bool wasDisposed) = 0; + public: virtual void Dispose(bool manual, bool wasDisposed) = 0; - public: void Dispose() + + public: void Dispose() override { this->Dispose(true); } - public: void Destruct() + public: void Destruct() override { try { @@ -73,14 +79,14 @@ namespace Platform::Disposables } } - protected: virtual void Dispose(bool manual) + public: virtual void Dispose(bool manual) { int compare_value = 1; bool originalDisposedValue = _disposed.compare_exchange_weak(compare_value, 0); auto wasDisposed = originalDisposedValue > 0; if (wasDisposed && !AllowMultipleDisposeCalls() && manual) { - Platform::Disposables::EnsureExtensions::NotDisposed(Platform::Exceptions::Ensure::Always, *this, ObjectName(), "Multiple dispose calls are not allowed. Override AllowMultipleDisposeCalls property to modify behavior."); + Platform::Disposables::EnsureExtensions::NotDisposed(Platform::Exceptions::Ensure::Always, this, ObjectName(), "Multiple dispose calls are not allowed. Override AllowMultipleDisposeCalls property to modify behavior."); } if (AllowMultipleDisposeAttempts() || !wasDisposed) { @@ -104,4 +110,6 @@ namespace Platform::Disposables }; } +std::stack> Platform::Disposables::DisposableBase::_disposablesWeekReferencesStack = std::stack>(); + #endif \ No newline at end of file diff --git a/cpp/Platform.Disposables/EnsureExtensions.h b/cpp/Platform.Disposables/EnsureExtensions.h index 90fb695..7f23f16 100644 --- a/cpp/Platform.Disposables/EnsureExtensions.h +++ b/cpp/Platform.Disposables/EnsureExtensions.h @@ -1,33 +1,28 @@ #ifndef DISPOSABLES_ENSURE_EXTENSION_H #define DISPOSABLES_ENSURE_EXTENSION_H -//TODO: this test includes -#include "../../../Exceptions/cpp/Platform.Exceptions/ExtensionRoots/EnsureAlwaysExtensionRoot.h" -#include "../../../Exceptions/cpp/Platform.Exceptions/ExtensionRoots/EnsureOnDebugExtensionRoot.h" -#include "../../../Exceptions/cpp/Platform.Exceptions/Ensure.h" - namespace Platform::Disposables { class EnsureExtensions { - public: static void NotDisposed(Platform::Exceptions::ExtensionRoots::EnsureAlwaysExtensionRoot root, IDisposable &disposable, std::string objectName, std::string message) + public: static void NotDisposed(Platform::Exceptions::ExtensionRoots::EnsureAlwaysExtensionRoot root, IDisposable* disposable, std::string objectName, std::string message) { - if (disposable.IsDisposed()) + if (disposable->IsDisposed()) { throw std::runtime_error(std::string("Attempt to access disposed object [").append(objectName).append("]: ").append(message).append(1, '.')); } } - public: static void NotDisposed(Platform::Exceptions::ExtensionRoots::EnsureAlwaysExtensionRoot root, IDisposable &disposable, std::string objectName) { NotDisposed(root, disposable, objectName, {}); } + public: static void NotDisposed(Platform::Exceptions::ExtensionRoots::EnsureAlwaysExtensionRoot root, IDisposable* disposable, std::string objectName) { NotDisposed(root, disposable, objectName, {}); } - public: static void NotDisposed(Platform::Exceptions::ExtensionRoots::EnsureAlwaysExtensionRoot root, IDisposable &disposable) { NotDisposed(root, disposable, {}, {}); } + public: static void NotDisposed(Platform::Exceptions::ExtensionRoots::EnsureAlwaysExtensionRoot root, IDisposable* disposable) { NotDisposed(root, disposable, {}, {}); } - public: static void NotDisposed(Platform::Exceptions::ExtensionRoots::EnsureOnDebugExtensionRoot root, IDisposable &disposable, std::string objectName, std::string message) { Platform::Disposables::EnsureExtensions::NotDisposed(Platform::Exceptions::Ensure::Always, disposable, objectName, message); } + public: static void NotDisposed(Platform::Exceptions::ExtensionRoots::EnsureOnDebugExtensionRoot root, IDisposable* disposable, std::string objectName, std::string message) { Platform::Disposables::EnsureExtensions::NotDisposed(Platform::Exceptions::Ensure::Always, disposable, objectName, message); } - public: static void NotDisposed(Platform::Exceptions::ExtensionRoots::EnsureOnDebugExtensionRoot root, IDisposable &disposable, std::string objectName) { Platform::Disposables::EnsureExtensions::NotDisposed(Platform::Exceptions::Ensure::Always, disposable, objectName, {}); } + public: static void NotDisposed(Platform::Exceptions::ExtensionRoots::EnsureOnDebugExtensionRoot root, IDisposable* disposable, std::string objectName) { Platform::Disposables::EnsureExtensions::NotDisposed(Platform::Exceptions::Ensure::Always, disposable, objectName, {}); } - public: static void NotDisposed(Platform::Exceptions::ExtensionRoots::EnsureOnDebugExtensionRoot root, IDisposable &disposable) { Platform::Disposables::EnsureExtensions::NotDisposed(Platform::Exceptions::Ensure::Always, disposable, {}, {}); } + public: static void NotDisposed(Platform::Exceptions::ExtensionRoots::EnsureOnDebugExtensionRoot root, IDisposable* disposable) { Platform::Disposables::EnsureExtensions::NotDisposed(Platform::Exceptions::Ensure::Always, disposable, {}, {}); } }; } diff --git a/cpp/Platform.Disposables/GenericObjectExtensions.h b/cpp/Platform.Disposables/GenericObjectExtensions.h index 38e1e35..1b6215a 100644 --- a/cpp/Platform.Disposables/GenericObjectExtensions.h +++ b/cpp/Platform.Disposables/GenericObjectExtensions.h @@ -9,13 +9,16 @@ namespace Platform::Disposables { try { - if (DisposableBase* disposableBase = (DisposableBase*)object) + if (std::is_base_of::value) // до изменения задания: if(disposableBase = (DisposableBase*)object) { + auto disposableBase = (DisposableBase*)object; + //TODO: add this method //disposableBase->DisposeIfNotDisposed(); } - else if (System::IDisposable* disposable = (System::IDisposable*)object) + else if (std::is_base_of::value) // до изменения задания: if(disposable = (IDisposable*)object) { + auto disposable = (IDisposable*)object; disposable->Dispose(); } return true; diff --git a/cpp/Platform.Disposables/Platform.Disposables.h b/cpp/Platform.Disposables/Platform.Disposables.h index b6ae950..b18d696 100644 --- a/cpp/Platform.Disposables/Platform.Disposables.h +++ b/cpp/Platform.Disposables/Platform.Disposables.h @@ -4,15 +4,8 @@ #define PLATFORM_DISPOSABLES -//TODO: this real includes - //#include - //#include -// - -//TODO: this test includes - #include "../../../Delegates/cpp/Platform.Delegates/Platform.Delegates.h" - #include "../../../Exceptions/cpp/Platform.Exceptions/Platform.Exceptions.h" -// + + #include "System.IDisposable.h"