| 1 | /* Copyright (C) 1998-2010 The gtkmm Development Team |
| 2 | * |
| 3 | * This library is free software; you can redistribute it and/or |
| 4 | * modify it under the terms of the GNU Lesser General Public |
| 5 | * License as published by the Free Software Foundation; either |
| 6 | * version 2.1 of the License, or (at your option) any later version. |
| 7 | * |
| 8 | * This library is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * Lesser General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public |
| 14 | * License along with this library; if not, see <https://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | #ifndef _GTKMM_OBJECT_H |
| 18 | #define _GTKMM_OBJECT_H |
| 19 | |
| 20 | #include <glibmm/object.h> |
| 21 | #include <gtkmm/base.h> |
| 22 | #include <gtkmmconfig.h> |
| 23 | |
| 24 | #include <utility> |
| 25 | |
| 26 | namespace Gtk |
| 27 | { |
| 28 | |
| 29 | class GTKMM_API Object; |
| 30 | |
| 31 | /** Mark a Gtk::Object as owned by its parent container widget, so you don't need to delete it manually. |
| 32 | * For instance, |
| 33 | * @code |
| 34 | * Gtk::Button* button = Gtk::manage( new Gtk::Button("Hello") ); |
| 35 | * vbox.pack_start(*button); //vbox will delete button when vbox is deleted. |
| 36 | * @endcode |
| 37 | * |
| 38 | * @param obj A Gtk::Object, such as a gtkmm widget. |
| 39 | * @result The Gtk::Object passed as the @a obj parameter. |
| 40 | */ |
| 41 | template<class T> |
| 42 | T* manage(T* obj) |
| 43 | { |
| 44 | obj->set_manage(); |
| 45 | return obj; |
| 46 | } |
| 47 | |
| 48 | /** Create a Gtk::Object such as a widget and Gtk::manage() it in a single step. |
| 49 | * This matches standard functions like std::make_unique<T>(args) and avoids you |
| 50 | * manually invoking the new operator, which is discouraged in modern C++ style. |
| 51 | * |
| 52 | * For instance, |
| 53 | * @code |
| 54 | * Gtk::Button* button = Gtk::make_managed<Gtk::Button>("Hello"); |
| 55 | * vbox.pack_start(*button); //vbox will delete button when vbox is deleted. |
| 56 | * @endcode |
| 57 | * |
| 58 | * @param args Arguments to pass to the constructor of the given template type. |
| 59 | * @result A new, managed object of that type, constructed with those arguments. |
| 60 | */ |
| 61 | template<class T, class... T_Args> |
| 62 | T* make_managed(T_Args&&... args) |
| 63 | { |
| 64 | return manage(new T(std::forward<T_Args>(args)...)); |
| 65 | } |
| 66 | |
| 67 | /** Gtk::Object is the base class for all widgets, and for a few non-widget objects such as |
| 68 | * Gtk::Adjustment. Gtk::Object predates Glib::Object; non-widgets that derive from Gtk::Object |
| 69 | * rather than Glib::Object do so for backward compatibility reasons. |
| 70 | * |
| 71 | * The most interesting difference between Gtk::Object and Glib::Object is the ability to use Gtk::manage() to delegate memory management to the container widget. Gtk::Objects can also be |
| 72 | * explicitly deleted at any time, instead of using only reference-counting, and container widgets |
| 73 | * can respond when their child objects are being deleted (for instance by removing the widget). |
| 74 | */ |
| 75 | |
| 76 | class GTKMM_API Object : public Glib::Object |
| 77 | { |
| 78 | public: |
| 79 | #ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 80 | typedef Object CppObjectType; |
| 81 | typedef Glib::Object_Class CppClassType; |
| 82 | typedef GObject BaseObjectType; |
| 83 | typedef GObjectClass BaseClassType; |
| 84 | #endif /* DOXYGEN_SHOULD_SKIP_THIS */ |
| 85 | |
| 86 | #ifdef GTKMM_DISABLE_DEPRECATED |
| 87 | /** This prevents use of Gtk::Object-derived classes with RefPtr. |
| 88 | * RefPtr should only be used with classes that have create() methods |
| 89 | * that return RefPtr. |
| 90 | */ |
| 91 | typedef int dont_allow_use_in_glib_refptr_; |
| 92 | #endif |
| 93 | |
| 94 | Object(Object&& src) noexcept; |
| 95 | Object& operator=(Object&& src) noexcept; |
| 96 | |
| 97 | ~Object() noexcept override; |
| 98 | |
| 99 | #ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 100 | |
| 101 | private: |
| 102 | friend class Object_Class; |
| 103 | static CppClassType object_class_; |
| 104 | |
| 105 | // noncopyable |
| 106 | Object(const Object&); |
| 107 | Object& operator=(const Object&); |
| 108 | |
| 109 | protected: |
| 110 | explicit Object(const Glib::ConstructParams& construct_params); |
| 111 | explicit Object(GObject* castitem); |
| 112 | |
| 113 | #endif /* DOXYGEN_SHOULD_SKIP_THIS */ |
| 114 | |
| 115 | public: |
| 116 | #ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 117 | static GType get_type() G_GNUC_CONST; |
| 118 | |
| 119 | |
| 120 | static GType get_base_type() G_GNUC_CONST; |
| 121 | #endif |
| 122 | |
| 123 | /// Provides access to the underlying C GObject. |
| 124 | GObject* gobj() { return reinterpret_cast<GObject*>(gobject_); } |
| 125 | |
| 126 | /// Provides access to the underlying C GObject. |
| 127 | const GObject* gobj() const { return reinterpret_cast<GObject*>(gobject_); } |
| 128 | |
| 129 | public: |
| 130 | #ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 131 | /** Used by Gtk::manage(). You should not need to use this directly. |
| 132 | */ |
| 133 | void set_manage() override; |
| 134 | #endif //DOXYGEN_SHOULD_SKIP_THIS |
| 135 | |
| 136 | #ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 137 | /** Private API. |
| 138 | */ |
| 139 | bool is_managed_() const; |
| 140 | #endif //DOXYGEN_SHOULD_SKIP_THIS |
| 141 | |
| 142 | protected: |
| 143 | |
| 144 | #ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 145 | /** Private API. |
| 146 | */ |
| 147 | void destroy_(); |
| 148 | #endif //DOXYGEN_SHOULD_SKIP_THIS |
| 149 | |
| 150 | #ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 151 | void _init_unmanage(bool is_toplevel = false); |
| 152 | void destroy_notify_() override; |
| 153 | void disconnect_cpp_wrapper(); |
| 154 | void _release_c_instance(); |
| 155 | static void callback_weak_notify_(void* data, GObject* gobject); //only connected for a short time. |
| 156 | |
| 157 | // set if flags used by derived classes. |
| 158 | bool referenced_; // = not managed. |
| 159 | bool gobject_disposed_; |
| 160 | #endif //DOXYGEN_SHOULD_SKIP_THIS |
| 161 | }; |
| 162 | |
| 163 | } // namespace Gtk |
| 164 | |
| 165 | #endif /* _GTKMM_OBJECT_H */ |