Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
value-utils.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2#ifndef INKSCAPE_UTIL_VALUE_UTILS_H
3#define INKSCAPE_UTIL_VALUE_UTILS_H
4
21#include <glibmm/utility.h>
22#include <glibmm/value.h>
23#include <gdkmm/contentprovider.h>
24
26
28template <class T>
29GType type()
30{
31 static auto const type = [] {
32 auto name = std::string{"inkscape_glibvalue_"};
33 Glib::append_canonical_typename(name, typeid(T).name());
34
35 return g_boxed_type_register_static(
36 name.c_str(),
37 +[] (void *p) -> void * {
38 return new (std::nothrow) T{*static_cast<T *>(p)};
39 },
40 +[] (void *p) {
41 delete static_cast<T *>(p);
42 }
43 );
44 }();
45 return type;
46}
47
49template <typename T>
50bool holds(GValue const *value)
51{
52 return G_VALUE_HOLDS(value, type<T>());
53}
54
56template <typename T>
57bool holds(Glib::ValueBase const &value)
58{
59 return holds<T>(value.gobj());
60}
61
63template <typename T>
64T *get(GValue *value)
65{
66 if (holds<T>(value)) {
67 return static_cast<T *>(g_value_get_boxed(value));
68 }
69 return nullptr;
70}
71
73template <typename T>
74T *get(Glib::ValueBase &value)
75{
76 return get<T>(value.gobj());
77}
78
80template <typename T>
81T const *get(GValue const *value)
82{
83 if (holds<T>(value)) {
84 return static_cast<T const *>(g_value_get_boxed(value));
85 }
86 return nullptr;
87}
88
90template <typename T>
91T const *get(Glib::ValueBase const &value)
92{
93 return get<T>(value.gobj());
94}
95
100template <typename T>
101Glib::ValueBase own(std::unique_ptr<T> t)
102{
103 assert(t);
104 Glib::ValueBase value;
105 value.init(type<T>());
106 g_value_take_boxed(value.gobj(), t.release());
107 return value;
108}
109
111template <typename T, typename... Args>
112Glib::ValueBase create(Args&&... args)
113{
114 return own(std::make_unique<T>(std::forward<Args>(args)...));
115}
116
118inline GValue release(Glib::ValueBase &&value)
119{
120 return std::exchange(*value.gobj(), GValue(G_VALUE_INIT));
121}
122
124template <typename T>
125std::unique_ptr<T> from_content_provider(Gdk::ContentProvider const &content_provider)
126{
127 Glib::ValueBase value;
128 value.init(type<T>());
129 // Fixme: Value inside content provider is copied rather than returned directly.
130 if (!gdk_content_provider_get_value(const_cast<Gdk::ContentProvider &>(content_provider).gobj(), value.gobj(), nullptr)) {
131 return {};
132 }
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);
136}
137
138} // namespace Inkscape::Util::GlibValue
139
140#endif // INKSCAPE_UTIL_VALUE_UTILS_H
GType type()
Returns the type used for storing an object of type T inside a value.
Definition value-utils.h:29
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.
Definition value-utils.h:50
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.
Definition value-utils.h:64
std::unique_ptr< Toolbar >(* create)()
Definition toolbars.cpp:56
Glib::ustring name
Definition toolbars.cpp:55