Add a proxy class to return from operator[]
This commit is contained in:
parent
f462562ab7
commit
ff0737f567
4 changed files with 61 additions and 6 deletions
|
@ -16,9 +16,26 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "env_base.hpp"
|
#include "env_base.hpp"
|
||||||
|
#include "string_view_cat.hpp"
|
||||||
|
|
||||||
namespace duck {
|
namespace duck {
|
||||||
bool EnvBase::empty() const noexcept {
|
bool EnvBase::empty() const noexcept {
|
||||||
return 0 == this->size();
|
return 0 == this->size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string_view EnvBase::operator[] (std::string_view name) const noexcept {
|
||||||
|
return this->get(name, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
detail::EnvVarProxy<std::string_view> EnvBase::operator[] (std::string_view name) noexcept {
|
||||||
|
return {name, this};
|
||||||
|
}
|
||||||
|
|
||||||
|
detail::EnvVarProxy<std::string> EnvBase::operator[] (std::string&& name) noexcept {
|
||||||
|
return {std::move(name), this};
|
||||||
|
}
|
||||||
|
|
||||||
|
detail::EnvVarProxy<std::string> EnvBase::operator[] (const std::string& name) noexcept {
|
||||||
|
return {to_string(name), this};
|
||||||
|
}
|
||||||
} //namespace duck
|
} //namespace duck
|
||||||
|
|
|
@ -22,6 +22,23 @@
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
namespace duck {
|
namespace duck {
|
||||||
|
class EnvBase;
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
template <typename S>
|
||||||
|
class EnvVarProxy {
|
||||||
|
public:
|
||||||
|
template <typename S1>
|
||||||
|
EnvVarProxy (S1&& name, EnvBase* env) noexcept;
|
||||||
|
EnvVarProxy& operator= (std::string_view new_val);
|
||||||
|
operator std::string_view() const noexcept;
|
||||||
|
|
||||||
|
private:
|
||||||
|
S m_name;
|
||||||
|
EnvBase* m_env;
|
||||||
|
};
|
||||||
|
} //namespace detail
|
||||||
|
|
||||||
class EnvBase {
|
class EnvBase {
|
||||||
public:
|
public:
|
||||||
typedef std::string_view mapped_type;
|
typedef std::string_view mapped_type;
|
||||||
|
@ -33,7 +50,11 @@ namespace duck {
|
||||||
EnvBase() = default;
|
EnvBase() = default;
|
||||||
virtual ~EnvBase() noexcept = default;
|
virtual ~EnvBase() noexcept = default;
|
||||||
|
|
||||||
virtual std::string_view operator[] (std::string_view name) const noexcept = 0;
|
std::string_view operator[] (std::string_view name) const noexcept;
|
||||||
|
detail::EnvVarProxy<std::string_view> operator[] (std::string_view name) noexcept;
|
||||||
|
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::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;
|
||||||
virtual void set (std::string_view name, std::string_view value) = 0;
|
virtual void set (std::string_view name, std::string_view value) = 0;
|
||||||
|
@ -43,4 +64,26 @@ namespace duck {
|
||||||
virtual bool empty() const noexcept;
|
virtual bool empty() const noexcept;
|
||||||
virtual void clear() noexcept = 0;
|
virtual void clear() noexcept = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
template <typename S>
|
||||||
|
template <typename S1>
|
||||||
|
inline EnvVarProxy<S>::EnvVarProxy (S1&& name, EnvBase* env) noexcept :
|
||||||
|
m_name(std::forward<S1>(name)),
|
||||||
|
m_env(env)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename S>
|
||||||
|
inline auto EnvVarProxy<S>::operator= (std::string_view new_val) -> EnvVarProxy& {
|
||||||
|
m_env->set(m_name, new_val);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename S>
|
||||||
|
inline EnvVarProxy<S>::operator std::string_view() const noexcept {
|
||||||
|
return (*m_env)[m_name];
|
||||||
|
}
|
||||||
|
} //namespace detail
|
||||||
|
|
||||||
} //namespace duck
|
} //namespace duck
|
||||||
|
|
|
@ -165,10 +165,6 @@ EnvReal::EnvReal() :
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string_view EnvReal::operator[] (std::string_view name) const noexcept {
|
|
||||||
return this->get(name, "");
|
|
||||||
}
|
|
||||||
|
|
||||||
std::optional<std::string_view> EnvReal::get (std::string_view name) const noexcept {
|
std::optional<std::string_view> EnvReal::get (std::string_view name) const noexcept {
|
||||||
const char* const ret = this->raw_fetch_env(name);
|
const char* const ret = this->raw_fetch_env(name);
|
||||||
if (ret)
|
if (ret)
|
||||||
|
|
|
@ -26,7 +26,6 @@ namespace duck {
|
||||||
EnvReal();
|
EnvReal();
|
||||||
virtual ~EnvReal() noexcept = default;
|
virtual ~EnvReal() noexcept = default;
|
||||||
|
|
||||||
virtual std::string_view operator[] (std::string_view name) const noexcept override;
|
|
||||||
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 set (std::string_view name, std::string_view value) override;
|
virtual void set (std::string_view name, std::string_view value) override;
|
||||||
|
|
Loading…
Reference in a new issue