CCF
Loading...
Searching...
No Matches
node_context.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
8
9namespace ccf
10{
12 {
13 protected:
14 std::map<std::string, std::shared_ptr<ccf::AbstractNodeSubSystem>>
16
18 const std::shared_ptr<ccf::AbstractNodeSubSystem>& subsystem,
19 const std::string& name)
20 {
21 if (subsystem == nullptr)
22 {
23 return;
24 }
25
26 const auto it = subsystems.find(name);
27 if (it != subsystems.end())
28 {
29 throw std::logic_error(
30 fmt::format("Already registered subsystem {}", name));
31 }
32
33 subsystems.emplace_hint(it, name, subsystem);
34 }
35
36 template <typename T>
37 [[nodiscard]] std::shared_ptr<T> get_subsystem(
38 const std::string& name) const
39 {
40 const auto it = subsystems.find(name);
41 if (it != subsystems.end())
42 {
43 // NB: May still be nullptr, if it->second.get() is not a T*
44 return std::dynamic_pointer_cast<T>(it->second);
45 }
46
47 return nullptr;
48 }
49
50 public:
51 virtual ~AbstractNodeContext() = default;
52
53 template <typename T>
54 void install_subsystem(const std::shared_ptr<T>& subsystem)
55 {
56 install_subsystem(subsystem, T::get_subsystem_name());
57 }
58
59 template <typename T>
60 [[nodiscard]] std::shared_ptr<T> get_subsystem() const
61 {
62 return get_subsystem<T>(T::get_subsystem_name());
63 }
64
65 [[nodiscard]] virtual ccf::NodeId get_node_id() const
66 {
67 return {};
68 }
69
70 [[nodiscard]] virtual ccf::crypto::Pem get_self_signed_certificate() const
71 {
72 return {};
73 }
74
76 const
77 {
78 auto historical_state_cache =
79 get_subsystem<ccf::historical::AbstractStateCache>();
80 if (historical_state_cache == nullptr)
81 {
82 throw std::logic_error(
83 "Calling get_historical_state before subsystem is installed");
84 }
85 return *historical_state_cache;
86 }
87
89 const
90 {
91 auto indexer = get_subsystem<ccf::indexing::IndexingStrategies>();
92 if (indexer == nullptr)
93 {
94 throw std::logic_error(
95 "Calling get_indexing_strategies before subsystem is installed");
96 }
97 return *indexer;
98 }
99 };
100}
Definition pem.h:18
Definition historical_queries_interface.h:67
Definition indexer_interface.h:21
Definition app_interface.h:14
Definition node_context.h:12
void install_subsystem(const std::shared_ptr< T > &subsystem)
Definition node_context.h:54
ccf::historical::AbstractStateCache & get_historical_state() const
Definition node_context.h:75
std::shared_ptr< T > get_subsystem(const std::string &name) const
Definition node_context.h:37
virtual ~AbstractNodeContext()=default
virtual ccf::NodeId get_node_id() const
Definition node_context.h:65
ccf::indexing::IndexingStrategies & get_indexing_strategies() const
Definition node_context.h:88
std::shared_ptr< T > get_subsystem() const
Definition node_context.h:60
virtual ccf::crypto::Pem get_self_signed_certificate() const
Definition node_context.h:70
void install_subsystem(const std::shared_ptr< ccf::AbstractNodeSubSystem > &subsystem, const std::string &name)
Definition node_context.h:17
std::map< std::string, std::shared_ptr< ccf::AbstractNodeSubSystem > > subsystems
Definition node_context.h:15