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