Skip to content

Commit

Permalink
cla-71: created intermediate class constrained_container, base class …
Browse files Browse the repository at this point in the history
…of layout_container now (#40)
  • Loading branch information
ebasconp authored Sep 27, 2024
1 parent 4b940ab commit 67b101d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 64 deletions.
82 changes: 82 additions & 0 deletions code/classeine-lib/clsn/ui/constrained_container.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// This file belongs to the Classeine project
//
// SPDX-License-Identifier: BSD-3-Clause
// SPDX-FileCopyrightText: © 2024 Ernesto Bascón Pantoja

#pragma once

#include <clsn/ui/container.h>

#include <clsn/ui/window.h>

namespace clsn::ui
{
template <typename Constraint>
class constrained_container_info final
{
std::shared_ptr<control> m_control_ptr;
Constraint m_constraint;

public:
constrained_container_info(std::shared_ptr<control> ctrl, Constraint constraint)
: m_control_ptr{std::move(ctrl)}
, m_constraint{constraint}
{
}

[[nodiscard]] auto get_control_ptr() { return m_control_ptr; }
[[nodiscard]] auto get_control_ptr() const -> const std::shared_ptr<control>& { return m_control_ptr; }

[[nodiscard]] auto get_constraint() const -> const Constraint& { return m_constraint; }
[[nodiscard]] auto get_constraint() -> Constraint& { return m_constraint; }
};

template <typename Constraint>
class constrained_container : public container<constrained_container_info<Constraint>>
{
public:
using constraint = Constraint;
using container_info = constrained_container_info<constraint>;

explicit constrained_container(std::string_view section_name)
: container<container_info>{section_name}
{
}

~constrained_container() override = default;

template <typename ControlType, typename... Args>
std::shared_ptr<ControlType> make_and_add(Args&&... args)
{
auto ptr = control::make<ControlType>();
add(ptr, std::forward<Args>(args)...);

return ptr;
}

template <typename... Args>
void add(std::shared_ptr<control> ctrl, Args&&... args)
{
constraint _constraint{std::forward<Args>(args)...};
check_if_valid_before_adding(_constraint);
this->add_element(ctrl, std::move(_constraint));
this->init_new_control(*ctrl);
}

auto to_control(container_info& e) -> control& override
{
return *e.get_control_ptr();
}

auto to_control(const container_info& e) const -> const control& override
{
return *e.get_control_ptr();
}

protected:
virtual void check_if_valid_before_adding(const constraint&) const
{
// Do nothing here
}
};
}
69 changes: 5 additions & 64 deletions code/classeine-lib/clsn/ui/layout_container.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,22 @@

#pragma once

#include <clsn/ui/container.h>

#include <clsn/ui/window.h>
#include <clsn/ui/constrained_container.h>

#include <clsn/ui/layouts/layout_utils.h>

namespace clsn::ui
{
template <typename Constraint>
class layout_container_control_and_constraint final
{
std::shared_ptr<control> m_control_ptr;
Constraint m_constraint;

public:
layout_container_control_and_constraint(std::shared_ptr<control> ctrl, Constraint constraint)
: m_control_ptr{std::move(ctrl)}
, m_constraint{constraint}
{
}

[[nodiscard]] auto get_control_ptr() { return m_control_ptr; }
[[nodiscard]] auto get_control_ptr() const -> const std::shared_ptr<control>& { return m_control_ptr; }

[[nodiscard]] auto get_constraint() const -> const Constraint& { return m_constraint; }
[[nodiscard]] auto get_constraint() -> Constraint& { return m_constraint; }
};

template <typename Layout>
class layout_container : public container<layout_container_control_and_constraint<typename Layout::constraint_type>>
class layout_container : public constrained_container<typename Layout::constraint_type>
{
public:
using constraint = typename Layout::constraint_type;
using control_and_constraint = layout_container_control_and_constraint<constraint>;

private:
Layout m_layout;

public:
using constraint_type = typename Layout::constraint_type;

explicit layout_container(std::string_view section_name)
: container<control_and_constraint>{section_name}
: constrained_container<constraint_type>{section_name}
{
}

Expand All @@ -61,24 +36,6 @@ namespace clsn::ui
return m_layout;
}

template <typename ControlType, typename... Args>
std::shared_ptr<ControlType> make_and_add(Args&&... args)
{
auto ptr = control::make<ControlType>();
add(ptr, std::forward<Args>(args)...);

return ptr;
}

template <typename... Args>
void add(std::shared_ptr<control> ctrl, Args&&... args)
{
constraint _constraint{std::forward<Args>(args)...};
check_if_valid_before_adding(_constraint);
this->add_element(ctrl, std::move(_constraint));
this->init_new_control(*ctrl);
}

void do_layout() override
{
if (this->get_visible_count() == 0)
Expand All @@ -103,21 +60,5 @@ namespace clsn::ui
j++;
}
}

auto to_control(control_and_constraint& e) -> control& override
{
return *e.get_control_ptr();
}

auto to_control(const control_and_constraint& e) const -> const control& override
{
return *e.get_control_ptr();
}

protected:
virtual void check_if_valid_before_adding(const constraint&) const
{
// Do nothing here
}
};
}

0 comments on commit 67b101d

Please sign in to comment.