Debian Codegraph @

examples/gobject.cpp

raw symbols SCIP
1 // no code generation is needed
2 // all required functionality is required by the basic code
3 #include <gi/gi.hpp>
4
5 #include <iostream>
6
7 namespace GObject_ = gi::repository::GObject;
8
9 static const int DEFAULT_AGE = 10;
10 static const char *PERSON_TYPE = "GIPerson";
11
12 // class must be a ObjectImpl to support properties and signals
13 class Person : public GObject_::impl::ObjectImpl
14 {
15 using self_type = Person;
16
17 private:
18 // the implementation/definition part of the properties
19 // the setup parameters shadow the corresponding g_param_spec_xxx
20 // so in practice define the property (name, nick, description)
21 // along with min, max, default and so (where applicable)
22 // (could also be in a constructor initializer list,
23 // but this way it applies to any constructor)
24 // this provides interface to set/get the actual value
25 gi::property<int> prop_age_{this, "age", "age", "age", 0, 100, DEFAULT_AGE};
26 gi::property<std::string> prop_firstname_{
27 this, "firstname", "firstname", "firstname", ""};
28 gi::property<std::string> prop_lastname_{
29 this, "lastname", "lastname", "lastname", ""};
30
31 public:
32 // likewise for signal
33 // public because there is no extra interface for owning class
34 // (both owner and outside can connect and/or emit)
35 // btw, using Person in this signature would not be the way to go,
36 // should stick to a plain wrapped type
37 gi::signal<void(GObject_::Object, int)> signal_trigger{this, "trigger"};
38 // std::string also works here,
39 // but that would cost an extra allocation during invocation
40 gi::signal<void(GObject_::Object, gi::cstring_v)> signal_example{
41 this, "example"};
42
43 public:
44 // old-style C++ first
45 Person() : ObjectImpl(this) {}
46
47 // new-style C-first;
48 // must use proper signature for constructor and superclass
49 // this also registers a "public" GType, whose name must be specified
50 Person(const InitData &id) : ObjectImpl(this, id, PERSON_TYPE) {}
51
52 // usually the above constructor is also used to during GType registration,
53 // as such "dummy instantiation" auto-magically collects property info, etc)
54 // however, it does involve an "incomplete" instantiation (with no .gobj_())
55 // if such is considered too inelegant or too costly, then alternatively
56 // a ::get_type_() can be defined that is then used to register type instead
57 // however, as the example below demonstrates, this may be much less ergonomic
58 // so it is likely only useful if really so desired or in advanced cases)
59 // (e.g. subclass-ing a subclass)
60 static GType get_type_disabled() // not really used due to extra suffix
61 {
62 // if this is used, the member initializers only need to mention name
63 // no longer the full spec(ification)
64 // (as the name then only serves to link the propertyspec to actual storage)
65 return register_type_<Person>(PERSON_TYPE, 0, {},
66 {{&self_type::prop_age_, "age", "age", "age", 0, 100, DEFAULT_AGE},
67 {&self_type::prop_firstname_, "firstname", "firstname", "firstname",
68 ""},
69 {&self_type::prop_lastname_, "lastname", "lastname", "lastname",
70 ""}},
71 {{&self_type::signal_trigger, "trigger"},
72 {&self_type::signal_example, "example"}});
73 }
74
75 // the public counterpart providing the same interface
76 // as with any wrapped object's predefined properties
77 gi::property_proxy<int> prop_age() { return prop_age_.get_proxy(); }
78
79 gi::property_proxy<std::string> prop_firstname()
80 {
81 return prop_firstname_.get_proxy();
82 }
83
84 gi::property_proxy<std::string> prop_lastname()
85 {
86 return prop_lastname_.get_proxy();
87 }
88
89 void action(int id)
90 {
91 std::cout << "Changing the properties of 'p'" << std::endl;
92 prop_firstname_ = "John";
93 prop_lastname_ = "Doe";
94 prop_age_ = 43;
95 std::cout << "Done changing the properties of 'p'" << std::endl;
96 // we were triggered after all
97 signal_trigger.emit(id);
98 }
99 };
100
101 void
102 on_firstname_changed(GObject_::Object, GObject_::ParamSpec)
103 {
104 std::cout << "- firstname changed!" << std::endl;
105 }
106
107 void
108 on_lastname_changed(GObject_::Object, GObject_::ParamSpec)
109 {
110 std::cout << "- lastname changed!" << std::endl;
111 }
112
113 void
114 on_age_changed(GObject_::Object, GObject_::ParamSpec)
115 {
116 std::cout << "- age changed!" << std::endl;
117 }
118
119 int
120 main(int /*argc*/, char ** /*argv*/)
121 {
122 Person p;
123 // should have default age
124 assert(p.prop_age().get() == DEFAULT_AGE);
125 // register some handlers that will be called when the values of the
126 // specified parameters are changed
127 p.prop_firstname().signal_notify().connect(&on_firstname_changed);
128 p.prop_lastname().signal_notify().connect(on_lastname_changed);
129 p.prop_age().signal_notify().connect(&on_age_changed);
130
131 // now change the properties and see that the handlers get called
132 p.action(0);
133
134 // (derived) object can be constructed on stack for simple cases
135 // but in other (real) cases it is recommended that it is heap based.
136 // so as not to have a naked ptr, it can be managed by a (special) shared
137 // ptr that uses the GObject refcount for (shared) ownership tracking
138 auto dp = gi::make_ref<Person, gi::construct_cpp_t>();
139 // however, the GObject world has no knowledge of the subclass
140 // (each instance is 1-to-1 with Person instance though).
141 // so when we get something from that world, we can (sort-of dynamic)
142 // cast to the subclass
143 auto l = [](GObject_::Object ob, int id) {
144 std::cout << " - triggered id " << id << std::endl;
145 // obtain Person
146 auto lp = gi::ref_ptr_cast<Person>(ob);
147 if (lp)
148 std::cout << " - it was a person!" << std::endl;
149 // it really should be ...
150 assert(lp);
151 };
152 dp->signal_trigger.connect(l);
153 dp->action(1);
154
155 // all of the above constructed Person in C++ centric style
156 // that is, as part of the constructor (super class) execution,
157 // a (derived) GObject is created and associated/extended with C++ side Person
158
159 // alternatively, another derived GObject type can be registered,
160 // which will trigger construction of a Person (as part of g_object_new()),
161 // and then again associate those
162 // the following essentially runs g_object_new()
163 static int CUSTOM_AGE = 5;
164 auto dpc = gi::make_ref<Person, gi::construct_c_t>("age", CUSTOM_AGE);
165 // this will have a different GType
166 assert(dpc->gobj_type_() != dp->gobj_type_());
167 // as registered using specified name
168 assert(g_type_from_name(PERSON_TYPE) == dpc->gobj_type_());
169 // and the right age as specified in (gobject style) construct params
170 assert(dpc->prop_age().get() == CUSTOM_AGE);
171
172 // if the class supports C-style, it is selected automatically
173 auto dpa = gi::make_ref<Person>();
174 // should have ended up with C-style GType
175 assert(dpa->gobj_type_() == dpc->gobj_type_());
176
177 return 0;
178 }