11#pragma once
22
3- #include < sol/sol.hpp>
4-
3+ #include < cstdint>
4+ #include < optional>
5+ #include < string>
56#include < string_view>
67
8+ #include < sol/sol.hpp>
9+
710#include " utils/str_cat.hpp"
811
912namespace devilution {
1013
14+ enum class LuaUserdataMemberType : uint8_t {
15+ ReadonlyProperty,
16+ Property,
17+ MemberFunction,
18+ Constructor,
19+ };
20+
1121inline std::string LuaSignatureKey (std::string_view key)
1222{
1323 return StrCat (" __sig_" , key);
@@ -18,40 +28,75 @@ inline std::string LuaDocstringKey(std::string_view key)
1828 return StrCat (" __doc_" , key);
1929}
2030
31+ inline std::string LuaUserdataMemberTypeKey (std::string_view key)
32+ {
33+ return StrCat (" __udt_" , key);
34+ }
35+
2136template <typename U, typename T>
22- void SetDocumented (sol::usertype<U> &table, std::string_view key, std::string_view signature, std::string_view doc, T &&value)
37+ void LuaSetDoc (sol::usertype<U> &table, std::string_view key, const char * signature, const char * doc, T &&value)
2338{
2439 table.set (key, std::forward<T>(value));
25- // TODO: figure out a way to set signature and docstring.
40+ table.set (LuaSignatureKey (key), sol::var (signature));
41+ table.set (LuaDocstringKey (key), sol::var (doc));
2642}
2743
28- // This overload works around compiler issues on clang-15 with member field references.
29- template <typename U, typename F>
30- void SetDocumented (sol::usertype<U> &table, std::string_view key, std::string_view signature, std::string_view doc, F U::*&&value)
44+ template <typename U, typename T>
45+ void LuaSetDocFn (sol::usertype<U> &table, std::string_view key, const char *signature, const char *doc, T &&value)
46+ {
47+ table.set_function (key, std::forward<T>(value));
48+ table.set (LuaSignatureKey (key), sol::var (signature));
49+ table.set (LuaDocstringKey (key), sol::var (doc));
50+ table.set (LuaUserdataMemberTypeKey (key), sol::var (static_cast <uint8_t >(LuaUserdataMemberType::MemberFunction)));
51+ }
52+
53+ template <typename U, typename G>
54+ void LuaSetDocReadonlyProperty (sol::usertype<U> &table, std::string_view key, const char *type, const char *doc, G &&getter)
3155{
32- table.set (key, std::forward<F U::*>(value));
33- // TODO: figure out a way to set signature and docstring.
56+ table.set (key, sol::readonly_property (std::forward<G>(getter)));
57+ table.set (LuaSignatureKey (key), sol::var (type));
58+ table.set (LuaDocstringKey (key), sol::var (doc));
59+ table.set (LuaUserdataMemberTypeKey (key), sol::var (static_cast <uint8_t >(LuaUserdataMemberType::ReadonlyProperty)));
3460}
3561
3662template <typename U, typename G, typename S>
37- void SetDocumented (sol::usertype<U> &table, std::string_view key, std::string_view signature, std::string_view doc, G &&getter, S &&setter)
63+ void LuaSetDocProperty (sol::usertype<U> &table, std::string_view key, const char *type, const char * doc, G &&getter, S &&setter)
3864{
3965 table.set (key, sol::property (std::forward<G>(getter), std::forward<S>(setter)));
40- // TODO: figure out a way to set signature and docstring.
66+ table.set (LuaSignatureKey (key), sol::var (type));
67+ table.set (LuaDocstringKey (key), sol::var (doc));
68+ table.set (LuaUserdataMemberTypeKey (key), sol::var (static_cast <uint8_t >(LuaUserdataMemberType::Property)));
69+ }
70+
71+ template <typename U, typename F>
72+ void LuaSetDocProperty (sol::usertype<U> &table, std::string_view key, const char *type, const char *doc, F U::*&&value)
73+ {
74+ table.set (key, value);
75+ table.set (LuaSignatureKey (key), sol::var (type));
76+ table.set (LuaDocstringKey (key), sol::var (doc));
77+ table.set (LuaUserdataMemberTypeKey (key), sol::var (static_cast <uint8_t >(LuaUserdataMemberType::Property)));
4178}
4279
4380template <typename T>
44- void SetDocumented (sol::table &table, std::string_view key, std::string_view signature, std::string_view doc, T &&value)
81+ void LuaSetDoc (sol::table &table, std::string_view key, std::string_view signature, std::string_view doc, T &&value)
4582{
4683 table.set (key, std::forward<T>(value));
4784 table.set (LuaSignatureKey (key), signature);
4885 table.set (LuaDocstringKey (key), doc);
4986}
5087
5188template <typename T>
52- void SetWithSignature (sol::table &table, std::string_view key, std::string_view signature, T &&value)
89+ void LuaSetDocFn (sol::table &table, std::string_view key, std::string_view signature, std::string_view doc , T &&value)
5390{
54- table.set (key, std::forward<T>(value));
91+ table.set_function (key, std::forward<T>(value));
92+ table.set (LuaSignatureKey (key), signature);
93+ table.set (LuaDocstringKey (key), doc);
94+ }
95+
96+ template <typename T>
97+ void LuaSetDocFn (sol::table &table, std::string_view key, std::string_view signature, T &&value)
98+ {
99+ table.set_function (key, std::forward<T>(value));
55100 table.set (LuaSignatureKey (key), signature);
56101}
57102
@@ -65,4 +110,25 @@ inline std::optional<std::string> GetDocstring(const sol::table &table, std::str
65110 return table.get <std::optional<std::string>>(LuaDocstringKey (key));
66111}
67112
113+ inline std::optional<std::string> GetLuaUserdataSignature (const sol::userdata &obj, std::string_view key)
114+ {
115+ return obj.get <std::optional<std::string>>(LuaSignatureKey (key));
116+ }
117+
118+ inline std::optional<std::string> GetLuaUserdataDocstring (const sol::userdata &obj, std::string_view key)
119+ {
120+ return obj.get <std::optional<std::string>>(LuaDocstringKey (key));
121+ }
122+
123+ inline std::optional<LuaUserdataMemberType> GetLuaUserdataMemberType (const sol::userdata &obj, std::string_view key, const sol::object &value)
124+ {
125+ std::optional<uint8_t > result = obj.get <std::optional<uint8_t >>(LuaUserdataMemberTypeKey (key));
126+ if (!result.has_value ()) {
127+ if (value.get_type () == sol::type::userdata) return LuaUserdataMemberType::Property;
128+ if (value.get_type () == sol::type::function && key == " new" ) return LuaUserdataMemberType::Constructor;
129+ return std::nullopt ;
130+ }
131+ return static_cast <LuaUserdataMemberType>(*result);
132+ }
133+
68134} // namespace devilution
0 commit comments