• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

traintastic / traintastic / 22831719852

08 Mar 2026 10:49PM UTC coverage: 26.825% (-0.006%) from 26.831%
22831719852

push

github

reinder
[output] fix: corrected include guard

8230 of 30680 relevant lines covered (26.83%)

186.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

7.79
/server/src/lua/object/interface.cpp
1
/**
2
 * This file is part of Traintastic,
3
 * see <https://github.com/traintastic/traintastic>.
4
 *
5
 * Copyright (C) 2024-2026 Reinder Feenstra
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
 */
21

22
#include "interface.hpp"
23
#include <traintastic/enum/outputchannel.hpp>
24
#include "object.hpp"
25
#include "../sandbox.hpp"
26
#include "../script.hpp"
27
#include "../check.hpp"
28
#include "../checkarguments.hpp"
29
#include "../checkvector.hpp"
30
#include "../push.hpp"
31
#include "../to.hpp"
32
#include "../metatable.hpp"
33
#include "../../hardware/input/inputcontroller.hpp"
34
#include "../../hardware/input/input.hpp"
35
#include "../../hardware/output/outputcontroller.hpp"
36
#include "../../hardware/output/output.hpp"
37

38
namespace Lua::Object {
39

40
void Interface::registerType(lua_State* L)
99✔
41
{
42
  MetaTable::clone(L, Object::metaTableName, metaTableName);
99✔
43
  lua_pushcfunction(L, __index);
99✔
44
  lua_setfield(L, -2, "__index");
99✔
45
  lua_pop(L, 1);
99✔
46
}
99✔
47

48
int Interface::index(lua_State* L, ::Interface& object)
×
49
{
50
  const auto key = to<std::string_view>(L, 2);
×
51
  if(auto* inputController = dynamic_cast<::InputController*>(&object))
×
52
  {
53
    LUA_OBJECT_PROPERTY(input_channels)
×
54
    {
55
      auto channels = inputController->inputChannels();
×
56
      lua_createtable(L, static_cast<int>(channels.size()), 0);
×
57
      lua_Integer n = 1;
×
58
      for(auto channel : channels)
×
59
      {
60
        Enum<std::remove_const_t<decltype(channels)::element_type>>::push(L, channel);
×
61
        lua_rawseti(L, -2, n);
×
62
        n++;
×
63
      }
64
      return 1;
×
65
    }
66
    LUA_OBJECT_METHOD(get_input)
×
67
  }
68
  if(auto* outputController = dynamic_cast<::OutputController*>(&object))
×
69
  {
70
    LUA_OBJECT_PROPERTY(output_channels)
×
71
    {
72
      auto channels = outputController->outputChannels();
×
73
      lua_createtable(L, static_cast<int>(channels.size()), 0);
×
74
      lua_Integer n = 1;
×
75
      for(auto channel : channels)
×
76
      {
77
        Enum<std::remove_const_t<decltype(channels)::element_type>>::push(L, channel);
×
78
        lua_rawseti(L, -2, n);
×
79
        n++;
×
80
      }
81
      return 1;
×
82
    }
83
    LUA_OBJECT_METHOD(get_output)
×
84
  }
85
  return Object::index(L, object);
×
86
}
87

88
int Interface::__index(lua_State* L)
×
89
{
90
  return index(L, *check<::Interface>(L, 1));
×
91
}
92

93
int Interface::get_input(lua_State* L)
×
94
{
95
  auto inputController = std::dynamic_pointer_cast<::InputController>(check<::Interface>(L, lua_upvalueindex(1)));
×
96
  assert(inputController);
×
97
  const auto inputChannels = inputController->inputChannels();
×
98
  assert(!inputChannels.empty());
×
99
  const bool channelOptional = inputChannels.size() == 1;
×
100
  const int argc = checkArguments(L, channelOptional ? 1 : 2, 2);
×
101
  const auto channel = (argc == 2) ? check<InputChannel>(L, 1) : inputChannels.front();
×
102
  const auto address = check<uint32_t>(L, argc);
×
103
  auto& stateData = Lua::Sandbox::getStateData(L);
×
104
  auto input = inputController->getInput(channel, address, stateData.script());
×
105
  if(input)
×
106
  {
107
    stateData.registerInput(inputController, input);
×
108
    Lua::push(L, input);
×
109
  }
110
  else
111
  {
112
    Lua::push(L, nullptr);
×
113
  }
114
  return 1;
×
115
}
×
116

117
int Interface::get_output(lua_State* L)
×
118
{
119
  checkArguments(L, 2, 3);
×
120
  auto outputController = std::dynamic_pointer_cast<::OutputController>(check<::Interface>(L, lua_upvalueindex(1)));
×
121
  assert(outputController);
×
122
  auto channel = check<::OutputChannel>(L, 1);
×
123
  OutputLocation location;
×
124
  switch(channel)
×
125
  {
126
    using enum OutputChannel;
127

128
    case Output:
×
129
    case Accessory:
130
    case AccessoryDCC:
131
    case AccessoryMotorola:
132
    case DCCext:
133
    case Turnout:
134
    case ShortEvent:
135
      checkArguments(L, 2);
×
136
      location = OutputAddress(check<uint32_t>(L, 2));
×
137
      break;
×
138

139
    case LongEvent:
×
140
      checkArguments(L, 3);
×
141
      location = OutputNodeAddress(check<uint32_t>(L, 2), check<uint32_t>(L, 3));
×
142
      break;
×
143

144
    case ECoSObject:
×
145
      checkArguments(L, 2);
×
146
      location = OutputECoSObject(check<uint16_t>(L, 2));
×
147
      break;
×
148
  }
149
  auto& stateData = Lua::Sandbox::getStateData(L);
×
150
  auto output = outputController->getOutput(channel, location, stateData.script());
×
151
  if(output)
×
152
  {
153
    stateData.registerOutput(outputController, output);
×
154
    Lua::push(L, output);
×
155
  }
156
  else
157
  {
158
    Lua::push(L, nullptr);
×
159
  }
160
  return 1;
×
161
}
×
162

163
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc