userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
environment_variables.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/engine/subprocess/environment_variables.hpp
4
/// @brief @copybrief engine::subprocess::EnvironmentVariables
5
6
#
include
<
string
>
7
#
include
<
unordered_map
>
8
9
#
include
<
userver
/
rcu
/
rcu
.
hpp
>
10
11
USERVER_NAMESPACE_BEGIN
12
13
namespace
engine::subprocess {
14
15
/// Iterable environment variables wrapper to update values.
16
class
EnvironmentVariablesUpdate
{
17
public
:
18
using
Map = std::unordered_map<std::string, std::string>;
19
20
/// Constructs a wrapper from a map.
21
explicit
EnvironmentVariablesUpdate
(Map vars)
22
: vars_(std::move(vars))
23
{}
24
25
/// Returns a non const iterator to the beginning.
26
auto
begin
() {
return
vars_.begin(); }
27
28
/// Returns a non const iterator to the ending.
29
auto
end
() {
return
vars_.end(); }
30
31
/// Returns a const iterator to the beginning.
32
auto
begin
()
const
{
return
vars_.begin(); }
33
34
/// Returns a const iterator to the ending.
35
auto
end
()
const
{
return
vars_.end(); }
36
37
private
:
38
Map vars_;
39
};
40
41
/// @brief Environment variables representation.
42
///
43
/// Wrapper to save environment variables and pass them to ProcessStarter::Exec.
44
/// Changing an instance does not change current environment
45
/// variables of the process.
46
class
EnvironmentVariables
{
47
public
:
48
using
Map =
EnvironmentVariablesUpdate
::Map;
49
50
/// @brief Constructs an instance from pairs: key, value taken from the map.
51
explicit
EnvironmentVariables
(Map vars);
52
53
EnvironmentVariables(
const
EnvironmentVariables
&) =
default
;
54
EnvironmentVariables
& operator=(
const
EnvironmentVariables
&) =
default
;
55
EnvironmentVariables(
EnvironmentVariables
&&)
noexcept
=
default
;
56
EnvironmentVariables
& operator=(
EnvironmentVariables
&&)
noexcept
=
default
;
57
58
/// @brief Updates variable.
59
/// @note If variable does not exist then it is added.
60
EnvironmentVariables
&
UpdateWith
(
EnvironmentVariablesUpdate
update);
61
62
/// @brief Returns the value of the variable.
63
/// @warning Throws std::runtime_error if there is no variable.
64
const
std::string&
GetValue
(
const
std::string& variable_name)
const
;
65
66
/// Returns the pointer to the value of the variable or
67
/// nullptr if there is no variable.
68
const
std::string*
GetValueOptional
(
const
std::string& variable_name)
const
;
69
70
/// Sets the value of the variable.
71
void
SetValue
(std::string variable_name, std::string value);
72
73
/// Returns the reference to the value.
74
std::string&
operator
[](
const
std::string& variable_name);
75
76
/// Compares equal if thee map of environment variables are equal.
77
bool
operator
==(
const
EnvironmentVariables
& rhs)
const
;
78
79
/// Checks whether the container is empty.
80
auto
empty
()
const
{
return
vars_.empty(); }
81
82
/// Returns the number of elements.
83
auto
size
()
const
{
return
vars_.size(); }
84
85
using
const_iterator = Map::const_iterator;
86
using
iterator = const_iterator;
87
88
/// Returns a const iterator to the beginning.
89
auto
begin
()
const
{
return
vars_.begin(); }
90
91
/// Returns a const iterator to the ending.
92
auto
end
()
const
{
return
vars_.end(); }
93
94
private
:
95
Map vars_;
96
};
97
98
/// Returns copy of the environment variables of the current process.
99
EnvironmentVariables
GetCurrentEnvironmentVariables
();
100
101
/// Returns thread-safe read only pointer
102
/// to the environment variables of the current process.
103
rcu
::
ReadablePtr
<
EnvironmentVariables
>
GetCurrentEnvironmentVariablesPtr
();
104
105
/// Fetches current environment variables for getting via
106
/// GetCurrentEnvironmentVariables or GetCurrentEnvironmentVariablesPtr.
107
void
UpdateCurrentEnvironmentVariables
();
108
109
/// Overwrite modes
110
enum
class
Overwrite
{
111
kAllowed
,
///< Overwrites or creates the environment variable
112
kForbidden
,
///< Creates new environment variable, else throws
113
///< std::runtime_error
114
kIgnored
///< Does not overwrite the environment variable if the variable
115
///< exists
116
};
117
118
/// @brief Sets the environment variable with the specified overwrite type.
119
/// @warning Not thread-safe.
120
void
SetEnvironmentVariable
(
121
const
std::string& variable_name,
122
const
std::string& value,
123
Overwrite
overwrite =
Overwrite
::
kForbidden
124
);
125
126
/// @brief Unsets the environment variable.
127
/// @warning Not thread-safe.
128
void
UnsetEnvironmentVariable
(
const
std::string& variable_name);
129
130
/// @brief RAII idiom guard of environment variables.
131
/// @warning The constructor and destructor are not thread-safe.
132
class
EnvironmentVariablesScope
{
133
public
:
134
EnvironmentVariablesScope();
135
~EnvironmentVariablesScope();
136
137
private
:
138
rcu
::
ReadablePtr
<
EnvironmentVariables
> old_env_;
139
};
140
141
}
// namespace engine::subprocess
142
143
USERVER_NAMESPACE_END
userver
engine
subprocess
environment_variables.hpp
Generated on
for userver by
Doxygen
1.17.0