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