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 std::shared_ptr<T> get_subsystem(const std::string& name) const
38 {
39 const auto it = subsystems.find(name);
40 if (it != subsystems.end())
41 {
42 // NB: May still be nullptr, if it->second.get() is not a T*
43 return std::dynamic_pointer_cast<T>(it->second);
44 }
45
46 return nullptr;
47 }
48
49 public:
50 virtual ~AbstractNodeContext() = default;
51
52 template <typename T>
53 void install_subsystem(const std::shared_ptr<T>& subsystem)
54 {
55 install_subsystem(subsystem, T::get_subsystem_name());
56 }
57
58 template <typename T>
59 std::shared_ptr<T> get_subsystem() const
60 {
61 return get_subsystem<T>(T::get_subsystem_name());
62 }
63
64 virtual ccf::NodeId get_node_id() const
65 {
66 return {};
67 }
68
70 {
71 return {};
72 }
73
75 {
76 auto historical_state_cache =
77 get_subsystem<ccf::historical::AbstractStateCache>();
78 if (historical_state_cache == nullptr)
79 {
80 throw std::logic_error(
81 "Calling get_historical_state before subsystem is installed");
82 }
83 return *historical_state_cache;
84 }
85
87 {
88 auto indexer = get_subsystem<ccf::indexing::IndexingStrategies>();
89 if (indexer == nullptr)
90 {
91 throw std::logic_error(
92 "Calling get_indexing_strategies before subsystem is installed");
93 }
94 return *indexer;
95 }
96 };
97}
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
ccf::indexing::IndexingStrategies & get_indexing_strategies()
Definition node_context.h:86
void install_subsystem(const std::shared_ptr< T > &subsystem)
Definition node_context.h:53
ccf::historical::AbstractStateCache & get_historical_state()
Definition node_context.h:74
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:64
std::shared_ptr< T > get_subsystem() const
Definition node_context.h:59
virtual ccf::crypto::Pem get_self_signed_certificate() const
Definition node_context.h:69
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