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