Skip to content

Commit

Permalink
[fix] memory issues, God
Browse files Browse the repository at this point in the history
  • Loading branch information
MrsRina committed Aug 23, 2024
1 parent 1074fd9 commit fc87952
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 9 deletions.
3 changes: 2 additions & 1 deletion devlog/commit.txt
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,5 @@
-- Deprecated `immut` widgets (I do not think we should do this for now).
-- Renamed `uint16_t` (flags) to `ekg::flags`.
-- Dynamic type infer in `ekg::slide<t>` call.
-- Added "box" option to `ekg::value_t<t, s>` (I know it is not how burrow and box from Rust works, just a stupid name).
-- Added "box" option to `ekg::value_t<t, s>` (I know it is not how burrow and box from Rust works, just a stupid name).
-- Fixed memory issues from `ekg::value_t<t, s>` but I do not believe it is totally safe, God, my brain can not figure what must do.
3 changes: 2 additions & 1 deletion include/ekg/ekg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ namespace ekg {
p_ui->set_font_size(ekg::font::normal);
p_ui->set_axis(ekg::axis::horizontal);
p_ui->box(new ekg::ui::slider::serializer_t<t>());
p_ui->reset_ownership();
p_ui->unsafe_set_number(ekg::retreive_number_type_from_var_type<t>());
p_ui->unsafe_set_type(ekg::type::slider);
p_ui->set_place(dock);
Expand Down Expand Up @@ -188,7 +189,7 @@ namespace ekg {
*/
ekg::ui::listbox *listbox(
std::string_view tag,
ekg::item item_list,
const ekg::item &item_list,
ekg::flags dock = ekg::dock::none
);

Expand Down
1 change: 0 additions & 1 deletion include/ekg/ui/slider/ui_slider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ namespace ekg::ui {
serialized_range.min = min;
serialized_range.max = max;
serialized_range.value.registry(this);
serialized_range.value = {};

return serialized_range;
};
Expand Down
17 changes: 15 additions & 2 deletions include/ekg/util/io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,16 @@ namespace ekg {
struct value_t {
protected:
bool changed {};
bool not_self_ownership {};
protected:
s *p_mommy_s {};
public:
t self {};
t *p_value {};
t *p_value {nullptr};
public:
value_t<t, s>() {
this->p_value = &this->self;
}
};

operator bool() {
return this->changed;
Expand All @@ -257,11 +258,13 @@ namespace ekg {

s *transfer_ownership(t *p_address) {
this->p_value = p_address;
this->not_self_ownership = true;
return this->p_mommy_s;
}

s *reset_ownership() {
this->p_value = &this->self;
this->not_self_ownership = false;
return this->p_mommy_s;
}

Expand All @@ -285,6 +288,16 @@ namespace ekg {
this->changed = false;
return was;
}

bool was_not_ownership_transfered() {
return this->not_self_ownership;
}

void align_ownership_mem_if_necessary() {
if (!this->not_self_ownership) {
this->p_value = &this->self;
}
}
};

enum attr {
Expand Down
8 changes: 7 additions & 1 deletion src/ekg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ ekg::ui::frame *ekg::frame(std::string_view tag, const ekg::vec2 &size, ekg::fla
ekg::ui::button *ekg::button(std::string_view text, ekg::flags dock) {
ekg::ui::button *p_ui {new ekg::ui::button()};
p_ui->registry(p_ui);
p_ui->reset_ownership();
p_ui->unsafe_set_type(ekg::type::button);
ekg::core->gen_widget(p_ui);

Expand All @@ -161,6 +162,7 @@ ekg::ui::button *ekg::button(std::string_view text, ekg::flags dock) {
ekg::ui::label *ekg::label(std::string_view text, ekg::flags dock) {
ekg::ui::label *p_ui {new ekg::ui::label()};
p_ui->registry(p_ui);
p_ui->reset_ownership();
p_ui->unsafe_set_type(ekg::type::label);
ekg::core->gen_widget(p_ui);

Expand All @@ -177,6 +179,7 @@ ekg::ui::label *ekg::label(std::string_view text, ekg::flags dock) {
ekg::ui::checkbox *ekg::checkbox(std::string_view text, bool value, ekg::flags dock) {
ekg::ui::checkbox *p_ui {new ekg::ui::checkbox()};
p_ui->registry(p_ui);
p_ui->reset_ownership();
p_ui->unsafe_set_type(ekg::type::checkbox);
ekg::core->gen_widget(p_ui);

Expand Down Expand Up @@ -220,6 +223,7 @@ ekg::popup(std::string_view tag, const std::vector<std::string> &component_list,
ekg::ui::textbox *ekg::textbox(std::string_view tag, std::string_view text, ekg::flags dock) {
ekg::ui::textbox *p_ui {new ekg::ui::textbox()};
p_ui->registry(p_ui);
p_ui->reset_ownership();
p_ui->unsafe_set_type(ekg::type::textbox);
ekg::core->gen_widget(p_ui);

Expand All @@ -236,11 +240,12 @@ ekg::ui::textbox *ekg::textbox(std::string_view tag, std::string_view text, ekg:

ekg::ui::listbox *ekg::listbox(
std::string_view tag,
ekg::item item_list,
const ekg::item &item_list,
ekg::flags dock
) {
ekg::ui::listbox *p_ui {new ekg::ui::listbox()};
p_ui->registry(p_ui);
p_ui->reset_ownership();
p_ui->unsafe_set_type(ekg::type::listbox);
ekg::core->gen_widget(p_ui);

Expand All @@ -262,6 +267,7 @@ ekg::ui::listbox *ekg::listbox_container(
) {
ekg::ui::listbox *p_ui {new ekg::ui::listbox()};
p_ui->registry(p_ui);
p_ui->reset_ownership();

p_ui->unsafe_set_type(ekg::type::listbox);
ekg::core->gen_widget(p_ui);
Expand Down
32 changes: 31 additions & 1 deletion src/ui/slider/ui_slider_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ float ekg::ui::slider_widget_calculate_target_pos(
};

ekg::ui::slider::range_t<double> &range {p_serializer->range_list.at(index)};
range.value.align_ownership_mem_if_necessary();
return (dimension * (range.value.get_value() - range.min) / (range.max - range.min));
}

Expand All @@ -249,6 +250,7 @@ float ekg::ui::slider_widget_calculate_target_pos(
};

ekg::ui::slider::range_t<float> &range {p_serializer->range_list.at(index)};
range.value.align_ownership_mem_if_necessary();
return (dimension * (range.value.get_value() - range.min) / (range.max - range.min));
}

Expand All @@ -258,6 +260,7 @@ float ekg::ui::slider_widget_calculate_target_pos(
};

ekg::ui::slider::range_t<int64_t> &range {p_serializer->range_list.at(index)};
range.value.align_ownership_mem_if_necessary();
return (dimension * (range.value.get_value() - range.min) / (range.max - range.min));
}

Expand All @@ -267,6 +270,7 @@ float ekg::ui::slider_widget_calculate_target_pos(
};

ekg::ui::slider::range_t<uint64_t> &range {p_serializer->range_list.at(index)};
range.value.align_ownership_mem_if_necessary();
return (dimension * (range.value.get_value() - range.min) / (range.max - range.min));
}

Expand All @@ -276,6 +280,7 @@ float ekg::ui::slider_widget_calculate_target_pos(
};

ekg::ui::slider::range_t<int32_t> &range {p_serializer->range_list.at(index)};
range.value.align_ownership_mem_if_necessary();
return (dimension * (range.value.get_value() - range.min) / (range.max - range.min));
}

Expand All @@ -285,6 +290,7 @@ float ekg::ui::slider_widget_calculate_target_pos(
};

ekg::ui::slider::range_t<uint32_t> &range {p_serializer->range_list.at(index)};
range.value.align_ownership_mem_if_necessary();
return (dimension * (range.value.get_value() - range.min) / (range.max - range.min));
}

Expand All @@ -294,6 +300,7 @@ float ekg::ui::slider_widget_calculate_target_pos(
};

ekg::ui::slider::range_t<int16_t> &range {p_serializer->range_list.at(index)};
range.value.align_ownership_mem_if_necessary();
return (dimension * (range.value.get_value() - range.min) / (range.max - range.min));
}

Expand All @@ -303,6 +310,7 @@ float ekg::ui::slider_widget_calculate_target_pos(
};

ekg::ui::slider::range_t<uint16_t> &range {p_serializer->range_list.at(index)};
range.value.align_ownership_mem_if_necessary();
return (dimension * (range.value.get_value() - range.min) / (range.max - range.min));
}

Expand All @@ -312,6 +320,7 @@ float ekg::ui::slider_widget_calculate_target_pos(
};

ekg::ui::slider::range_t<int8_t> &range {p_serializer->range_list.at(index)};
range.value.align_ownership_mem_if_necessary();
return (dimension * (range.value.get_value() - range.min) / (range.max - range.min));
}

Expand All @@ -321,6 +330,7 @@ float ekg::ui::slider_widget_calculate_target_pos(
};

ekg::ui::slider::range_t<uint8_t> &range {p_serializer->range_list.at(index)};
range.value.align_ownership_mem_if_necessary();
return (dimension * (range.value.get_value() - range.min) / (range.max - range.min));
}
}
Expand All @@ -336,6 +346,8 @@ void ekg::ui::slider_widget_calculate_value(ekg::feature *&p_feature, ekg::numbe
};

ekg::ui::slider::range_t<double> &range {p_serializer->range_list.at(index)};
range.value.align_ownership_mem_if_necessary();

if (factor == 0) {
range.value.set_value(range.min);
} else {
Expand All @@ -346,10 +358,12 @@ void ekg::ui::slider_widget_calculate_value(ekg::feature *&p_feature, ekg::numbe

case ekg::number::float32: {
ekg::ui::slider::serializer_t<float> *p_serializer {
static_cast<ekg::ui::slider::serializer_t<float>*>(p_feature)
(ekg::ui::slider::serializer_t<float>*) p_feature
};

ekg::ui::slider::range_t<float> &range {p_serializer->range_list.at(index)};
range.value.align_ownership_mem_if_necessary();

if (factor == 0) {
range.value.set_value(range.min);
} else {
Expand All @@ -365,6 +379,8 @@ void ekg::ui::slider_widget_calculate_value(ekg::feature *&p_feature, ekg::numbe
};

ekg::ui::slider::range_t<int64_t> &range {p_serializer->range_list.at(index)};
range.value.align_ownership_mem_if_necessary();

if (factor == 0) {
range.value.set_value(range.min);
} else {
Expand All @@ -379,6 +395,8 @@ void ekg::ui::slider_widget_calculate_value(ekg::feature *&p_feature, ekg::numbe
};

ekg::ui::slider::range_t<uint64_t> &range {p_serializer->range_list.at(index)};
range.value.align_ownership_mem_if_necessary();

if (factor == 0) {
range.value.set_value(range.min);
} else {
Expand All @@ -393,6 +411,8 @@ void ekg::ui::slider_widget_calculate_value(ekg::feature *&p_feature, ekg::numbe
};

ekg::ui::slider::range_t<int32_t> &range {p_serializer->range_list.at(index)};
range.value.align_ownership_mem_if_necessary();

if (factor == 0) {
range.value.set_value(range.min);
} else {
Expand All @@ -407,6 +427,8 @@ void ekg::ui::slider_widget_calculate_value(ekg::feature *&p_feature, ekg::numbe
};

ekg::ui::slider::range_t<uint32_t> &range {p_serializer->range_list.at(index)};
range.value.align_ownership_mem_if_necessary();

if (factor == 0) {
range.value.set_value(range.min);
} else {
Expand All @@ -421,6 +443,8 @@ void ekg::ui::slider_widget_calculate_value(ekg::feature *&p_feature, ekg::numbe
};

ekg::ui::slider::range_t<int16_t> &range {p_serializer->range_list.at(index)};
range.value.align_ownership_mem_if_necessary();

if (factor == 0) {
range.value.set_value(range.min);
} else {
Expand All @@ -435,6 +459,8 @@ void ekg::ui::slider_widget_calculate_value(ekg::feature *&p_feature, ekg::numbe
};

ekg::ui::slider::range_t<uint16_t> &range {p_serializer->range_list.at(index)};
range.value.align_ownership_mem_if_necessary();

if (factor == 0) {
range.value.set_value(range.min);
} else {
Expand All @@ -449,6 +475,8 @@ void ekg::ui::slider_widget_calculate_value(ekg::feature *&p_feature, ekg::numbe
};

ekg::ui::slider::range_t<int8_t> &range {p_serializer->range_list.at(index)};
range.value.align_ownership_mem_if_necessary();

if (factor == 0) {
range.value.set_value(range.min);
} else {
Expand All @@ -463,6 +491,8 @@ void ekg::ui::slider_widget_calculate_value(ekg::feature *&p_feature, ekg::numbe
};

ekg::ui::slider::range_t<uint8_t> &range {p_serializer->range_list.at(index)};
range.value.align_ownership_mem_if_necessary();

if (factor == 0) {
range.value.set_value(range.min);
} else {
Expand Down
5 changes: 3 additions & 2 deletions test/src/ekg_gui_showcase_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,11 +646,12 @@ int32_t showcase_useless_window() {
"meow",
30.0f,
10.0f,
100.0f,
20.0f,
ekg::dock::fill | ekg::dock::next
);

p->range<float>(1, -10.0f, 100.0f);
p->range<float>(1, 0.0f, 1.0f);
p->range<float>(2, -10.0f, 200.0f);

ekg::button("Dead-allocate the instance of life", ekg::dock::fill | ekg::dock::next)
->set_task(
Expand Down

0 comments on commit fc87952

Please sign in to comment.