Ocean
Loading...
Searching...
No Matches
DebugElements.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_BASE_DEBUG_ELEMENTS_H
9#define META_OCEAN_BASE_DEBUG_ELEMENTS_H
10
11#include "ocean/base/Base.h"
12#include "ocean/base/Callback.h"
13#include "ocean/base/Frame.h"
14#include "ocean/base/Lock.h"
16
17#include <map>
18#include <unordered_set>
19
20namespace Ocean
21{
22
23/**
24 * This class implements the base class for a container for debug elements.
25 * To use this class, create an own singleton class and derive from this object.
26 * @ingroup base
27 */
28class OCEAN_BASE_EXPORT DebugElements
29{
30 public:
31
32 /**
33 * This class implements a scoped hierarchy.
34 * The hierarchy exists as long as this object exists.
35 */
36 class OCEAN_BASE_EXPORT ScopedHierarchyBase
37 {
38 public:
39
40 /**
41 * Releases the hierarchy and disposes this object.
42 */
43 inline ~ScopedHierarchyBase();
44
45 /**
46 * Explicitly exchanges the hierarchy instead of releasing it and creating a new one.
47 * Actually pops the current hierarchy and pushes the new one.
48 * @param newHierarchyItem The new hierarchy item to be pushed, must be valid
49 */
50 void change(const std::string& newHierarchyItem);
51
52 /**
53 * Explicitly releases the hierarchy (already before the destructor is called).
54 */
55 void release();
56
57 protected:
58
59 /**
60 * Disabled copy constructor.
61 * @param scopedHierarchy The hierarchy object which would have been copied
62 */
63 ScopedHierarchyBase(const ScopedHierarchyBase& scopedHierarchy) = delete;
64
65 /**
66 * Creates a new scoped object and pushes the given hierarchy.
67 * @param owner The owner of the scoped object
68 * @param hierarchyItem The hierarchy item to be pushed, must be valid
69 */
70 inline ScopedHierarchyBase(DebugElements& owner, const std::string& hierarchyItem);
71
72 /**
73 * Disabled copy operator.
74 * @param scopedHierarchy The hierarchy object which would have been copied
75 * @return Reference to this object
76 */
77 ScopedHierarchyBase& operator=(const ScopedHierarchyBase& scopedHierarchy) = delete;
78
79 protected:
80
81 /// The owner of this object.
83
84 /// The hierarchy item of this object.
85 std::string hierarchyItem_;
86 };
87
88 /**
89 * Definition of a vector holding strings (a hierarchy).
90 */
91 typedef std::vector<std::string> Hierarchy;
92
93 /**
94 * Definition of a callback function for an updated debug element.
95 */
97
98 protected:
99
100 /**
101 * Definition of a map mapping hierarchies to frames.
102 */
103 typedef std::map<Hierarchy, Frame> HierarchyMap;
104
105 /**
106 * Definition of a map mapping element ids to hierarchy maps.
107 */
108 typedef std::map<uint32_t, HierarchyMap> ElementMap;
109
110 /**
111 * Definition of a set holding element ids.
112 */
113 typedef std::unordered_set<uint32_t> ElementSet;
114
115 public:
116
117 /**
118 * Returns whether a specific debug elements is activated.
119 * Call this function before preparing the debug information (and updating via updateElement) to reduce the performance overhead when debugging.
120 * @param elementId The id for which the activation state is returned
121 * @return True, if the debug element is activate; False, if the debug element is deactivated
122 */
123 bool isElementActive(const uint32_t elementId) const;
124
125 /**
126 * Activates a specific debug element.
127 * @param elementId The id of the element to activate
128 * @see isElementActivated(), deactivateElement().
129 */
130 void activateElement(const uint32_t elementId);
131
132 /**
133 * Deactivates a specific debug element.
134 * @param elementId The id of the element to activate
135 * @see activateElement().
136 */
137 void deactivateElement(const uint32_t elementId);
138
139 /**
140 * Explicitly activates all elements (to avoid defining all active elements individually).
141 * Beware: Activating a couple of debug elements (and preparing the debug data in particular) can reduce the performance.
142 */
144
145 /**
146 * Pushes a new hierarchy.
147 * @param hierarchyItem The hierarchy item to be pushed
148 */
149 void pushHierarchyItem(const std::string& hierarchyItem);
150
151 /**
152 * Pops the most recent hierarchy item.
153 */
155
156 /**
157 * Updates the debug information of a specific debug element.
158 * In case one (or several) sub-element bits are set, the element with matching sub-element bit mask will be updated only.
159 * Beware: Due to performance reasons, the specific debug element must be activated before it can be updated,<br>
160 * as the preparation of debug information for disabled elements can be skipped.
161 * @param elementId The id of the debug element, should be a value from `DebugId`
162 * @param frame The new debug frame of the debug element, will be moved
163 * @see setSubElementBit(), activateElement().
164 */
165 void updateElement(const uint32_t elementId, Frame&& frame);
166
167 /**
168 * Updates the debug information of a specific debug element.
169 * In case one (or several) sub-element bits are set, the element with matching sub-element bit mask will be updated only.
170 * Beware: Due to performance reasons, the specific debug element must be activated before it can be updated,<br>
171 * as the preparation of debug information for disabled elements can be skipped.
172 * @param elementId The id of the debug element, should be a value from `DebugId`
173 * @param frame The new debug frame of the debug element, will be moved
174 * @param explicitHierarchy The explicit hierarchy which will be used instead of the actual internal known hierarchy
175 * @see setSubElementBit(), activateElement().
176 */
177 void updateElement(const uint32_t elementId, Frame&& frame, const Hierarchy& explicitHierarchy);
178
179 /**
180 * Updates the debug information of a specific debug element.
181 * Beware: Due to performance reasons, the specific debug element must be activated before it can be updated,<br>
182 * as the preparation of debug information for disabled elements can be skipped.
183 * @param elementId The id of the debug element, should be a value from `DebugId`
184 * @param frame The new debug frame of the debug element, will be copied
185 * @see activateElement(), activateAllElements().
186 */
187 void updateElement(const uint32_t elementId, const Frame& frame);
188
189 /**
190 * Updates the debug information of a specific debug element.
191 * Beware: Due to performance reasons, the specific debug element must be activated before it can be updated,<br>
192 * as the preparation of debug information for disabled elements can be skipped.
193 * @param elementId The id of the debug element, should be a value from `DebugId`
194 * @param frame The new debug frame of the debug element, will be copied
195 * @param explicitHierarchy The explicit hierarchy which will be used instead of the actual internal known hierarchy
196 * @see activateElement(), activateAllElements().
197 */
198 void updateElement(const uint32_t elementId, const Frame& frame, const Hierarchy& explicitHierarchy);
199
200 /**
201 * Returns the most recent debug frame of a specific debug element.
202 * If more than one debug element with individual hierarchies exist, one of the elements will be returned.<br>
203 * Beware: Due to performance reasons, the specific debug element must be activated before calling this function.
204 * @param elementId The id for which the frame will be returned
205 * @param popElement True, to remove the debug element; False, to keep the debug element until it gets updated again
206 * @return The copy of the frame which is associated with the id, an invalid frame in case the id does not exist
207 * @see isElementActive().
208 */
209 Frame element(const uint32_t elementId, const bool popElement = false);
210
211 /**
212 * Returns the most recent debug frame of a specific debug element.
213 * Beware: Due to performance reasons, the specific debug element must be activated before calling this function.
214 * @param elementId The id for which the frame will be returned
215 * @param hierarchy The hierarchy for which the element will be returned
216 * @param popElement True, to remove the debug element; False, to keep the debug element until it gets updated again
217 * @return The copy of the frame which is associated with the id, an invalid frame in case the id does not exist
218 * @see isElementActive().
219 */
220 Frame element(const uint32_t elementId, const Hierarchy& hierarchy, const bool popElement = false);
221
222 /**
223 * Returns the most recent debug frame of a specific debug element if the debug element is activate and exists.
224 * If more than one debug element with individual hierarchies exist, one of the elements will be returned.
225 * @param elementId The id for which the frame will be returned
226 * @param frame The resulting copy of the debug frame which is associated with the id
227 * @param popElement True, to remove the debug element; False, to keep the debug element until it gets updated again
228 * @return True, if the debug element is activate and exists
229 */
230 bool elementIfActivate(const uint32_t elementId, Frame& frame, const bool popElement = false);
231
232 /**
233 * Returns the most recent debug frame of a specific debug element if the debug element is activate and exists.
234 * @param elementId The id for which the frame will be returned
235 * @param frame The resulting copy of the debug frame which is associated with the id
236 * @param hierarchy The hierarchy for which the element will be returned
237 * @param popElement True, to remove the debug element; False, to keep the debug element until it gets updated again
238 * @return True, if the debug element is activate and exists
239 */
240 bool elementIfActivate(const uint32_t elementId, Frame& frame, const Hierarchy& hierarchy, const bool popElement = false);
241
242 /**
243 * Returns all existing hierarchies for a given element id.
244 * @param elementId The id for which all hierarchies will be returned
245 * @return The hierarchies of the element, an empty vector if the element does not exist
246 */
247 std::vector<Hierarchy> hierarchies(const uint32_t elementId) const;
248
249 /**
250 * Sets the optional callback function for updated debug element.
251 * The callback function will be called whenever a debug element is updated.
252 * @param callback The callback function to be set, can be invalid to remove a previously set function
253 */
254 inline void setElementUpdateCallback(const ElementUpdateCallback& callback);
255
256 protected:
257
258 /**
259 * Protected default constructor.
260 */
261 inline DebugElements();
262
263 protected:
264
265 /// The set holding activate debug element ids.
267
268 /// True, if all debug elements are activated (this state overrides the individual ids of `activeIds_`).
270
271 /// The map mapping ids to frames.
273
274 /// The current hierarchy.
276
277 /// Optional callback function for updated debug elements.
279
280 /// The data lock.
281 mutable Lock lock_;
282};
283
285 owner_(owner),
286 hierarchyItem_(hierarchyItem)
287{
288 ocean_assert(!hierarchyItem_.empty());
289
291}
292
294{
295 if (!hierarchyItem_.empty())
296 {
297 owner_.popHierarchyItem();
298 }
299}
300
303{
304 // nothing to do here
305}
306
308{
309 const ScopedLock scopedLock(lock_);
310
311 elementUpdateCallback_ = callback;
312}
313
314}
315
316#endif // META_OCEAN_BASE_DEBUG_ELEMENTS_H
This class implements a container for callback functions.
Definition Callback.h:3456
This class implements a scoped hierarchy.
Definition DebugElements.h:37
void release()
Explicitly releases the hierarchy (already before the destructor is called).
~ScopedHierarchyBase()
Releases the hierarchy and disposes this object.
Definition DebugElements.h:293
void change(const std::string &newHierarchyItem)
Explicitly exchanges the hierarchy instead of releasing it and creating a new one.
ScopedHierarchyBase & operator=(const ScopedHierarchyBase &scopedHierarchy)=delete
Disabled copy operator.
DebugElements & owner_
The owner of this object.
Definition DebugElements.h:82
std::string hierarchyItem_
The hierarchy item of this object.
Definition DebugElements.h:85
ScopedHierarchyBase(const ScopedHierarchyBase &scopedHierarchy)=delete
Disabled copy constructor.
This class implements the base class for a container for debug elements.
Definition DebugElements.h:29
std::map< Hierarchy, Frame > HierarchyMap
Definition of a map mapping hierarchies to frames.
Definition DebugElements.h:103
ElementUpdateCallback elementUpdateCallback_
Optional callback function for updated debug elements.
Definition DebugElements.h:278
void updateElement(const uint32_t elementId, Frame &&frame)
Updates the debug information of a specific debug element.
std::map< uint32_t, HierarchyMap > ElementMap
Definition of a map mapping element ids to hierarchy maps.
Definition DebugElements.h:108
ElementMap elementMap_
The map mapping ids to frames.
Definition DebugElements.h:272
void updateElement(const uint32_t elementId, const Frame &frame, const Hierarchy &explicitHierarchy)
Updates the debug information of a specific debug element.
ElementSet activeElements_
The set holding activate debug element ids.
Definition DebugElements.h:266
Frame element(const uint32_t elementId, const Hierarchy &hierarchy, const bool popElement=false)
Returns the most recent debug frame of a specific debug element.
void updateElement(const uint32_t elementId, Frame &&frame, const Hierarchy &explicitHierarchy)
Updates the debug information of a specific debug element.
bool elementIfActivate(const uint32_t elementId, Frame &frame, const Hierarchy &hierarchy, const bool popElement=false)
Returns the most recent debug frame of a specific debug element if the debug element is activate and ...
Lock lock_
The data lock.
Definition DebugElements.h:281
Hierarchy hierarchy_
The current hierarchy.
Definition DebugElements.h:275
DebugElements()
Protected default constructor.
Definition DebugElements.h:301
void setElementUpdateCallback(const ElementUpdateCallback &callback)
Sets the optional callback function for updated debug element.
Definition DebugElements.h:307
void pushHierarchyItem(const std::string &hierarchyItem)
Pushes a new hierarchy.
bool isElementActive(const uint32_t elementId) const
Returns whether a specific debug elements is activated.
void deactivateElement(const uint32_t elementId)
Deactivates a specific debug element.
Frame element(const uint32_t elementId, const bool popElement=false)
Returns the most recent debug frame of a specific debug element.
bool elementIfActivate(const uint32_t elementId, Frame &frame, const bool popElement=false)
Returns the most recent debug frame of a specific debug element if the debug element is activate and ...
void activateAllElements()
Explicitly activates all elements (to avoid defining all active elements individually).
void updateElement(const uint32_t elementId, const Frame &frame)
Updates the debug information of a specific debug element.
void activateElement(const uint32_t elementId)
Activates a specific debug element.
std::vector< Hierarchy > hierarchies(const uint32_t elementId) const
Returns all existing hierarchies for a given element id.
void popHierarchyItem()
Pops the most recent hierarchy item.
std::vector< std::string > Hierarchy
Definition of a vector holding strings (a hierarchy).
Definition DebugElements.h:91
bool allElementsActivate_
True, if all debug elements are activated (this state overrides the individual ids of activeIds_).
Definition DebugElements.h:269
std::unordered_set< uint32_t > ElementSet
Definition of a set holding element ids.
Definition DebugElements.h:113
Callback< void, const uint32_t, const Frame *, const Hierarchy * > ElementUpdateCallback
Definition of a callback function for an updated debug element.
Definition DebugElements.h:96
This class implements Ocean's image class.
Definition Frame.h:1808
This class implements a recursive lock object.
Definition Lock.h:31
This class implements a scoped lock object for recursive lock objects.
Definition Lock.h:135
The namespace covering the entire Ocean framework.
Definition Accessor.h:15