object.hpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #if defined(Hiro_Object)
  2. struct mObject {
  3. Declare(Object)
  4. mObject();
  5. virtual ~mObject();
  6. mObject(const mObject&) = delete;
  7. mObject& operator=(const mObject&) = delete;
  8. explicit operator bool() const;
  9. auto abstract() const -> bool;
  10. auto adjustOffset(int displacement) -> type&;
  11. auto enabled(bool recursive = false) const -> bool;
  12. virtual auto focused() const -> bool;
  13. auto font(bool recursive = false) const -> Font;
  14. virtual auto group() const -> Group;
  15. auto offset() const -> int;
  16. auto parent() const -> mObject*;
  17. auto parentComboButton(bool recursive = false) const -> mComboButton*;
  18. auto parentComboEdit(bool recursive = false) const -> mComboEdit*;
  19. auto parentFrame(bool recursive = false) const -> mFrame*;
  20. auto parentIconView(bool recursive = false) const -> mIconView*;
  21. auto parentMenu(bool recursive = false) const -> mMenu*;
  22. auto parentMenuBar(bool recursive = false) const -> mMenuBar*;
  23. auto parentPopupMenu(bool recursive = false) const -> mPopupMenu*;
  24. auto parentSizable(bool recursive = false) const -> mSizable*;
  25. auto parentTabFrame(bool recursive = false) const -> mTabFrame*;
  26. auto parentTabFrameItem(bool recursive = false) const -> mTabFrameItem*;
  27. auto parentTableView(bool recursive = false) const -> mTableView*;
  28. auto parentTableViewItem(bool recursive = false) const -> mTableViewItem*;
  29. auto parentTreeView(bool recursive = false) const -> mTreeView*;
  30. auto parentTreeViewItem(bool recursive = false) const -> mTreeViewItem*;
  31. auto parentWidget(bool recursive = false) const -> mWidget*;
  32. auto parentWindow(bool recursive = false) const -> mWindow*;
  33. virtual auto remove() -> type&;
  34. virtual auto reset() -> type&;
  35. virtual auto setEnabled(bool enabled = true) -> type&;
  36. virtual auto setFocused() -> type&;
  37. virtual auto setFont(const Font& font = {}) -> type&;
  38. virtual auto setGroup(sGroup group = {}) -> type&;
  39. virtual auto setParent(mObject* parent = nullptr, int offset = -1) -> type&;
  40. virtual auto setVisible(bool visible = true) -> type&;
  41. auto visible(bool recursive = false) const -> bool;
  42. template<typename T = string> auto attribute(const string& name) const -> T {
  43. if(auto attribute = state.attributes.find(name)) {
  44. if(attribute->value().is<T>()) return attribute->value().get<T>();
  45. }
  46. return {};
  47. }
  48. //this template basically disables implicit template type deduction:
  49. //if setAttribute(name, value) is called without a type, the type will be a string, so attribute(name) will just work.
  50. //if setAttribute<T>(name, value) is called, the type will be T. as such, U must be cast to T on assignment.
  51. //when T = string, value must be convertible to a string.
  52. //U defaults to a string, so that setAttribute(name, {values, ...}) will deduce U as a string.
  53. template<typename T = string, typename U = string> auto setAttribute(const string& name, const U& value) -> type& {
  54. if constexpr(std::is_same_v<T, string> && !std::is_same_v<U, string>) {
  55. return setAttribute(name, string{value});
  56. }
  57. if(auto attribute = state.attributes.find(name)) {
  58. if((const T&)value) attribute->setValue((const T&)value);
  59. else state.attributes.remove(*attribute);
  60. } else {
  61. if((const T&)value) state.attributes.insert({name, (const T&)value});
  62. }
  63. return *this;
  64. }
  65. //private:
  66. struct State {
  67. set<Attribute> attributes;
  68. Font font;
  69. mObject* parent = nullptr;
  70. int offset = -1;
  71. char enabled = true;
  72. char visible = true;
  73. } state;
  74. wObject instance;
  75. pObject* delegate = nullptr;
  76. virtual auto construct() -> void;
  77. virtual auto destruct() -> void;
  78. };
  79. #endif