Show pageOld revisionsBacklinksExport to PDFBack to top This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== Common Patterns ====== ===== Singleton (Unique) ===== 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:<code cpp> 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; } </code> You can then query for the unique trait like so:<code cpp> auto EnemyBalance = GetSingleton<FGameBalance, FEnemy>(Mechanism); </code> en/toolworks/docs/apparatus/patterns.txt Last modified: 2022/06/08 21:45by jispar