@@ -954,6 +954,9 @@ l.set("co", co); // set const instance by copy
954
954
l.set(" co" , std::move(co)); // set const instance by move
955
955
// or
956
956
l.set(" co" , std::add_const_t <Obj>{}); // set const instance by move
957
+ // or
958
+ l.set<const Obj>(" co" , o); // copy non-const "o" to a const instance
959
+
957
960
// The variable "co" is const, it can access all registered members variables
958
961
// and registered const member functins of Obj.
959
962
```
@@ -1903,24 +1906,27 @@ if (f.failed()) {
1903
1906
1904
1907
#### 3.3 Type of argument and return
1905
1908
1906
- Do not support using C++ reference type as function's argument or return type.
1907
-
1908
- If using reference as argument, it will make a copy of the referenced
1909
- argument into Lua, won't implicitly take its address, meaning that it
1910
- will not share the same object in Lua with that in C++, and this
1911
- behaves differently with that in C++. This may confuse users, so
1912
- explicitly forbid it.
1909
+ ##### Argument
1913
1910
1914
- Directly using the underlying type if you want to make a copy of the
1915
- argument into Lua.
1911
+ Do not support non-const lvalue reference as luaw::function's argument.
1912
+
1913
+ If using this kind of reference, it will make a copy of the referenced
1914
+ argument into Lua, won't implicitly take its address, meaning that it
1915
+ will not share the same object in Lua with C++.
1916
+ And this behaves differently with that in C++. This may confuse users,
1917
+ so explicitly forbid it.
1916
1918
1917
- Or if you want to share the same argument objects in Lua with C++,
1919
+ If you want to share the same argument objects in Lua with C++,
1918
1920
so you can modify them in Lua, you can use raw pointer type if there is
1919
1921
only one kind of raw pointer type in all arguments, or use smart
1920
- pointer type or ` luaw::ptrw ` type, and these are safer and more reassuring.
1922
+ pointer type or ` peacalm::luaw::ptrw ` type, and these two are safer and
1923
+ more reassuring.
1924
+
1925
+ ##### Return
1921
1926
1922
1927
Since we cannot make C++ reference type of reference to values in Lua,
1923
- so using C++ reference type as return type is also forbidden.
1928
+ so using any C++ reference type as return type is forbidden.
1929
+
1924
1930
To get a reference of a Lua value, can use type
1925
1931
[ ` luaw::luavalueref ` ] ( https://github.com/peacalm/cpp-luaw?tab=readme-ov-file#9-reference-of-lua-values-in-c ) .
1926
1932
@@ -3173,38 +3179,50 @@ int main() {
3173
3179
##### Set const instances to Lua
3174
3180
3175
3181
Const property of an instance in C++ will be perfectly transfered to Lua using
3176
- "set" method.
3182
+ `set` method (if `Hint` was not provided).
3183
+
3184
+ Or explicitly provide a const type as `Hint` then `set` will construct a const
3185
+ instance by a given value, which must be convertible to type `Hint`.
3186
+
3177
3187
3178
3188
Example:
3179
3189
3180
3190
```C++
3191
+
3192
+ // ==== Set const instance:
3181
3193
const Obj co;
3182
3194
l.set("co", co); // set const instance by copy
3183
3195
// or
3184
3196
l.set("co", std::move(co)); // set const instance by move
3185
3197
// or
3186
3198
l.set("co", std::add_const_t<Obj>{}); // set const instance by move
3187
3199
// or
3200
+ l.set<const Obj>("co", Obj{}); // copy non-const instance to be a const instance
3201
+
3202
+ // ==== Set raw pointer of const instance:
3188
3203
const Obj co2;
3189
3204
// set low-level const pointer to be a lightuserdata which is also const
3190
- l.set("co", &co2);
3191
- // The variable "co" (set by any method above) is const in Lua,
3192
- // it can access all registered member variables (but can't modify) and
3193
- // registered const member functins of Obj.
3205
+ l.set("cop", &co2);
3194
3206
3195
-
3196
- // Set underlying const instance using smart pointer.
3207
+ // ==== Set underlying const instance using smart pointer:
3197
3208
// "sco" is not const, by it's underlying object is const,
3198
3209
// so it behaves same as "co":
3199
3210
auto sco = std::make_shared<const Obj>();
3200
3211
l.set("sco", sco); // by copy
3201
3212
l.set("sco", std::move(sco)); // by move
3202
3213
l.set("sco", std::make_shared<const Obj>()); // by move
3203
- // or
3214
+
3215
+ // ==== Set raw pointer of smart pointer:
3204
3216
auto sco2 = std::make_shared<const Obj>();
3205
- l.set("sco", &sco2); // set as lightuserdata, also underlying const
3217
+ l.set("scop", &sco2); // set as lightuserdata, also underlying const
3218
+
3206
3219
```
3207
3220
3221
+ The variables set by methods above can read all registered member variables
3222
+ and call registered const member functins of ` Obj ` .
3223
+ But can't modify member variables and can't call non-const member functions.
3224
+
3225
+
3208
3226
#### 5.10 Get instances from Lua
3209
3227
3210
3228
Use "get" method, we can get an instance or a pointer of an instance from Lua.
0 commit comments