File indexing completed on 2025-01-18 09:50:10
0001
0002
0003
0004
0005
0006
0007 #ifndef BOOST_PROCESS_V2_ENV_HPP
0008 #define BOOST_PROCESS_V2_ENV_HPP
0009 #include <string>
0010 #include <vector>
0011
0012 #include <boost/process/v2/detail/config.hpp>
0013 #include <boost/process/v2/detail/throw_error.hpp>
0014 #include <boost/process/v2/process_handle.hpp>
0015 #include <boost/process/v2/pid.hpp>
0016
0017 #include <boost/process/v2/environment.hpp>
0018
0019 BOOST_PROCESS_V2_BEGIN_NAMESPACE
0020
0021 namespace detail
0022 {
0023 namespace ext
0024 {
0025
0026 #if defined(BOOST_PROCESS_V2_WINDOWS)
0027 using native_env_handle_type = wchar_t *;
0028 using native_env_iterator = wchar_t *;
0029 #elif defined(__FreeBSD__)
0030 using native_env_handle_type = char **;
0031 using native_env_iterator = char **;
0032 #else
0033 using native_env_handle_type = char *;
0034 using native_env_iterator = char *;
0035 #endif
0036
0037 struct native_env_handle_deleter
0038 {
0039 BOOST_PROCESS_V2_DECL void operator()(native_env_handle_type) const;
0040 };
0041
0042 BOOST_PROCESS_V2_DECL native_env_iterator next(native_env_iterator nh);
0043 BOOST_PROCESS_V2_DECL native_env_iterator find_end(native_env_iterator nh);
0044 BOOST_PROCESS_V2_DECL const environment::char_type * dereference(native_env_iterator iterator);
0045
0046 }
0047 }
0048
0049 namespace ext {
0050
0051
0052 struct env_view
0053 {
0054 using native_handle_type = detail::ext::native_env_handle_type;
0055 using value_type = environment::key_value_pair_view;
0056
0057 env_view() = default;
0058 env_view(env_view && nt) = default;
0059
0060 native_handle_type native_handle() { return handle_.get(); }
0061
0062
0063 struct iterator
0064 {
0065 using value_type = environment::key_value_pair_view;
0066 using difference_type = int;
0067 using reference = environment::key_value_pair_view;
0068 using pointer = environment::key_value_pair_view;
0069 using iterator_category = std::forward_iterator_tag;
0070
0071 iterator() = default;
0072 iterator(const iterator & ) = default;
0073 iterator(const detail::ext::native_env_iterator &native_handle) : iterator_(native_handle) {}
0074
0075 iterator & operator++()
0076 {
0077 iterator_ = detail::ext::next(iterator_);
0078 return *this;
0079 }
0080
0081 iterator operator++(int)
0082 {
0083 auto last = *this;
0084 iterator_ = detail::ext::next(iterator_);
0085 return last;
0086 }
0087 environment::key_value_pair_view operator*() const
0088 {
0089 return detail::ext::dereference(iterator_);
0090 }
0091
0092 friend bool operator==(const iterator & l, const iterator & r) {return l.iterator_ == r.iterator_;}
0093 friend bool operator!=(const iterator & l, const iterator & r) {return l.iterator_ != r.iterator_;}
0094
0095 private:
0096 detail::ext::native_env_iterator iterator_;
0097 };
0098
0099 iterator begin() const {return iterator(handle_.get());}
0100 iterator end() const {return iterator(detail::ext::find_end(handle_.get()));}
0101
0102 private:
0103 friend BOOST_PROCESS_V2_DECL env_view env(pid_type pid, error_code & ec);
0104 #if defined(BOOST_PROCESS_V2_WINDOWS)
0105 friend BOOST_PROCESS_V2_DECL env_view env(HANDLE handle, error_code & ec);
0106 #endif
0107
0108 std::unique_ptr<typename remove_pointer<detail::ext::native_env_handle_type>::type,
0109 detail::ext::native_env_handle_deleter> handle_;
0110 };
0111
0112 #if defined(BOOST_PROCESS_V2_WINDOWS)
0113 BOOST_PROCESS_V2_DECL env_view env(HANDLE handle, error_code & ec);
0114 BOOST_PROCESS_V2_DECL env_view env(HANDLE handle);
0115 #endif
0116
0117
0118
0119 BOOST_PROCESS_V2_DECL env_view env(pid_type pid, error_code & ec);
0120 BOOST_PROCESS_V2_DECL env_view env(pid_type pid);
0121
0122 template<typename Executor>
0123 BOOST_PROCESS_V2_DECL env_view env(basic_process_handle<Executor> & handle, error_code & ec)
0124 {
0125 #if defined(BOOST_PROCESS_V2_WINDOWS)
0126 return env(handle.native_handle(), ec);
0127 #else
0128 return env(handle.id(), ec);
0129 #endif
0130 }
0131
0132 template<typename Executor>
0133 BOOST_PROCESS_V2_DECL env_view env(basic_process_handle<Executor> & handle)
0134 {
0135 #if defined(BOOST_PROCESS_V2_WINDOWS)
0136 return env(handle.native_handle());
0137 #else
0138 return env(handle.id());
0139 #endif
0140 }
0141
0142
0143
0144
0145
0146 }
0147
0148 BOOST_PROCESS_V2_END_NAMESPACE
0149
0150 #if defined(BOOST_PROCESS_V2_HEADER_ONLY)
0151
0152 #include <boost/process/v2/ext/impl/env.ipp>
0153
0154 #endif
0155 #endif