+namespace experimental {
+
+// Class to model the process environment in idiomatic C++
+//
+// Changes to the modeled environment do not change the process environment
+// unless `setAsCurrentEnvironment()` is called.
+struct EnvironmentState {
+ using EnvType = std::unordered_map<std::string, std::string>;
+
+ // Returns an EnvironmentState containing a copy of the current process
+ // environment. Subsequent changes to the process environment do not
+ // alter the stored model. If the process environment is altered during the
+ // execution of this method the results are not defined.
+ //
+ // Throws MalformedEnvironment if the process environment cannot be modeled.
+ static EnvironmentState fromCurrentEnvironment();
+
+ // Returns an empty EnvironmentState
+ static EnvironmentState empty() {
+ return {};
+ }
+
+ explicit EnvironmentState(EnvType const& env) : env_(env) {}
+ explicit EnvironmentState(EnvType&& env) : env_(std::move(env)) {}
+
+ // Get the model environment for querying.
+ EnvType const& operator*() const {
+ return env_;
+ }
+ EnvType const* operator->() const {
+ return &env_;
+ }
+
+ // Get the model environment for mutation or querying.
+ EnvType& operator*() {
+ return env_;
+ }
+ EnvType* operator->() {
+ return &env_;
+ }
+
+ // Update the process environment with the one in the stored model.
+ // Subsequent changes to the model do not alter the process environment. The
+ // state of the process environment during execution of this method is not
+ // defined. If the process environment is altered by another thread during the
+ // execution of this method the results are not defined.
+ void setAsCurrentEnvironment();
+
+ // Get a copy of the model environment in the form used by `folly::Subprocess`
+ std::vector<std::string> toVector() const;
+
+ // Get a copy of the model environment in the form commonly used by C routines
+ // such as execve, execle, etc. Example usage:
+ //
+ // EnvironmentState forChild{};
+ // ... manipulate `forChild` as needed ...
+ // execve("/bin/program",pArgs,forChild.toPointerArray().get());
+ std::unique_ptr<char*, void (*)(char**)> toPointerArray() const;
+
+ private:
+ EnvironmentState() {}
+ EnvType env_;
+};
+
+struct MalformedEnvironment : std::runtime_error {
+ using std::runtime_error::runtime_error;
+};
+} // namespace experimental
+