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};
}
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) {
StdMutex m(this->mutex());
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[] (const std::string& name) noexcept;
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;
std::optional<std::string_view> 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<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;
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);});
}
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<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;}));
if (ret_def)
return def;

View file

@ -33,9 +33,6 @@ namespace duck {
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 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<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 unset_implem (std::string_view name, Mutex& m) 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;
}
std::optional<std::string_view> EnvReal::get (std::string_view name) const noexcept {
const char* const ret = this->raw_fetch_env(name);
std::optional<std::string_view> 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<std::mutex> lock(pm.env_mutex());
const char* EnvReal::raw_fetch_env (std::string_view name, Mutex& m) const noexcept {
std::lock_guard<Mutex> lock(m);
return duck::invoke_with_zstr(
name,

View file

@ -54,9 +54,6 @@ namespace duck {
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 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<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 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<char[]> m_name_buff;
std::size_t m_name_buff_len;