Migrate more methods to the _implem convention.

This commit is contained in:
King_DuckZ 2020-04-13 19:27:30 +02:00
parent a2b31dfd28
commit 19d3f329d3
6 changed files with 47 additions and 26 deletions

View file

@ -92,6 +92,21 @@ void EnvBase::set (std::string_view name, std::string_view value) {
this->set_implem(name, value, m); 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) { void EnvBase::set_nolock (EnvBase& obj, std::string_view name, std::string_view value) {
EmptyMutex m; EmptyMutex m;
obj.set_implem(name, value, m); obj.set_implem(name, value, m);

View file

@ -68,21 +68,26 @@ namespace duck {
virtual std::optional<std::string_view> get (std::string_view name) const noexcept = 0; virtual std::optional<std::string_view> get (std::string_view name) const noexcept = 0;
virtual std::string_view get (std::string_view name, std::string_view def) 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); void set (std::string_view name, std::string_view value);
virtual void unset (std::string_view name) noexcept = 0; void unset (std::string_view name) noexcept;
virtual bool is_set (std::string_view name) const noexcept = 0; bool is_set (std::string_view name) const noexcept;
virtual size_type size() const noexcept = 0; size_type size() const noexcept;
virtual bool empty() const noexcept; bool empty() const noexcept;
void clear() noexcept; void clear() noexcept;
virtual std::mutex& mutex() noexcept = 0; virtual std::mutex& mutex() const noexcept = 0;
protected: protected:
virtual void set_all_into (EnvBase& other) const = 0; virtual void set_all_into (EnvBase& other) const = 0;
static void set_all_into (const EnvBase& obj, EnvBase& other) { obj.set_all_into(other); } static void set_all_into (const EnvBase& obj, EnvBase& other) { obj.set_all_into(other); }
virtual void clear_implem(Mutex& m) = 0; virtual void clear_implem(Mutex& m) = 0;
static void clear_nolock (EnvBase& obj) noexcept; static void clear_nolock (EnvBase& obj) noexcept;
virtual void set_implem (std::string_view name, std::string_view value, Mutex& m) = 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); 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 { namespace detail {

View file

@ -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<Mutex> lock(m);
run_if_found(m_map, name, [&](auto& it)noexcept{m_map.erase(it);}); 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);}); 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<Mutex> lock(m);
return run_if_found(m_map, name, [](auto&)noexcept{return true;}); 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<Mutex> lock(m);
return m_map.size(); return m_map.size();
} }
@ -146,7 +149,7 @@ auto EnvFake::cend() const -> const_iterator {
return m_map.cend(); return m_map.cend();
} }
std::mutex& EnvFake::mutex() noexcept { std::mutex& EnvFake::mutex() const noexcept {
return m_mutex; return m_mutex;
} }

View file

@ -35,21 +35,21 @@ namespace duck {
virtual std::optional<std::string_view> get (std::string_view name) const noexcept override; virtual std::optional<std::string_view> 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 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 begin() const;
const_iterator cbegin() const; const_iterator cbegin() const;
const_iterator end() const; const_iterator end() const;
const_iterator cend() const; const_iterator cend() const;
virtual std::mutex& mutex() noexcept override; virtual std::mutex& mutex() const noexcept override;
protected: protected:
virtual void set_all_into (EnvBase& other) const override; virtual void set_all_into (EnvBase& other) const override;
virtual void clear_implem(Mutex& m) 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 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: private:
std::unordered_map<std::string_view, std::unique_ptr<char[]>> m_map_strong_cpy; std::unordered_map<std::string_view, std::unique_ptr<char[]>> m_map_strong_cpy;

View file

@ -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(); auto& pm = pointer_map();
std::lock_guard<std::mutex> lock(pm.env_mutex()); std::lock_guard<Mutex> lock(m);
duck::invoke_with_zstr( duck::invoke_with_zstr(
name, name,
@ -262,9 +262,8 @@ void EnvReal::unset (std::string_view name) noexcept {
); );
} }
bool EnvReal::is_set (std::string_view name) const noexcept { bool EnvReal::is_set_implem (std::string_view name, Mutex& m) const {
auto& pm = pointer_map(); std::lock_guard<Mutex> lock(m);
std::lock_guard<std::mutex> lock(pm.env_mutex());
return duck::invoke_with_zstr( return duck::invoke_with_zstr(
name, name,
@ -276,9 +275,8 @@ bool EnvReal::is_set (std::string_view name) const noexcept {
); );
} }
auto EnvReal::size() const noexcept -> size_type { auto EnvReal::size_implem (Mutex& m) const -> size_type {
auto& pm = pointer_map(); std::lock_guard<Mutex> lock(m);
std::lock_guard<std::mutex> lock(pm.env_mutex());
assert(environ); assert(environ);
size_type z = 0; size_type z = 0;
@ -332,7 +330,7 @@ auto EnvReal::cend() const -> const_iterator {
return {::environ + this->size()}; return {::environ + this->size()};
} }
std::mutex& EnvReal::mutex() noexcept { std::mutex& EnvReal::mutex() const noexcept {
return pointer_map().env_mutex(); return pointer_map().env_mutex();
} }

View file

@ -56,21 +56,21 @@ namespace duck {
virtual std::optional<std::string_view> get (std::string_view name) const noexcept override; virtual std::optional<std::string_view> 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 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 begin() const;
const_iterator cbegin() const; const_iterator cbegin() const;
const_iterator end() const; const_iterator end() const;
const_iterator cend() const; const_iterator cend() const;
virtual std::mutex& mutex() noexcept override; virtual std::mutex& mutex() const noexcept override;
protected: protected:
virtual void set_all_into (EnvBase& other) const override; virtual void set_all_into (EnvBase& other) const override;
virtual void clear_implem(Mutex& m) 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 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: private:
const char* raw_fetch_env (std::string_view name) const noexcept; const char* raw_fetch_env (std::string_view name) const noexcept;