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