SDOM - Simple SDL Document Object Model
A lightweight, extensible Document Object Model for SDL-based applications.
Loading...
Searching...
No Matches
SDOM_DisplayHandle.hpp
Go to the documentation of this file.
1// SDOM_DisplayHandle.hpp
2
3#pragma once
4
5// #include <sol/sol.hpp>
6// #include <string>
7// #include <sstream>
8// #include <SDOM/SDOM.hpp>
10
11// NOTE: this ~= "DisplayHandle(getName(), getType())"
12
13
14
15
16
17namespace SDOM
18{
19 class Core;
20 class Factory;
21 class IDisplayObject;
22
24 {
25 using SUPER = IDataObject;
26
27 public:
28
30 DisplayHandle(const std::string& name, const std::string& type) : name_(name), type_(type) {}
32 : name_(other.name_), type_(other.type_) {}
33
34 // Provide explicit copy-assignment to avoid implicitly-declared
35 // deprecated assignment operator warnings when a user-provided
36 // copy constructor exists.
38 if (this != &other) {
39 name_ = other.name_;
40 type_ = other.type_;
41 }
42 return *this;
43 }
44
45 // Provide move-assignment as well for completeness
47 if (this != &other) {
48 name_ = std::move(other.name_);
49 type_ = std::move(other.type_);
50 }
51 return *this;
52 }
53
54 // construction from nullptr
55 DisplayHandle(std::nullptr_t) { reset(); }
56
57 virtual ~DisplayHandle();
58
59 // virtual methods from IDataObject
60 virtual bool onInit() override { return true; }
61 virtual void onQuit() override {}
62 virtual bool onUnitTest() override { return true; }
63
64 IDisplayObject* get() const;
65
66 template<typename T>
67 T* as() const
68 {
69 return dynamic_cast<T*>(get());
70 }
71
72 // Convenience: get raw IDisplayObject pointer (non-owning), or nullptr if not available
73 IDisplayObject& operator*() const { return *get(); }
74 IDisplayObject* operator->() const { return get(); }
75 operator bool() const { return get() != nullptr; }
76
77 // DisplayHandle& operator=(const DisplayHandle& other) {
78 // if (this != &other) {
79 // name_ = other.name_;
80 // type_ = other.type_;
81 // }
82 // return *this;
83 // }
84
85 // // Allow assignment from nullptr
86 // DisplayHandle& operator=(std::nullptr_t)
87 // return *this;
88 // Comparison operators
89 bool operator==(std::nullptr_t) const { return get() == nullptr; }
90 bool operator!=(std::nullptr_t) const { return get() != nullptr; }
91 // bool operator==(const DisplayHandle& other) const { return name_ == other.name_ && type_ == other.type_; }
92 bool operator==(const DisplayHandle& other) const { return name_ == other.name_; }
93 bool operator!=(const DisplayHandle& other) const { return !(*this == other); }
94 // }
95
96 void reset() {
97 // Clear internal state (pointer, name, etc.)
98 // Example:
99 // ptr_ = nullptr;
100 name_.clear();
101 // ...other members...
102 }
103 std::string getName() const { return name_; }
104 std::string getType() const { return type_; }
105 void setName(const std::string& newName) { name_ = newName; }
106 void setType(const std::string& newType) { type_ = newType; }
107
108 std::string str() const {
109 std::ostringstream oss;
110 oss << "name: '" << getName() << "' type: `" << getType() << "` " << get();
111 return oss.str();
112 }
113
114 const char* c_str() const {
115 // Store the formatted string in a member variable to keep it alive
116 formatted_ = str();
117 return formatted_.c_str();
118 }
119
120 bool isValid() const {
121 return get() != nullptr;
122 }
123
124 // --- LUA Wrapper Functions --- //
125
126 IDisplayObject* get_lua(DisplayHandle* self) const { return self->get(); }
127 bool isValid_lua(DisplayHandle* self) const { return self->isValid(); }
128 std::string getName_lua(DisplayHandle* self) const { return self->getName(); }
129 std::string getType_lua(DisplayHandle* self) const { return self->getType(); }
130
131 // Shared Lua handle usertype helpers (augmentable by base/descendants)
132 inline static constexpr const char* LuaHandleName = "DisplayHandle";
133
134 // Ensure the handle usertype exists in this Lua state and return its table.
135 static sol::table ensure_handle_table(sol::state_view lua);
136
137 // Bind only the minimal, safe helpers if absent (idempotent).
138 static void bind_minimal(sol::state_view lua);
139
140
141 public:
142 inline static Factory* factory_ = nullptr;
143 private:
144 // Factory::Factory() { DisplayHandle<T>::factory_ = this; }
145 friend Core;
146 friend Factory;
147
148 std::string name_;
149 std::string type_;
150
151 mutable std::string formatted_; // to keep the formatted string alive for c_str()
152
153
154 protected:
155 // --- LUA Registration --- //
156 // virtual void _registerLua(const std::string& typeName, sol::state_view lua);
157 sol::usertype<DisplayHandle> objHandleType_;
158
159 protected:
160 // Resolve a Lua child spec (string, DisplayHandle, or table{ child=... | name=... })
161 static DisplayHandle resolveChildSpec(const sol::object& spec);
162
163 virtual void _registerLuaBindings(const std::string& typeName, sol::state_view lua) override;
164
165 }; // end class DisplayHandle
166
167} // namespace SDOM
The central singleton framework object for SDOM.
Definition SDOM_Core.hpp:27
Definition SDOM_DisplayHandle.hpp:24
DisplayHandle & operator=(const DisplayHandle &other)
Definition SDOM_DisplayHandle.hpp:37
virtual bool onInit() override
Definition SDOM_DisplayHandle.hpp:60
std::string getName_lua(DisplayHandle *self) const
Definition SDOM_DisplayHandle.hpp:128
IDisplayObject * operator->() const
Definition SDOM_DisplayHandle.hpp:74
const char * c_str() const
Definition SDOM_DisplayHandle.hpp:114
virtual bool onUnitTest() override
Runs unit tests for this object.
Definition SDOM_DisplayHandle.hpp:62
IDisplayObject * get() const
Definition SDOM_DisplayHandle.cpp:24
bool operator!=(const DisplayHandle &other) const
Definition SDOM_DisplayHandle.hpp:93
DisplayHandle(const DisplayHandle &other)
Definition SDOM_DisplayHandle.hpp:31
static sol::table ensure_handle_table(sol::state_view lua)
Definition SDOM_DisplayHandle.cpp:61
DisplayHandle & operator=(DisplayHandle &&other) noexcept
Definition SDOM_DisplayHandle.hpp:46
T * as() const
Definition SDOM_DisplayHandle.hpp:67
void reset()
Definition SDOM_DisplayHandle.hpp:96
static void bind_minimal(sol::state_view lua)
Definition SDOM_DisplayHandle.cpp:80
void setName(const std::string &newName)
Definition SDOM_DisplayHandle.hpp:105
static constexpr const char * LuaHandleName
Definition SDOM_DisplayHandle.hpp:132
bool operator!=(std::nullptr_t) const
Definition SDOM_DisplayHandle.hpp:90
std::string getType_lua(DisplayHandle *self) const
Definition SDOM_DisplayHandle.hpp:129
virtual ~DisplayHandle()
Definition SDOM_DisplayHandle.cpp:19
bool isValid() const
Definition SDOM_DisplayHandle.hpp:120
static DisplayHandle resolveChildSpec(const sol::object &spec)
Definition SDOM_DisplayHandle.cpp:34
bool operator==(std::nullptr_t) const
Definition SDOM_DisplayHandle.hpp:89
DisplayHandle()
Definition SDOM_DisplayHandle.cpp:14
virtual void _registerLuaBindings(const std::string &typeName, sol::state_view lua) override
Definition SDOM_DisplayHandle.cpp:97
virtual void onQuit() override
Definition SDOM_DisplayHandle.hpp:61
void setType(const std::string &newType)
Definition SDOM_DisplayHandle.hpp:106
sol::usertype< DisplayHandle > objHandleType_
Definition SDOM_DisplayHandle.hpp:157
IDisplayObject * get_lua(DisplayHandle *self) const
Definition SDOM_DisplayHandle.hpp:126
DisplayHandle(const std::string &name, const std::string &type)
Definition SDOM_DisplayHandle.hpp:30
std::string str() const
Definition SDOM_DisplayHandle.hpp:108
DisplayHandle(std::nullptr_t)
Definition SDOM_DisplayHandle.hpp:55
bool operator==(const DisplayHandle &other) const
Definition SDOM_DisplayHandle.hpp:92
IDisplayObject & operator*() const
Definition SDOM_DisplayHandle.hpp:73
static Factory * factory_
Definition SDOM_DisplayHandle.hpp:142
std::string getName() const
Definition SDOM_DisplayHandle.hpp:103
std::string getType() const
Definition SDOM_DisplayHandle.hpp:104
bool isValid_lua(DisplayHandle *self) const
Definition SDOM_DisplayHandle.hpp:127
Definition SDOM_Factory.hpp:43
Definition SDOM_IDataObject.hpp:107
std::string getName() const
Definition SDOM_IDataObject.hpp:129
Definition SDOM_IDisplayObject.hpp:153
std::string getType() const
Definition SDOM_IDisplayObject.hpp:281
Contains all core classes and utilities for the SDOM library.
Definition lua_BindHelpers.hpp:7