Flow 3.0.1
Flow project: Full implementation reference.
dynamic_cfg_context.hpp
Go to the documentation of this file.
1/* Flow
2 * Copyright 2023 Akamai Technologies, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the
5 * "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy
7 * of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in
12 * writing, software distributed under the License is
13 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14 * CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing
16 * permissions and limitations under the License. */
17
18/// @file
19#pragma once
20
21#include "flow/cfg/cfg_fwd.hpp"
23#include <boost/function.hpp>
24#include <algorithm>
25#include <type_traits>
26
27namespace flow::cfg
28{
29
30/**
31 * Class which facilitates managing access to a dynamic configuration. Another class can make use of this one by means
32 * of a composition relationship, either through inheritance (probably `private` or `protected`) or by containing one or
33 * more `Dynamic_cfg_context` members. If using inheritance, the class will gain methods which can be used to access
34 * the dynamic configuration. If using composition by members, more than one dynamic configuration can be used, each of
35 * which can be accessed separately by calling the access methods of the associated member.
36 *
37 * A "configuration," here, is a data object (probably a `struct`), which is referred to as the "root", containing an
38 * internal data object, which is referred to as the "target". The target is the configuration object which is expected
39 * to be normally accessed. The root and the target can be the same.
40 *
41 * @tparam Root
42 * Type for the root configuration. This should meet the requirements of the template argument for
43 * `Option_set`. A `Root::Const_ptr` type for a ref-counted pointer to an immutable `Root` must at least be
44 * defined (which can be provided by deriving from `util::Shared_ptr_alias_holder`). See Option_set.
45 * @tparam Target
46 * Type for the target configuration.
47 * @tparam Target_ptr
48 * Please leave this at its default. Background: This would not have been a template parameter in the
49 * first place, had the authors known of `pointer_traits::rebind` at that the time. To preserve
50 * backwards compatibility this parameter remains for now (albeit deprecated).
51 *
52 * @todo flow::cfg::Dynamic_cfg_context `Target_ptr` is deprecated and shall be always left at its default
53 * value in future code; eventually remove it entirely and hard-code the default value internally.
54 */
55template<typename Root, typename Target,
56 typename Target_ptr> // See default in _fwd.hpp.
58{
59 static_assert(std::is_same_v<typename Target_ptr::element_type, std::add_const_t<Target>>,
60 "The `element_type` of `Target_ptr` must be `const Target`.");
61
62public:
63 // Types.
64
65 /// Type alias for `Target_ptr`.
66 using Target_ptr_type = Target_ptr;
67
68 /// Type for a function object which returns a ref-counted pointer to an immutable root configuration object.
69 using Get_root_func = Function<typename Root::Const_ptr ()>;
70
71 /// Type for a function object which translates a `Root` object to a contained `Target` object.
72 using Root_to_target_func = Function<const Target& (const Root&)>;
73
74 // Constructors/destructor.
75
76 /**
77 * Constructor.
78 *
79 * @param get_root_func_moved
80 * Returns a ref-counted pointer to the (immutable) root configuration object.
81 * @param root_to_target_func_moved
82 * Translates a root configuration object to a contained target configuration object.
83 */
84 Dynamic_cfg_context(Get_root_func&& get_root_func_moved, Root_to_target_func&& root_to_target_func_moved);
85
86 /**
87 * Constructor.
88 *
89 * This produces a `Dynamic_cfg_context` which will obtain its configuration from a `Config_manager`.
90 *
91 * @param config_manager
92 * A `Config_manager` which is currently managing the desired dynamic configuration.
93 * @param root_to_target_func_moved
94 * Translates a root configuration object to a contained target configuration object.
95 * @param d_value_set_idx
96 * The dynamic config slot index of `config_manager` which corresponds to the desired dynamic configuration.
97 * @see `Config_manager`.
98 */
99 template<typename... S_d_value_set>
101 Root_to_target_func&& root_to_target_func_moved, size_t d_value_set_idx = 0);
102
103 // Methods.
104
105 /**
106 * Obtain the root configuration.
107 *
108 * @return a ref-counted pointer to the (immutable) root configuration object.
109 */
110 typename Root::Const_ptr root_dynamic_cfg() const;
111
112 /**
113 * Obtain the target configuration.
114 *
115 * This method provides the key mechanism of the class. The returned pointer object can be used to easily access the
116 * target object, but will additionally cause the containing root object to be held valid in memory.
117 *
118 * @return a ref-counted pointer to the (immutable) target configuration object, but which shares ownership of the
119 * containing root configuration object.
120 */
121 Target_ptr dynamic_cfg() const;
122
123private:
124 // Data.
125
126 /// Called to obtain the root configuration.
128
129 /// Translates a root configuration object to a contained target configuration object.
131}; // class Dynamic_cfg_context
132
133// Template implementations.
134
135template<typename Root, typename Target, typename Target_ptr>
137 (Get_root_func&& get_root_func_moved, Root_to_target_func&& root_to_target_func_moved) :
138 m_get_root_func(std::move(get_root_func_moved)),
139 m_root_to_target_func(std::move(root_to_target_func_moved))
140{
141}
142
143template<typename Root, typename Target, typename Target_ptr>
144template<typename... S_d_value_set>
146 (const Config_manager<S_d_value_set...>& config_manager, Root_to_target_func&& root_to_target_func_moved,
147 size_t d_value_set_idx) :
148 m_get_root_func([&config_manager, d_value_set_idx]()
149 { return config_manager.template dynamic_values<Root>(d_value_set_idx); }),
150 m_root_to_target_func(std::move(root_to_target_func_moved))
151{
152}
153
154template<typename Root, typename Target, typename Target_ptr>
156{
157 return m_get_root_func();
158}
159
160template<typename Root, typename Target, typename Target_ptr>
162{
163 auto root = root_dynamic_cfg();
164 /* This aliasing constructor provides the key mechanism: the constructed pointer object *shares ownership* of `root`,
165 * but *stores* (and operates on) a pointer to the target object (contained by and translated from `root`). */
166 return Target_ptr{root, &(m_root_to_target_func(*root))};
167}
168
169} // namespace flow::cfg
Manages a config setup, intended for a single daemon process, by maintaining 1 or more set(s) of stat...
Class which facilitates managing access to a dynamic configuration.
const Root_to_target_func m_root_to_target_func
Translates a root configuration object to a contained target configuration object.
Target_ptr dynamic_cfg() const
Obtain the target configuration.
Root::Const_ptr root_dynamic_cfg() const
Obtain the root configuration.
Target_ptr Target_ptr_type
Type alias for Target_ptr.
const Get_root_func m_get_root_func
Called to obtain the root configuration.
Dynamic_cfg_context(Get_root_func &&get_root_func_moved, Root_to_target_func &&root_to_target_func_moved)
Constructor.
Flow module that facilitates configuring modules, such as applications and APIs, via statically and/o...
Definition: cfg_fwd.hpp:112