Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
SortedSet_Factory.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Containers_Concrete_SortedSet_Factory_h_
5#define _Stroika_Foundation_Containers_Concrete_SortedSet_Factory_h_
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9/**
10 * \file
11 */
12
14 template <typename T>
15 class SortedSet;
16}
17
18namespace Stroika::Foundation::Containers::Factory {
19
20 using Common::ITotallyOrderingComparer;
21
22 /**
23 * \brief Singleton factory object - Used to create the default backend implementation of a SortedSet<> container; typically not called directly
24 *
25 * Note - you can override the underlying factory dynamically by calling SortedSet_Factory<T,COMPARER>::Register ().
26 *
27 * \note \em Thread-Safety <a href="Thread-Safety.md#C++-Standard-Thread-Safety">C++-Standard-Thread-Safety</a>
28 */
29 template <typename T, ITotallyOrderingComparer<T> COMPARER = less<T>>
31 public:
32 static_assert (not is_reference_v<T> and not is_reference_v<COMPARER>,
33 "typically if this fails its because a (possibly indirect) caller forgot to use forward<TTT>(), or remove_cvref_t");
34
35 public:
36 /**
37 * The type of object produced by the factory.
38 */
40
41 public:
42 /**
43 * Function type to create an ConstructedType object.
44 */
45 using FactoryFunctionType = function<ConstructedType (const COMPARER& comparer)>;
46
47 public:
48 /**
49 * Hints can be used in factory constructor to guide the choice of the best container implementation/backend.
50 */
51 struct Hints {};
52
53 public:
54 /**
55 * Construct a factory for producing new ConstructedType objects. The default is to use whatever was registered with
56 * SortedSet_Factory::Register (), but a specific factory can easily be constructed with provided arguments.
57 */
58 constexpr SortedSet_Factory ();
59 constexpr SortedSet_Factory (const Hints& hints);
60 constexpr SortedSet_Factory (const FactoryFunctionType& f);
61 constexpr SortedSet_Factory (const SortedSet_Factory&) = default;
62
63 public:
64 /**
65 * This can be called anytime, before main(), or after. BUT - beware, any calls to Register must
66 * be externally synchronized, meaning effectively that they must happen before the creation of any
67 * threads, to be safe. Also note, since this returns a const reference, any calls to Register() after
68 * a call to Default, even if synchronized, is suspect.
69 */
70 static const SortedSet_Factory& Default ();
71
72 public:
73 /**
74 * You can call this directly, but there is no need, as the SortedSet<T,TRAITS> CTOR does so automatically.
75 */
76 nonvirtual ConstructedType operator() (const COMPARER& comparer = {}) const;
77
78 public:
79 /**
80 * Register a default global factory for ConstructedType objects (of the templated type/parameters).
81 * No need to call, typically, as the default factory is generally fine.
82 *
83 * \par Example Usage
84 * \code
85 * SortedSet_Factory::Register(SortedSet_Factory{SortedSet_Factory::Hints{.fOptimizeForLookupSpeedOverUpdateSpeed=true});
86 * SortedSet_Factory::Register(); // or use defaults
87 * \endcode
88 *
89 * \note \em Thread-Safety <a href="Thread-Safety.md#C++-Standard-Thread-Safety">C++-Standard-Thread-Safety</a>
90 * BUT - special note/restriction - must be called before any threads call Association_Factory::SortedSet_Factory() OR
91 * SortedSet_Factory::Default(), which effectively means must be called at the start of main, but before creating any threads
92 * which might use the factory).
93 *
94 * \note this differs markedly from Stroika 2.1, where Register could be called anytime, and was internally synchronized.
95 *
96 * \note If you wanted a dynamically changeable factory (change after main), you could write one yourself with its own internal synchronization,
97 * set the global one here, then perform the changes to its internal structure through another API.
98 */
99 static void Register (const optional<SortedSet_Factory>& f = nullopt);
100
101 private:
102 FactoryFunctionType fFactory_;
103
104 private:
105 // function to assure magically constructed even if called before main
106 static SortedSet_Factory& AccessDefault_ ();
107 };
108
109}
110
111/*
112 ********************************************************************************
113 ******************************* Implementation Details *************************
114 ********************************************************************************
115 */
116#include "SortedSet_Factory.inl"
117
118#endif /*_Stroika_Foundation_Containers_Concrete_SortedSet_Factory_h_ */
Singleton factory object - Used to create the default backend implementation of a SortedSet<> contain...
function< ConstructedType(const COMPARER &comparer)> FactoryFunctionType
nonvirtual ConstructedType operator()(const COMPARER &comparer={}) const
static void Register(const optional< SortedSet_Factory > &f=nullopt)