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