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