|
| 1 | +#pragma once |
| 2 | +#ifndef LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_CONFIGURATION_RUNTIME_HPP_INCLUDED |
| 3 | +#define LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_CONFIGURATION_RUNTIME_HPP_INCLUDED |
| 4 | + |
| 5 | +/// \file configuration_runtime.hpp |
| 6 | +/// \brief Binding and runtime-instantiation contract for materialized plans. |
| 7 | + |
| 8 | +#include "configuration.hpp" |
| 9 | +#include "loggers.hpp" |
| 10 | + |
| 11 | +#include <map> |
| 12 | +#include <memory> |
| 13 | +#include <string> |
| 14 | +#include <typeindex> |
| 15 | +#include <vector> |
| 16 | + |
| 17 | +namespace logit { |
| 18 | + |
| 19 | + /// \class ConfigurationBindingSet |
| 20 | + /// \brief Type-erased programmatic resources resolved for one logger. |
| 21 | + /// |
| 22 | + /// Bindings deliberately remain outside the parser-neutral document. A |
| 23 | + /// resolver and its factory agree on the concrete type associated with each |
| 24 | + /// name; the configuration layer only owns the lifetime of the shared value. |
| 25 | + class ConfigurationBindingSet { |
| 26 | + public: |
| 27 | + bool empty() const { return m_values.empty(); } |
| 28 | + |
| 29 | + bool contains(const std::string& name) const { |
| 30 | + std::map<std::string, Entry>::const_iterator it = m_values.find(name); |
| 31 | + return it != m_values.end() && static_cast<bool>(it->second.value); |
| 32 | + } |
| 33 | + |
| 34 | + template <typename T> |
| 35 | + void set(const std::string& name, const std::shared_ptr<T>& value) { |
| 36 | + m_values[name] = Entry(value, std::type_index(typeid(T))); |
| 37 | + } |
| 38 | + |
| 39 | + template <typename T> |
| 40 | + std::shared_ptr<T> get(const std::string& name) const { |
| 41 | + std::map<std::string, Entry>::const_iterator it = m_values.find(name); |
| 42 | + if (it == m_values.end() || !it->second.value || |
| 43 | + it->second.type != std::type_index(typeid(T))) { |
| 44 | + return std::shared_ptr<T>(); |
| 45 | + } |
| 46 | + return std::static_pointer_cast<T>(it->second.value); |
| 47 | + } |
| 48 | + |
| 49 | + private: |
| 50 | + struct Entry { |
| 51 | + Entry() : value(), type(typeid(void)) {} |
| 52 | + Entry(const std::shared_ptr<void>& value, const std::type_index& type) |
| 53 | + : value(value), type(type) {} |
| 54 | + |
| 55 | + std::shared_ptr<void> value; |
| 56 | + std::type_index type; |
| 57 | + }; |
| 58 | + |
| 59 | + std::map<std::string, Entry> m_values; |
| 60 | + }; |
| 61 | + |
| 62 | + /// \class ConfigurationBindingResolver |
| 63 | + /// \brief Resolves required programmatic bindings without constructing a logger. |
| 64 | + class ConfigurationBindingResolver { |
| 65 | + public: |
| 66 | + virtual ~ConfigurationBindingResolver() {} |
| 67 | + |
| 68 | + /// \brief Resolve bindings for one materialized logger. |
| 69 | + /// |
| 70 | + /// Diagnostic paths are relative to the logger root. The document layer |
| 71 | + /// prefixes them with `loggers[N]`. |
| 72 | + virtual bool resolve(const ConfigurationMaterializedLogger& logger, |
| 73 | + ConfigurationBindingSet& bindings, |
| 74 | + ConfigurationDiagnostics& diagnostics) const = 0; |
| 75 | + }; |
| 76 | + |
| 77 | + /// \class ConfigurationLoggerFactory |
| 78 | + /// \brief Creates one runtime logger after bindings have been resolved. |
| 79 | + /// Factory side effects are owned by the caller and are not transactional; |
| 80 | + /// only publication of the caller-visible logger vector is atomic. |
| 81 | + /// A factory owns the concrete binding types it expects and must retrieve |
| 82 | + /// required values through `ConfigurationBindingSet::get<T>()`. If a |
| 83 | + /// required value has the wrong type, the factory must fail closed by |
| 84 | + /// adding a diagnostic or returning an empty logger pointer. |
| 85 | + class ConfigurationLoggerFactory { |
| 86 | + public: |
| 87 | + virtual ~ConfigurationLoggerFactory() {} |
| 88 | + |
| 89 | + /// \brief Instantiate one logger from a complete plan entry. |
| 90 | + /// |
| 91 | + /// Returning an empty pointer is a failed instantiation. Diagnostic |
| 92 | + /// paths are relative to the logger root and are rebased by the document |
| 93 | + /// layer. |
| 94 | + virtual std::shared_ptr<ILogger> instantiate( |
| 95 | + const ConfigurationMaterializedLogger& logger, |
| 96 | + const ConfigurationBindingSet& bindings, |
| 97 | + ConfigurationDiagnostics& diagnostics) const = 0; |
| 98 | + }; |
| 99 | + |
| 100 | + /// \brief Resolve bindings and publish a complete logger set atomically. |
| 101 | + /// |
| 102 | + /// The caller-visible logger vector is updated only after every binding has |
| 103 | + /// resolved and every factory call has returned a logger. Factories may |
| 104 | + /// still perform external side effects while constructing instances; those |
| 105 | + /// effects are not transactional and are not rolled back if a later factory |
| 106 | + /// fails. |
| 107 | + inline bool instantiate_configuration( |
| 108 | + const ConfigurationPlan& plan, |
| 109 | + const ConfigurationBindingResolver& resolver, |
| 110 | + const ConfigurationLoggerFactory& factory, |
| 111 | + std::vector<std::shared_ptr<ILogger>>& loggers, |
| 112 | + ConfigurationDiagnostics& diagnostics) { |
| 113 | + diagnostics = ConfigurationDiagnostics(); |
| 114 | + std::vector<ConfigurationBindingSet> resolved_bindings; |
| 115 | + resolved_bindings.reserve(plan.loggers.size()); |
| 116 | + bool bindings_ok = true; |
| 117 | + for (std::size_t index = 0; index < plan.loggers.size(); ++index) { |
| 118 | + const ConfigurationMaterializedLogger& entry = plan.loggers[index]; |
| 119 | + ConfigurationBindingSet bindings; |
| 120 | + ConfigurationDiagnostics local_diagnostics; |
| 121 | + const bool resolved = resolver.resolve(entry, bindings, local_diagnostics); |
| 122 | + |
| 123 | + bool missing_binding = false; |
| 124 | + for (std::vector<std::string>::const_iterator required = |
| 125 | + entry.required_bindings.begin(); |
| 126 | + required != entry.required_bindings.end(); ++required) { |
| 127 | + if (!bindings.contains(*required)) { |
| 128 | + local_diagnostics.add(ConfigurationErrorCode::MissingProgrammaticBinding, |
| 129 | + "binding." + *required, |
| 130 | + "Required programmatic binding was not resolved"); |
| 131 | + missing_binding = true; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + const std::string prefix = "loggers[" + std::to_string(index) + "]"; |
| 136 | + if (!resolved || !local_diagnostics.empty() || missing_binding) { |
| 137 | + bindings_ok = false; |
| 138 | + if (local_diagnostics.empty()) { |
| 139 | + diagnostics.add(ConfigurationErrorCode::BindingResolutionFailed, |
| 140 | + prefix + ".binding", |
| 141 | + "Programmatic bindings could not be resolved"); |
| 142 | + } else { |
| 143 | + diagnostics.append_rebased(local_diagnostics, prefix); |
| 144 | + } |
| 145 | + } |
| 146 | + resolved_bindings.push_back(bindings); |
| 147 | + } |
| 148 | + |
| 149 | + if (!bindings_ok) return false; |
| 150 | + |
| 151 | + std::vector<std::shared_ptr<ILogger>> candidate; |
| 152 | + candidate.reserve(plan.loggers.size()); |
| 153 | + for (std::size_t index = 0; index < plan.loggers.size(); ++index) { |
| 154 | + const ConfigurationMaterializedLogger& entry = plan.loggers[index]; |
| 155 | + const std::string prefix = "loggers[" + std::to_string(index) + "]"; |
| 156 | + |
| 157 | + ConfigurationDiagnostics factory_diagnostics; |
| 158 | + std::shared_ptr<ILogger> runtime_logger = |
| 159 | + factory.instantiate(entry, resolved_bindings[index], factory_diagnostics); |
| 160 | + if (!runtime_logger || !factory_diagnostics.empty()) { |
| 161 | + if (factory_diagnostics.empty()) { |
| 162 | + diagnostics.add(ConfigurationErrorCode::InstantiationFailed, |
| 163 | + prefix, |
| 164 | + "Logger factory could not instantiate the plan entry"); |
| 165 | + } else { |
| 166 | + diagnostics.append_rebased(factory_diagnostics, prefix); |
| 167 | + } |
| 168 | + return false; |
| 169 | + } |
| 170 | + candidate.push_back(runtime_logger); |
| 171 | + } |
| 172 | + |
| 173 | + loggers = candidate; |
| 174 | + return true; |
| 175 | + } |
| 176 | + |
| 177 | +} // namespace logit |
| 178 | + |
| 179 | +#endif // LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_CONFIGURATION_RUNTIME_HPP_INCLUDED |
0 commit comments