Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed translate #67

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions cpp/Platform.Disposables/Disposable.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
namespace Platform::Disposables
#ifndef DISPOSABLES_DISPOSABLE_H
#define DISPOSABLES_DISPOSABLE_H

#include <functional>

#include "DisposableBase.h"
#include "Disposal.h"

namespace Platform::Disposables
{
template <typename ...> class Disposable;
template<> class Disposable<> : public DisposableBase
Expand All @@ -22,22 +30,22 @@

public: Disposable() { OnDispose = _emptyDelegate; }

public: Disposable(std::function<void()> action) : Disposable(action) { }

public: Disposable(std::function<Disposal> disposal) : Disposable(disposal) { }

protected: void Dispose(bool manual, bool wasDisposed) override { this->RaiseOnDisposeEvent(manual, wasDisposed); }

protected: void RaiseOnDisposeEvent(bool manual, bool wasDisposed) { this->OnDispose(manual, wasDisposed); }

public: template <typename T> static bool TryDisposeAndResetToDefault(T* object)
{
auto result = object.TryDispose();
auto result = object->TryDispose();
if (result)
{
object = 0;
}
return result;
}
};
}
}


#endif
51 changes: 35 additions & 16 deletions cpp/Platform.Disposables/DisposableBase.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
namespace Platform::Disposables
#ifndef DISPOSABLES_DISPOSABLE_BASE_H
#define DISPOSABLES_DISPOSABLE_BASE_H

#include <stack>




#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<WeakReference<DisposableBase>> _disposablesWeekReferencesStack = ConcurrentStack<WeakReference<DisposableBase>>();
private: static std::stack<std::weak_ptr<DisposableBase>> _disposablesWeekReferencesStack;

private: volatile std::int32_t _disposed;
private: volatile std::atomic<std::int32_t> _disposed;

public: bool IsDisposed()
{
Expand All @@ -26,12 +41,12 @@
return false;
}

static DisposableBase() { std::atexit(OnProcessExit); }

protected: DisposableBase()
{
_disposed = 0;
_disposablesWeekReferencesStack.Push(WeakReference<DisposableBase>(this, false));
_disposablesWeekReferencesStack.push(std::weak_ptr<DisposableBase>(std::shared_ptr<DisposableBase>(this)));
std::atexit(OnProcessExit);
}

~DisposableBase() { Destruct(); }
Expand All @@ -41,14 +56,13 @@
public: void Dispose()
{
this->Dispose(true);
GC.SuppressFinalize(this);
}

public: void Destruct()
{
try
{
if (!IsDisposed)
if (!IsDisposed())
{
this->Dispose(false);
}
Expand All @@ -61,28 +75,33 @@

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);
}
}

private: static void OnProcessExit()
{
while (_disposablesWeekReferencesStack.TryPop(out WeakReference<DisposableBase> 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();
}
}
}
};
}
}

#endif
22 changes: 17 additions & 5 deletions cpp/Platform.Disposables/Disposable[TPrimary, TAuxiliary].h
Original file line number Diff line number Diff line change
@@ -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 <typename ...> class Disposable;

template <typename TPrimary, typename TAuxiliary> class Disposable<TPrimary, TAuxiliary> : public Disposable<TPrimary>
{
public: const TAuxiliary AuxiliaryObject;
public: Platform::Delegates::MulticastDelegate<Disposal> OnDispose;
public: const TPrimary Object;

public: Disposable(TPrimary object, TAuxiliary auxiliaryObject, std::function<void(TPrimary, TAuxiliary)> action)
: Disposable<TPrimary>(object)
Expand All @@ -18,11 +26,11 @@
};
}

public: Disposable(TPrimary object, TAuxiliary auxiliaryObject, std::function<void()> action) : Disposable<TPrimary>(object, action) { return AuxiliaryObject = auxiliaryObject; }
public: Disposable(TPrimary object, TAuxiliary auxiliaryObject, std::function<void()> action) : Disposable<TPrimary>(object, action) { AuxiliaryObject = auxiliaryObject; }

public: Disposable(TPrimary object, TAuxiliary auxiliaryObject, Disposal disposal) : Disposable<TPrimary>(object, disposal) { return AuxiliaryObject = auxiliaryObject; }
public: Disposable(TPrimary object, TAuxiliary auxiliaryObject, Disposal disposal) : Disposable<TPrimary>(object, disposal) { AuxiliaryObject = auxiliaryObject; }

public: Disposable(TPrimary object, TAuxiliary auxiliaryObject) : Disposable<TPrimary>(object) { return AuxiliaryObject = auxiliaryObject; }
public: Disposable(TPrimary object, TAuxiliary auxiliaryObject) : Disposable<TPrimary>(object) { AuxiliaryObject = auxiliaryObject; }

public: Disposable(TPrimary object) : Disposable<TPrimary>(object) { }

Expand All @@ -44,5 +52,9 @@
AuxiliaryObject.TryDispose();
Object.TryDispose();
}


};
}

#endif
27 changes: 21 additions & 6 deletions cpp/Platform.Disposables/Disposable[T].h
Original file line number Diff line number Diff line change
@@ -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 <typename ...> class Disposable;

template <typename T> class Disposable<T> : public Disposable<>
{
public: const T Object;
Expand All @@ -17,9 +25,9 @@
};
}

public: Disposable(T object, std::function<void()> action) : Disposable<>(action) { return Object = object; }
public: Disposable(T object, std::function<void()> 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; }

Expand All @@ -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();
}
};
}
}

#endif
7 changes: 6 additions & 1 deletion cpp/Platform.Disposables/Disposal.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
namespace Platform::Disposables
#ifndef DISPOSABLES_DISPOSAl_H
#define DISPOSABLES_DISPOSAl_H

namespace Platform::Disposables
{
using Disposal = void(bool, bool);
}

#endif
15 changes: 13 additions & 2 deletions cpp/Platform.Disposables/EnsureExtensions.h
Original file line number Diff line number Diff line change
@@ -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, '.'));
}
Expand All @@ -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
16 changes: 11 additions & 5 deletions cpp/Platform.Disposables/GenericObjectExtensions.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
namespace Platform::Disposables
#ifndef DISPOSABLES_GENERIC_DISPOSABLE_H
#define DISPOSABLES_GENERIC_DISPOSABLE_H

namespace Platform::Disposables
{
class GenericObjectExtensions
{
public: template <typename T> static bool TryDispose(T object)
{
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;
}
Expand All @@ -26,3 +30,5 @@
public: template <typename T> static void DisposeIfPossible(T object) { TryDispose(object); }
};
}

#endif
12 changes: 10 additions & 2 deletions cpp/Platform.Disposables/IDisposable.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
namespace Platform::Disposables
#ifndef DISPOSABLES_IDISPOSABLE_H
#define DISPOSABLES_IDISPOSABLE_H

#include "System.IDisposable.h"


namespace Platform::Disposables
{
class IDisposable : public System::IDisposable
{
Expand All @@ -7,4 +13,6 @@

virtual void Destruct() = 0;
};
}
}

#endif
11 changes: 9 additions & 2 deletions cpp/Platform.Disposables/IDisposableExtensions.h
Original file line number Diff line number Diff line change
@@ -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
13 changes: 9 additions & 4 deletions cpp/Platform.Disposables/Platform.Disposables.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
#ifndef PLATFORM_DISPOSABLES
#define PLATFORM_DISPOSABLES

//#include <Platform.Converters.h>
//#include <Platform.Hashing.h>

#include <Platform.Delegates.h>
#include <Platform.Exceptions.h>
//TODO: this real includes
//#include <Platform.Converters.h>
//#include <Platform.Hashing.h>
//

//TODO: this test includes
#include "../../../Delegates/cpp/Platform.Delegates/Platform.Delegates.h"
#include "../../../Exceptions/cpp/Platform.Exceptions/Platform.Exceptions.h"
//

#include "System.IDisposable.h"

Expand Down