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-2023 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 getParent() const & -> FObject*;
116  auto getChild (int) const & -> FObject*;
117  auto getChildren() & -> FObjectList&;
118  auto getChildren() const & -> const FObjectList&;
119  auto getMaxChildren() const & noexcept -> std::size_t;
120  auto numOfChildren() const & -> std::size_t;
121  auto begin() -> iterator;
122  auto end() -> iterator;
123  auto begin() const -> const_iterator;
124  auto end() const -> const_iterator;
125  auto cbegin() const noexcept -> const_iterator;
126  auto cend() const noexcept -> const_iterator;
127  auto rbegin() -> reverse_iterator;
128  auto rend() -> reverse_iterator;
129  auto rbegin() const -> const_reverse_iterator;
130  auto rend() const -> const_reverse_iterator;
131  auto crbegin() const noexcept -> const_reverse_iterator;
132  auto crend() const noexcept -> const_reverse_iterator;
133  auto front() -> reference;
134  auto back() -> reference;
135  auto front() const -> const_reference;
136  auto back() const -> const_reference;
137 
138  // Mutator
139  void setMaxChildren (std::size_t) noexcept;
140 
141  // Inquiries
142  auto hasParent() const & noexcept -> bool;
143  auto hasChildren() const & -> bool;
144  auto isChild (const FObject*) const & -> bool;
145  auto isDirectChild (const FObject*) const & -> bool;
146  auto isWidget() const noexcept -> bool;
147  auto isInstanceOf (const FString&) const -> bool;
148 
149  // Methods
150  void removeParent() &;
151  void addChild (FObject*) &;
152  void delChild (FObject*) &;
153  void setParent (FObject*) &;
154 
155  // Event handler
156  virtual auto event (FEvent*) -> bool;
157 
158  protected:
159  // Mutator
160  void setWidgetProperty (bool = true);
161 
162  // Event handler
163  virtual void onTimer (FTimerEvent*);
164  virtual void onUserEvent (FUserEvent*);
165 
166  private:
167  // Data members
168  FObject* parent_obj{nullptr};
169  FObjectList children_list{}; // no children yet
170  std::size_t max_children{UNLIMITED};
171  bool has_parent{false};
172  bool widget_object{false};
173 };
174 
175 // FObject inline functions
176 //----------------------------------------------------------------------
177 inline auto FObject::getClassName() const -> FString
178 { return "FObject"; }
179 
180 //----------------------------------------------------------------------
181 inline auto FObject::getParent() const & -> FObject*
182 { return parent_obj; }
183 
184 //----------------------------------------------------------------------
185 inline auto FObject::getChildren() & -> FObjectList&
186 { return children_list; }
187 
188 //----------------------------------------------------------------------
189 inline auto FObject::getChildren() const & -> const FObjectList&
190 { return children_list; }
191 
192 //----------------------------------------------------------------------
193 inline auto FObject::getMaxChildren() const & noexcept -> std::size_t
194 { return max_children; }
195 
196 //----------------------------------------------------------------------
197 inline auto FObject::numOfChildren() const & -> std::size_t
198 { return children_list.size(); }
199 
200 //----------------------------------------------------------------------
201 inline auto FObject::begin() -> iterator
202 { return children_list.begin(); }
203 
204 //----------------------------------------------------------------------
205 inline auto FObject::end() -> iterator
206 { return children_list.end(); }
207 
208 //----------------------------------------------------------------------
209 inline auto FObject::begin() const -> const_iterator
210 { return children_list.begin(); }
211 
212 //----------------------------------------------------------------------
213 inline auto FObject::end() const -> const_iterator
214 { return children_list.end(); }
215 
216 //----------------------------------------------------------------------
217 inline auto FObject::cbegin() const noexcept -> const_iterator
218 { return children_list.cbegin(); }
219 
220 //----------------------------------------------------------------------
221 inline auto FObject::cend() const noexcept -> const_iterator
222 { return children_list.cend(); }
223 
224 //----------------------------------------------------------------------
225 inline auto FObject::rbegin() -> reverse_iterator
226 { return children_list.rbegin(); }
227 
228 //----------------------------------------------------------------------
229 inline auto FObject::rend() -> reverse_iterator
230 { return children_list.rend(); }
231 
232 //----------------------------------------------------------------------
233 inline auto FObject::rbegin() const -> const_reverse_iterator
234 { return children_list.rbegin(); }
235 
236 //----------------------------------------------------------------------
237 inline auto FObject::rend() const -> const_reverse_iterator
238 { return children_list.rend(); }
239 
240 //----------------------------------------------------------------------
241 inline auto FObject::crbegin() const noexcept -> const_reverse_iterator
242 { return children_list.crbegin(); }
243 
244 //----------------------------------------------------------------------
245 inline auto FObject::crend() const noexcept -> const_reverse_iterator
246 { return children_list.crend(); }
247 
248 //----------------------------------------------------------------------
249 inline auto FObject::front() -> reference
250 { return children_list.front(); }
251 
252 //----------------------------------------------------------------------
253 inline auto FObject::back() -> reference
254 { return children_list.back(); }
255 
256 //----------------------------------------------------------------------
257 inline auto FObject::front() const -> const_reference
258 { return children_list.front(); }
259 
260 //----------------------------------------------------------------------
261 inline auto FObject::back() const -> const_reference
262 { return children_list.back(); }
263 
264 //----------------------------------------------------------------------
265 inline void FObject::setMaxChildren (std::size_t max) noexcept
266 { max_children = max; }
267 
268 //----------------------------------------------------------------------
269 inline auto FObject::hasParent() const & noexcept -> bool
270 { return has_parent; }
271 
272 //----------------------------------------------------------------------
273 inline auto FObject::hasChildren() const & -> bool
274 { return ! children_list.empty(); }
275 
276 //----------------------------------------------------------------------
277 inline auto FObject::isDirectChild (const FObject* obj) const & -> bool
278 { return obj->getParent() == this; }
279 
280 //----------------------------------------------------------------------
281 inline auto FObject::isWidget() const noexcept -> bool
282 { return widget_object; }
283 
284 //----------------------------------------------------------------------
285 inline auto FObject::isInstanceOf (const FString& classname) const -> bool
286 { return classname == getClassName(); }
287 
288 //----------------------------------------------------------------------
289 inline void FObject::setWidgetProperty (bool is_widget)
290 { widget_object = is_widget; }
291 
292 } // namespace finalcut
293 
294 #endif // FOBJECT_H
Definition: fevent.h:300
Definition: class_template.cpp:25
Definition: fevent.h:316
Definition: fobject.h:79
Definition: fstring.h:79
Definition: fevent.h:101
Definition: ftimer.h:315