From ea773868877d293a5583036752999f41f75a4967 Mon Sep 17 00:00:00 2001 From: Jakob Bornecrantz Date: Thu, 22 Jun 2023 16:19:14 +0100 Subject: [PATCH] u/var: Use a struct to carry information in root object callbacks --- src/xrt/auxiliary/util/u_var.cpp | 16 ++++++++++------ src/xrt/auxiliary/util/u_var.h | 16 +++++++++++++++- src/xrt/state_trackers/gui/gui_scene_debug.c | 10 +++++----- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/xrt/auxiliary/util/u_var.cpp b/src/xrt/auxiliary/util/u_var.cpp index 57bcfcfc4..8e132a141 100644 --- a/src/xrt/auxiliary/util/u_var.cpp +++ b/src/xrt/auxiliary/util/u_var.cpp @@ -41,6 +41,7 @@ class Obj { public: std::string name = {}; + struct u_var_root_info info = {}; std::vector vars = {}; }; @@ -50,17 +51,17 @@ public: class Tracker { public: - std::unordered_map counters = {}; + std::unordered_map counters = {}; std::unordered_map map = {}; bool on = false; bool tested = false; public: - int + uint32_t getNumber(const std::string &name) { auto s = counters.find(name); - int count = int(s != counters.end() ? s->second : 0) + 1; + uint32_t count = (s != counters.end() ? s->second : 0u) + 1u; counters[name] = count; return count; @@ -129,9 +130,10 @@ u_var_add_root(void *root, const char *c_name, bool suffix_with_number) } auto name = std::string(c_name); + uint32_t count = 0; // Zero means no number. if (suffix_with_number) { - int count = gTracker.getNumber(name); + count = gTracker.getNumber(name); std::stringstream ss; ss << name << " #" << count; @@ -140,6 +142,8 @@ u_var_add_root(void *root, const char *c_name, bool suffix_with_number) auto &obj = gTracker.map[(ptrdiff_t)root] = Obj(); obj.name = name; + obj.info.name = obj.name.c_str(); + obj.info.number = count; } extern "C" void @@ -172,13 +176,13 @@ u_var_visit(u_var_root_cb enter_cb, u_var_root_cb exit_cb, u_var_elm_cb elem_cb, } for (Obj *obj : tmp) { - enter_cb(obj->name.c_str(), priv); + enter_cb(&obj->info, priv); for (auto &var : obj->vars) { elem_cb(&var.info, priv); } - exit_cb(obj->name.c_str(), priv); + exit_cb(&obj->info, priv); } } diff --git a/src/xrt/auxiliary/util/u_var.h b/src/xrt/auxiliary/util/u_var.h index 417ea3172..140bfa2da 100644 --- a/src/xrt/auxiliary/util/u_var.h +++ b/src/xrt/auxiliary/util/u_var.h @@ -276,12 +276,26 @@ struct u_var_info } gui; }; +/*! + * Struct containing the information about a root object. + * + * @ingroup aux_util + */ +struct u_var_root_info +{ + //! The displayed name. + const char *name; + + //! The number of the window, or zero. + uint32_t number; +}; + /*! * Callback for entering and leaving root nodes. * * @ingroup aux_util */ -typedef void (*u_var_root_cb)(const char *, void *); +typedef void (*u_var_root_cb)(struct u_var_root_info *info, void *); /*! * Callback on each variable a root node has. diff --git a/src/xrt/state_trackers/gui/gui_scene_debug.c b/src/xrt/state_trackers/gui/gui_scene_debug.c index 91f3c5e3d..f2161fb6c 100644 --- a/src/xrt/state_trackers/gui/gui_scene_debug.c +++ b/src/xrt/state_trackers/gui/gui_scene_debug.c @@ -323,13 +323,13 @@ on_draggable_u16_var(const char *name, void *ptr) } static void -on_root_enter(const char *name, void *priv) +on_root_enter(struct u_var_root_info *info, void *priv) { struct draw_state *state = (struct draw_state *)priv; state->vis_i = 0; state->vis_stack[0] = true; - igBegin(name, NULL, 0); + igBegin(info->name, NULL, 0); } static float @@ -493,7 +493,7 @@ on_elem(struct u_var_info *info, void *priv) } static void -on_root_exit(const char *name, void *priv) +on_root_exit(struct u_var_root_info *info, void *priv) { struct draw_state *state = (struct draw_state *)priv; assert(state->vis_i == 0 && "Unbalanced GUI_HEADER_BEGIN/END pairs"); @@ -511,7 +511,7 @@ on_root_exit(const char *name, void *priv) */ static void -on_root_enter_sink(const char *name, void *priv) +on_root_enter_sink(struct u_var_root_info *info, void *priv) {} static void @@ -529,7 +529,7 @@ on_elem_sink_debug_remove(struct u_var_info *info, void *priv) } static void -on_root_exit_sink(const char *name, void *priv) +on_root_exit_sink(struct u_var_root_info *info, void *priv) {}