1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891 |
- //
- // process/environment.hpp
- // ~~~~~~~~~~~~~~~~~~~~~~~~
- //
- // Copyright (c) 2021 Klemens D. Morgenstern (klemens dot morgenstern at gmx dot net)
- //
- // Distributed under the Boost Software License, Version 1.0. (See accompanying
- // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- #ifndef BOOST_PROCESS_V2_ENVIRONMENT_HPP
- #define BOOST_PROCESS_V2_ENVIRONMENT_HPP
- #include <boost/process/v2/detail/config.hpp>
- #include <boost/process/v2/cstring_ref.hpp>
- #include <boost/process/v2/detail/utf8.hpp>
- #include <boost/type_traits.hpp>
- #include <functional>
- #include <memory>
- #include <numeric>
- #include <vector>
- #if !defined(GENERATING_DOCUMENTATION)
- #if defined(BOOST_PROCESS_V2_WINDOWS)
- #include <boost/process/v2/detail/environment_win.hpp>
- #else
- #include <boost/process/v2/detail/environment_posix.hpp>
- #endif
- #endif
- BOOST_PROCESS_V2_BEGIN_NAMESPACE
- /// Namespace for information and functions regarding the calling process.
- namespace environment
- {
- #if defined(GENERATING_DOCUMENTATION)
- /// A char traits type that reflects the OS rules for string representing environment keys.
- /** Can be an alias of std::char_traits. May only be defined for `char` and `wchar_t`.
- *
- * Windows treats keys as case-insensitive yet perserving. The char traits are made to reflect
- * that behaviour.
- */
- tempalte<typename Char>
- using key_char_traits = implementation_defined ;
- /// A char traits type that reflects the OS rules for string representing environment values.
- /** Can be an alias of std::char_traits. May only be defined for `char` and `wchar_t`.
- */
- tempalte<typename Char>
- using value_char_traits = implementation_defined ;
- /// The character type used by the environment. Either `char` or `wchar_t`.
- using char_type = implementation_defined ;
- /// The equal character in an environment string used to separate key and value.
- constexpr char_type equality_sign = implementation_defined;
- /// The delimiter in environemtn lists. Commonly used by the `PATH` variable.
- constexpr char_type delimiter = implementation_defined;
- /// The native handle of an environment. Note that this can be an owning pointer and is generally not thread safe.
- using native_handle = implementation_defined;
- #endif
- /// The iterator used by a value or value_view to iterator through environments that are lists.
- struct value_iterator
- {
- using string_view_type = basic_string_view<char_type, value_char_traits<char_type>>;
- using difference_type = int;
- using reference = string_view_type;
- using iterator_category = std::forward_iterator_tag;
- value_iterator & operator++()
- {
- const auto delim = view_.find(delimiter);
- if (delim != string_view_type::npos)
- view_ = view_.substr(delim + 1);
- else
- view_ = view_.substr(view_.size());
- return *this;
- }
- value_iterator operator++(int)
- {
- auto last = *this;
- ++(*this);
- return last;
- }
- string_view_type operator*() const
- {
- const auto delim = view_.find(delimiter);
- if (delim == string_view_type::npos)
- return view_;
- else
- return view_.substr(0, delim);
- }
- value_iterator() = default;
- value_iterator(const value_iterator & ) = default;
- value_iterator(string_view_type view, std::size_t offset = 0u) : view_(view.substr(offset))
- {
- }
- friend bool operator==(const value_iterator & l, const value_iterator & r) { return l.view_ == r.view_; }
- friend bool operator!=(const value_iterator & l, const value_iterator & r) { return l.view_ != r.view_; }
- private:
- string_view_type view_;
- };
- /// A view type for a key of an environment
- struct key_view
- {
- using value_type = char_type;
- using traits_type = key_char_traits<char_type>;
- using string_view_type = basic_string_view<char_type, traits_type>;
- using string_type = std::basic_string<char_type, key_char_traits<char_type>>;
- key_view() noexcept = default;
- key_view( const key_view& p ) = default;
- key_view( key_view&& p ) noexcept = default;
- template<typename Source,
- typename = typename std::enable_if<is_constructible<string_view_type, Source>::value>::type>
- key_view( const Source& source ) : value_(source) {}
- key_view( const char_type * p) : value_(p) {}
- key_view( char_type * p) : value_(p) {}
- ~key_view() = default;
- key_view& operator=( const key_view& p ) = default;
- key_view& operator=( key_view&& p ) noexcept = default;
- key_view& operator=( string_view_type source )
- {
- value_ = source;
- return *this;
- }
- void swap( key_view& other ) noexcept
- {
- std::swap(value_, other.value_);
- }
- string_view_type native() const noexcept {return value_;}
- operator string_view_type() const {return native();}
- int compare( const key_view& p ) const noexcept {return value_.compare(p.value_);}
- int compare( string_view_type str ) const {return value_.compare(str);}
- int compare( const value_type* s ) const {return value_.compare(s);}
- template< class CharT, class Traits = std::char_traits<CharT>,
- class Alloc = std::allocator<CharT> >
- std::basic_string<CharT,Traits,Alloc>
- basic_string( const Alloc& alloc = Alloc()) const
- {
- return BOOST_PROCESS_V2_NAMESPACE::detail::conv_string<CharT, Traits>(
- value_.data(), value_.size(), alloc);
- }
- std::string string() const {return basic_string<char>();}
- std::wstring wstring() const {return basic_string<wchar_t>();}
- string_type native_string() const
- {
- return basic_string<char_type, key_char_traits<char_type>>();
- }
- friend bool operator==(key_view l, key_view r) { return l.value_ == r.value_; }
- friend bool operator!=(key_view l, key_view r) { return l.value_ != r.value_; }
- friend bool operator<=(key_view l, key_view r) { return l.value_ <= r.value_; }
- friend bool operator>=(key_view l, key_view r) { return l.value_ >= r.value_; }
- friend bool operator< (key_view l, key_view r) { return l.value_ < r.value_; }
- friend bool operator> (key_view l, key_view r) { return l.value_ > r.value_; }
- bool empty() const {return value_.empty(); }
- template< class CharT, class Traits >
- friend std::basic_ostream<CharT,Traits>&
- operator<<( std::basic_ostream<CharT,Traits>& os, const key_view& p )
- {
- os << BOOST_PROCESS_V2_NAMESPACE::quoted(p.basic_string<CharT,Traits>());
- return os;
- }
- template< class CharT, class Traits >
- friend std::basic_istream<CharT,Traits>&
- operator>>( std::basic_istream<CharT,Traits>& is, key_view& p )
- {
- std::basic_string<CharT, Traits> t;
- is >> BOOST_PROCESS_V2_NAMESPACE::quoted(t);
- p = t;
- return is;
- }
- const value_type * data() const {return value_.data(); }
- std::size_t size() const {return value_.size(); }
- private:
- string_view_type value_;
- };
- /// A view for a value in an environment
- struct value_view
- {
- using value_type = char_type;
- using string_view_type = basic_cstring_ref<char_type, value_char_traits<char_type>>;
- using string_type = std::basic_string<char_type, value_char_traits<char_type>>;
- using traits_type = value_char_traits<char_type>;
- value_view() noexcept = default;
- value_view( const value_view& p ) = default;
- value_view( value_view&& p ) noexcept = default;
- template<typename Source, typename = typename std::enable_if<is_constructible<string_view_type, Source>::value>::type>
- value_view( const Source& source ) : value_(source) {}
- value_view( const char_type * p) : value_(p) {}
- value_view( char_type * p) : value_(p) {}
- ~value_view() = default;
- value_view& operator=( const value_view& p ) = default;
- value_view& operator=( value_view&& p ) noexcept = default;
- value_view& operator=( string_view_type source )
- {
- value_ = source;
- return *this;
- }
- void swap( value_view& other ) noexcept
- {
- std::swap(value_, other.value_);
- }
- string_view_type native() const noexcept {return value_;}
- operator string_view_type() const {return native();}
- operator typename string_view_type::string_view_type() const {return value_; }
- int compare( const value_view& p ) const noexcept {return value_.compare(p.value_);}
- int compare( string_view_type str ) const {return value_.compare(str);}
- int compare( const value_type* s ) const {return value_.compare(s);}
- template< class CharT, class Traits = std::char_traits<CharT>,
- class Alloc = std::allocator<CharT> >
- std::basic_string<CharT,Traits,Alloc>
- basic_string( const Alloc& alloc = Alloc() ) const
- {
- return BOOST_PROCESS_V2_NAMESPACE::detail::conv_string<CharT, Traits>(
- value_.data(), value_.size(), alloc);
- }
- std::string string() const {return basic_string<char>();}
- std::wstring wstring() const {return basic_string<wchar_t>();}
- string_type native_string() const
- {
- return basic_string<char_type, value_char_traits<char_type>>();
- }
- bool empty() const {return value_.empty(); }
- friend bool operator==(value_view l, value_view r) { return l.value_ == r.value_; }
- friend bool operator!=(value_view l, value_view r) { return l.value_ != r.value_; }
- friend bool operator<=(value_view l, value_view r) { return l.value_ <= r.value_; }
- friend bool operator>=(value_view l, value_view r) { return l.value_ >= r.value_; }
- friend bool operator< (value_view l, value_view r) { return l.value_ < r.value_; }
- friend bool operator> (value_view l, value_view r) { return l.value_ > r.value_; }
- template< class CharT, class Traits >
- friend std::basic_ostream<CharT,Traits>&
- operator<<( std::basic_ostream<CharT,Traits>& os, const value_view& p )
- {
- os << BOOST_PROCESS_V2_NAMESPACE::quoted(p.basic_string<CharT,Traits>());
- return os;
- }
- template< class CharT, class Traits >
- friend std::basic_istream<CharT,Traits>&
- operator>>( std::basic_istream<CharT,Traits>& is, value_view& p )
- {
- std::basic_string<CharT, Traits> t;
- is >> BOOST_PROCESS_V2_NAMESPACE::quoted(t);
- p = t;
- return is;
- }
- value_iterator begin() const {return value_iterator(value_.data());}
- value_iterator end() const {return value_iterator(value_.data() , value_.size());}
- const char_type * c_str() {return value_.c_str(); }
- const value_type * data() const {return value_.data(); }
- std::size_t size() const {return value_.size(); }
- private:
- string_view_type value_;
- };
- /// A view for a key value pair in an environment
- struct key_value_pair_view
- {
- using value_type = char_type;
- using string_type = std::basic_string<char_type>;
- using string_view_type = basic_cstring_ref<char_type>;
- using traits_type = std::char_traits<char_type>;
- key_value_pair_view() noexcept = default;
- key_value_pair_view( const key_value_pair_view& p ) = default;
- key_value_pair_view( key_value_pair_view&& p ) noexcept = default;
- template<typename Source,
- typename = typename std::enable_if<is_constructible<string_view_type, Source>::value>::type>
- key_value_pair_view( const Source& source ) : value_(source) {}
- key_value_pair_view( const char_type * p) : value_(p) {}
- key_value_pair_view( char_type * p) : value_(p) {}
- ~key_value_pair_view() = default;
- key_value_pair_view& operator=( const key_value_pair_view& p ) = default;
- key_value_pair_view& operator=( key_value_pair_view&& p ) noexcept = default;
- void swap( key_value_pair_view& other ) noexcept
- {
- std::swap(value_, other.value_);
- }
- string_view_type native() const noexcept {return value_;}
- operator string_view_type() const {return native();}
- operator typename string_view_type::string_view_type() const {return value_; }
- int compare( key_value_pair_view p ) const noexcept
- {
- const auto c = key().compare(p.key());
- if (c != 0)
- return c;
- return value().compare(p.value());
- }
- int compare( const string_type& str ) const
- {
- return compare(key_value_pair_view(str));
- }
- int compare( string_view_type str ) const
- {
- string_type st(str.data(), str.size());
- return compare(st);
- }
- int compare( const value_type* s ) const
- {
- return compare(key_value_pair_view(s));
- }
- template< class CharT, class Traits = std::char_traits<CharT>, class Alloc = std::allocator<CharT> >
- std::basic_string<CharT,Traits,Alloc>
- basic_string( const Alloc& alloc = Alloc()) const
- {
- return BOOST_PROCESS_V2_NAMESPACE::detail::conv_string<CharT, Traits>(value_.begin(), value_.size(), alloc);
- }
- std::string string() const {return basic_string<char>();}
- std::wstring wstring() const {return basic_string<wchar_t>();}
- string_type native_string() const
- {
- return basic_string<char_type>();
- }
- bool empty() const {return value_.empty(); }
- key_view key() const
- {
- auto eq = value_.find(equality_sign);
- if (eq == 0)
- {
- auto eq2 = value_.find(equality_sign, 1);
- if (eq2 != string_type::npos)
- eq = eq2;
- }
- const auto res = native().substr(0, eq == string_view_type::npos ? value_.size() : eq);
- return key_view::string_view_type(res.data(), res.size());
- }
- value_view value() const
- {
- auto eq = value_.find(equality_sign);
- if (eq == 0)
- {
- auto eq2 = value_.find(equality_sign, 1);
- if (eq2 != string_type::npos)
- eq = eq2;
- }
- return environment::value_view(native().substr(eq + 1));
- }
- friend bool operator==(key_value_pair_view l, key_value_pair_view r) { return l.compare(r) == 0; }
- friend bool operator!=(key_value_pair_view l, key_value_pair_view r) { return l.compare(r) != 0; }
- friend bool operator<=(key_value_pair_view l, key_value_pair_view r) { return l.compare(r) <= 0; }
- friend bool operator>=(key_value_pair_view l, key_value_pair_view r) { return l.compare(r) >= 0; }
- friend bool operator< (key_value_pair_view l, key_value_pair_view r) { return l.compare(r) < 0; }
- friend bool operator> (key_value_pair_view l, key_value_pair_view r) { return l.compare(r) > 0; }
- template< class CharT, class Traits >
- friend std::basic_ostream<CharT,Traits>&
- operator<<( std::basic_ostream<CharT,Traits>& os, const key_value_pair_view& p )
- {
- os << BOOST_PROCESS_V2_NAMESPACE::quoted(p.basic_string<CharT,Traits>());
- return os;
- }
- template< class CharT, class Traits >
- friend std::basic_istream<CharT,Traits>&
- operator>>( std::basic_istream<CharT,Traits>& is, key_value_pair_view& p )
- {
- std::basic_string<CharT, Traits> t;
- is >> BOOST_PROCESS_V2_NAMESPACE::quoted(t);
- p = t;
- return is;
- }
- template<std::size_t Idx>
- inline auto get() const -> typename conditional<Idx == 0u, BOOST_PROCESS_V2_NAMESPACE::environment::key_view,
- BOOST_PROCESS_V2_NAMESPACE::environment::value_view>::type;
- const value_type * c_str() const noexcept
- {
- return value_.data();
- }
- const value_type * data() const {return value_.data(); }
- std::size_t size() const {return value_.size(); }
- private:
- string_view_type value_;
- };
- template<>
- inline key_view key_value_pair_view::get<0u>() const
- {
- return key();
- }
- template<>
- inline value_view key_value_pair_view::get<1u>() const
- {
- return value();
- }
- namespace detail
- {
- template<typename Char>
- std::size_t hash_step(std::size_t prev, Char c, std::char_traits<Char>)
- {
- return prev ^ (c << 1);
- }
- }
- inline std::size_t hash_value(const key_view & value)
- {
- std::size_t hash = 0u;
- for (auto c = value.data(); *c != *v2::detail::null_char_(*c); c++)
- hash = detail::hash_step(hash, *c, key_view::traits_type{});
- return hash ;
- }
- inline std::size_t hash_value(const BOOST_PROCESS_V2_NAMESPACE::environment::value_view & value)
- {
- std::size_t hash = 0u;
- for (auto c = value.data(); *c != *v2::detail::null_char_(*c); c++)
- hash = detail::hash_step(hash, *c, value_view::traits_type{});
- return hash ;
- }
- inline std::size_t hash_value(const BOOST_PROCESS_V2_NAMESPACE::environment::key_value_pair_view & value)
- {
- std::size_t hash = 0u;
- for (auto c = value.data(); *c != *v2::detail::null_char_(*c); c++)
- hash = detail::hash_step(hash, *c, key_value_pair_view::traits_type{});
- return hash ;
- }
- /// A class representing a key within an environment.
- struct key
- {
- using value_type = char_type;
- using traits_type = key_char_traits<char_type>;
- using string_type = std::basic_string<char_type, traits_type>;
- using string_view_type = basic_string_view<char_type, traits_type>;
- key() {}
- key( const key& p ) = default;
- key( key&& p ) noexcept = default;
- key( const string_type& source ) : value_(source) {}
- key( string_type&& source ) : value_(std::move(source)) {}
- key( const value_type * raw ) : value_(raw) {}
- key( value_type * raw ) : value_(raw) {}
- explicit key(key_view kv) : value_(kv.native_string()) {}
- template< class Source >
- key( const Source& source,
- decltype(std::declval<Source>().data()) = nullptr,
- decltype(std::declval<Source>().size()) = 0u)
- : value_(
- BOOST_PROCESS_V2_NAMESPACE::detail::conv_string<char_type, traits_type>(
- source.data(), source.size()))
- {
- }
- key(const typename conditional<is_same<value_type, char>::value, wchar_t, char>::type * raw)
- : value_(BOOST_PROCESS_V2_NAMESPACE::detail::conv_string<char_type, traits_type>(
- raw, std::char_traits<std::decay<std::remove_pointer<decltype(raw)>::type>::type>::length(raw)))
- {
- }
- template< class InputIt >
- key( InputIt first, InputIt last)
- : key(std::basic_string<typename std::iterator_traits<typename std::decay<InputIt>::type>::value_type>(first, last))
- {
- }
- ~key() = default;
- key& operator=( const key& p ) = default;
- key& operator=( key&& p )
- {
- value_ = std::move(p.value_);
- return *this;
- }
- key& operator=( string_type&& source )
- {
- value_ = std::move(source);
- return *this;
- }
- template< class Source >
- key& operator=( const Source& source )
- {
- value_ = BOOST_PROCESS_V2_NAMESPACE::detail::conv_string<char_type, traits_type>(source.data(), source.size());
- return *this;
- }
- key& assign( string_type&& source )
- {
- value_ = std::move(source);
- return *this;
- }
- template< class Source >
- key& assign( const Source& source )
- {
- value_ = BOOST_PROCESS_V2_NAMESPACE::detail::conv_string<char_type, traits_type>(source.data(), source.size());
- return *this;
- }
- template< class InputIt >
- key& assign( InputIt first, InputIt last )
- {
- return assign(std::basic_string<typename std::iterator_traits<typename std::decay<InputIt>::type>::value_type>(first, last));
- }
- void clear() {value_.clear();}
- void swap( key& other ) noexcept
- {
- std::swap(value_, other.value_);
- }
- const value_type* c_str() const noexcept {return value_.c_str();}
- const string_type& native() const noexcept {return value_;}
- string_view_type native_view() const noexcept {return value_;}
- operator string_type() const {return native();}
- operator string_view_type() const {return native_view();}
- int compare( const key& p ) const noexcept {return value_.compare(p.value_);}
- int compare( const string_type& str ) const {return value_.compare(str);}
- int compare( string_view_type str ) const {return -str.compare(value_);}
- int compare( const value_type* s ) const {return value_.compare(s);}
- template< class CharT, class Traits = std::char_traits<CharT>,
- class Alloc = std::allocator<CharT> >
- std::basic_string<CharT,Traits,Alloc>
- basic_string( const Alloc& alloc = Alloc()) const
- {
- return BOOST_PROCESS_V2_NAMESPACE::detail::conv_string<CharT, Traits>(
- value_.data(), value_.size(), alloc);
- }
- std::string string() const {return basic_string<char>();}
- std::wstring wstring() const {return basic_string<wchar_t>();}
- const string_type & native_string() const
- {
- return value_;
- }
- bool empty() const {return value_.empty(); }
- friend bool operator==(const key & l, const key & r) { return l.value_ == r.value_; }
- friend bool operator!=(const key & l, const key & r) { return l.value_ != r.value_; }
- friend bool operator<=(const key & l, const key & r) { return l.value_ <= r.value_; }
- friend bool operator>=(const key & l, const key & r) { return l.value_ >= r.value_; }
- friend bool operator< (const key & l, const key & r) { return l.value_ < r.value_; }
- friend bool operator> (const key & l, const key & r) { return l.value_ > r.value_; }
- template< class CharT, class Traits >
- friend std::basic_ostream<CharT,Traits>&
- operator<<( std::basic_ostream<CharT,Traits>& os, const key& p )
- {
- os << BOOST_PROCESS_V2_NAMESPACE::quoted(p.basic_string<CharT,Traits>());
- return os;
- }
- template< class CharT, class Traits >
- friend std::basic_istream<CharT,Traits>&
- operator>>( std::basic_istream<CharT,Traits>& is, key& p )
- {
- std::basic_string<CharT, Traits> t;
- is >> BOOST_PROCESS_V2_NAMESPACE::quoted(t);
- p = t;
- return is;
- }
- const value_type * data() const {return value_.data(); }
- std::size_t size() const {return value_.size(); }
- private:
- string_type value_;
- };
- #if !defined(GENERATING_DOCUMENTATION)
- template<typename T, typename U>
- typename std::enable_if<
- ((std::is_same<T, key>::value || std::is_same<T, key_view>::value) &&
- std::is_convertible<const U &, key_view>::value)
- ||
- ((std::is_same<U, key>::value || std::is_same<U, key_view>::value) &&
- std::is_convertible<const T & , key_view>::value),
- bool>::type
- operator==(const T &l, const U & r) { return key_view(l) == key_view(r); }
- template<typename T, typename U>
- typename std::enable_if<
- ((std::is_same<T, key>::value || std::is_same<T, key_view>::value) &&
- std::is_convertible<const U &, key_view>::value)
- ||
- ((std::is_same<U, key>::value || std::is_same<U, key_view>::value) &&
- std::is_convertible<const T & , key_view>::value),
- bool>::type
- operator!=(const T &l, const U & r) { return key_view(l) != key_view(r); }
- template<typename T, typename U>
- typename std::enable_if<
- ((std::is_same<T, key>::value || std::is_same<T, key_view>::value) &&
- std::is_convertible<const U &, key_view>::value)
- ||
- ((std::is_same<U, key>::value || std::is_same<U, key_view>::value) &&
- std::is_convertible<const T & , key_view>::value),
- bool>::type
- operator<=(const T &l, const U & r) { return key_view(l) <= key_view(r); }
- template<typename T, typename U>
- typename std::enable_if<
- ((std::is_same<T, key>::value || std::is_same<T, key_view>::value) &&
- std::is_convertible<const U &, key_view>::value)
- ||
- ((std::is_same<U, key>::value || std::is_same<U, key_view>::value) &&
- std::is_convertible<const T & , key_view>::value),
- bool>::type
- operator <(const T &l, const U & r) { return key_view(l) < key_view(r); }
- template<typename T, typename U>
- typename std::enable_if<
- ((std::is_same<T, key>::value || std::is_same<T, key_view>::value) &&
- std::is_convertible<const U &, key_view>::value)
- ||
- ((std::is_same<U, key>::value || std::is_same<U, key_view>::value) &&
- std::is_convertible<const T & , key_view>::value),
- bool>::type
- operator>=(const T &l, const U & r) { return key_view(l) >= key_view(r); }
- template<typename T, typename U>
- typename std::enable_if<
- ((std::is_same<T, key>::value || std::is_same<T, key_view>::value) &&
- std::is_convertible<const U &, key_view>::value)
- ||
- ((std::is_same<U, key>::value || std::is_same<U, key_view>::value) &&
- std::is_convertible<const T & , key_view>::value),
- bool>::type
- operator >(const T &l, const U & r) { return key_view(l) > key_view(r); }
- #else
- bool operator==(const value_view &, const value_view);
- bool operator!=(const value_view &, const value_view);
- bool operator<=(const value_view &, const value_view);
- bool operator< (const value_view &, const value_view);
- bool operator> (const value_view &, const value_view);
- bool operator>=(const value_view &, const value_view);
- #endif
- struct value
- {
- using value_type = char_type;
- using traits_type = value_char_traits<char_type>;
- using string_type = std::basic_string<char_type, traits_type>;
- using string_view_type = basic_cstring_ref<char_type, traits_type>;
- value() {}
- value( const value& p ) = default;
- value( const string_type& source ) : value_(source) {}
- value( string_type&& source ) : value_(std::move(source)) {}
- value( const value_type * raw ) : value_(raw) {}
- value( value_type * raw ) : value_(raw) {}
- explicit value(value_view kv) : value_(kv.c_str()) {}
- template< class Source >
- value( const Source& source,
- decltype(std::declval<Source>().data()) = nullptr,
- decltype(std::declval<Source>().size()) = 0u)
- : value_(BOOST_PROCESS_V2_NAMESPACE::detail::conv_string<char_type, traits_type>(
- source.data(), source.size()))
- {
- }
- value(const typename conditional<is_same<value_type, char>::value, wchar_t, char>::type * raw)
- : value_(BOOST_PROCESS_V2_NAMESPACE::detail::conv_string<char_type, traits_type>(
- raw, std::char_traits<std::decay<std::remove_pointer<decltype(raw)>::type>::type>::length(raw)))
- {
- }
- template< class InputIt >
- value( InputIt first, InputIt last)
- : value(std::basic_string<typename std::iterator_traits<typename std::decay<InputIt>::type>::value_type>(first, last))
- {
- }
- ~value() = default;
- value& operator=( const value& p ) = default;
- value& operator=( value&& p )
- {
- value_ = std::move(p.value_);
- return *this;
- }
- value& operator=( string_type&& source )
- {
- value_ = std::move(source);
- return *this;
- }
- template< class Source >
- value& operator=( const Source& source )
- {
- value_ = BOOST_PROCESS_V2_NAMESPACE::detail::conv_string<char_type, traits_type>(
- source.data(), source.size);
- return *this;
- }
- value& assign( string_type&& source )
- {
- value_ = std::move(source);
- return *this;
- }
- template< class Source >
- value& assign( const Source& source )
- {
- value_ = BOOST_PROCESS_V2_NAMESPACE::detail::conv_string<char_type, traits_type>(
- source.data(), source.size());
- return *this;
- }
- template< class InputIt >
- value& assign( InputIt first, InputIt last )
- {
- return assign(std::basic_string<typename std::iterator_traits<typename std::decay<InputIt>::type>::value_type>(first, last));
- }
- void push_back(const value & sv)
- {
- (value_ += delimiter) += sv;
- }
- void clear() {value_.clear();}
- void swap( value& other ) noexcept
- {
- std::swap(value_, other.value_);
- }
- const value_type* c_str() const noexcept {return value_.c_str();}
- const string_type& native() const noexcept {return value_;}
- string_view_type native_view() const noexcept {return value_;}
- operator string_type() const {return native();}
- operator string_view_type() const {return native_view();}
- operator typename string_view_type::string_view_type() const {return value_; }
- int compare( const value& p ) const noexcept {return value_.compare(p.value_);}
- int compare( const string_type& str ) const {return value_.compare(str);}
- int compare( string_view_type str ) const {return -str.compare(value_);}
- int compare( const value_type* s ) const {return value_.compare(s);}
- template< class CharT, class Traits = std::char_traits<CharT>,
- class Alloc = std::allocator<CharT> >
- std::basic_string<CharT,Traits,Alloc>
- basic_string( const Alloc& alloc = Alloc()) const
- {
- return BOOST_PROCESS_V2_NAMESPACE::detail::conv_string<CharT, Traits>(
- value_.data(), value_.size(),alloc);
- }
- std::string string() const {return basic_string<char>();}
- std::wstring wstring() const {return basic_string<wchar_t>();}
- const string_type & native_string() const
- {
- return value_;
- }
- bool empty() const {return value_.empty(); }
- friend bool operator==(const value & l, const value & r) { return l.value_ == r.value_; }
- friend bool operator!=(const value & l, const value & r) { return l.value_ != r.value_; }
- friend bool operator<=(const value & l, const value & r) { return l.value_ <= r.value_; }
- friend bool operator>=(const value & l, const value & r) { return l.value_ >= r.value_; }
- friend bool operator< (const value & l, const value & r) { return l.value_ < r.value_; }
- friend bool operator> (const value & l, const value & r) { return l.value_ > r.value_; }
- template< class CharT, class Traits >
- friend std::basic_ostream<CharT,Traits>&
- operator<<( std::basic_ostream<CharT,Traits>& os, const value& p )
- {
- os << BOOST_PROCESS_V2_NAMESPACE::quoted(p.basic_string<CharT,Traits>());
- return os;
- }
- template< class CharT, class Traits >
- friend std::basic_istream<CharT,Traits>&
- operator>>( std::basic_istream<CharT,Traits>& is, value& p )
- {
- std::basic_string<CharT, Traits> t;
- is >> BOOST_PROCESS_V2_NAMESPACE::quoted(t);
- p = t;
- return is;
- }
- value_iterator begin() const {return value_iterator(value_.data());}
- value_iterator end() const {return value_iterator(value_.data(), value_.size());}
- const value_type * data() const {return value_.data(); }
- std::size_t size() const {return value_.size(); }
- private:
- string_type value_;
- };
- #if !defined(GENERATING_DOCUMENTATION)
- template<typename T, typename U>
- typename std::enable_if<
- ((std::is_same<T, value>::value || std::is_same<T, value_view>::value) &&
- std::is_convertible<const U &, value_view>::value)
- ||
- ((std::is_same<U, value>::value || std::is_same<U, value_view>::value) &&
- std::is_convertible<const T & , value_view>::value),
- bool>::type
- operator==(const T &l, const U & r) { return value_view(l) == value_view(r); }
- template<typename T, typename U>
- typename std::enable_if<
- ((std::is_same<T, value>::value || std::is_same<T, value_view>::value) &&
- std::is_convertible<const U &, value_view>::value)
- ||
- ((std::is_same<U, value>::value || std::is_same<U, value_view>::value) &&
- std::is_convertible<const T & , value_view>::value),
- bool>::type
- operator!=(const T &l, const U & r) { return value_view(l) != value_view(r); }
- template<typename T, typename U>
- typename std::enable_if<
- ((std::is_same<T, value>::value || std::is_same<T, value_view>::value) &&
- std::is_convertible<const U &, value_view>::value)
- ||
- ((std::is_same<U, value>::value || std::is_same<U, value_view>::value) &&
- std::is_convertible<const T & , value_view>::value),
- bool>::type
- operator<=(const T &l, const U & r) { return value_view(l) <= value_view(r); }
- template<typename T, typename U>
- typename std::enable_if<
- ((std::is_same<T, value>::value || std::is_same<T, value_view>::value) &&
- std::is_convertible<const U &, value_view>::value)
- ||
- ((std::is_same<U, value>::value || std::is_same<U, value_view>::value) &&
- std::is_convertible<const T & , value_view>::value),
- bool>::type
- operator <(const T &l, const U & r) { return value_view(l) < value_view(r); }
- template<typename T, typename U>
- typename std::enable_if<
- ((std::is_same<T, value>::value || std::is_same<T, value_view>::value) &&
- std::is_convertible<const U &, value_view>::value)
- ||
- ((std::is_same<U, value>::value || std::is_same<U, value_view>::value) &&
- std::is_convertible<const T & , value_view>::value),
- bool>::type
- operator>=(const T &l, const U & r) { return value_view(l) >= value_view(r); }
- template<typename T, typename U>
- typename std::enable_if<
- ((std::is_same<T, value>::value || std::is_same<T, value_view>::value) &&
- std::is_convertible<const U &, value_view>::value)
- ||
- ((std::is_same<U, value>::value || std::is_same<U, value_view>::value) &&
- std::is_convertible<const T & , value_view>::value),
- bool>::type
- operator >(const T &l, const U & r) { return value_view(l) > value_view(r); }
- #else
- bool operator==(const value_view &, const value_view);
- bool operator!=(const value_view &, const value_view);
- bool operator<=(const value_view &, const value_view);
- bool operator< (const value_view &, const value_view);
- bool operator> (const value_view &, const value_view);
- bool operator>=(const value_view &, const value_view);
- #endif
- struct key_value_pair
- {
- using value_type = char_type;
- using traits_type = std::char_traits<char_type>;
- using string_type = std::basic_string<char_type>;
- using string_view_type = basic_cstring_ref<char_type>;
- key_value_pair() {}
- key_value_pair( const key_value_pair& p ) = default;
- key_value_pair( key_value_pair&& p ) noexcept = default;
- key_value_pair(key_view key, value_view value) : value_(key.basic_string<char_type, traits_type>() + equality_sign +
- value.basic_string<char_type, traits_type>()) {}
- key_value_pair(key_view key, std::initializer_list<basic_string_view<char_type>> values)
- {
- const auto sz = std::accumulate(values.begin(), values.end(),
- key.size(), [](std::size_t sz, const basic_string_view<char_type> & str) { return sz + str.size() + 1;});
- value_.reserve(sz);
- value_.append(key.data(), key.size());
- value_ += equality_sign;
- for (auto & value : values)
- {
- if (value_.back() != equality_sign)
- value_ += delimiter;
- value_.append(value.data(), value.size());
- }
- }
- key_value_pair( const string_type& source ) : value_(source) {}
- key_value_pair( string_type&& source ) : value_(std::move(source)) {}
- key_value_pair( const value_type * raw ) : value_(raw) {}
- key_value_pair( value_type * raw ) : value_(raw) {}
- explicit key_value_pair(key_value_pair_view kv) : value_(kv.c_str()) {}
- template< class Source >
- key_value_pair( const Source& source,
- decltype(std::declval<Source>().data()) = nullptr,
- decltype(std::declval<Source>().size()) = 0u)
- : value_(BOOST_PROCESS_V2_NAMESPACE::detail::conv_string<char_type, traits_type>(
- source.data(), source.size()))
- {
- }
- template< typename Key,
- typename Value >
- key_value_pair(
- const std::pair<Key, Value> & kv/*,
- typename std::enable_if<std::is_constructible<struct key, Key >::value &&
- std::is_constructible<struct value, Value>::value
- >::type = 0*/) : value_(((struct key)(kv.first)).basic_string<char_type, traits_type>() + equality_sign
- + ((struct value)(kv.second)).basic_string<char_type, traits_type>())
- {}
- key_value_pair(const typename conditional<is_same<value_type, char>::value, wchar_t, char>::type * raw)
- : value_(BOOST_PROCESS_V2_NAMESPACE::detail::conv_string<char_type, traits_type>(
- raw,
- std::char_traits<std::decay<std::remove_pointer<decltype(raw)>::type>::type>::length(raw)))
- {
- }
- template< class InputIt , typename std::iterator_traits<InputIt>::iterator_category>
- key_value_pair( InputIt first, InputIt last )
- : key_value_pair(std::basic_string<typename std::iterator_traits<typename std::decay<InputIt>::type>::value_type>(first, last))
- {
- }
- ~key_value_pair() = default;
- key_value_pair& operator=( const key_value_pair& p ) = default;
- key_value_pair& operator=( key_value_pair&& p )
- {
- value_ = std::move(p.value_);
- return *this;
- }
- key_value_pair& operator=( string_type&& source )
- {
- value_ = std::move(source);
- return *this;
- }
- template< class Source >
- key_value_pair& operator=( const Source& source )
- {
- value_ = BOOST_PROCESS_V2_NAMESPACE::detail::conv_string<char_type, traits_type>(
- source.data(), source.size());
- return *this;
- }
- key_value_pair& assign( string_type&& source )
- {
- value_ = std::move(source);
- return *this;
- }
- template< class Source >
- key_value_pair& assign( const Source& source )
- {
- value_ = BOOST_PROCESS_V2_NAMESPACE::detail::conv_string<char_type, traits_type>(
- source.data(), source.size());
- return *this;
- }
- template< class InputIt >
- key_value_pair& assign( InputIt first, InputIt last )
- {
- return assign(std::basic_string<typename std::iterator_traits<typename std::decay<InputIt>::type>::value_type>(first, last));
- }
- void clear() {value_.clear();}
- void swap( key_value_pair& other ) noexcept
- {
- std::swap(value_, other.value_);
- }
- const value_type* c_str() const noexcept {return value_.c_str();}
- const string_type& native() const noexcept {return value_;}
- string_view_type native_view() const noexcept {return value_;}
- operator string_type() const {return native();}
- operator string_view_type() const {return native_view();}
- operator typename string_view_type::string_view_type() const {return native_view();}
- operator key_value_pair_view() const {return native_view();}
- int compare( const key_value_pair& p ) const noexcept
- {
- return key_value_pair_view(*this).compare(key_value_pair_view(p));
- }
-
- int compare( const string_type& str ) const
- {
- return key_value_pair_view(*this).compare(str);
- }
- int compare( string_view_type str ) const
- {
- return key_value_pair_view(*this).compare(str);
- }
- int compare( const value_type* s ) const
- {
- return key_value_pair_view(*this).compare(s);
- }
- template< class CharT, class Traits = std::char_traits<CharT>, class Alloc = std::allocator<CharT> >
- std::basic_string<CharT,Traits,Alloc>
- basic_string( const Alloc& alloc = Alloc() ) const
- {
- return BOOST_PROCESS_V2_NAMESPACE::detail::conv_string<CharT, Traits>(value_.data(), value_.size(), alloc);
- }
- std::string string() const {return basic_string<char>();}
- std::wstring wstring() const {return basic_string<wchar_t>();}
- const string_type & native_string() const
- {
- return value_;
- }
- friend bool operator==(const key_value_pair & l, const key_value_pair & r) { return l.compare(r) == 0; }
- friend bool operator!=(const key_value_pair & l, const key_value_pair & r) { return l.compare(r) != 0; }
- friend bool operator<=(const key_value_pair & l, const key_value_pair & r) { return l.compare(r) <= 0; }
- friend bool operator>=(const key_value_pair & l, const key_value_pair & r) { return l.compare(r) >= 0; }
- friend bool operator< (const key_value_pair & l, const key_value_pair & r) { return l.compare(r) < 0; }
- friend bool operator> (const key_value_pair & l, const key_value_pair & r) { return l.compare(r) > 0; }
- bool empty() const {return value_.empty(); }
- struct key_view key() const
- {
- auto eq = value_.find(equality_sign);
- if (eq == 0)
- {
- auto eq2 = value_.find(equality_sign, 1);
- if (eq2 != string_type::npos)
- eq = eq2;
- }
- const auto k = native_view().substr(0, eq);
- return BOOST_PROCESS_V2_NAMESPACE::environment::key_view::string_view_type (k.data(), k.size());
- }
- struct value_view value() const
- {
- auto eq = value_.find(equality_sign);
- if (eq == 0)
- {
- auto eq2 = value_.find(equality_sign, 1);
- if (eq2 != string_type::npos)
- eq = eq2;
- }
- return value_view::string_view_type(native_view().substr(eq + 1));
- }
- template< class CharT, class Traits >
- friend std::basic_ostream<CharT,Traits>&
- operator<<( std::basic_ostream<CharT,Traits>& os, const key_value_pair& p )
- {
- os << BOOST_PROCESS_V2_NAMESPACE::quoted(p.basic_string<CharT,Traits>());
- return os;
- }
- template< class CharT, class Traits >
- friend std::basic_istream<CharT,Traits>&
- operator>>( std::basic_istream<CharT,Traits>& is, key_value_pair& p )
- {
- is >> BOOST_PROCESS_V2_NAMESPACE::quoted(p.value_);
- return is;
- }
- const value_type * data() const {return value_.data(); }
- std::size_t size() const {return value_.size(); }
- template<std::size_t Idx>
- inline auto get() const
- -> typename conditional<Idx == 0u, BOOST_PROCESS_V2_NAMESPACE::environment::key_view,
- BOOST_PROCESS_V2_NAMESPACE::environment::value_view>::type;
- private:
- string_type value_;
- };
- #if !defined(GENERATING_DOCUMENTATION)
- template<typename T, typename U>
- typename std::enable_if<
- ((std::is_same<T, key_value_pair>::value || std::is_same<T, key_value_pair_view>::value) &&
- std::is_convertible<const U &, key_value_pair_view>::value)
- ||
- ((std::is_same<U, key_value_pair>::value || std::is_same<U, key_value_pair_view>::value) &&
- std::is_convertible<const T & , key_value_pair_view>::value),
- bool>::type
- operator==(const T &l, const U & r) { return key_value_pair_view(l) == key_value_pair_view(r); }
- template<typename T, typename U>
- typename std::enable_if<
- ((std::is_same<T, key_value_pair>::value || std::is_same<T, key_value_pair_view>::value) &&
- std::is_convertible<const U &, key_value_pair_view>::value)
- ||
- ((std::is_same<U, key_value_pair>::value || std::is_same<U, key_value_pair_view>::value) &&
- std::is_convertible<const T & , key_value_pair_view>::value),
- bool>::type
- operator!=(const T &l, const U & r) { return key_value_pair_view(l) != key_value_pair_view(r); }
- template<typename T, typename U>
- typename std::enable_if<
- ((std::is_same<T, key_value_pair>::value || std::is_same<T, key_value_pair_view>::value) &&
- std::is_convertible<const U &, key_value_pair_view>::value)
- ||
- ((std::is_same<U, key_value_pair>::value || std::is_same<U, key_value_pair_view>::value) &&
- std::is_convertible<const T & , key_value_pair_view>::value),
- bool>::type
- operator<=(const T &l, const U & r) { return key_value_pair_view(l) <= key_value_pair_view(r); }
- template<typename T, typename U>
- typename std::enable_if<
- ((std::is_same<T, key_value_pair>::value || std::is_same<T, key_value_pair_view>::value) &&
- std::is_convertible<const U &, key_value_pair_view>::value)
- ||
- ((std::is_same<U, key_value_pair>::value || std::is_same<U, key_value_pair_view>::value) &&
- std::is_convertible<const T & , key_value_pair_view>::value),
- bool>::type
- operator <(const T &l, const U & r) { return key_value_pair_view(l) < key_value_pair_view(r); }
- template<typename T, typename U>
- typename std::enable_if<
- ((std::is_same<T, key_value_pair>::value || std::is_same<T, key_value_pair_view>::value) &&
- std::is_convertible<const U &, key_value_pair_view>::value)
- ||
- ((std::is_same<U, key_value_pair>::value || std::is_same<U, key_value_pair_view>::value) &&
- std::is_convertible<const T & , key_value_pair_view>::value),
- bool>::type
- operator>=(const T &l, const U & r) { return key_value_pair_view(l) >= key_value_pair_view(r); }
- template<typename T, typename U>
- typename std::enable_if<
- ((std::is_same<T, key_value_pair>::value || std::is_same<T, key_value_pair_view>::value) &&
- std::is_convertible<const U &, key_value_pair_view>::value)
- ||
- ((std::is_same<U, key_value_pair>::value || std::is_same<U, key_value_pair_view>::value) &&
- std::is_convertible<const T & , key_value_pair_view>::value),
- bool>::type
- operator >(const T &l, const U & r) { return key_value_pair_view(l) > key_value_pair_view(r); }
- #else
- bool operator==(const key_value_pair_view &, const key_value_pair_view);
- bool operator!=(const key_value_pair_view &, const key_value_pair_view);
- bool operator<=(const key_value_pair_view &, const key_value_pair_view);
- bool operator< (const key_value_pair_view &, const key_value_pair_view);
- bool operator> (const key_value_pair_view &, const key_value_pair_view);
- bool operator>=(const key_value_pair_view &, const key_value_pair_view);
- #endif
- template<>
- inline key_view key_value_pair::get<0u>() const
- {
- return key();
- }
- template<>
- inline value_view key_value_pair::get<1u>() const
- {
- return value();
- }
- }
- BOOST_PROCESS_V2_END_NAMESPACE
- namespace std
- {
- template<>
- class tuple_size<BOOST_PROCESS_V2_NAMESPACE::environment::key_value_pair> : integral_constant<std::size_t, 2u> {};
- template<>
- class tuple_element<0u, BOOST_PROCESS_V2_NAMESPACE::environment::key_value_pair>
- {
- public:
- using type = BOOST_PROCESS_V2_NAMESPACE::environment::key_view;
- };
- template<>
- class tuple_element<1u, BOOST_PROCESS_V2_NAMESPACE::environment::key_value_pair>
- {
- public:
- using type = BOOST_PROCESS_V2_NAMESPACE::environment::value_view;
- };
- template<std::size_t Idx>
- inline auto get(const BOOST_PROCESS_V2_NAMESPACE::environment::key_value_pair & kvp)
- -> typename std::tuple_element<Idx, BOOST_PROCESS_V2_NAMESPACE::environment::key_value_pair>::type
- {
- return kvp.get<Idx>();
- }
- template<>
- class tuple_size<BOOST_PROCESS_V2_NAMESPACE::environment::key_value_pair_view> : integral_constant<std::size_t, 2u> {};
- template<>
- class tuple_element<0u, BOOST_PROCESS_V2_NAMESPACE::environment::key_value_pair_view>
- {
- public:
- using type = BOOST_PROCESS_V2_NAMESPACE::environment::key_view;
- };
- template<>
- class tuple_element<1u, BOOST_PROCESS_V2_NAMESPACE::environment::key_value_pair_view>
- {
- public:
- using type = BOOST_PROCESS_V2_NAMESPACE::environment::value_view;
- };
- template<std::size_t Idx>
- inline auto get(BOOST_PROCESS_V2_NAMESPACE::environment::key_value_pair_view kvp)
- -> typename std::tuple_element<Idx, BOOST_PROCESS_V2_NAMESPACE::environment::key_value_pair_view>::type
- {
- return kvp.get<Idx>();
- }
- }
- BOOST_PROCESS_V2_BEGIN_NAMESPACE
- namespace environment
- {
- /// A view object for the current environment of this process.
- /**
- * The view might (windows) or might not (posix) be owning;
- * if it owns it will deallocate the on destruction, like a unique_ptr.
- *
- * Note that accessing the environment in this way is not thread-safe.
- *
- * @code
- *
- * void dump_my_env(current_view env = current())
- * {
- * for (auto & [k, v] : env)
- * std::cout << k.string() << " = " << v.string() << std::endl;
- * }
- *
- * @endcode
- *
- *
- */
- struct current_view
- {
- using native_handle_type = environment::native_handle_type;
- using value_type = key_value_pair_view;
- current_view() = default;
- current_view(current_view && nt) = default;
- native_handle_type native_handle() { return handle_.get(); }
- struct iterator
- {
- using value_type = key_value_pair_view;
- using difference_type = int;
- using reference = key_value_pair_view;
- using pointer = key_value_pair_view;
- using iterator_category = std::forward_iterator_tag;
- iterator() = default;
- iterator(const iterator & ) = default;
- iterator(const native_iterator &native_handle) : iterator_(native_handle) {}
- iterator & operator++()
- {
- iterator_ = detail::next(iterator_);
- return *this;
- }
- iterator operator++(int)
- {
- auto last = *this;
- iterator_ = detail::next(iterator_);
- return last;
- }
- key_value_pair_view operator*() const
- {
- return detail::dereference(iterator_);
- }
- friend bool operator==(const iterator & l, const iterator & r) {return l.iterator_ == r.iterator_;}
- friend bool operator!=(const iterator & l, const iterator & r) {return l.iterator_ != r.iterator_;}
- private:
- environment::native_iterator iterator_;
- };
- iterator begin() const {return iterator(handle_.get());}
- iterator end() const {return iterator(detail::find_end(handle_.get()));}
- private:
- std::unique_ptr<typename remove_pointer<native_handle_type>::type,
- detail::native_handle_deleter> handle_{environment::detail::load_native_handle()};
- };
- /// Obtain a handle to the current environment
- inline current_view current() {return current_view();}
- namespace detail
- {
- template<typename Environment>
- auto find_key(Environment & env, key_view ky)
- -> typename std::enable_if<std::is_convertible<decltype(*std::begin(env)), key_value_pair_view>::value, value_view>::type
- {
- const auto itr = std::find_if(std::begin(env), std::end(env),
- [&](key_value_pair_view vp)
- {
- auto tmp = std::get<0>(vp) == ky;
- if (tmp)
- return true;
- else
- return false;
- });
-
- if (itr != std::end(env))
- return key_value_pair_view(*itr).value();
- else
- return {};
- }
- template<typename Environment>
- auto find_key(Environment & env, key_view ky)
- -> typename std::enable_if<
- !std::is_convertible<decltype(*std::begin(env)), key_value_pair_view>::value &&
- std::is_convertible<decltype(*std::begin(env)), key_value_pair>::value,
- value>::type
- {
- const auto itr = std::find_if(std::begin(env), std::end(env),
- [&](key_value_pair vp)
- {
- auto tmp = std::get<0>(vp) == ky;
- if (tmp)
- return true;
- else
- return false;
- });
- if (itr != std::end(env))
- return key_value_pair(*itr).value();
- else
- return {};
- }
- }
- /// Find the home folder in an environment-like type.
- /**
- * @param env The environment to search. Defaults to the current environment of this process
- *
- * The environment type passed in must be a range with value T that fulfills the following requirements:
- *
- * For `T value`
- *
- * - std::get<0>(value) must return a type comparable to `key_view`.
- * - std::get<1>(value) must return a type convertible to filesystem::path.
- *
- * @return A filesystem::path to the home directory or an empty path if it cannot be found.
- *
- */
- template<typename Environment = current_view>
- inline filesystem::path home(Environment && env = current())
- {
- #if defined(BOOST_PROCESS_V2_WINDOWS)
- return detail::find_key(env, L"HOMEDRIVE").native_string()
- + detail::find_key(env, L"HOMEPATH").native_string();
- #else
- return detail::find_key(env, "HOME").native_string();
- #endif
- }
- /// Find the executable `name` in an environment-like type.
- /**
- * @param env The environment to search. Defaults to the current environment of this process
- *
- * The environment type passed in must be a range with value T that fulfills the following requirements:
- *
- * For `T value`
- *
- * - std::get<0>(value) must return a type comparable to `key_view`.
- * - std::get<1>(value) must return a type convertible to `value_view`.
- *
- *
- * @return A filesystem::path to the executable or an empty path if it cannot be found.
- *
- */
- template<typename Environment = current_view>
- inline BOOST_PROCESS_V2_NAMESPACE::filesystem::path find_executable(
- BOOST_PROCESS_V2_NAMESPACE::filesystem::path name,
- Environment && env = current())
- {
- #if defined(BOOST_PROCESS_V2_WINDOWS)
- auto path = detail::find_key(env, L"PATH");
- auto pathext = detail::find_key(env, L"PATHEXT");
- for (auto pp_view : path)
- {
- // first check if it has the extension already
- BOOST_PROCESS_V2_NAMESPACE::filesystem::path full_nm(name);
- BOOST_PROCESS_V2_NAMESPACE::filesystem::path pp(pp_view.begin(), pp_view.end());
- auto p = pp / full_nm;
- error_code ec;
- if (detail::is_executable(p, ec) && !ec)
- return p;
- for (auto ext : pathext)
- {
- ec.clear();
- BOOST_PROCESS_V2_NAMESPACE::filesystem::path nm(name);
- nm.concat(ext.begin(), ext.end());
- auto p = pp / nm;
- if (detail::is_executable(p, ec) && !ec)
- return p;
- }
- }
- #else
- for (auto pp_view : detail::find_key(env, "PATH"))
- {
- auto p = BOOST_PROCESS_V2_NAMESPACE::filesystem::path(pp_view.begin(), pp_view.end()) / name;
- error_code ec;
- bool is_exec = detail::is_executable(p, ec);
- if (!ec && is_exec)
- return p;
- }
- #endif
- return {};
- }
- /// Get an environment variable from the current process.
- inline value get(const key & k, error_code & ec) { return detail::get(k.c_str(), ec);}
- /// Throwing @overload value get(const key & k, error_code & ec)
- inline value get(const key & k)
- {
- error_code ec;
- auto tmp = detail::get(k.c_str(), ec);
- BOOST_PROCESS_V2_NAMESPACE::detail::throw_error(ec, "environment::get");
- return tmp;
- }
- /// Disambiguating @overload value get(const key & k, error_code & ec)
- inline value get(basic_cstring_ref<char_type, key_char_traits<char_type>> k, error_code & ec)
- {
- return detail::get(k, ec);
- }
- /// Disambiguating @overload value get(const key & k)
- inline value get(basic_cstring_ref<char_type, key_char_traits<char_type>> k)
- {
- error_code ec;
- auto tmp = detail::get(k, ec);
- BOOST_PROCESS_V2_NAMESPACE::detail::throw_error(ec, "environment::get");
- return tmp;
- }
- /// Disambiguating @overload value get(const key & k, error_code & ec)
- inline value get(const char_type * c, error_code & ec) { return detail::get(c, ec);}
- /// Disambiguating @overload value get(const key & k)
- inline value get(const char_type * c)
- {
- error_code ec;
- auto tmp = detail::get(c, ec);
- BOOST_PROCESS_V2_NAMESPACE::detail::throw_error(ec, "environment::get");
- return tmp;
- }
- /// Set an environment variable for the current process.
- inline void set(const key & k, value_view vw, error_code & ec) { detail::set(k, vw, ec);}
- /// Throwing @overload void set(const key & k, value_view vw, error_code & ec)
- inline void set(const key & k, value_view vw)
- {
- error_code ec;
- detail::set(k, vw, ec);
- BOOST_PROCESS_V2_NAMESPACE::detail::throw_error(ec, "environment::set");
- }
- /// Disambiguating @overload void set(const key & k, value_view vw, error_code & ec)
- inline void set(basic_cstring_ref<char_type, key_char_traits<char_type>> k, value_view vw, error_code & ec) { detail::set(k, vw, ec);}
- /// Disambiguating @overload void set(const key & k, value_view vw, error_code & ec)
- inline void set(basic_cstring_ref<char_type, key_char_traits<char_type>> k, value_view vw)
- {
- error_code ec;
- detail::set(k, vw, ec);
- BOOST_PROCESS_V2_NAMESPACE::detail::throw_error(ec, "environment::set");
- }
- /// Disambiguating @overload void set(const key & k, value_view vw, error_code & ec)
- inline void set(const char_type * k, value_view vw, error_code & ec) { detail::set(k, vw, ec);}
- /// Disambiguating @overload void set(const key & k, value_view vw, error_code & ec)
- inline void set(const char_type * k, value_view vw)
- {
- error_code ec;
- detail::set(k, vw, ec);
- BOOST_PROCESS_V2_NAMESPACE::detail::throw_error(ec, "environment::set");
- }
- /// Disambiguating @overload void set(const key & k, value_view vw, error_code & ec)
- template<typename Char, typename = typename std::enable_if<!std::is_same<Char, char_type>::value>::type>
- inline void set(const key & k, const Char * vw, error_code & ec)
- {
- value val{vw};
- detail::set(k, val, ec);
- }
- /// Disambiguating @overload void set(const key & k, value_view vw, error_code & ec)
- template<typename Char, typename = typename std::enable_if<!std::is_same<Char, char_type>::value>::type>
- inline void set(const key & k, const Char * vw)
- {
- error_code ec;
- value val{vw};
- detail::set(k, val, ec);
- BOOST_PROCESS_V2_NAMESPACE::detail::throw_error(ec, "environment::set");
- }
- /// Disambiguating @overload void set(const key & k, value_view vw, error_code & ec)
- template<typename Char, typename = typename std::enable_if<!std::is_same<Char, char_type>::value>::type>
- inline void set(basic_cstring_ref<char_type, key_char_traits<char_type>> k, const Char * vw, error_code & ec)
- {
- value val{vw};
- detail::set(k, val, ec);
- }
- /// Disambiguating @overload void set(const key & k, value_view vw, error_code & ec)
- template<typename Char, typename = typename std::enable_if<!std::is_same<Char, char_type>::value>::type>
- inline void set(basic_cstring_ref<char_type, key_char_traits<char_type>> k, const Char * vw)
- {
- error_code ec;
- value val{vw};
- detail::set(k, val, ec);
- BOOST_PROCESS_V2_NAMESPACE::detail::throw_error(ec, "environment::set");
- }
- /// Disambiguating @overload void set(const key & k, value_view vw, error_code & ec)
- template<typename Char, typename = typename std::enable_if<!std::is_same<Char, char_type>::value>::type>
- inline void set(const char_type * k, const Char * vw, error_code & ec)
- {
- value val{vw};
- detail::set(k, val, ec);
- }
- /// Disambiguating @overload void set(const key & k, value_view vw, error_code & ec)
- template<typename Char, typename = typename std::enable_if<!std::is_same<Char, char_type>::value>::type>
- inline void set(const char_type * k, const Char * vw)
- {
- error_code ec;
- value val{vw};
- detail::set(k, val, ec);
- BOOST_PROCESS_V2_NAMESPACE::detail::throw_error(ec, "environment::set");
- }
- /// Remove an environment variable from the current process.
- inline void unset(const key & k, error_code & ec) { detail::unset(k, ec);}
- /// Throwing @overload void unset(const key & k, error_code & ec)
- inline void unset(const key & k)
- {
- error_code ec;
- detail::unset(k, ec);
- BOOST_PROCESS_V2_NAMESPACE::detail::throw_error(ec, "environment::unset");
- }
- /// Disambiguating @overload void unset(const key & k, error_code & ec)
- inline void unset(basic_cstring_ref<char_type, key_char_traits<char_type>> k, error_code & ec)
- {
- detail::unset(k, ec);
- }
- /// Disambiguating @overload void unset(const key & k, error_code & ec)
- inline void unset(basic_cstring_ref<char_type, key_char_traits<char_type>> k)
- {
- error_code ec;
- detail::unset(k, ec);
- BOOST_PROCESS_V2_NAMESPACE::detail::throw_error(ec, "environment::unset");
- }
- /// Disambiguating @overload void unset(const key & k, error_code & ec)
- inline void unset(const char_type * c, error_code & ec) { detail::unset(c, ec);}
- /// Disambiguating @overload void unset(const key & k, error_code & ec)
- inline void unset(const char_type * c)
- {
- error_code ec;
- detail::unset(c, ec);
- BOOST_PROCESS_V2_NAMESPACE::detail::throw_error(ec, "environment::unset");
- }
- }
- // sub process environment stuff
- #if defined(BOOST_PROCESS_V2_WINDOWS)
- namespace windows { struct default_launcher ;}
- #else
- namespace posix { struct default_launcher ;}
- #endif
- /// Initializer for the environment of sub process.
- /**
- * This will set the environment in a subprocess:
- *
- * @code {.cpp}
- *
- * process proc{executor, find_executable("printenv"), {"foo"}, process_environment{"foo=bar"}};
- * @endcode
- *
- * The environment initializer will persist it's state, so that it can
- * be used multiple times. Do however note the the Operating System is
- * allowed to modify the internal state.
- *
- * @code {.cpp}
- * auto exe = find_executable("printenv");
- * process_environment env = {"FOO=BAR", "BAR=FOO"};
- *
- * process proc1(executor, exe, {"FOO"}, env);
- * process proc2(executor, exe, {"BAR"}, env);
- * @endcode
- *
- *
- */
- struct process_environment
- {
- #if defined(BOOST_PROCESS_V2_WINDOWS)
- template<typename Args>
- static
- std::vector<wchar_t> build_env(Args && args,
- typename std::enable_if<
- std::is_convertible<
- decltype(*std::begin(std::declval<Args>())),
- wcstring_ref>::value>::type * = nullptr)
- {
- std::vector<wchar_t> res;
- std::size_t sz = 1;
- for (wcstring_ref cs : std::forward<Args>(args))
- sz =+ cs.size() + 1;
- res.reserve(sz);
-
- for (wcstring_ref cs : std::forward<Args>(args))
- res.insert(res.end(), cs.begin(), std::next(cs.end()));
-
- res.push_back(L'\0');
- return res;
- }
- template<typename Args>
- std::vector<wchar_t> build_env(Args && args,
- typename std::enable_if<
- !std::is_convertible<
- decltype(*std::begin(std::declval<Args>())),
- wcstring_ref>::value>::type * = nullptr)
- {
- for (auto && arg: std::forward<Args>(args))
- env_buffer.emplace_back(arg);
- return build_env(env_buffer);
- }
- process_environment(std::initializer_list<string_view> sv) : unicode_env{build_env(sv)} {}
- process_environment(std::initializer_list<wstring_view> sv) : unicode_env{build_env(sv)} {}
- template<typename Args>
- process_environment(Args && args) : unicode_env{build_env(std::forward<Args>(args))}
- {
- }
- error_code error() {return ec;}
- error_code ec;
- std::vector<environment::key_value_pair> env_buffer;
- std::vector<wchar_t> unicode_env;
- BOOST_PROCESS_V2_DECL
- error_code on_setup(windows::default_launcher & launcher,
- const filesystem::path &, const std::wstring &);
- #else
- template<typename Args>
- static
- std::vector<const char *> build_env(Args && args,
- typename std::enable_if<
- std::is_convertible<
- decltype(*std::begin(std::declval<Args>())),
- cstring_ref>::value>::type * = nullptr)
- {
- std::vector<const char *> env;
- for (auto && e : args)
- env.push_back(e.c_str());
- env.push_back(nullptr);
- return env;
- }
- template<typename Args>
- std::vector<const char *> build_env(Args && args,
- typename std::enable_if<
- !std::is_convertible<
- decltype(*std::begin(std::declval<Args>())),
- cstring_ref>::value>::type * = nullptr)
- {
- std::vector<const char *> env;
- for (auto && arg: std::forward<Args>(args))
- env_buffer.emplace_back(arg);
- for (auto && e : env_buffer)
- env.push_back(e.c_str());
- env.push_back(nullptr);
- return env;
- }
- process_environment(std::initializer_list<string_view> sv) : env{build_env(sv)} { }
- template<typename Args>
- process_environment(Args && args) : env(build_env(std::forward<Args>(args)))
- {
- }
- BOOST_PROCESS_V2_DECL
- error_code on_setup(posix::default_launcher & launcher,
- const filesystem::path &, const char * const *);
- std::vector<environment::key_value_pair> env_buffer;
- std::vector<const char *> env;
- #endif
- };
- BOOST_PROCESS_V2_END_NAMESPACE
- namespace std
- {
- template<>
- struct hash<BOOST_PROCESS_V2_NAMESPACE::environment::key_view>
- {
- std::size_t operator()( BOOST_PROCESS_V2_NAMESPACE::environment::key_view kv) const noexcept
- {
- return BOOST_PROCESS_V2_NAMESPACE::environment::hash_value(kv);
- }
- };
- template<>
- struct hash<BOOST_PROCESS_V2_NAMESPACE::environment::value_view>
- {
- std::size_t operator()( BOOST_PROCESS_V2_NAMESPACE::environment::value_view kv) const noexcept
- {
- return BOOST_PROCESS_V2_NAMESPACE::environment::hash_value(kv);
- }
- };
- template<>
- struct hash<BOOST_PROCESS_V2_NAMESPACE::environment::key_value_pair_view>
- {
- std::size_t operator()( BOOST_PROCESS_V2_NAMESPACE::environment::key_value_pair_view kv) const noexcept
- {
- return BOOST_PROCESS_V2_NAMESPACE::environment::hash_value(kv);
- }
- };
- template<>
- struct hash<BOOST_PROCESS_V2_NAMESPACE::environment::key>
- {
- std::size_t operator()( BOOST_PROCESS_V2_NAMESPACE::environment::key_view kv) const noexcept
- {
- return BOOST_PROCESS_V2_NAMESPACE::environment::hash_value(kv);
- }
- };
- template<>
- struct hash<BOOST_PROCESS_V2_NAMESPACE::environment::value>
- {
- std::size_t operator()( BOOST_PROCESS_V2_NAMESPACE::environment::value_view kv) const noexcept
- {
- return BOOST_PROCESS_V2_NAMESPACE::environment::hash_value(kv);
- }
- };
- template<>
- struct hash<BOOST_PROCESS_V2_NAMESPACE::environment::key_value_pair>
- {
- std::size_t operator()( BOOST_PROCESS_V2_NAMESPACE::environment::key_value_pair_view kv) const noexcept
- {
- return BOOST_PROCESS_V2_NAMESPACE::environment::hash_value(kv);
- }
- };
- }
- #endif //BOOST_PROCESS_V2_ENVIRONMENT_HPP
|