Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:48:47

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