traintastic/server/src/lua/object.cpp
Reinder Feenstra 222c219fb7
Einige Prüfungen sind fehlgeschlagen
Build / client macos-15-arm64 (push) Has been cancelled
Build / client macos-15-intel (push) Has been cancelled
Build / client ubuntu_24.04 (push) Has been cancelled
Build / client ubuntu_24.04_arm64 (push) Has been cancelled
Build / client raspberrypios_arm64 (push) Has been cancelled
Build / client raspberrypios_arm7 (push) Has been cancelled
Build / client windows_x64_msvc (push) Has been cancelled
Build / server ubuntu_24.04 (debug+ccov) (push) Has been cancelled
Build / server macos-15-arm64 (push) Has been cancelled
Build / server macos-15-intel (push) Has been cancelled
Build / server raspberrypios_arm64 (push) Has been cancelled
Build / server raspberrypios_arm7 (push) Has been cancelled
Build / server ubuntu_24.04 (push) Has been cancelled
Build / server ubuntu_24.04_arm64 (push) Has been cancelled
Build / server windows_x64_clang (push) Has been cancelled
Build / language files (push) Has been cancelled
Build / manual (push) Has been cancelled
Build / Update contributers in README.md (push) Has been cancelled
Build / shared data raspberrypios_10 (push) Has been cancelled
Build / shared data ubuntu_24.04 (push) Has been cancelled
Build / package innosetup (push) Has been cancelled
Build / Deploy to website (push) Has been cancelled
[cbus] added Lua support for sending CBUS/VLCB message and DCC commands
2026-02-21 16:21:28 +01:00

95 Zeilen
2.8 KiB
C++

/**
* This file is part of Traintastic,
* see <https://github.com/traintastic/traintastic>.
*
* Copyright (C) 2019-2026 Reinder Feenstra
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "object.hpp"
#include "object/object.hpp"
#include "object/objectlist.hpp"
#include "object/interface.hpp"
#include "object/cbusinterface.hpp"
#include "object/loconetinterface.hpp"
#include "object/scriptthrottle.hpp"
namespace Lua::Object {
constexpr char const* objectsGlobal = "objects";
void registerTypes(lua_State* L)
{
Object::registerType(L);
ObjectList::registerType(L);
Interface::registerType(L);
CBUSInterface::registerType(L);
LocoNetInterface::registerType(L);
ScriptThrottle::registerType(L);
// weak table for object userdata:
lua_newtable(L);
lua_newtable(L); // metatable
lua_pushliteral(L, "__mode");
lua_pushliteral(L, "v");
lua_rawset(L, -3);
lua_setmetatable(L, -2);
lua_setglobal(L, objectsGlobal);
}
void push(lua_State* L, ::Object& value)
{
push(L, value.shared_from_this());
}
void push(lua_State* L, const ObjectPtr& value)
{
if(value)
{
lua_getglobal(L, objectsGlobal);
lua_rawgetp(L, -1, value.get());
if(lua_isnil(L, -1)) // object not in table
{
lua_pop(L, 1); // remove nil
new(lua_newuserdata(L, sizeof(ObjectPtrWeak))) ObjectPtrWeak(value);
if(dynamic_cast<::CBUSInterface*>(value.get()))
{
luaL_setmetatable(L, CBUSInterface::metaTableName);
}
else if(dynamic_cast<::LocoNetInterface*>(value.get()))
luaL_setmetatable(L, LocoNetInterface::metaTableName);
else if(dynamic_cast<AbstractObjectList*>(value.get()))
luaL_setmetatable(L, ObjectList::metaTableName);
else if(dynamic_cast<::ScriptThrottle*>(value.get()))
{
luaL_setmetatable(L, ScriptThrottle::metaTableName);
}
else
luaL_setmetatable(L, Object::metaTableName);
lua_pushvalue(L, -1); // copy userdata on stack
lua_rawsetp(L, -3, value.get()); // add object to table
}
lua_insert(L, lua_gettop(L) - 1); // swap table and userdata
lua_pop(L, 1); // remove table
}
else
lua_pushnil(L);
}
}