aboutsummaryrefslogtreecommitdiff
path: root/include/cru/ui/controls/SingleChildControl.h
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2025-11-21 21:43:42 +0800
committerYuqian Yang <crupest@crupest.life>2025-11-21 21:43:42 +0800
commit3b875091c445b7465b9bd044914318989a94d2ad (patch)
treea358aebb488ec1ddc86bf87b8038bacd5d7515cb /include/cru/ui/controls/SingleChildControl.h
parent3cda35dbcbbe1e3854b880169c0efa0fc7a79264 (diff)
downloadcru-3b875091c445b7465b9bd044914318989a94d2ad.tar.gz
cru-3b875091c445b7465b9bd044914318989a94d2ad.tar.bz2
cru-3b875091c445b7465b9bd044914318989a94d2ad.zip
Clean codes. Remove member function const.
Diffstat (limited to 'include/cru/ui/controls/SingleChildControl.h')
-rw-r--r--include/cru/ui/controls/SingleChildControl.h29
1 files changed, 17 insertions, 12 deletions
diff --git a/include/cru/ui/controls/SingleChildControl.h b/include/cru/ui/controls/SingleChildControl.h
index f748a38d..684b8403 100644
--- a/include/cru/ui/controls/SingleChildControl.h
+++ b/include/cru/ui/controls/SingleChildControl.h
@@ -1,41 +1,46 @@
#pragma once
#include "Control.h"
+#include <cassert>
+
namespace cru::ui::controls {
template <typename TRenderObject>
class SingleChildControl : public Control {
protected:
- SingleChildControl() : container_render_object_(new TRenderObject()) {
- container_render_object_->SetAttachedControl(this);
+ SingleChildControl(std::string name) : Control(std::move(name)) {
+ container_render_object_.SetAttachedControl(this);
}
public:
Control* GetChild() {
- return GetChildren().empty() ? nullptr : GetChildren().front();
+ const auto& children = GetChildren();
+ assert(children.empty() || children.size() == 1);
+ return children.empty() ? nullptr : children.front();
}
void SetChild(Control* child) {
- if (GetChild() == child) return;
- if (!GetChildren().empty()) {
+ auto old_child = GetChild();
+ if (old_child == child) return;
+ if (old_child) {
RemoveChildAt(0);
}
if (child) {
InsertChildAt(child, 0);
}
- container_render_object_->SetChild(
- child == nullptr ? nullptr : child->GetRenderObject());
+ container_render_object_.SetChild(child ? child->GetRenderObject()
+ : nullptr);
}
- render::RenderObject* GetRenderObject() const override {
- return container_render_object_.get();
+ render::RenderObject* GetRenderObject() override {
+ return &container_render_object_;
}
- TRenderObject* GetContainerRenderObject() const {
- return container_render_object_.get();
+ TRenderObject* GetContainerRenderObject() {
+ return &container_render_object_;
}
private:
- std::unique_ptr<TRenderObject> container_render_object_;
+ TRenderObject container_render_object_;
};
} // namespace cru::ui::controls