userver: userver/dynamic_config/impl/snapshot.hpp Source File
Loading...
Searching...
No Matches
snapshot.hpp
1#pragma once
2
3#include <any>
4#include <cstddef>
5#include <exception>
6#include <functional>
7#include <string>
8#include <type_traits>
9#include <typeindex>
10#include <typeinfo>
11#include <vector>
12
13#include <userver/dynamic_config/fwd.hpp>
14#include <userver/formats/json_fwd.hpp>
15
16USERVER_NAMESPACE_BEGIN
17
18namespace dynamic_config::impl {
19
20using Factory = std::function<std::any(const DocsMap&)>;
21
22[[noreturn]] void WrapGetError(const std::exception& ex, std::type_index type);
23
24formats::json::Value DocsMapGet(const DocsMap&, std::string_view key);
25
26using ConfigId = std::size_t;
27
28ConfigId Register(std::string&& name, Factory factory,
29 std::string&& default_docs_map_string);
30
31struct InternalTag final {
32 explicit InternalTag() = default;
33};
34
35std::any MakeConfig(ConfigId id, const DocsMap&);
36
37std::string_view GetName(ConfigId id);
38
39DocsMap MakeDefaultDocsMap();
40
41struct ConfigIdGetter final {
42 template <typename Key>
43 static ConfigId Get(const Key& key) noexcept {
44 return key.id_;
45 }
46};
47
48class SnapshotData final {
49 public:
50 SnapshotData() = default;
51
52 explicit SnapshotData(const std::vector<KeyValue>& config_variables);
53
54 SnapshotData(const DocsMap& defaults, const std::vector<KeyValue>& overrides);
55
56 SnapshotData(const SnapshotData& defaults,
57 const std::vector<KeyValue>& overrides);
58
59 SnapshotData(SnapshotData&&) noexcept = default;
60 SnapshotData& operator=(SnapshotData&&) noexcept = default;
61
62 template <typename T>
63 const T& Get(ConfigId id) const {
64 try {
65 return std::any_cast<const T&>(DoGet(id));
66 } catch (const std::exception& ex) {
67 impl::WrapGetError(ex, typeid(T));
68 }
69 }
70
71 bool IsEmpty() const noexcept;
72
73 private:
74 const std::any& DoGet(ConfigId id) const;
75
76 std::vector<std::any> user_configs_;
77};
78
79class StorageData;
80
81} // namespace dynamic_config::impl
82
83USERVER_NAMESPACE_END