sapling/eden/fs/utils/test/ScopedEnvVar.h
Chad Austin 2a6dd2879d folly::Optional -> std::optional
Summary: Eden's on C++17 so fully cross the rubicon!

Reviewed By: strager

Differential Revision: D10498200

fbshipit-source-id: 4e2af5a8d5cef9a106e8c05e6f93ea9e5b8e696e
2018-10-23 18:51:59 -07:00

54 lines
1.3 KiB
C++

/*
* Copyright (c) 2004-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#pragma once
#include <folly/Range.h>
#include <optional>
namespace facebook {
namespace eden {
/**
* A helper class for manipulating an environment variable,
* and restoring it to its original state at the end of the current scope.
*/
class ScopedEnvVar {
public:
explicit ScopedEnvVar(folly::StringPiece name);
~ScopedEnvVar();
/**
* Unset the environment variable.
*/
void unset();
/**
* Set the environment variable
*/
void set(const char* value);
void set(const std::string& value);
void set(folly::StringPiece value);
private:
/**
* The environment variable name.
*/
std::optional<std::string> name_;
/**
* The original value of this environment variable that we should restore it
* to on destruction of this ScopedEnvVar. This will be std::nullopt if the
* environment variable was originally unset, and should be unset on
* destruction.
*/
std::optional<std::string> origValue_;
};
} // namespace eden
} // namespace facebook