| 1 | #ifndef GI_PARAMSPEC_HPP |
| 2 | #define GI_PARAMSPEC_HPP |
| 3 | |
| 4 | #include "objectbase.hpp" |
| 5 | #include "value.hpp" |
| 6 | |
| 7 | GI_MODULE_EXPORT |
| 8 | namespace gi |
| 9 | { |
| 10 | // slightly nasty; will be generated |
| 11 | namespace repository |
| 12 | { |
| 13 | namespace GObject |
| 14 | { |
| 15 | enum class ParamFlags : std::underlying_type<::GParamFlags>::type; |
| 16 | } |
| 17 | } // namespace repository |
| 18 | |
| 19 | namespace detail |
| 20 | { |
| 21 | struct GParamSpecFuncs |
| 22 | { |
| 23 | static void *ref(void *data) { return g_param_spec_ref((GParamSpec *)data); } |
| 24 | static void *sink(void *data) |
| 25 | { |
| 26 | return g_param_spec_ref_sink((GParamSpec *)data); |
| 27 | } |
| 28 | static void free(void *data) { g_param_spec_unref((GParamSpec *)data); } |
| 29 | static void *float_(void *data) { return data; } |
| 30 | }; |
| 31 | |
| 32 | using ParamFlags = repository::GObject::ParamFlags; |
| 33 | |
| 34 | // helper paramspec type |
| 35 | template<typename T> |
| 36 | struct param_spec_constructor; |
| 37 | |
| 38 | #define GI_DECLARE_PARAM_SPEC(cpptype, suffix) \ |
| 39 | template<> \ |
| 40 | struct param_spec_constructor<cpptype> \ |
| 41 | { \ |
| 42 | typedef std::true_type range_type; \ |
| 43 | static const constexpr decltype(&g_param_spec_##suffix) new_ = \ |
| 44 | g_param_spec_##suffix; \ |
| 45 | }; |
| 46 | |
| 47 | GI_DECLARE_PARAM_SPEC(char, char) |
| 48 | GI_DECLARE_PARAM_SPEC(unsigned char, uchar) |
| 49 | GI_DECLARE_PARAM_SPEC(int, int) |
| 50 | GI_DECLARE_PARAM_SPEC(unsigned int, uint) |
| 51 | GI_DECLARE_PARAM_SPEC(long, long) |
| 52 | GI_DECLARE_PARAM_SPEC(unsigned long, ulong) |
| 53 | GI_DECLARE_PARAM_SPEC(long long, int64) |
| 54 | GI_DECLARE_PARAM_SPEC(unsigned long long, uint64) |
| 55 | GI_DECLARE_PARAM_SPEC(float, float) |
| 56 | GI_DECLARE_PARAM_SPEC(double, double) |
| 57 | |
| 58 | #undef GI_DECLARE_PARAM_SPEC |
| 59 | |
| 60 | // specialize appropriately with static new_ member |
| 61 | template<typename T, typename Enable = void> |
| 62 | struct ParamSpecFactory; |
| 63 | |
| 64 | template<typename T> |
| 65 | struct ParamSpecFactory<T, |
| 66 | typename std::enable_if<param_spec_constructor<T>::range_type::value>::type> |
| 67 | { |
| 68 | static GParamSpec *new_(const gi::cstring_v name, const gi::cstring_v nick, |
| 69 | const gi::cstring_v blurb, T min, T max, T _default, |
| 70 | ParamFlags flags = (ParamFlags)G_PARAM_READWRITE) |
| 71 | { |
| 72 | return param_spec_constructor<T>::new_(name.c_str(), nick.c_str(), |
| 73 | blurb.c_str(), min, max, _default, (GParamFlags)flags); |
| 74 | } |
| 75 | |
| 76 | static GParamSpec *new_(const gi::cstring_v name, const gi::cstring_v nick, |
| 77 | const gi::cstring_v blurb, T min, T max, |
| 78 | ParamFlags flags = (ParamFlags)G_PARAM_READWRITE) |
| 79 | { |
| 80 | return new_(name, nick, blurb, min, max, T{}, flags); |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | template<> |
| 85 | struct ParamSpecFactory<bool> |
| 86 | { |
| 87 | static GParamSpec *new_(const gi::cstring_v name, const gi::cstring_v nick, |
| 88 | const gi::cstring_v blurb, bool _default, |
| 89 | ParamFlags flags = (ParamFlags)G_PARAM_READWRITE) |
| 90 | { |
| 91 | return g_param_spec_boolean(name.c_str(), nick.c_str(), blurb.c_str(), |
| 92 | _default, (GParamFlags)flags); |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | template<> |
| 97 | struct ParamSpecFactory<gpointer> |
| 98 | { |
| 99 | static GParamSpec *new_(const gi::cstring_v name, const gi::cstring_v nick, |
| 100 | const gi::cstring_v blurb, |
| 101 | ParamFlags flags = (ParamFlags)G_PARAM_READWRITE) |
| 102 | { |
| 103 | return g_param_spec_pointer( |
| 104 | name.c_str(), nick.c_str(), blurb.c_str(), (GParamFlags)flags); |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | template<> |
| 109 | struct ParamSpecFactory<std::string> |
| 110 | { |
| 111 | static GParamSpec *new_(const gi::cstring_v name, const gi::cstring_v nick, |
| 112 | const gi::cstring_v blurb, const gi::cstring_v _default, |
| 113 | ParamFlags flags = (ParamFlags)G_PARAM_READWRITE) |
| 114 | { |
| 115 | return g_param_spec_string(name.c_str(), nick.c_str(), blurb.c_str(), |
| 116 | _default.c_str(), (GParamFlags)flags); |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | template<> |
| 121 | struct ParamSpecFactory<gi::cstring> : public ParamSpecFactory<std::string> |
| 122 | {}; |
| 123 | |
| 124 | template<typename T> |
| 125 | struct ParamSpecFactory<T, |
| 126 | typename std::enable_if<traits::is_object<T>::value>::type> |
| 127 | { |
| 128 | static GParamSpec *new_(const gi::cstring_v name, const gi::cstring_v nick, |
| 129 | const gi::cstring_v blurb, |
| 130 | ParamFlags flags = (ParamFlags)G_PARAM_READWRITE) |
| 131 | { |
| 132 | return g_param_spec_object(name.c_str(), nick.c_str(), blurb.c_str(), |
| 133 | traits::gtype<T>::get_type(), (GParamFlags)flags); |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | template<typename T> |
| 138 | struct ParamSpecFactory<T, |
| 139 | typename std::enable_if<traits::is_boxed<T>::value>::type> |
| 140 | { |
| 141 | static GParamSpec *new_(const gi::cstring_v name, const gi::cstring_v nick, |
| 142 | const gi::cstring_v blurb, |
| 143 | ParamFlags flags = (ParamFlags)G_PARAM_READWRITE) |
| 144 | { |
| 145 | return g_param_spec_boxed(name.c_str(), nick.c_str(), blurb.c_str(), |
| 146 | traits::gtype<T>::get_type(), (GParamFlags)flags); |
| 147 | } |
| 148 | }; |
| 149 | |
| 150 | template<typename T> |
| 151 | struct ParamSpecFactory<T, |
| 152 | typename std::enable_if<traits::is_enum_or_bitfield<T>::value>::type> |
| 153 | { |
| 154 | static GParamSpec *new_(const gi::cstring_v name, const gi::cstring_v nick, |
| 155 | const gi::cstring_v blurb, guint _default = 0, |
| 156 | ParamFlags flags = (ParamFlags)G_PARAM_READWRITE) |
| 157 | { |
| 158 | GType t = traits::gtype<T>::get_type(); |
| 159 | // FIXME compile-time determination rather than dynamic ?? |
| 160 | return G_TYPE_IS_FLAGS(t) |
| 161 | ? g_param_spec_flags(name.c_str(), nick.c_str(), blurb.c_str(), |
| 162 | t, _default, (GParamFlags)flags) |
| 163 | : g_param_spec_enum(name.c_str(), nick.c_str(), blurb.c_str(), t, |
| 164 | _default, (GParamFlags)flags); |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | } // namespace detail |
| 169 | |
| 170 | namespace repository |
| 171 | { |
| 172 | // slightly nasty |
| 173 | namespace GObject |
| 174 | { |
| 175 | class ParamSpec; |
| 176 | } |
| 177 | |
| 178 | template<> |
| 179 | struct declare_cpptype_of<GParamSpec> |
| 180 | { |
| 181 | typedef GObject::ParamSpec type; |
| 182 | }; |
| 183 | |
| 184 | namespace GObject |
| 185 | { |
| 186 | class ParamSpec : public detail::WrapperBase<GParamSpec, |
| 187 | detail::GParamSpecFuncs, G_TYPE_PARAM> |
| 188 | { |
| 189 | typedef WrapperBase<GParamSpec, detail::GParamSpecFuncs, G_TYPE_PARAM> super; |
| 190 | |
| 191 | public: |
| 192 | ParamSpec(std::nullptr_t = nullptr) {} |
| 193 | |
| 194 | template<typename T, typename... Args> |
| 195 | static ParamSpec new_(Args &&...args) |
| 196 | { |
| 197 | return static_cast<ParamSpec &&>( |
| 198 | super(detail::ParamSpecFactory<T>::new_(std::forward<Args>(args)...))); |
| 199 | } |
| 200 | |
| 201 | // special override case |
| 202 | static ParamSpec new_(const gi::cstring_v name, ParamSpec overridden) |
| 203 | { |
| 204 | return static_cast<ParamSpec &&>( |
| 205 | super(g_param_spec_override(name.c_str(), overridden.gobj_()))); |
| 206 | } |
| 207 | |
| 208 | gi::cstring_v get_blurb() { return g_param_spec_get_blurb(gobj_()); } |
| 209 | |
| 210 | gi::cstring_v get_nick() { return g_param_spec_get_nick(gobj_()); } |
| 211 | |
| 212 | gi::cstring_v get_name() { return g_param_spec_get_name(gobj_()); } |
| 213 | |
| 214 | GQuark get_name_quark() { return g_param_spec_get_name_quark(gobj_()); } |
| 215 | |
| 216 | repository::GObject::Value get_default_value() |
| 217 | { |
| 218 | return gi::wrap(g_param_spec_get_default_value(gobj_()), transfer_none); |
| 219 | } |
| 220 | |
| 221 | ParamSpec get_redirect_target() |
| 222 | { |
| 223 | return gi::wrap(g_param_spec_get_redirect_target(gobj_()), transfer_none); |
| 224 | } |
| 225 | |
| 226 | // struct fields |
| 227 | const gchar *name_() const { return gobj_()->name; } |
| 228 | |
| 229 | ParamFlags value_type() const { return (ParamFlags)gobj_()->flags; } |
| 230 | |
| 231 | GType value_type_() const { return gobj_()->value_type; } |
| 232 | |
| 233 | GType owner_type_() const { return gobj_()->owner_type; } |
| 234 | }; |
| 235 | |
| 236 | } // namespace GObject |
| 237 | |
| 238 | } // namespace repository |
| 239 | |
| 240 | } // namespace gi |
| 241 | |
| 242 | #endif // GI_PARAMSPEC_HPP |