userver: userver/dynamic_config/snapshot.hpp Source File
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
snapshot.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/dynamic_config/snapshot.hpp
4/// @brief @copybrief dynamic_config::Snapshot
5
6#include <optional>
7#include <type_traits>
8
9#include <userver/dynamic_config/impl/snapshot.hpp>
10#include <userver/utils/fast_pimpl.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace dynamic_config {
15
16/// @brief A config key is a unique identifier for a config variable
17/// @snippet core/src/components/logging_configurator.cpp key
18template <auto Parser>
19struct Key final {
20 static auto Parse(const DocsMap& docs_map) { return Parser(docs_map); }
21};
22
23/// Get the type of a config variable, given its key
24template <typename Key>
25using VariableOfKey = impl::VariableOfKey<Key>;
26
27// clang-format off
28
29/// @brief The storage for a snapshot of configs
30///
31/// When a config update comes in via new `DocsMap`, configs of all
32/// the registered types are constructed and stored in `Config`. After that
33/// the `DocsMap` is dropped.
34///
35/// Config types are automatically registered if they are accessed with `Get`
36/// somewhere in the program.
37///
38/// ## Usage example:
39/// @snippet components/component_sample_test.cpp Sample user component runtime config source
40
41// clang-format on
42class Snapshot final {
43 public:
44 Snapshot(const Snapshot&);
45 Snapshot& operator=(const Snapshot&);
46
47 Snapshot(Snapshot&&) noexcept;
48 Snapshot& operator=(Snapshot&&) noexcept;
49
50 ~Snapshot();
51
52 /// Used to access individual configs in the type-safe config map
53 template <typename Key>
54 const VariableOfKey<Key>& operator[](Key key) const& {
55 return GetData()[key];
56 }
57
58 /// Used to access individual configs in the type-safe config map
59 template <typename Key>
60 const VariableOfKey<Key>& operator[](Key) && {
61 static_assert(!sizeof(Key), "keep the Snapshot before using, please");
62 }
63
64 /// Deprecated, use `config[key]` instead
65 template <typename T>
66 const T& Get() const& {
67 return GetData()[Key<impl::ParseByConstructor<T>>{}];
68 }
69
70 /// Deprecated, use `config[key]` instead
71 template <typename T>
72 const T& Get() && {
73 static_assert(!sizeof(T), "keep the Snapshot before using, please");
74 }
75
76 private:
77 explicit Snapshot(const impl::StorageData& storage);
78
79 const impl::SnapshotData& GetData() const;
80
81 // for the constructor
82 friend class Source;
83 friend class impl::StorageData;
84
85 struct Impl;
86 utils::FastPimpl<Impl, 16, 8> impl_;
87};
88
89} // namespace dynamic_config
90
91USERVER_NAMESPACE_END