CCF/include/ccf/node_context.h

98 строки
2.4 KiB
C++
Исходник Обычный вид История

2021-03-09 18:44:13 +03:00
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the Apache 2.0 License.
#pragma once
#include "ccf/historical_queries_interface.h"
#include "ccf/indexing/indexer_interface.h"
#include "ccf/node_subsystem_interface.h"
2021-03-09 18:44:13 +03:00
2024-06-28 17:44:01 +03:00
namespace ccf
2021-03-09 18:44:13 +03:00
{
struct AbstractNodeContext
{
protected:
std::map<std::string, std::shared_ptr<ccf::AbstractNodeSubSystem>>
subsystems;
void install_subsystem(
const std::shared_ptr<ccf::AbstractNodeSubSystem>& subsystem,
const std::string& name)
{
if (subsystem == nullptr)
{
return;
}
const auto it = subsystems.find(name);
if (it != subsystems.end())
{
throw std::logic_error(
fmt::format("Already registered subsystem {}", name));
}
subsystems.emplace_hint(it, name, subsystem);
}
template <typename T>
std::shared_ptr<T> get_subsystem(const std::string& name) const
{
const auto it = subsystems.find(name);
if (it != subsystems.end())
{
// NB: May still be nullptr, if it->second.get() is not a T*
return std::dynamic_pointer_cast<T>(it->second);
}
return nullptr;
}
2022-10-07 13:02:11 +03:00
public:
virtual ~AbstractNodeContext() = default;
template <typename T>
void install_subsystem(const std::shared_ptr<T>& subsystem)
{
install_subsystem(subsystem, T::get_subsystem_name());
}
template <typename T>
std::shared_ptr<T> get_subsystem() const
{
return get_subsystem<T>(T::get_subsystem_name());
}
virtual ccf::NodeId get_node_id() const
{
return {};
}
2024-06-27 18:53:08 +03:00
virtual ccf::crypto::Pem get_self_signed_certificate() const
{
return {};
}
ccf::historical::AbstractStateCache& get_historical_state()
{
auto historical_state_cache =
get_subsystem<ccf::historical::AbstractStateCache>();
if (historical_state_cache == nullptr)
{
throw std::logic_error(
"Calling get_historical_state before subsystem is installed");
}
return *historical_state_cache;
}
ccf::indexing::IndexingStrategies& get_indexing_strategies()
{
auto indexer = get_subsystem<ccf::indexing::IndexingStrategies>();
if (indexer == nullptr)
{
throw std::logic_error(
"Calling get_indexing_strategies before subsystem is installed");
}
return *indexer;
}
2021-03-09 18:44:13 +03:00
};
}