en:toolworks:docs:apparatus:patterns

This is an old revision of the document!


Common Patterns

Sometimes you need to get an instance of a certain Subject that has a globally unique combination of Traits. This can be thought of something like a singleton pattern within a classical OOP approach.

You can totally achieve that with the help of a simple method like this:

template < typename T, typename ...Ts >
T GetSingleton(AMechanism* const Mechanism)
{
    const auto Filter = FFilter::Make<T, Ts...>();
    const auto Chain = Mechanism->Enchain(Filter);

    auto Cursor = Chain->Iterate(0, 1);
    if (!Cursor.Provide())
    {
        checkNoEntry()
        return T();
    }
    const auto Trait = Cursor.GetTrait<T>();
    verifyf(!Cursor.Provide(), TEXT("Two singleton objects detected!"));
    return Trait;
}

  • en/toolworks/docs/apparatus/patterns.1651658361.txt.gz
  • Last modified: 2022/05/04 12:59
  • by vladius