From 30feef6a7240d2d1394aa7b0eaaf0cbff0be8ebd Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sat, 18 Mar 2023 19:45:00 -0700 Subject: [PATCH] Migrate env_stack_t::get_or_null to environment_t Allows it to be used when we only have an environment_t. --- src/env.cpp | 16 +++++++++------- src/env.h | 13 ++++++++++--- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/env.cpp b/src/env.cpp index 5eef968cf..6d05e653c 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -189,6 +189,15 @@ wcstring environment_t::get_pwd_slash() const { return pwd; } +std::unique_ptr 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(variable.acquire()); +} + null_environment_t::~null_environment_t() = default; maybe_t null_environment_t::get(const wcstring &key, env_mode_flags_t mode) const { UNUSED(key); @@ -1476,13 +1485,6 @@ const std::shared_ptr &env_stack_t::principal_ref() { new env_stack_t(env_stack_impl_t::create())}; return s_principal; } -__attribute__((unused)) std::unique_ptr 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() - : std::unique_ptr(new env_var_t(variable.value())); -} env_stack_t::~env_stack_t() = default; diff --git a/src/env.h b/src/env.h index 3f596804c..b3901a4e4 100644 --- a/src/env.h +++ b/src/env.h @@ -194,6 +194,10 @@ class environment_t { virtual wcstring_list_t get_names(env_mode_flags_t flags) const = 0; virtual ~environment_t(); + /// \return a environment variable as a unique pointer, or nullptr if none. + std::unique_ptr get_or_null(const wcstring &key, + env_mode_flags_t mode = ENV_DEFAULT) const; + /// Returns the PWD with a terminating slash. virtual wcstring get_pwd_slash() const; }; @@ -284,15 +288,18 @@ class env_stack_t final : public environment_t { /// Slightly optimized implementation. wcstring get_pwd_slash() const override; + /// "Override" of get_or_null, as autocxx doesn't understand inheritance. + std::unique_ptr 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. /// 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). /// \return a list of events for changed variables. std::vector> universal_sync(bool always); - __attribute__((unused)) std::unique_ptr 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. static const std::shared_ptr &principal_ref(); static env_stack_t &principal() { return *principal_ref(); }