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

diegoarjz / aswpp / 13214353970

08 Feb 2025 09:24AM UTC coverage: 80.412% (-1.5%) from 81.867%
13214353970

push

github

web-flow
Merge pull request #12 from diegoarjz/add_bool_args_and_retur

Add functionality to set bool args and return values

31 of 44 new or added lines in 3 files covered. (70.45%)

9 existing lines in 2 files now uncovered.

624 of 776 relevant lines covered (80.41%)

10.65 hits per line

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

80.95
/include/aswpp/script_engine.h
1
#pragma once
2

3
#include "aswpp/class_register.h"
4

5
#include "aswpp/script_module.h"
6

7
#include "angelscript.h"
8

9
#include <magic_enum/magic_enum.hpp>
10

11
#include <iostream>
12
#include <memory>
13

14
namespace aswpp {
15
/**
16
 * Entry point for scripting.
17
 */
18
class Engine {
19
public:
20
  //! Default ctor
21
  Engine();
22
  ~Engine();
23

24
  //! Checks if the Engine is in a valid state.
25
  bool IsValid() const;
26

27
  //----------------------------------------
28
  //! \section module-api Module API
29

30
  //! Creates a and attaches Module with the give \a name and \a source.
31
  //! Returns nullptr if attaching the module fails.
32
  ModulePtr CreateModule(const std::string &name, const std::string &source);
33

34
  //! Attaches a Module previously created to the engine.
35
  bool Attach(const ModulePtr &module);
36

37
  //----------------------------------------
38
  //! \section run-methods Run Methods
39

40
  //! Runs the given function with the arguments.
41
  template <class... ArgTypes>
42
  bool Run(const std::string &moduleName, const std::string &function,
43
           ArgTypes &...args) {
44
    if (!prepare(moduleName, function)) {
45
      return false;
46
    }
47
    if (!setArgs<sizeof...(ArgTypes)>(args...)) {
48
      return false;
49
    }
50
    if (!run(moduleName, function)) {
51
      return false;
52
    }
53
    return true;
54
  }
55

56
  //! Runs the given function with the arguments and the return value.
57
  template <class Return, class... ArgTypes>
58
  bool Run(const std::string &moduleName, const std::string &function,
44✔
59
           Return *ret, ArgTypes &...args) {
60
    if (!prepare(moduleName, function)) {
44✔
61
      return false;
×
62
    }
63
    if (!setArgs<sizeof...(ArgTypes)>(args...)) {
44✔
64
      return false;
×
65
    }
66
    if (!run(moduleName, function)) {
44✔
67
      return false;
×
68
    }
69
    if constexpr (std::is_enum<Return>::value) {
70
      int32_t returnValue;
71
      getReturnValue<int32_t>(&returnValue);
5✔
72
      *ret = static_cast<Return>(returnValue);
5✔
73
    } else {
74
      getReturnValue(ret);
75
    }
76
    return true;
44✔
77
  }
78

79
  //----------------------------------------
80
  //! \section Register Enum Methods
81

82
  //! Registers an enum with \a name
83
  bool RegisterEnum(const std::string &enumName);
84

85
  //! Registers an enum \a value with the \a valueName in the \a enumName enum.
86
  bool RegisterEnumValue(const std::string &enumName,
87
                         const std::string &valueName, int value);
88

89
  //! Registers the enum \a R with \a enumName and all of its values.
90
  template <class R> bool RegisterEnum(const char *enumName) {
10✔
91
    if (!RegisterEnum(enumName)) {
20✔
92
      std::cerr << "Error registering enum '" << enumName << "'" << std::endl;
2✔
93
      return false;
2✔
94
    }
95

96
    for (const auto &v : magic_enum::enum_values<R>()) {
24✔
97
      const auto enumMemberName = magic_enum::enum_name(v);
16✔
98
      if (!RegisterEnumValue(enumName,
80✔
99
                             static_cast<std::string>(enumMemberName).c_str(),
48✔
100
                             static_cast<int>(v))) {
101
        std::cerr << "Error registering enum member '" << enumName
102
                  << "::" << enumMemberName << "'" << std::endl;
×
103
        return false;
×
104
      }
105
    }
106

107
    return true;
8✔
108
  }
109

110
  //----------------------------------------
111
  //! \section Register Free Functions
112

113
  //! Registers the function \func as a global function
114
  template <typename RetType, typename... Args>
115
  bool Register(const char *signature, RetType (*func)(Args...)) {
116
    auto asEngine = engine();
117
    const int r = asEngine->RegisterGlobalFunction(signature, asFUNCTION(func),
118
                                                   asCALL_CDECL);
119
    return r > 0;
120
  }
121

122
  //----------------------------------------
123
  //! \section Register Classes
124

125
  template <class C, class O = RefObjectType<C>,
126
            class Ref = RefCountBehaviour<C>, class F = FactoryBehaviour<C>>
127
  ClassRegister<C, O, Ref, F> RegisterClass(const char *className) {
128
    return ClassRegister<C, O, Ref, F>(engine(), className);
129
  }
130

131
private:
132
  bool prepare(const std::string &moduleName, const std::string &function);
133
  bool run(const std::string &moduleName, const std::string &function);
134
  bool release();
135

136
  bool setFunctionObjectArg(int i, void *val);
137
  bool setFunctionObjectArg(int i, const void *val);
138

139
  template <typename T> bool setFunctionArg(int i, T val) {
1✔
140
    auto r = setFunctionArg<int32_t>(i, static_cast<int64_t>(val));
1✔
141
    return r;
1✔
142
  }
143

144
  template <std::size_t Count> bool setArgs() { return true; }
145

146
  template <std::size_t Count, class ArgType, class... ArgTypes>
147
  bool setArgs(ArgType &arg, ArgTypes &...args) {
148
    return setArgs<Count, ArgType>(arg) && setArgs<Count - 1>(args...);
149
  }
150

151
  template <std::size_t Count, class ArgType> bool setArgs(ArgType &arg) {
14✔
152
    if constexpr (std::is_scalar<ArgType>::value) {
153
      return setFunctionArg(Count - 1, arg);
14✔
154
    }
28✔
155
    else {
28✔
NEW
156
      return setFunctionObjectArg(Count - 1, &arg);
×
157
    }
28✔
UNCOV
158
  }
×
159

28✔
160
  asIScriptEngine *engine();
×
161

28✔
162
  template <typename T> void getReturnValue(T *value);
39✔
163

34✔
164
  class Impl;
34✔
165
  std::unique_ptr<Impl> m_impl;
23✔
166
};
2✔
167

2✔
168
//----------------------------------------
2✔
169
// Specializations
2✔
170
template <> bool Engine::setFunctionArg<int64_t>(int i, int64_t val);
171
template <> bool Engine::setFunctionArg<uint64_t>(int i, uint64_t val);
172
template <> bool Engine::setFunctionArg<int32_t>(int i, int32_t val);
173
template <> bool Engine::setFunctionArg<uint32_t>(int i, uint32_t val);
174
template <> bool Engine::setFunctionArg<int16_t>(int i, int16_t val);
175
template <> bool Engine::setFunctionArg<uint16_t>(int i, uint16_t val);
176
template <> bool Engine::setFunctionArg<int8_t>(int i, int8_t val);
177
template <> bool Engine::setFunctionArg<uint8_t>(int i, uint8_t val);
178
template <> bool Engine::setFunctionArg<char>(int i, char val);
179
template <> bool Engine::setFunctionArg<float>(int i, float val);
180
template <> bool Engine::setFunctionArg<double>(int i, double val);
181
template <> bool Engine::setFunctionArg<bool>(int i, bool val);
182

183
template <> void Engine::getReturnValue(int64_t *value);
184
template <> void Engine::getReturnValue(uint64_t *value);
185
template <> void Engine::getReturnValue(int32_t *value);
186
template <> void Engine::getReturnValue(uint32_t *value);
187
template <> void Engine::getReturnValue(int16_t *value);
188
template <> void Engine::getReturnValue(uint16_t *value);
189
template <> void Engine::getReturnValue(int8_t *value);
190
template <> void Engine::getReturnValue(uint8_t *value);
191
template <> void Engine::getReturnValue(float *value);
192
template <> void Engine::getReturnValue(double *value);
193
template <> void Engine::getReturnValue(bool *value);
194
} // namespace aswpp
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