From e70f0b4de81d24d22a29a2af03c669368fce6af2 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Sat, 24 Nov 2018 20:38:48 +0100 Subject: wanda: initial commit --- src/variant.hpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/variant.hpp (limited to 'src/variant.hpp') diff --git a/src/variant.hpp b/src/variant.hpp new file mode 100644 index 0000000..8a5fb03 --- /dev/null +++ b/src/variant.hpp @@ -0,0 +1,47 @@ +#ifndef WANDA_VARIANT_HPP +#define WANDA_VARIANT_HPP + +#include + +#include +#include +#include + +namespace wanda { + +struct variant { + + explicit variant(GVariant * variant); + + ~variant(); + + template std::optional get() const { + if(!m_value) { + return std::nullopt; + } + + if constexpr(std::is_same_v) { + if(is_of_type(G_VARIANT_TYPE_BOOLEAN)) { + return static_cast(g_variant_get_boolean(m_value)); + } + } else if constexpr(std::is_same_v) { + if(is_of_type(G_VARIANT_TYPE_STRING)) { + auto length = gsize{}; + auto value = g_variant_get_string(m_value, &length); + return std::string(value, static_cast(length)); + } + } + + return std::nullopt; + } + + +private: + bool is_of_type(GVariantType const * const type) const; + + GVariant * const m_value; +}; + +} + +#endif \ No newline at end of file -- cgit v1.2.3