1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-16 13:29:56 +00:00
openmw/components/interpreter/interpreter.hpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

81 lines
2.3 KiB
C++
Raw Normal View History

2010-06-28 17:20:45 +00:00
#ifndef INTERPRETER_INTERPRETER_H_INCLUDED
#define INTERPRETER_INTERPRETER_H_INCLUDED
2010-06-28 18:46:15 +00:00
#include <map>
2022-01-27 19:18:57 +00:00
#include <memory>
#include <stack>
#include <stdexcept>
2022-01-27 19:18:57 +00:00
#include <utility>
2010-06-28 18:46:15 +00:00
#include <components/misc/strings/format.hpp>
2022-01-27 19:18:57 +00:00
#include "opcodes.hpp"
#include "program.hpp"
2010-06-28 17:20:45 +00:00
#include "runtime.hpp"
#include "types.hpp"
namespace Interpreter
{
struct Program;
2010-06-28 17:20:45 +00:00
class Interpreter
{
std::stack<Runtime> mCallstack;
bool mRunning = false;
2010-06-28 17:20:45 +00:00
Runtime mRuntime;
2022-01-27 19:18:57 +00:00
std::map<int, std::unique_ptr<Opcode1>> mSegment0;
std::map<int, std::unique_ptr<Opcode1>> mSegment2;
std::map<int, std::unique_ptr<Opcode1>> mSegment3;
std::map<int, std::unique_ptr<Opcode0>> mSegment5;
2022-09-22 18:26:05 +00:00
2010-06-28 18:46:15 +00:00
void execute(Type_Code code);
2022-09-22 18:26:05 +00:00
void begin();
2022-09-22 18:26:05 +00:00
void end();
2022-09-22 18:26:05 +00:00
template <typename T, typename... Args>
void installSegment(auto& segment, std::string_view name, int code, Args&&... args)
2022-01-27 19:18:57 +00:00
{
if (segment.find(code) != segment.end())
throw std::invalid_argument(Misc::StringUtils::format(
"Duplicated interpreter instruction code in segment %s: 0x%x", name, code));
segment.emplace(code, std::make_unique<T>(std::forward<Args>(args)...));
2022-01-27 19:18:57 +00:00
}
2022-09-22 18:26:05 +00:00
2010-06-28 17:20:45 +00:00
public:
Interpreter() = default;
Interpreter(const Interpreter&) = delete;
Interpreter& operator=(const Interpreter&) = delete;
2022-09-22 18:26:05 +00:00
2022-01-27 19:18:57 +00:00
template <typename T, typename... TArgs>
void installSegment0(int code, TArgs&&... args)
{
installSegment<T>(mSegment0, "0", code, std::forward<TArgs>(args)...);
2022-01-27 19:18:57 +00:00
}
2022-09-22 18:26:05 +00:00
2022-01-27 19:18:57 +00:00
template <typename T, typename... TArgs>
void installSegment2(int code, TArgs&&... args)
{
installSegment<T>(mSegment2, "2", code, std::forward<TArgs>(args)...);
2022-01-27 19:18:57 +00:00
}
2022-09-22 18:26:05 +00:00
2022-01-27 19:18:57 +00:00
template <typename T, typename... TArgs>
void installSegment3(int code, TArgs&&... args)
{
installSegment<T>(mSegment3, "3", code, std::forward<TArgs>(args)...);
2022-01-27 19:18:57 +00:00
}
2022-09-22 18:26:05 +00:00
2022-01-27 19:18:57 +00:00
template <typename T, typename... TArgs>
void installSegment5(int code, TArgs&&... args)
{
installSegment<T>(mSegment5, "5", code, std::forward<TArgs>(args)...);
2022-01-27 19:18:57 +00:00
}
2022-09-22 18:26:05 +00:00
void run(const Program& program, Context& context);
2010-06-28 17:20:45 +00:00
};
}
#endif