diff --git a/src/env_base.cpp b/src/env_base.cpp index 33a4cac..c4d33fa 100644 --- a/src/env_base.cpp +++ b/src/env_base.cpp @@ -87,6 +87,16 @@ detail::EnvVarProxy EnvBase::operator[] (const std::string& name) n return {to_string(name), this}; } +std::optional EnvBase::get (std::string_view name) const noexcept { + StdMutex m(this->mutex()); + return call_noexcept([&,this](){return this->get_implem(name, m);}, to_string_view(__func__)); +} + +std::string_view EnvBase::get (std::string_view name, std::string_view def) const noexcept { + StdMutex m(this->mutex()); + return call_noexcept([&,this](){return this->get_implem(name, def, m);}, to_string_view(__func__)); +} + void EnvBase::set (std::string_view name, std::string_view value) { StdMutex m(this->mutex()); this->set_implem(name, value, m); diff --git a/src/env_base.hpp b/src/env_base.hpp index 2567597..9b96ac4 100644 --- a/src/env_base.hpp +++ b/src/env_base.hpp @@ -65,8 +65,8 @@ namespace duck { detail::EnvVarProxy operator[] (std::string&& name) noexcept; detail::EnvVarProxy operator[] (const std::string& name) noexcept; - virtual std::optional get (std::string_view name) const noexcept = 0; - virtual std::string_view get (std::string_view name, std::string_view def) const noexcept = 0; + std::optional get (std::string_view name) const noexcept; + std::string_view get (std::string_view name, std::string_view def) const noexcept; void set (std::string_view name, std::string_view value); void unset (std::string_view name) noexcept; bool is_set (std::string_view name) const noexcept; @@ -82,6 +82,8 @@ namespace duck { virtual void clear_implem(Mutex& m) = 0; static void clear_nolock (EnvBase& obj) noexcept; + virtual std::optional get_implem (std::string_view name, Mutex& m) const = 0; + virtual std::string_view get_implem (std::string_view name, std::string_view def, Mutex& m) const = 0; virtual void set_implem (std::string_view name, std::string_view value, Mutex& m) = 0; static void set_nolock (EnvBase& obj, std::string_view name, std::string_view value); diff --git a/src/env_fake.cpp b/src/env_fake.cpp index 169d3fa..844d366 100644 --- a/src/env_fake.cpp +++ b/src/env_fake.cpp @@ -74,12 +74,14 @@ EnvFake::EnvFake (bool starts_empty) : { } -std::optional EnvFake::get (std::string_view name) const noexcept { +std::optional EnvFake::get_implem (std::string_view name, Mutex& m) const { + std::lock_guard lock(m); return run_if_found(m_map, name, [](auto& it)noexcept{return std::make_optional(it->second);}); } -std::string_view EnvFake::get (std::string_view name, std::string_view def) const noexcept { +std::string_view EnvFake::get_implem (std::string_view name, std::string_view def, Mutex& m) const { bool ret_def{true}; + std::lock_guard lock(m); std::string_view maybe_ret(run_if_found(m_map, name, [&ret_def](auto& it)noexcept{ret_def=false; return it->second;})); if (ret_def) return def; diff --git a/src/env_fake.hpp b/src/env_fake.hpp index 5de66e9..ac33e7d 100644 --- a/src/env_fake.hpp +++ b/src/env_fake.hpp @@ -33,9 +33,6 @@ namespace duck { EnvFake& operator= (const EnvBase& other); - virtual std::optional get (std::string_view name) const noexcept override; - virtual std::string_view get (std::string_view name, std::string_view def) const noexcept override; - const_iterator begin() const; const_iterator cbegin() const; const_iterator end() const; @@ -46,6 +43,8 @@ namespace duck { protected: virtual void set_all_into (EnvBase& other) const override; virtual void clear_implem(Mutex& m) override; + virtual std::optional get_implem (std::string_view name, Mutex& m) const override; + virtual std::string_view get_implem (std::string_view name, std::string_view def, Mutex& m) const override; virtual void set_implem (std::string_view name, std::string_view value, Mutex& m) override; virtual void unset_implem (std::string_view name, Mutex& m) override; virtual bool is_set_implem (std::string_view name, Mutex& m) const override; diff --git a/src/env_real.cpp b/src/env_real.cpp index 9d6ce4e..d3ce6d3 100644 --- a/src/env_real.cpp +++ b/src/env_real.cpp @@ -209,16 +209,16 @@ EnvReal& EnvReal::operator= (const EnvBase& other) { return *this; } -std::optional EnvReal::get (std::string_view name) const noexcept { - const char* const ret = this->raw_fetch_env(name); +std::optional EnvReal::get_implem (std::string_view name, Mutex& m) const { + const char* const ret = this->raw_fetch_env(name, m); if (ret) return to_string_view(ret); else return {}; } -std::string_view EnvReal::get (std::string_view name, std::string_view def) const noexcept { - const char* const ret = this->raw_fetch_env(name); +std::string_view EnvReal::get_implem (std::string_view name, std::string_view def, Mutex& m) const { + const char* const ret = this->raw_fetch_env(name, m); if (ret) return to_string_view(ret); else @@ -298,9 +298,8 @@ void EnvReal::clear_implem(Mutex& m) { } } -const char* EnvReal::raw_fetch_env (std::string_view name) const noexcept { - auto& pm = pointer_map(); - std::lock_guard lock(pm.env_mutex()); +const char* EnvReal::raw_fetch_env (std::string_view name, Mutex& m) const noexcept { + std::lock_guard lock(m); return duck::invoke_with_zstr( name, diff --git a/src/env_real.hpp b/src/env_real.hpp index 9e61022..f61e596 100644 --- a/src/env_real.hpp +++ b/src/env_real.hpp @@ -54,9 +54,6 @@ namespace duck { EnvReal& operator= (const EnvBase& other); - virtual std::optional get (std::string_view name) const noexcept override; - virtual std::string_view get (std::string_view name, std::string_view def) const noexcept override; - const_iterator begin() const; const_iterator cbegin() const; const_iterator end() const; @@ -67,13 +64,15 @@ namespace duck { protected: virtual void set_all_into (EnvBase& other) const override; virtual void clear_implem(Mutex& m) override; + virtual std::optional get_implem (std::string_view name, Mutex& m) const override; + virtual std::string_view get_implem (std::string_view name, std::string_view def, Mutex& m) const override; virtual void set_implem (std::string_view name, std::string_view value, Mutex& m) override; virtual void unset_implem (std::string_view name, Mutex& m) override; virtual bool is_set_implem (std::string_view name, Mutex& m) const override; virtual size_type size_implem(Mutex& m) const override; private: - const char* raw_fetch_env (std::string_view name) const noexcept; + const char* raw_fetch_env (std::string_view name, Mutex& m) const noexcept; std::unique_ptr m_name_buff; std::size_t m_name_buff_len;