Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef ABSTRACT_WIDGETS_H_
- #define ABSTRACT_WIDGETS_H_
- #include <cassert>
- #include <memory>
- #include <vector>
- #include "iostream"
- namespace widgets {
- struct container;
- struct ParentGuard {
- friend container;
- private:
- explicit ParentGuard() = default;
- };
- // NOLINTNEXTLINE
- struct widget {
- private:
- container *father{nullptr};
- protected:
- widget() : w_width(0), w_height(0){};
- // NOLINTNEXTLINE
- int w_width;
- // NOLINTNEXTLINE
- int w_height;
- public:
- [[nodiscard]] virtual int width() const = 0;
- [[nodiscard]] virtual int height() const = 0;
- [[nodiscard]] virtual widget *child_at(int x, int y) {
- if (x >= 0 && x < width() && y >= 0 && y < height()) {
- return this;
- }
- return nullptr;
- };
- widget(const widget &) = delete;
- widget(widget &&) = delete;
- widget &operator=(const widget &) = delete;
- widget &operator=(widget &&) = delete;
- virtual ~widget() = default;
- // NOLINTNEXTLINE
- void set_parent(container *input, const ParentGuard &) {
- father = input;
- }
- [[nodiscard]] container *parent() const {
- return father;
- }
- };
- struct container : widget {
- virtual void update_layout() = 0;
- private:
- static const ParentGuard parent_guard;
- protected:
- void add_parent(widget *child) {
- child->set_parent(static_cast<container *>(this), parent_guard);
- }
- static void remove_parent(widget *child) {
- child->set_parent(nullptr, parent_guard);
- }
- };
- inline const widgets::ParentGuard widgets::container::parent_guard{};
- } // namespace widgets
- #endif // ABSTRACT_WIDGETS_H_
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement