Skip to content

Commit

Permalink
Refactoring ISingleton
Browse files Browse the repository at this point in the history
  • Loading branch information
iconstudio committed Oct 6, 2023
1 parent 122d32e commit c7f60fb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Editor/TestEditor/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected override async void OnLaunched(LaunchActivatedEventArgs args)
myProject.Activate();
}

public static App GetInstance() => ISingleton<App>.SingleInstance;
public static App GetInstance() => ISingleton<App>.Instance;

private static LRESULT MainHook(HWND hWnd, uint uMsg, WPARAM wParam, LPARAM lParam, nuint id, nuint refdata)
{
Expand Down
2 changes: 1 addition & 1 deletion Editor/TestEditor/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ public MainWindow()
}

public Frame GetContent() => rootFrame;
public static MainWindow GetInstance() => ISingleton<MainWindow>.SingleInstance;
public static MainWindow GetInstance() => ISingleton<MainWindow>.Instance;
}
}
17 changes: 8 additions & 9 deletions Editor/TestEditor/Utilities/ISingleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,35 @@
{
public interface ISingleton<T> where T : class, new()
{
private static T _Instance;

public static T SingleInstance => _Instance;
protected static T Instance { get; set; }
protected static T SingleInstance => Instance;

/// <param name="instance"></param>
/// <exception cref="SingletonException"></exception>
public static void SetInstance(T instance)
{
if (_Instance is not null)
if (Instance is not null)
{
throw new SingletonException($"Duplicated instance of {nameof(T)}");
}

_Instance = instance;
Instance = instance;
}
public static bool TryInstance(T instance)
{
if (_Instance is not null)
if (Instance is not null)
{
return false;
}

_Instance = instance;
Instance = instance;
return true;
}
public static T GetInstance()
{
lock (SingleInstance)
lock (Instance)
{
return SingleInstance;
return Instance;
}
}
}
Expand Down

0 comments on commit c7f60fb

Please sign in to comment.