1 // -*- c++ -*-
2 #ifndef _GLIBMM_CLASS_H
3 #define _GLIBMM_CLASS_H
4 
5 /* $Id$ */
6 
7 /* Copyright 2001 Free Software Foundation
8  * Copyright (C) 1998-2002 The gtkmm Development Team
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include <glib-object.h>
25 #include <glibmmconfig.h> //Include this here so that the /private/*.h classes have access to GLIBMM_VFUNCS_ENABLED
26 
27 #include <vector> //For interface properties that custom types might override.
28 #include <tuple>
29 
30 #ifndef DOXYGEN_SHOULD_SKIP_THIS
31 
32 namespace Glib
33 {
34 class GLIBMM_API Interface_Class;
35 
36 class GLIBMM_API Class
37 {
38 public:
39   /* No constructor/destructor:
40    * Glib::Class objects are used only as static data, which would cause
41    * lots of ugly global constructor invocations.  These are avoidable,
42    * because the C/C++ standard explicitly specifies that all _static_ data
43    * is zero-initialized at program start.
44    */
45   // Class();
46   //~Class() noexcept;
47 
48   // static void class_init_function(BaseClassType *p);
49   // static void object_init_function(BaseObjectType *o);
50   // GType get_type() = 0; //Creates the GType when this is first called.
51 
52   // Hook for translating API
53   // static Glib::Object* wrap_new(GObject*);
54 
55   inline GType get_type() const;
56 
57   // TODO: Remove this method at the next ABI/API break.
58   /** Register a static custom GType, derived from the parent of this class's type.
59    * The parent type of the registered custom type is the same C class as the parent
60    * of the get_type() type. If a type with the specified name is already registered,
61    * nothing is done. register_derived_type() must have been called.
62    * @param custom_type_name The name of the registered type is
63    *        "gtkmm__CustomObject_" + canonic(custom_type_name), where canonic()
64    *        replaces special characters with '+'.
65    * @return The registered type.
66    */
67   GType clone_custom_type(const char* custom_type_name) const;
68 
69   /// The type that holds pointers to the interfaces of custom types.
70   using interface_class_vector_type = std::vector<const Interface_Class*>;
71 
72   /** The type that holds pointers to extra class init functions of custom types.
73    * The std::tuple contains a function pointer and a pointer to class data.
74    * The class data pointer can be nullptr, if the function does not need it.
75    */
76   using class_init_funcs_type = std::vector<std::tuple<GClassInitFunc, void*>>;
77 
78   // TODO: Remove this method at the next ABI/API break.
79   /** Register a static custom GType, derived from the parent of this class's type.
80    * The parent type of the registered custom type is the same C class as the parent
81    * of the get_type() type. If a type with the specified name is already registered,
82    * nothing is done. register_derived_type() must have been called.
83    * @param custom_type_name The name of the registered type is
84    *        "gtkmm__CustomObject_" + canonic(custom_type_name), where canonic()
85    *        replaces special characters with '+'.
86    * @param interface_classes Interfaces that the custom type implements.
87    * @return The registered type.
88    */
89   GType clone_custom_type(
90     const char* custom_type_name, const interface_class_vector_type& interface_classes) const;
91 
92   /** Register a static custom GType, derived from the parent of this class's type.
93    * The parent type of the registered custom type is the same C class as the parent
94    * of the get_type() type. If a type with the specified name is already registered,
95    * nothing is done. register_derived_type() must have been called.
96    * @param custom_type_name The name of the registered type is
97    *        "gtkmm__CustomObject_" + canonic(custom_type_name), where canonic()
98    *        replaces special characters with '+'.
99    * @param interface_classes Interfaces that the custom type implements (can be nullptr).
100    * @param class_init_funcs Extra class init functions (can be nullptr). These
101    *        functions, if any, are called after the class init function of this
102    *        class's type, e.g. Gtk::Widget.
103    * @param instance_init_func Instance init function (can be nullptr).
104    * @return The registered type.
105    */
106   GType clone_custom_type(
107     const char* custom_type_name, const interface_class_vector_type* interface_classes,
108     const class_init_funcs_type* class_init_funcs, GInstanceInitFunc instance_init_func) const;
109 
110 protected:
111   GType gtype_;
112   GClassInitFunc class_init_func_;
113 
114   /** Register a GType, derived from the @a base_type.
115    */
116   void register_derived_type(GType base_type);
117 
118   /** Register a GType, derived from the @a base_type.
119    * @param module If this is not 0 then g_type_module_register_type() will be used. Otherwise
120    * g_type_register_static() will be used.
121    */
122   void register_derived_type(GType base_type, GTypeModule* module);
123 
124 private:
125   static void custom_class_base_finalize_function(void* g_class);
126   static void custom_class_init_function(void* g_class, void* class_data);
127 
128 public:
129 #ifndef DOXYGEN_SHOULD_SKIP_THIS
130   // The type that holds the values of the interface properties of custom types.
131   using iface_properties_type = std::vector<GValue*>;
132   // The quark used for storing/getting the interface properties of custom types.
133   static GQuark iface_properties_quark;
134 #endif
135 };
136 
137 inline GType
get_type()138 Class::get_type() const
139 {
140   return gtype_;
141 }
142 
143 } // namespace Glib
144 
145 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
146 
147 #endif /* _GLIBMM_CLASS_H */
148