Advertisement
kolbka_

Untitled

Dec 5th, 2021
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1.  
  2. #ifndef ABSTRACT_WIDGETS_H_
  3. #define ABSTRACT_WIDGETS_H_
  4.  
  5. #include <cassert>
  6. #include <memory>
  7. #include <vector>
  8. #include "iostream"
  9.  
  10. namespace widgets {
  11. struct container;
  12.  
  13. struct ParentGuard {
  14. friend container;
  15.  
  16. private:
  17. explicit ParentGuard() = default;
  18. };
  19.  
  20. // NOLINTNEXTLINE
  21. struct widget {
  22. private:
  23. container *father{nullptr};
  24.  
  25. protected:
  26. widget() : w_width(0), w_height(0){};
  27. // NOLINTNEXTLINE
  28. int w_width;
  29. // NOLINTNEXTLINE
  30. int w_height;
  31.  
  32. public:
  33. [[nodiscard]] virtual int width() const = 0;
  34. [[nodiscard]] virtual int height() const = 0;
  35. [[nodiscard]] virtual widget *child_at(int x, int y) {
  36. if (x >= 0 && x < width() && y >= 0 && y < height()) {
  37. return this;
  38. }
  39. return nullptr;
  40. };
  41. widget(const widget &) = delete;
  42. widget(widget &&) = delete;
  43. widget &operator=(const widget &) = delete;
  44. widget &operator=(widget &&) = delete;
  45. virtual ~widget() = default;
  46. // NOLINTNEXTLINE
  47.  
  48. void set_parent(container *input, const ParentGuard &) {
  49. father = input;
  50. }
  51.  
  52. [[nodiscard]] container *parent() const {
  53. return father;
  54. }
  55. };
  56.  
  57. struct container : widget {
  58. virtual void update_layout() = 0;
  59.  
  60. private:
  61. static const ParentGuard parent_guard;
  62.  
  63. protected:
  64. void add_parent(widget *child) {
  65. child->set_parent(static_cast<container *>(this), parent_guard);
  66. }
  67.  
  68. static void remove_parent(widget *child) {
  69. child->set_parent(nullptr, parent_guard);
  70. }
  71. };
  72.  
  73. inline const widgets::ParentGuard widgets::container::parent_guard{};
  74. } // namespace widgets
  75.  
  76. #endif // ABSTRACT_WIDGETS_H_
  77.  
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement