FINAL CUT
fobject.h
1 /***********************************************************************
2 * fobject.h - Object container base class of all widget objects *
3 * *
4 * This file is part of the FINAL CUT widget toolkit *
5 * *
6 * Copyright 2015-2025 Markus Gans *
7 * *
8 * FINAL CUT is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as *
10 * published by the Free Software Foundation; either version 3 of *
11 * the License, or (at your option) any later version. *
12 * *
13 * FINAL CUT is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with this program. If not, see *
20 * <http://www.gnu.org/licenses/>. *
21 ***********************************************************************/
22 
23 /* Inheritance diagram
24  * ═══════════════════
25  *
26  * ▕▔▔▔▔▔▔▔▔▔▔▔▔▔▔▏
27  * ▕ FObjectTimer ▏
28  * ▕▁▁▁▁▁▁▁▁▁▁▁▁▁▁▏
29  * ▲
30  * │
31  * ▕▔▔▔▔▔▔▔▔▔▏
32  * ▕ FObject ▏
33  * ▕▁▁▁▁▁▁▁▁▁▏
34  */
35 
36 #ifndef FOBJECT_H
37 #define FOBJECT_H
38 
39 #if !defined (USE_FINAL_H) && !defined (COMPILE_FINAL_CUT)
40  #error "Only <final/final.h> can be included directly."
41 #endif
42 
43 #if !defined (__cplusplus)
44  #error "You need a C++ compiler like g++ or clang++"
45 #elif __cplusplus > 1 && __cplusplus < 201402L
46  #error "Your C++ compiler does not support the C++14 standard!"
47 #endif
48 
49 #include <sys/time.h> // need for gettimeofday
50 #include <cstdlib>
51 #include <cstring>
52 #include <memory>
53 #include <vector>
54 
55 #include "final/ftimer.h"
56 #include "final/ftypes.h"
57 #include "final/util/fstring.h"
58 
59 namespace finalcut
60 {
61 
62 // class forward declaration
63 class FEvent;
64 class FKeyEvent;
65 class FMouseEvent;
66 class FWheelEvent;
67 class FFocusEvent;
68 class FAccelEvent;
69 class FShowEvent;
70 class FHideEvent;
71 class FCloseEvent;
72 class FTimerEvent;
73 class FUserEvent;
74 
75 //----------------------------------------------------------------------
76 // class FObject
77 //----------------------------------------------------------------------
78 
79 class FObject : public FObjectTimer
80 {
81  public:
82  // Using-declarations
83  using FObjectList = std::vector<FObject*>;
84  using iterator = FObjectList::iterator;
85  using reverse_iterator = FObjectList::reverse_iterator;
86  using const_iterator = FObjectList::const_iterator;
87  using const_reverse_iterator = FObjectList::const_reverse_iterator;
88  using reference = FObjectList::reference;
89  using const_reference = FObjectList::const_reference;
90  using difference_type = FObjectList::difference_type;
91 
92  // Constants
93  static constexpr auto UNLIMITED = static_cast<std::size_t>(-1);
94 
95  // Constructor
96  explicit FObject (FObject* = nullptr);
97 
98  // Disable copy constructor
99  FObject (const FObject&) = delete;
100 
101  // Disable move constructor
102  FObject (FObject&&) noexcept = delete;
103 
104  // Destructor
105  ~FObject() override;
106 
107  // Disable copy assignment operator (=)
108  auto operator = (const FObject&) -> FObject& = delete;
109 
110  // Disable move assignment operator (=)
111  auto operator = (FObject&&) noexcept -> FObject& = delete;
112 
113  // Accessors
114  auto getClassName() const -> FString override;
115  auto getSelf() const noexcept -> FObject*;
116  auto getParent() const noexcept -> FObject*;
117  auto getChild (int) const -> FObject*;
118  auto getChildren() noexcept -> FObjectList&;
119  auto getChildren() const noexcept -> const FObjectList&;
120  auto getMaxChildren() const noexcept -> std::size_t;
121  auto numOfChildren() const noexcept -> std::size_t;
122  auto begin() noexcept -> iterator;
123  auto end() noexcept -> iterator;
124  auto begin() const noexcept -> const_iterator;
125  auto end() const noexcept -> const_iterator;
126  auto cbegin() const noexcept -> const_iterator;
127  auto cend() const noexcept -> const_iterator;
128  auto rbegin() noexcept -> reverse_iterator;
129  auto rend() noexcept -> reverse_iterator;
130  auto rbegin() const noexcept -> const_reverse_iterator;
131  auto rend() const noexcept -> const_reverse_iterator;
132  auto crbegin() const noexcept -> const_reverse_iterator;
133  auto crend() const noexcept -> const_reverse_iterator;
134  auto front() -> reference;
135  auto back() -> reference;
136  auto front() const -> const_reference;
137  auto back() const -> const_reference;
138 
139  // Mutator
140  void setMaxChildren (std::size_t) noexcept;
141 
142  // Predicates
143  auto hasParent() const noexcept -> bool;
144  auto hasChildren() const noexcept -> bool;
145  auto isChild (const FObject*) const -> bool;
146  auto isDirectChild (const FObject*) const noexcept -> bool;
147  auto isWidget() const noexcept -> bool;
148  auto isInstanceOf (const FString&) const -> bool;
149 
150  // Methods
151  void removeParent();
152  void addChild (FObject*);
153  void delChild (FObject*);
154  void setParent (FObject*);
155 
156  // Event handler
157  virtual auto event (FEvent*) -> bool;
158 
159  protected:
160  // Mutator
161  void setWidgetProperty (bool = true) noexcept;
162 
163  // Event handler
164  virtual void onTimer (FTimerEvent*);
165  virtual void onUserEvent (FUserEvent*);
166 
167  private:
168  // Data members
169  FObject* parent_obj{nullptr};
170  FObject* self_obj{this};
171  FObjectList children_list{}; // no children yet
172  std::size_t max_children{UNLIMITED};
173  bool has_parent{false};
174  bool is_widget_object{false};
175 };
176 
177 // FObject inline functions
178 //----------------------------------------------------------------------
179 inline auto FObject::getClassName() const -> FString
180 { return "FObject"; }
181 
182 //----------------------------------------------------------------------
183 inline auto FObject::getSelf() const noexcept -> FObject*
184 { return self_obj; }
185 
186 //----------------------------------------------------------------------
187 inline auto FObject::getParent() const noexcept -> FObject*
188 { return parent_obj; }
189 
190 //----------------------------------------------------------------------
191 inline auto FObject::getChildren() noexcept -> FObjectList&
192 { return children_list; }
193 
194 //----------------------------------------------------------------------
195 inline auto FObject::getChildren() const noexcept -> const FObjectList&
196 { return children_list; }
197 
198 //----------------------------------------------------------------------
199 inline auto FObject::getMaxChildren() const noexcept -> std::size_t
200 { return max_children; }
201 
202 //----------------------------------------------------------------------
203 inline auto FObject::numOfChildren() const noexcept -> std::size_t
204 { return children_list.size(); }
205 
206 //----------------------------------------------------------------------
207 inline auto FObject::begin() noexcept -> iterator
208 { return children_list.begin(); }
209 
210 //----------------------------------------------------------------------
211 inline auto FObject::end() noexcept -> iterator
212 { return children_list.end(); }
213 
214 //----------------------------------------------------------------------
215 inline auto FObject::begin() const noexcept -> const_iterator
216 { return children_list.begin(); }
217 
218 //----------------------------------------------------------------------
219 inline auto FObject::end() const noexcept -> const_iterator
220 { return children_list.end(); }
221 
222 //----------------------------------------------------------------------
223 inline auto FObject::cbegin() const noexcept -> const_iterator
224 { return children_list.cbegin(); }
225 
226 //----------------------------------------------------------------------
227 inline auto FObject::cend() const noexcept -> const_iterator
228 { return children_list.cend(); }
229 
230 //----------------------------------------------------------------------
231 inline auto FObject::rbegin() noexcept -> reverse_iterator
232 { return children_list.rbegin(); }
233 
234 //----------------------------------------------------------------------
235 inline auto FObject::rend() noexcept -> reverse_iterator
236 { return children_list.rend(); }
237 
238 //----------------------------------------------------------------------
239 inline auto FObject::rbegin() const noexcept -> const_reverse_iterator
240 { return children_list.rbegin(); }
241 
242 //----------------------------------------------------------------------
243 inline auto FObject::rend() const noexcept -> const_reverse_iterator
244 { return children_list.rend(); }
245 
246 //----------------------------------------------------------------------
247 inline auto FObject::crbegin() const noexcept -> const_reverse_iterator
248 { return children_list.crbegin(); }
249 
250 //----------------------------------------------------------------------
251 inline auto FObject::crend() const noexcept -> const_reverse_iterator
252 { return children_list.crend(); }
253 
254 //----------------------------------------------------------------------
255 inline auto FObject::front() -> reference
256 { return children_list.front(); }
257 
258 //----------------------------------------------------------------------
259 inline auto FObject::back() -> reference
260 { return children_list.back(); }
261 
262 //----------------------------------------------------------------------
263 inline auto FObject::front() const -> const_reference
264 { return children_list.front(); }
265 
266 //----------------------------------------------------------------------
267 inline auto FObject::back() const -> const_reference
268 { return children_list.back(); }
269 
270 //----------------------------------------------------------------------
271 inline void FObject::setMaxChildren (std::size_t max) noexcept
272 { max_children = max; }
273 
274 //----------------------------------------------------------------------
275 inline auto FObject::hasParent() const noexcept -> bool
276 { return has_parent; }
277 
278 //----------------------------------------------------------------------
279 inline auto FObject::hasChildren() const noexcept -> bool
280 { return ! children_list.empty(); }
281 
282 //----------------------------------------------------------------------
283 inline auto FObject::isDirectChild (const FObject* obj) const noexcept -> bool
284 { return obj->getParent() == this; }
285 
286 //----------------------------------------------------------------------
287 inline auto FObject::isWidget() const noexcept -> bool
288 { return is_widget_object; }
289 
290 //----------------------------------------------------------------------
291 inline auto FObject::isInstanceOf (const FString& classname) const -> bool
292 { return classname == getClassName(); }
293 
294 //----------------------------------------------------------------------
295 inline void FObject::setWidgetProperty (bool is_widget) noexcept
296 { is_widget_object = is_widget; }
297 
298 } // namespace finalcut
299 
300 #endif // FOBJECT_H
Definition: fevent.h:311
Definition: class_template.cpp:25
Definition: fevent.h:327
Definition: fobject.h:79
Definition: fstring.h:82
Definition: fevent.h:101
Definition: ftimer.h:316