u/var: Use a struct to carry information in root object callbacks

This commit is contained in:
Jakob Bornecrantz 2023-06-22 16:19:14 +01:00
parent 7ef4007196
commit ea77386887
3 changed files with 30 additions and 12 deletions

View file

@ -41,6 +41,7 @@ class Obj
{ {
public: public:
std::string name = {}; std::string name = {};
struct u_var_root_info info = {};
std::vector<Var> vars = {}; std::vector<Var> vars = {};
}; };
@ -50,17 +51,17 @@ public:
class Tracker class Tracker
{ {
public: public:
std::unordered_map<std::string, size_t> counters = {}; std::unordered_map<std::string, uint32_t> counters = {};
std::unordered_map<ptrdiff_t, Obj> map = {}; std::unordered_map<ptrdiff_t, Obj> map = {};
bool on = false; bool on = false;
bool tested = false; bool tested = false;
public: public:
int uint32_t
getNumber(const std::string &name) getNumber(const std::string &name)
{ {
auto s = counters.find(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; counters[name] = count;
return 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); auto name = std::string(c_name);
uint32_t count = 0; // Zero means no number.
if (suffix_with_number) { if (suffix_with_number) {
int count = gTracker.getNumber(name); count = gTracker.getNumber(name);
std::stringstream ss; std::stringstream ss;
ss << name << " #" << count; 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(); auto &obj = gTracker.map[(ptrdiff_t)root] = Obj();
obj.name = name; obj.name = name;
obj.info.name = obj.name.c_str();
obj.info.number = count;
} }
extern "C" void 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) { for (Obj *obj : tmp) {
enter_cb(obj->name.c_str(), priv); enter_cb(&obj->info, priv);
for (auto &var : obj->vars) { for (auto &var : obj->vars) {
elem_cb(&var.info, priv); elem_cb(&var.info, priv);
} }
exit_cb(obj->name.c_str(), priv); exit_cb(&obj->info, priv);
} }
} }

View file

@ -276,12 +276,26 @@ struct u_var_info
} gui; } 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. * Callback for entering and leaving root nodes.
* *
* @ingroup aux_util * @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. * Callback on each variable a root node has.

View file

@ -323,13 +323,13 @@ on_draggable_u16_var(const char *name, void *ptr)
} }
static void 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; struct draw_state *state = (struct draw_state *)priv;
state->vis_i = 0; state->vis_i = 0;
state->vis_stack[0] = true; state->vis_stack[0] = true;
igBegin(name, NULL, 0); igBegin(info->name, NULL, 0);
} }
static float static float
@ -493,7 +493,7 @@ on_elem(struct u_var_info *info, void *priv)
} }
static void 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; struct draw_state *state = (struct draw_state *)priv;
assert(state->vis_i == 0 && "Unbalanced GUI_HEADER_BEGIN/END pairs"); 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 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 static void
@ -529,7 +529,7 @@ on_elem_sink_debug_remove(struct u_var_info *info, void *priv)
} }
static void static void
on_root_exit_sink(const char *name, void *priv) on_root_exit_sink(struct u_var_root_info *info, void *priv)
{} {}