aboutsummaryrefslogtreecommitdiff
path: root/src/variant.hpp
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@gmail.com>2018-11-24 20:38:48 +0100
committerFelix Morgner <felix.morgner@gmail.com>2018-11-24 20:38:48 +0100
commite70f0b4de81d24d22a29a2af03c669368fce6af2 (patch)
treef91dfd02434ce460e6120c7747aa0092cbd2327f /src/variant.hpp
downloadwanda-e70f0b4de81d24d22a29a2af03c669368fce6af2.tar.xz
wanda-e70f0b4de81d24d22a29a2af03c669368fce6af2.zip
wanda: initial commit
Diffstat (limited to 'src/variant.hpp')
-rw-r--r--src/variant.hpp47
1 files changed, 47 insertions, 0 deletions
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 <gio/gio.h>
+
+#include <optional>
+#include <string>
+#include <type_traits>
+
+namespace wanda {
+
+struct variant {
+
+ explicit variant(GVariant * variant);
+
+ ~variant();
+
+ template<typename Type> std::optional<Type> get() const {
+ if(!m_value) {
+ return std::nullopt;
+ }
+
+ if constexpr(std::is_same_v<Type, bool>) {
+ if(is_of_type(G_VARIANT_TYPE_BOOLEAN)) {
+ return static_cast<bool>(g_variant_get_boolean(m_value));
+ }
+ } else if constexpr(std::is_same_v<Type, std::string>) {
+ 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<std::size_t>(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