diff options
Diffstat (limited to 'include/cru/ui/controls/SingleChildControl.h')
| -rw-r--r-- | include/cru/ui/controls/SingleChildControl.h | 29 |
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 |
