MCPcopy Create free account
hub / github.com/ThePhD/sol2 / main

Function main

examples/source/customization_base_object_catch.cpp:150–214  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

148}
149
150int main() {
151 // test our customization points out
152 std::cout << "=== Base object customization points ==="
153 << std::endl;
154
155 sol::state lua;
156 lua.open_libraries(
157 sol::lib::base, sol::lib::string, sol::lib::table);
158
159 lua["objectCache"] = lua.create_table();
160
161 // Do basic type binding.
162 auto luaBaseObject
163 = lua.new_usertype<BaseObject>("tes3baseObject");
164 luaBaseObject["objectType"]
165 = sol::readonly_property(&BaseObject::getObjectType);
166 luaBaseObject["doArmorThing"] = &BaseObject::doArmorThing;
167 luaBaseObject["doWeaponThing"] = &BaseObject::doWeaponThing;
168 auto luaArmorObject = lua.new_usertype<Armor>("tes3armor");
169 luaArmorObject[sol::base_classes]
170 = sol::bases<BaseObject>();
171 luaArmorObject["doArmorThing"] = &Armor::doArmorThing;
172 auto luaWeaponObject
173 = lua.new_usertype<Weapon>("tes3weapon");
174 luaWeaponObject[sol::base_classes]
175 = sol::bases<BaseObject>();
176 luaWeaponObject["doWeaponThing"] = &Weapon::doWeaponThing;
177
178 // Objects we'll play with.
179 BaseObject base;
180 Armor armor;
181 Weapon weapon;
182
183 // Push some objects to lua.
184 std::cout << "Normal pointers..." << std::endl;
185 lua["ptrBase"] = &base;
186 lua["ptrArmor"] = &armor;
187 lua["ptrWeapon"] = &weapon;
188 std::cout << std::endl;
189
190 // Same objects but as base objects to test mapping.
191 std::cout << "Base-cast pointers..." << std::endl;
192 lua["ptrBaseAsBase"] = static_cast<BaseObject*>(&base);
193 lua["ptrArmorAsBase"] = static_cast<BaseObject*>(&armor);
194 lua["ptrWeaponAsBase"] = static_cast<BaseObject*>(&weapon);
195 std::cout << std::endl;
196
197 std::cout << "Smart direct pointers..." << std::endl;
198 lua["sharedBase"] = std::make_shared<BaseObject>();
199 lua["sharedArmor"] = std::make_shared<Armor>();
200 lua["sharedArmor"] = std::make_shared<Weapon>();
201 std::cout << std::endl;
202
203 std::cout << "Smart pointers put as the base class..."
204 << std::endl;
205 lua["sharedBaseAsBase"] = (std::shared_ptr<BaseObject>)
206 std::make_shared<BaseObject>();
207 lua["sharedArmorAsBase"] = (std::shared_ptr<BaseObject>)

Callers

nothing calls this directly

Calls 3

readonly_propertyFunction · 0.85
open_librariesMethod · 0.80
create_tableMethod · 0.80

Tested by

no test coverage detected