Senzing C++ SDK
C++ SDK for Senzing v4, patterned after the official C# SDK
Loading...
Searching...
No Matches
SzCoreEnvironment.hpp
Go to the documentation of this file.
1// SzCoreEnvironment.hpp -- native-backed SzEnvironment implementation + builder.
2//
3// Ports core/SzCoreEnvironment.cs. Owns one lazily-created, cached instance of
4// each subsystem, guarded by a mutex. Get* accessors call EnsureActive() (which
5// throws SzEnvironmentDestroyedException after Destroy()) and return a non-owning
6// reference to the env-owned subsystem.
7//
8// All accessors are implemented: GetProduct, GetEngine, GetConfigManager,
9// GetDiagnostic, GetLogging, plus Destroy, IsDestroyed, GetActiveConfigID, and
10// Reinitialize. GetLogging is the exception to the EnsureActive() rule: it
11// works before any native init and after Destroy().
12#ifndef SENZING_SDK_CORE_SZCOREENVIRONMENT_HPP
13#define SENZING_SDK_CORE_SZCOREENVIRONMENT_HPP
14
15#include <cstdint>
16#include <memory>
17#include <mutex>
18#include <optional>
19#include <string>
20
22
23namespace senzing::sdk::core {
24
25class SzCoreProduct;
28class SzCoreEngine;
29
31class SzCoreEnvironment final : public SzEnvironment {
32public:
34 static constexpr const char* kDefaultInstanceName = "Senzing Instance";
36 static constexpr const char* kDefaultSettings = "{ }";
37
39 class Builder {
40 public:
41 Builder& InstanceName(const std::string& instanceName);
42 Builder& Settings(const std::string& settings);
43 Builder& VerboseLogging(bool verboseLogging);
47 Builder& ConfigID(int64_t configID);
48 std::unique_ptr<SzCoreEnvironment> Build();
49
50 private:
51 std::string instanceName_{kDefaultInstanceName};
52 std::string settings_{kDefaultSettings};
53 bool verboseLogging_{false};
54 std::optional<int64_t> configID_;
55 };
56
59
65
67
70
71 // ---- SzEnvironment ----
72 SzProduct& GetProduct() override;
73 SzEngine& GetEngine() override;
76 int64_t GetActiveConfigID() override;
77 void Reinitialize(int64_t configID) override;
78 void Destroy() override;
79 bool IsDestroyed() override;
83 SzLogging& GetLogging() override;
84
85 // ---- Internal accessors used by owned subsystems ----
86 [[nodiscard]] const std::string& GetInstanceName() const noexcept;
87 [[nodiscard]] const std::string& GetSettings() const noexcept;
88 [[nodiscard]] bool IsVerboseLogging() const noexcept;
89
96 [[nodiscard]] std::optional<int64_t> GetConfigID() const noexcept;
97
105
106private:
107 SzCoreEnvironment(std::string instanceName, std::string settings,
108 bool verboseLogging, std::optional<int64_t> configID);
109
112 void EnsureActiveLocked() const;
113
117 SzCoreEngine& EnsureEngine();
118
119 std::string instanceName_;
120 std::string settings_;
121 bool verboseLogging_;
122 std::optional<int64_t> configID_;
123
124 mutable std::mutex monitor_;
125 bool destroyed_{false};
126
127 // Per-process singleton enforcement (mirrors C# active-instance pattern).
128 static std::mutex s_classMutex_;
129 static SzCoreEnvironment* s_activeInstance_;
130
131 std::unique_ptr<SzCoreProduct> coreProduct_;
132 std::unique_ptr<SzCoreConfigManager> coreConfigManager_;
133 std::unique_ptr<SzCoreDiagnostic> coreDiagnostic_;
134 std::unique_ptr<SzCoreEngine> coreEngine_;
135};
136
137} // namespace senzing::sdk::core
138
139#endif // SENZING_SDK_CORE_SZCOREENVIRONMENT_HPP
Manages persisted Senzing configurations and the default config registry.
Definition SzConfigManager.hpp:18
Diagnostic operations against the Senzing repository.
Definition SzDiagnostic.hpp:11
The core Senzing entity-resolution engine.
Definition SzEngine.hpp:28
Factory + lifecycle for the Senzing SDK subsystems.
Definition SzEnvironment.hpp:21
Senzing native logging (libSzLogging).
Definition SzLogging.hpp:657
Provides access to the Senzing product version and license metadata.
Definition SzProduct.hpp:10
Core implementation of SzConfigManager backed by the native SzConfig_* and SzConfigMgr_* modules.
Definition SzCoreConfigManager.hpp:24
Core implementation of SzDiagnostic backed by the native SzDiagnostic_* module.
Definition SzCoreDiagnostic.hpp:21
Core implementation of SzEngine backed by the native Sz_* module.
Definition SzCoreEngine.hpp:27
Fluent builder for constructing an SzCoreEnvironment.
Definition SzCoreEnvironment.hpp:39
Builder & VerboseLogging(bool verboseLogging)
Builder & InstanceName(const std::string &instanceName)
Builder & Settings(const std::string &settings)
std::unique_ptr< SzCoreEnvironment > Build()
Builder & ConfigID(int64_t configID)
Pins an explicit configuration ID for native initialization (mirrors C# AbstractBuilder....
Core implementation of SzEnvironment backed by the native libSz modules.
Definition SzCoreEnvironment.hpp:31
SzLogging & GetLogging() override
No liveness check: logging is process-global native state that needs no engine initialization,...
bool IsDestroyed() override
Returns true once Destroy() has been initiated/completed.
static constexpr const char * kDefaultSettings
Default settings when none are supplied (mirrors C#).
Definition SzCoreEnvironment.hpp:36
static constexpr const char * kDefaultInstanceName
Default native instance name when none is supplied (mirrors C#).
Definition SzCoreEnvironment.hpp:34
static Builder NewBuilder()
Creates a new builder.
const std::string & GetSettings() const noexcept
SzEngine & GetEngine() override
Returns the env-owned SzEngine (lazily created).
void Destroy() override
Destroys the environment and all owned subsystems. Idempotent.
int64_t GetActiveConfigID() override
Returns the currently active configuration ID.
SzCoreEnvironment & operator=(const SzCoreEnvironment &)=delete
const std::string & GetInstanceName() const noexcept
std::optional< int64_t > GetConfigID() const noexcept
The explicit configuration ID this environment is pinned to, or std::nullopt to use the repository's ...
static SzCoreEnvironment * GetActiveInstance()
The single active environment instance for this process, or nullptr if none is active.
SzConfigManager & GetConfigManager() override
Returns the env-owned SzConfigManager (lazily created).
SzProduct & GetProduct() override
Returns the env-owned SzProduct (lazily created).
void Reinitialize(int64_t configID) override
Reinitializes the engine/diagnostic with the given configuration ID.
SzCoreEnvironment(const SzCoreEnvironment &)=delete
bool IsVerboseLogging() const noexcept
SzDiagnostic & GetDiagnostic() override
Returns the env-owned SzDiagnostic (lazily created).
void EnsureActive()
Locks the environment mutex and throws SzEnvironmentDestroyedException if this environment has been d...
Core implementation of SzProduct backed by the native SzProduct_* module.
Definition SzCoreProduct.hpp:18
Definition SzCoreConfig.hpp:20