Debian Codegraph @

gi/exception.hpp

raw symbols SCIP
1 #ifndef GI_EXCEPTION_HPP
2 #define GI_EXCEPTION_HPP
3
4 #include "base.hpp"
5 #include "wrap.hpp"
6
7 GI_MODULE_EXPORT
8 namespace gi
9 {
10 namespace detail
11 {
12 inline std::logic_error
13 transform_error(GType tp, const char *name = nullptr)
14 {
15 auto n = g_type_name(tp);
16 auto msg = std::string("could not transform value to type ") +
17 detail::make_string(n);
18 if (name)
19 msg += std::string(" of property \'") + name + "\'";
20 return std::invalid_argument(msg);
21 }
22
23 inline std::logic_error
24 unknown_property_error(GType tp, const gchar *property)
25 {
26 auto n = g_type_name(tp);
27 auto msg = std::string("object of type ") + detail::make_string(n) +
28 " does not have property \'" + detail::make_string(property) +
29 "\'";
30 return std::invalid_argument(msg);
31 }
32
33 inline std::logic_error
34 unknown_signal_error(GType tp, const std::string &name)
35 {
36 auto n = g_type_name(tp);
37 auto msg = std::string("object of type ") + detail::make_string(n) +
38 " does not have signal \'" + name + "\'";
39 return std::invalid_argument(msg);
40 }
41
42 inline std::logic_error
43 invalid_signal_callback_error(
44 GType tp, const std::string &name, const std::string &_msg)
45 {
46 auto n = g_type_name(tp);
47 auto msg = std::string("invalid callback for signal ") + n + "::" + name +
48 "; " + _msg;
49 return std::invalid_argument(msg);
50 }
51
52 // partially generated GError wrapper
53 class Error : public gi::detail::GBoxedWrapperBase<Error, GError>
54 {
55 typedef gi::detail::GBoxedWrapperBase<Error, GError> super_type;
56
57 public:
58 Error(GError *obj = nullptr) : super_type(obj) {}
59
60 static GType get_type_() G_GNUC_CONST { return g_error_get_type(); }
61
62 // use with care; dangling reference caution applies here
63 gint &code_() { return gobj_()->code; }
64 const gint &code_() const { return gobj_()->code; }
65
66 // use with care; dangling reference caution applies here
67 gi::cstring_v message_() const { return gobj_()->message; }
68
69 // gboolean g_error_matches (const GError* error, GQuark domain, gint code);
70 inline bool matches(GQuark domain, gint code) const
71 {
72 return g_error_matches(gobj_(), domain, code);
73 }
74
75 }; // class
76
77 } // namespace detail
78
79 namespace repository
80 {
81 namespace GLib
82 {
83 class Error_Ref;
84
85 class Error
86 : public std::runtime_error,
87 public detail::GBoxedWrapper<Error, ::GError, detail::Error, Error_Ref>
88 {
89 typedef std::runtime_error super;
90
91 static inline std::string make_message(GError *error)
92 {
93 return error ? detail::make_string(g_quark_to_string(error->domain)) +
94 ": " + detail::make_string(error->message) + "(" +
95 std::to_string(error->code) + ")"
96 : "";
97 }
98
99 public:
100 explicit Error(GError *obj = nullptr) : super(make_message(obj))
101 {
102 data_ = obj;
103 }
104
105 // GError* g_error_new_literal (GQuark domain, gint code, const gchar*
106 // message);
107 static inline Error new_literal(
108 GQuark domain, gint code, const std::string &message)
109 {
110 return Error(g_error_new_literal(
111 domain, code, gi::unwrap(message, gi::transfer_none)));
112 }
113
114 // GError* g_error_copy (const GError* error);
115 inline Error copy() const { return Error(g_error_copy(gobj_())); }
116
117 // override wrap since we are no longer in a simple single-base case
118 template<typename Cpp, typename Enable = typename std::enable_if<
119 std::is_base_of<Error, Cpp>::value>::type>
120 static Cpp wrap(const typename Cpp::BaseObjectType *obj)
121 {
122 static_assert(sizeof(Cpp) == sizeof(Error), "type wrap not supported");
123 Error w(const_cast<GError *>(obj));
124 return std::move(*static_cast<Cpp *>(&w));
125 }
126 };
127
128 class Error_Ref
129 : public gi::detail::GBoxedRefWrapper<GLib::Error, ::GError, detail::Error>
130 {
131 typedef gi::detail::GBoxedRefWrapper<GLib::Error, ::GError, detail::Error>
132 super_type;
133 using super_type::super_type;
134
135 // GError* g_error_copy (const GError* error);
136 inline Error copy() const { return Error(g_error_copy(gobj_())); }
137 };
138
139 } // namespace GLib
140
141 template<>
142 struct declare_cpptype_of<GError>
143 {
144 typedef GLib::Error type;
145 };
146
147 } // namespace repository
148
149 inline void
150 check_error(GError *error)
151 {
152 if (error)
153 detail::try_throw(repository::GLib::Error(error));
154 }
155
156 namespace detail
157 {
158 inline repository::GLib::Error
159 missing_symbol_error(const std::string &symbol)
160 {
161 ::GQuark domain = g_quark_from_static_string("gi-error-quark");
162 auto error =
163 g_error_new(domain, 0, "could not find symbol %s", symbol.c_str());
164 return repository::GLib::Error(error);
165 }
166 } // namespace detail
167
168 } // namespace gi
169
170 #endif // GI_EXCEPTION_HPP