rycp.Singleton
1.0.1
dotnet add package rycp.Singleton --version 1.0.1
NuGet\Install-Package rycp.Singleton -Version 1.0.1
<PackageReference Include="rycp.Singleton" Version="1.0.1" />
<PackageVersion Include="rycp.Singleton" Version="1.0.1" />
<PackageReference Include="rycp.Singleton" />
paket add rycp.Singleton --version 1.0.1
#r "nuget: rycp.Singleton, 1.0.1"
#:package rycp.Singleton@1.0.1
#addin nuget:?package=rycp.Singleton&version=1.0.1
#tool nuget:?package=rycp.Singleton&version=1.0.1
Singleton
CRTPとmozc式シングルトンを組み合わせた、安全で使いやすいシングルトン量産システムです。
概要
このシングルトン実装は、CRTP(Curiously Recurring Template Pattern) と mozc式シングルトン の長所を組み合わせた、C++向けのヘッダオンリーライブラリです。
テンプレートベースのアプローチにより、型安全性を保ちながら、シンプルで直感的なシングルトン実装が可能になります。
使い方
基本的な使用方法
シングルトンにしたいクラスを以下のように定義するだけです:
class MyClass : public Singleton<MyClass>
{
friend class Singleton<MyClass>;
// private コンストラクタなど必要な実装
private:
MyClass() = default;
};
重要なポイント:
Singleton<MyClass>を継承するSingleton<MyClass>をフレンドクラスに設定する
これだけで、MyClass はシングルトンとして機能します。
インスタンスの取得
MyClass& instance = MyClass::GetInstance();
インスタンスの解放
プログラム終了時に、以下のコードを実行してください:
SingletonController::Release();
このメソッドを呼び出すと、すべてのシングルトンオブジェクトが生成とは逆順で安全に解放されます。
これにより、複数のシングルトン間の依存関係がある場合でも、正しい順序でクリーンアップが行われます。
特徴
✅ 型安全 — テンプレートにより、コンパイル時に型チェックが行われます
✅ ヘッダオンリー — ヘッダファイルをインクルードするだけで使えます
✅ 安全な破棄 — シングルトン間の依存関係を自動的に管理します
使用例
#include "singleton.hpp"
// ロギングシステム
class Logger : public Singleton<Logger>
{
friend class Singleton<Logger>;
private:
Logger() = default;
public:
void Log(const std::string& message)
{
std::cout << "[LOG] " << message << std::endl;
}
};
// 設定管理
class Config : public Singleton<Config>
{
friend class Singleton<Config>;
private:
Config() = default;
public:
void LoadConfig()
{
// 設定ロード処理
}
};
int main()
{
// シングルトンの使用
Logger::GetInstance().Log("Application started");
Config::GetInstance().LoadConfig();
// ... アプリケーション処理 ...
// プログラム終了時にすべてのシングルトンを解放
SingletonController::Release();
return 0;
}
ライセンス
このプロジェクトは MIT License の下で公開されています。
詳しくは LICENSE ファイルをご覧ください。
注意事項
- シングルトンの破棄順序を確実にするため、プログラム終了時には必ず
SingletonController::Release()を呼び出してください
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| native | native is compatible. |
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.