diff --git a/src/env_base.cpp b/src/env_base.cpp index 453904f..33a4cac 100644 --- a/src/env_base.cpp +++ b/src/env_base.cpp @@ -92,6 +92,21 @@ void EnvBase::set (std::string_view name, std::string_view value) { this->set_implem(name, value, m); } +void EnvBase::unset (std::string_view name) noexcept { + StdMutex m(this->mutex()); + call_noexcept([&,this](){this->unset_implem(name, m);}, to_string_view(__func__)); +} + +bool EnvBase::is_set (std::string_view name) const noexcept { + StdMutex m(this->mutex()); + return call_noexcept([&,this](){return this->is_set_implem(name, m);}, to_string_view(__func__)); +} + +auto EnvBase::size() const noexcept -> size_type { + StdMutex m(this->mutex()); + return call_noexcept([this,&m](){return this->size_implem(m);}, to_string_view(__func__)); +} + void EnvBase::set_nolock (EnvBase& obj, std::string_view name, std::string_view value) { EmptyMutex m; obj.set_implem(name, value, m); diff --git a/src/env_base.hpp b/src/env_base.hpp index eeeeba0..2567597 100644 --- a/src/env_base.hpp +++ b/src/env_base.hpp @@ -68,21 +68,26 @@ namespace duck { 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; void set (std::string_view name, std::string_view value); - virtual void unset (std::string_view name) noexcept = 0; - virtual bool is_set (std::string_view name) const noexcept = 0; - virtual size_type size() const noexcept = 0; - virtual bool empty() const noexcept; + void unset (std::string_view name) noexcept; + bool is_set (std::string_view name) const noexcept; + size_type size() const noexcept; + bool empty() const noexcept; void clear() noexcept; - virtual std::mutex& mutex() noexcept = 0; + virtual std::mutex& mutex() const noexcept = 0; protected: virtual void set_all_into (EnvBase& other) const = 0; static void set_all_into (const EnvBase& obj, EnvBase& other) { obj.set_all_into(other); } virtual void clear_implem(Mutex& m) = 0; static void clear_nolock (EnvBase& obj) noexcept; + 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); + + virtual void unset_implem (std::string_view name, Mutex& m) = 0; + virtual bool is_set_implem (std::string_view name, Mutex& m) const = 0; + virtual size_type size_implem(Mutex& m) const = 0; }; namespace detail { diff --git a/src/env_fake.cpp b/src/env_fake.cpp index 05b85e8..169d3fa 100644 --- a/src/env_fake.cpp +++ b/src/env_fake.cpp @@ -111,16 +111,19 @@ void EnvFake::set_implem (std::string_view name, std::string_view value, Mutex& } } -void EnvFake::unset (std::string_view name) noexcept { +void EnvFake::unset_implem (std::string_view name, Mutex& m) { + std::lock_guard lock(m); run_if_found(m_map, name, [&](auto& it)noexcept{m_map.erase(it);}); run_if_found(m_map_strong_cpy, name, [&](auto& it)noexcept{m_map_strong_cpy.erase(it);}); } -bool EnvFake::is_set (std::string_view name) const noexcept { +bool EnvFake::is_set_implem (std::string_view name, Mutex& m) const { + std::lock_guard lock(m); return run_if_found(m_map, name, [](auto&)noexcept{return true;}); } -auto EnvFake::size() const noexcept -> size_type { +auto EnvFake::size_implem (Mutex& m) const -> size_type { + std::lock_guard lock(m); return m_map.size(); } @@ -146,7 +149,7 @@ auto EnvFake::cend() const -> const_iterator { return m_map.cend(); } -std::mutex& EnvFake::mutex() noexcept { +std::mutex& EnvFake::mutex() const noexcept { return m_mutex; } diff --git a/src/env_fake.hpp b/src/env_fake.hpp index 236004d..5de66e9 100644 --- a/src/env_fake.hpp +++ b/src/env_fake.hpp @@ -35,21 +35,21 @@ namespace duck { 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; - virtual void unset (std::string_view name) noexcept override; - virtual bool is_set (std::string_view name) const noexcept override; - virtual size_type size() const noexcept override; const_iterator begin() const; const_iterator cbegin() const; const_iterator end() const; const_iterator cend() const; - virtual std::mutex& mutex() noexcept override; + virtual std::mutex& mutex() const noexcept override; protected: virtual void set_all_into (EnvBase& other) const override; virtual void clear_implem(Mutex& m) 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: std::unordered_map> m_map_strong_cpy; diff --git a/src/env_real.cpp b/src/env_real.cpp index 63d7933..9d6ce4e 100644 --- a/src/env_real.cpp +++ b/src/env_real.cpp @@ -246,9 +246,9 @@ void EnvReal::set_implem (std::string_view name, std::string_view value, Mutex& } } -void EnvReal::unset (std::string_view name) noexcept { +void EnvReal::unset_implem (std::string_view name, Mutex& m) { auto& pm = pointer_map(); - std::lock_guard lock(pm.env_mutex()); + std::lock_guard lock(m); duck::invoke_with_zstr( name, @@ -262,9 +262,8 @@ void EnvReal::unset (std::string_view name) noexcept { ); } -bool EnvReal::is_set (std::string_view name) const noexcept { - auto& pm = pointer_map(); - std::lock_guard lock(pm.env_mutex()); +bool EnvReal::is_set_implem (std::string_view name, Mutex& m) const { + std::lock_guard lock(m); return duck::invoke_with_zstr( name, @@ -276,9 +275,8 @@ bool EnvReal::is_set (std::string_view name) const noexcept { ); } -auto EnvReal::size() const noexcept -> size_type { - auto& pm = pointer_map(); - std::lock_guard lock(pm.env_mutex()); +auto EnvReal::size_implem (Mutex& m) const -> size_type { + std::lock_guard lock(m); assert(environ); size_type z = 0; @@ -332,7 +330,7 @@ auto EnvReal::cend() const -> const_iterator { return {::environ + this->size()}; } -std::mutex& EnvReal::mutex() noexcept { +std::mutex& EnvReal::mutex() const noexcept { return pointer_map().env_mutex(); } diff --git a/src/env_real.hpp b/src/env_real.hpp index 72ff5d3..9e61022 100644 --- a/src/env_real.hpp +++ b/src/env_real.hpp @@ -56,21 +56,21 @@ namespace duck { 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; - virtual void unset (std::string_view name) noexcept override; - virtual bool is_set (std::string_view name) const noexcept override; - virtual size_type size() const noexcept override; const_iterator begin() const; const_iterator cbegin() const; const_iterator end() const; const_iterator cend() const; - virtual std::mutex& mutex() noexcept override; + virtual std::mutex& mutex() const noexcept override; protected: virtual void set_all_into (EnvBase& other) const override; virtual void clear_implem(Mutex& m) 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;