| 1 | #ifndef GI_OBJECTBASE_HPP |
| 2 | #define GI_OBJECTBASE_HPP |
| 3 | |
| 4 | #include "gi_inc.hpp" |
| 5 | |
| 6 | GI_MODULE_EXPORT |
| 7 | namespace gi |
| 8 | { |
| 9 | namespace detail |
| 10 | { |
| 11 | struct GObjectFuncs |
| 12 | { |
| 13 | static void *ref(void *data) { return g_object_ref(data); } |
| 14 | static void *sink(void *data) { return g_object_ref_sink(data); } |
| 15 | static void free(void *data) { g_object_unref(data); } |
| 16 | static void *float_(void *data) |
| 17 | { |
| 18 | g_object_force_floating((GObject *)data); |
| 19 | return g_object_ref(data); |
| 20 | } |
| 21 | }; |
| 22 | |
| 23 | template<typename Funcs, typename CType = void> |
| 24 | class Wrapper |
| 25 | { |
| 26 | protected: |
| 27 | CType *data_ = nullptr; |
| 28 | |
| 29 | static CType *_ref(CType *data) { return data ? Funcs::ref(data) : data; } |
| 30 | static CType *_sink(CType *data) { return data ? Funcs::sink(data) : data; } |
| 31 | static CType *_float(CType *data) |
| 32 | { |
| 33 | return data ? Funcs::float_(data) : data; |
| 34 | } |
| 35 | static void _deleter(CType *&data) |
| 36 | { |
| 37 | if (data) |
| 38 | Funcs::free(data); |
| 39 | } |
| 40 | |
| 41 | public: |
| 42 | Wrapper(decltype(data_) d = nullptr, bool own = true, bool sink = true) |
| 43 | : data_(own ? d : (sink ? _sink(d) : _ref(d))) |
| 44 | {} |
| 45 | |
| 46 | ~Wrapper() { _deleter(data_); } |
| 47 | |
| 48 | Wrapper(const Wrapper &other) |
| 49 | { |
| 50 | _deleter(data_); |
| 51 | data_ = _ref(other.data_); |
| 52 | } |
| 53 | |
| 54 | Wrapper(Wrapper &&other) noexcept |
| 55 | { |
| 56 | _deleter(data_); |
| 57 | data_ = other.data_; |
| 58 | other.data_ = nullptr; |
| 59 | } |
| 60 | |
| 61 | explicit operator bool() const { return (bool)data_; } |
| 62 | |
| 63 | Wrapper &operator=(const Wrapper &other) |
| 64 | { |
| 65 | if (&other != this) { |
| 66 | _deleter(data_); |
| 67 | data_ = _ref(other.data_); |
| 68 | } |
| 69 | return *this; |
| 70 | } |
| 71 | |
| 72 | Wrapper &operator=(Wrapper &&other) noexcept |
| 73 | { |
| 74 | if (&other != this) { |
| 75 | _deleter(data_); |
| 76 | data_ = other.data_; |
| 77 | other.data_ = nullptr; |
| 78 | } |
| 79 | return *this; |
| 80 | } |
| 81 | |
| 82 | bool operator==(const Wrapper &other) const { return data_ == other.data_; } |
| 83 | |
| 84 | bool operator==(std::nullptr_t o) const |
| 85 | { |
| 86 | (void)o; |
| 87 | return data_ == o; |
| 88 | } |
| 89 | |
| 90 | bool operator!=(const Wrapper &other) const { return data_ != other.data_; } |
| 91 | |
| 92 | bool operator!=(std::nullptr_t o) const { return data_ != o; } |
| 93 | |
| 94 | CType *gobj_() { return this->data_; } |
| 95 | const CType *gobj_() const { return this->data_; } |
| 96 | }; |
| 97 | |
| 98 | class wrapper_tag |
| 99 | {}; |
| 100 | |
| 101 | template<typename CType, typename Funcs, GType GTYPE_> |
| 102 | class WrapperBase : public Wrapper<Funcs>, public wrapper_tag |
| 103 | { |
| 104 | typedef WrapperBase self; |
| 105 | typedef Wrapper<Funcs> super_type; |
| 106 | |
| 107 | public: |
| 108 | typedef CType BaseObjectType; |
| 109 | |
| 110 | BaseObjectType *gobj_() { return (BaseObjectType *)this->data_; } |
| 111 | const BaseObjectType *gobj_() const |
| 112 | { |
| 113 | return (const BaseObjectType *)this->data_; |
| 114 | } |
| 115 | BaseObjectType *gobj_copy_() const |
| 116 | { |
| 117 | return (BaseObjectType *)self::_ref(this->data_); |
| 118 | } |
| 119 | BaseObjectType *gobj_float_() const |
| 120 | { |
| 121 | return (BaseObjectType *)self::_float(this->data_); |
| 122 | } |
| 123 | BaseObjectType *release_() |
| 124 | { |
| 125 | void *r = nullptr; |
| 126 | std::swap(this->data_, r); |
| 127 | return (BaseObjectType *)r; |
| 128 | } |
| 129 | |
| 130 | static GType get_type_() { return GTYPE_; } |
| 131 | GType gobj_type_() { return GTYPE_; } |
| 132 | |
| 133 | WrapperBase(BaseObjectType *p = nullptr, bool own = true, bool argout = true) |
| 134 | : super_type(p, own, argout) |
| 135 | {} |
| 136 | |
| 137 | WrapperBase(const self &other) = default; |
| 138 | WrapperBase(self &&other) = default; |
| 139 | |
| 140 | self &operator=(const self &other) = default; |
| 141 | self &operator=(self &&other) = default; |
| 142 | |
| 143 | // always arrange to sink by default nowadays |
| 144 | template<typename Cpp> |
| 145 | static Cpp wrap( |
| 146 | const typename Cpp::BaseObjectType *obj, bool own, bool argout = true) |
| 147 | { |
| 148 | static_assert(sizeof(Cpp) == sizeof(self), "type wrap not supported"); |
| 149 | static_assert(std::is_base_of<self, Cpp>::value, "type wrap not supported"); |
| 150 | WrapperBase w((self::BaseObjectType *)(obj), own, argout); |
| 151 | return std::move(*static_cast<Cpp *>(&w)); |
| 152 | } |
| 153 | }; |
| 154 | |
| 155 | typedef WrapperBase<void, GObjectFuncs, G_TYPE_NONE> ObjectBase; |
| 156 | |
| 157 | // foundation for Variant that will be generated |
| 158 | struct GVariantFuncs |
| 159 | { |
| 160 | static void *ref(void *data) { return g_variant_ref((GVariant *)data); } |
| 161 | static void *sink(void *data) { return g_variant_ref_sink((GVariant *)data); } |
| 162 | static void free(void *data) { g_variant_unref((GVariant *)data); } |
| 163 | static void *float_(void *data) { return data; } |
| 164 | }; |
| 165 | |
| 166 | typedef WrapperBase<GVariant, GVariantFuncs, G_TYPE_VARIANT> VariantWrapper; |
| 167 | |
| 168 | } // namespace detail |
| 169 | |
| 170 | } // namespace gi |
| 171 | |
| 172 | #endif // GI_OBJECTBASE_HPP |