Migrate env_stack_t::get_or_null to environment_t

Allows it to be used when we only have an environment_t.
This commit is contained in:
ridiculousfish 2023-03-18 19:45:00 -07:00
parent 3fab931e86
commit 30feef6a72
2 changed files with 19 additions and 10 deletions

View File

@ -189,6 +189,15 @@ wcstring environment_t::get_pwd_slash() const {
return pwd; return pwd;
} }
std::unique_ptr<env_var_t> environment_t::get_or_null(wcstring const &key,
env_mode_flags_t mode) const {
auto variable = this->get(key, mode);
if (!variable.has_value()) {
return nullptr;
}
return make_unique<env_var_t>(variable.acquire());
}
null_environment_t::~null_environment_t() = default; null_environment_t::~null_environment_t() = default;
maybe_t<env_var_t> null_environment_t::get(const wcstring &key, env_mode_flags_t mode) const { maybe_t<env_var_t> null_environment_t::get(const wcstring &key, env_mode_flags_t mode) const {
UNUSED(key); UNUSED(key);
@ -1476,13 +1485,6 @@ const std::shared_ptr<env_stack_t> &env_stack_t::principal_ref() {
new env_stack_t(env_stack_impl_t::create())}; new env_stack_t(env_stack_impl_t::create())};
return s_principal; return s_principal;
} }
__attribute__((unused)) std::unique_ptr<env_var_t> env_stack_t::get_or_null(
wcstring const &key, env_mode_flags_t mode) const {
auto variable = get(key, mode);
return variable.missing_or_empty()
? std::unique_ptr<env_var_t>()
: std::unique_ptr<env_var_t>(new env_var_t(variable.value()));
}
env_stack_t::~env_stack_t() = default; env_stack_t::~env_stack_t() = default;

View File

@ -194,6 +194,10 @@ class environment_t {
virtual wcstring_list_t get_names(env_mode_flags_t flags) const = 0; virtual wcstring_list_t get_names(env_mode_flags_t flags) const = 0;
virtual ~environment_t(); virtual ~environment_t();
/// \return a environment variable as a unique pointer, or nullptr if none.
std::unique_ptr<env_var_t> get_or_null(const wcstring &key,
env_mode_flags_t mode = ENV_DEFAULT) const;
/// Returns the PWD with a terminating slash. /// Returns the PWD with a terminating slash.
virtual wcstring get_pwd_slash() const; virtual wcstring get_pwd_slash() const;
}; };
@ -284,15 +288,18 @@ class env_stack_t final : public environment_t {
/// Slightly optimized implementation. /// Slightly optimized implementation.
wcstring get_pwd_slash() const override; wcstring get_pwd_slash() const override;
/// "Override" of get_or_null, as autocxx doesn't understand inheritance.
std::unique_ptr<env_var_t> get_or_null(const wcstring &key,
env_mode_flags_t mode = ENV_DEFAULT) const {
return environment_t::get_or_null(key, mode);
}
/// Synchronizes universal variable changes. /// Synchronizes universal variable changes.
/// If \p always is set, perform synchronization even if there's no pending changes from this /// If \p always is set, perform synchronization even if there's no pending changes from this
/// instance (that is, look for changes from other fish instances). /// instance (that is, look for changes from other fish instances).
/// \return a list of events for changed variables. /// \return a list of events for changed variables.
std::vector<rust::Box<Event>> universal_sync(bool always); std::vector<rust::Box<Event>> universal_sync(bool always);
__attribute__((unused)) std::unique_ptr<env_var_t> get_or_null(
const wcstring &key, env_mode_flags_t mode = ENV_DEFAULT) const;
// Compatibility hack; access the "environment stack" from back when there was just one. // Compatibility hack; access the "environment stack" from back when there was just one.
static const std::shared_ptr<env_stack_t> &principal_ref(); static const std::shared_ptr<env_stack_t> &principal_ref();
static env_stack_t &principal() { return *principal_ref(); } static env_stack_t &principal() { return *principal_ref(); }