rycp.Singleton 1.0.1

dotnet add package rycp.Singleton --version 1.0.1
                    
NuGet\Install-Package rycp.Singleton -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="rycp.Singleton" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="rycp.Singleton" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="rycp.Singleton" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add rycp.Singleton --version 1.0.1
                    
#r "nuget: rycp.Singleton, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package rycp.Singleton@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=rycp.Singleton&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=rycp.Singleton&version=1.0.1
                    
Install as a Cake Tool

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 Compatible and additional computed target framework versions.
native native is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last Updated
1.0.1 75 5/30/2026
1.0.0 72 5/30/2026