2 * Copyright © 2013 Canonical Ltd.
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License version 3,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 * Authored by: Thomas Voß <thomas.voss@canonical.com>
19 #ifndef CORE_POSIX_THIS_PROCESS_H_
20 #define CORE_POSIX_THIS_PROCESS_H_
22 #include <core/posix/visibility.h>
27 #include <system_error>
34 namespace this_process
39 * @brief for_each invokes a functor for every key-value pair in the environment.
40 * @param [in] functor Invoked for every key-value pair.
42 CORE_POSIX_DLL_PUBLIC void for_each(
43 const std::function<void(const std::string&, const std::string&)>& functor) noexcept(true);
46 * @brief get queries the value of an environment variable.
47 * @throw std::runtime_error if there is no variable with the given key defined in the env.
48 * @param [in] key Name of the variable to query the value for.
49 * @return Contents of the variable.
51 CORE_POSIX_DLL_PUBLIC std::string get_or_throw(const std::string& key);
54 * @brief get queries the value of an environment variable.
55 * @param [in] key Name of the variable to query the value for.
56 * @param [in] default_value Default value to return when key is not present in the environment.
57 * @return Contents of the variable or an empty string if the variable is not defined.
59 CORE_POSIX_DLL_PUBLIC std::string get(
60 const std::string& key,
61 const std::string& default_value = std::string()) noexcept(true);
64 * @brief unset_or_throw removes the variable with name key from the environment.
65 * @throw std::system_error in case of errors.
66 * @param [in] key Name of the variable to unset.
68 CORE_POSIX_DLL_PUBLIC void unset_or_throw(const std::string& key);
71 * @brief unset removes the variable with name key from the environment.
72 * @return false in case of errors, true otherwise.
73 * @param [in] key Name of the variable to unset.
74 * @param [out] se Receives error details if unset returns false.
76 CORE_POSIX_DLL_PUBLIC bool unset(const std::string& key,
77 std::error_code& se) noexcept(true);
80 * @brief set_or_throw will adjust the contents of the variable identified by key to the provided value.
81 * @throw std::system_error in case of errors.
82 * @param [in] key Name of the variable to set the value for.
83 * @param [in] value New contents of the variable.
85 CORE_POSIX_DLL_PUBLIC void set_or_throw(const std::string& key,
86 const std::string& value);
88 * @brief set will adjust the contents of the variable identified by key to the provided value.
89 * @return false in case of errors, true otherwise.
90 * @param [in] key Name of the variable to set the value for.
91 * @param [in] value New contents of the variable.
92 * @param [out] se Receives the details in case of errors.
94 CORE_POSIX_DLL_PUBLIC bool set(const std::string &key,
95 const std::string &value,
96 std::error_code& se) noexcept(true);
100 * @brief Returns a Process instance corresponding to this process.
102 CORE_POSIX_DLL_PUBLIC Process instance() noexcept(true);
105 * @brief Query the parent of the process.
106 * @return The parent of the process.
108 CORE_POSIX_DLL_PUBLIC Process parent() noexcept(true);
111 * @brief Access this process's stdin.
113 CORE_POSIX_DLL_PUBLIC std::istream& cin() noexcept(true);
116 * @brief Access this process's stdout.
118 CORE_POSIX_DLL_PUBLIC std::ostream& cout() noexcept(true);
121 * @brief Access this process's stderr.
123 CORE_POSIX_DLL_PUBLIC std::ostream& cerr() noexcept(true);
128 #endif // CORE_POSIX_THIS_PROCESS_H_