2#ifndef INKSCAPE_UTIL_VALUE_UTILS_H
3#define INKSCAPE_UTIL_VALUE_UTILS_H
21#include <glibmm/utility.h>
22#include <glibmm/value.h>
23#include <gdkmm/contentprovider.h>
31 static auto const type = [] {
32 auto name = std::string{
"inkscape_glibvalue_"};
33 Glib::append_canonical_typename(
name,
typeid(T).
name());
35 return g_boxed_type_register_static(
37 +[] (
void *p) ->
void * {
38 return new (std::nothrow) T{*static_cast<T *>(p)};
41 delete static_cast<T *
>(p);
52 return G_VALUE_HOLDS(value, type<T>());
57bool holds(Glib::ValueBase
const &value)
59 return holds<T>(value.gobj());
66 if (holds<T>(value)) {
67 return static_cast<T *
>(g_value_get_boxed(value));
74T *
get(Glib::ValueBase &value)
76 return get<T>(value.gobj());
81T
const *
get(GValue
const *value)
83 if (holds<T>(value)) {
84 return static_cast<T
const *
>(g_value_get_boxed(value));
91T
const *
get(Glib::ValueBase
const &value)
93 return get<T>(value.gobj());
101Glib::ValueBase
own(std::unique_ptr<T> t)
104 Glib::ValueBase value;
105 value.init(type<T>());
106 g_value_take_boxed(value.gobj(), t.release());
111template <
typename T,
typename... Args>
114 return own(std::make_unique<T>(std::forward<Args>(args)...));
120 return std::exchange(*value.gobj(), GValue(G_VALUE_INIT));
127 Glib::ValueBase value;
128 value.init(type<T>());
130 if (!gdk_content_provider_get_value(
const_cast<Gdk::ContentProvider &
>(content_provider).gobj(), value.gobj(),
nullptr)) {
133 auto const t =
static_cast<T *
>(g_value_get_boxed(value.gobj()));
134 *value.gobj() = G_VALUE_INIT;
135 return std::unique_ptr<T>(t);
GType type()
Returns the type used for storing an object of type T inside a value.
std::unique_ptr< T > from_content_provider(Gdk::ContentProvider const &content_provider)
Attempt to get a value of type T from a content provider, returning it on success,...
bool holds(GValue const *value)
Tests whether a value contains an object of type T.
Glib::ValueBase own(std::unique_ptr< T > t)
Return a value containing and taking ownership of the given T instance.
GValue release(Glib::ValueBase &&value)
Release the value from its owning wrapper, leaving the original in an empty state.
T * get(GValue *value)
Returns a borrowed pointer to the T held by a value if it holds one, else nullptr.