Migrate get() to get_implem().

This commit is contained in:
King_DuckZ 2020-04-13 19:37:13 +02:00
parent 19d3f329d3
commit f696e1e3a0
6 changed files with 29 additions and 18 deletions

View file

@ -87,6 +87,16 @@ detail::EnvVarProxy<std::string> EnvBase::operator[] (const std::string& name) n
return {to_string(name), this}; return {to_string(name), this};
} }
std::optional<std::string_view> 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) { void EnvBase::set (std::string_view name, std::string_view value) {
StdMutex m(this->mutex()); StdMutex m(this->mutex());
this->set_implem(name, value, m); this->set_implem(name, value, m);

View file

@ -65,8 +65,8 @@ namespace duck {
detail::EnvVarProxy<std::string> operator[] (std::string&& name) noexcept; detail::EnvVarProxy<std::string> operator[] (std::string&& name) noexcept;
detail::EnvVarProxy<std::string> operator[] (const std::string& name) noexcept; detail::EnvVarProxy<std::string> operator[] (const std::string& name) noexcept;
virtual std::optional<std::string_view> get (std::string_view name) const noexcept = 0; std::optional<std::string_view> get (std::string_view name) const noexcept;
virtual std::string_view get (std::string_view name, std::string_view def) const noexcept = 0; 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 set (std::string_view name, std::string_view value);
void unset (std::string_view name) noexcept; void unset (std::string_view name) noexcept;
bool is_set (std::string_view name) const noexcept; bool is_set (std::string_view name) const noexcept;
@ -82,6 +82,8 @@ namespace duck {
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 std::optional<std::string_view> 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; 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);

View file

@ -74,12 +74,14 @@ EnvFake::EnvFake (bool starts_empty) :
{ {
} }
std::optional<std::string_view> EnvFake::get (std::string_view name) const noexcept { std::optional<std::string_view> EnvFake::get_implem (std::string_view name, Mutex& m) const {
std::lock_guard<Mutex> lock(m);
return run_if_found(m_map, name, [](auto& it)noexcept{return std::make_optional(it->second);}); 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}; bool ret_def{true};
std::lock_guard<Mutex> lock(m);
std::string_view maybe_ret(run_if_found(m_map, name, [&ret_def](auto& it)noexcept{ret_def=false; return it->second;})); 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) if (ret_def)
return def; return def;

View file

@ -33,9 +33,6 @@ namespace duck {
EnvFake& operator= (const EnvBase& other); EnvFake& operator= (const EnvBase& other);
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;
const_iterator begin() const; const_iterator begin() const;
const_iterator cbegin() const; const_iterator cbegin() const;
const_iterator end() const; const_iterator end() const;
@ -46,6 +43,8 @@ namespace duck {
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 std::optional<std::string_view> 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 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 void unset_implem (std::string_view name, Mutex& m) override;
virtual bool is_set_implem (std::string_view name, Mutex& m) const override; virtual bool is_set_implem (std::string_view name, Mutex& m) const override;

View file

@ -209,16 +209,16 @@ EnvReal& EnvReal::operator= (const EnvBase& other) {
return *this; return *this;
} }
std::optional<std::string_view> EnvReal::get (std::string_view name) const noexcept { std::optional<std::string_view> EnvReal::get_implem (std::string_view name, Mutex& m) const {
const char* const ret = this->raw_fetch_env(name); const char* const ret = this->raw_fetch_env(name, m);
if (ret) if (ret)
return to_string_view(ret); return to_string_view(ret);
else else
return {}; return {};
} }
std::string_view EnvReal::get (std::string_view name, std::string_view def) const noexcept { 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); const char* const ret = this->raw_fetch_env(name, m);
if (ret) if (ret)
return to_string_view(ret); return to_string_view(ret);
else else
@ -298,9 +298,8 @@ void EnvReal::clear_implem(Mutex& m) {
} }
} }
const char* EnvReal::raw_fetch_env (std::string_view name) const noexcept { const char* EnvReal::raw_fetch_env (std::string_view name, Mutex& m) const noexcept {
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,

View file

@ -54,9 +54,6 @@ namespace duck {
EnvReal& operator= (const EnvBase& other); EnvReal& operator= (const EnvBase& other);
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;
const_iterator begin() const; const_iterator begin() const;
const_iterator cbegin() const; const_iterator cbegin() const;
const_iterator end() const; const_iterator end() const;
@ -67,13 +64,15 @@ namespace duck {
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 std::optional<std::string_view> 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 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 void unset_implem (std::string_view name, Mutex& m) override;
virtual bool is_set_implem (std::string_view name, Mutex& m) const override; virtual bool is_set_implem (std::string_view name, Mutex& m) const override;
virtual size_type size_implem(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, Mutex& m) const noexcept;
std::unique_ptr<char[]> m_name_buff; std::unique_ptr<char[]> m_name_buff;
std::size_t m_name_buff_len; std::size_t m_name_buff_len;