1 // -*- Mode: C++; tab-width:2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi:tw=80:et:ts=2:sts=2
3 //
4 // -----------------------------------------------------------------------
5 //
6 // This file is part of RLVM, a RealLive virtual machine clone.
7 //
8 // -----------------------------------------------------------------------
9 //
10 // Copyright (C) 2006, 2007 Elliot Glaysher
11 //
12 // This program is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation; either version 3 of the License, or
15 // (at your option) any later version.
16 //
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
25 //
26 // -----------------------------------------------------------------------
27
28 #ifndef SRC_MACHINE_GENERAL_OPERATIONS_H_
29 #define SRC_MACHINE_GENERAL_OPERATIONS_H_
30
31 #include <memory>
32 #include <string>
33 #include <vector>
34
35 #include "machine/rloperation.h"
36 #include "machine/rloperation/rlop_store.h"
37 #include "machine/rloperation/references.h"
38
39 class System;
40 class EventSystem;
41 class GraphicsSystem;
42 class TextPage;
43 class TextSystem;
44 class SoundSystem;
45 class CGMTable;
46
47 // Templated implementation details:
48 #include "machine/general_operations_impl.h"
49
50 // CallFunction(), the public interface to all the above binder classes.
51 template <typename OBJTYPE>
CallFunction(void (OBJTYPE::* s)(const int))52 RLOperation* CallFunction(void (OBJTYPE::*s)(const int)) {
53 return new binderImpl::Op_CallWithInt<OBJTYPE>(s);
54 }
55
56 template <typename OBJTYPE>
CallFunction(void (OBJTYPE::* s)(RLMachine &,const int))57 RLOperation* CallFunction(void (OBJTYPE::*s)(RLMachine&, const int)) {
58 return new binderImpl::Op_CallWithMachineInt<OBJTYPE>(s);
59 }
60
61 template <typename OBJTYPE>
CallFunction(void (OBJTYPE::* s)(RLMachine &,const int,const int))62 RLOperation* CallFunction(void (OBJTYPE::*s)(RLMachine&,
63 const int,
64 const int)) {
65 return new binderImpl::Op_CallWithMachineIntInt<OBJTYPE>(s);
66 }
67
68 template <typename OBJTYPE>
CallFunction(void (OBJTYPE::* s)(const int,const int))69 RLOperation* CallFunction(void (OBJTYPE::*s)(const int, const int)) {
70 return new binderImpl::Op_CallWithIntInt<OBJTYPE>(s);
71 }
72
73 template <typename OBJTYPE>
CallFunction(void (OBJTYPE::* s)(const std::string &))74 RLOperation* CallFunction(void (OBJTYPE::*s)(const std::string&)) {
75 return new binderImpl::Op_CallWithString<OBJTYPE>(s);
76 }
77
78 template <typename OBJTYPE>
CallFunction(void (OBJTYPE::* s)())79 RLOperation* CallFunction(void (OBJTYPE::*s)()) {
80 return new binderImpl::Op_CallMethod<OBJTYPE>(s);
81 }
82
83 // Calls the incoming function with value.
84 template <typename OBJTYPE, typename VALTYPE>
CallFunctionWith(void (OBJTYPE::* s)(VALTYPE),VALTYPE val)85 RLOperation* CallFunctionWith(void (OBJTYPE::*s)(VALTYPE), VALTYPE val) {
86 return new binderImpl::Op_CallWithConstant<OBJTYPE, VALTYPE>(s, val);
87 }
88
89 template <typename OBJTYPE, typename VALONE, typename VALTWO>
CallFunctionWith(void (OBJTYPE::* s)(VALONE,VALTWO),VALONE one,VALTWO two)90 RLOperation* CallFunctionWith(void (OBJTYPE::*s)(VALONE, VALTWO),
91 VALONE one,
92 VALTWO two) {
93 return new binderImpl::Op_CallWithConstantConstant<OBJTYPE, VALONE, VALTWO>(
94 s, one, two);
95 }
96
97 // Returns the int value of the passed in function as the store register.
98 template <typename RETTYPE>
ReturnIntValue(RETTYPE (* s)())99 RLOperation* ReturnIntValue(RETTYPE (*s)()) {
100 return new binderImpl::Op_ReturnFunctionIntValue<RETTYPE>(s);
101 }
102
103 template <typename OBJTYPE, typename RETTYPE>
ReturnIntValue(RETTYPE (OBJTYPE::* s)()const)104 RLOperation* ReturnIntValue(RETTYPE (OBJTYPE::*s)() const) {
105 return new binderImpl::Op_ReturnIntValue<OBJTYPE, RETTYPE>(s);
106 }
107
108 template <typename OBJTYPE, typename RETTYPE>
ReturnIntValue(RETTYPE (OBJTYPE::* s)(int)const)109 RLOperation* ReturnIntValue(RETTYPE (OBJTYPE::*s)(int) const) {
110 return new binderImpl::Op_ReturnIntValueWithInt<OBJTYPE, RETTYPE>(s);
111 }
112
113 template <typename OBJTYPE, typename RETTYPE>
ReturnIntValue(RETTYPE (OBJTYPE::* s)(const std::string &)const)114 RLOperation* ReturnIntValue(RETTYPE (OBJTYPE::*s)(const std::string&) const) {
115 return new binderImpl::Op_ReturnIntValueWithString<OBJTYPE, RETTYPE>(s);
116 }
117
118 template <typename OBJTYPE>
returnStringValue(const std::string & (OBJTYPE::* s)()const)119 RLOperation* returnStringValue(const std::string& (OBJTYPE::*s)()
120 const) { // NOLINT
121 return new binderImpl::Op_ReturnStringValue<OBJTYPE>(s);
122 }
123
124 // Special adapter for multiple Dispatch versions of operations. This
125 // operation structure will take a Argc_T< >
126 //
127 // For example, consider the two functions @c InitFrame and @c
128 // InitFrames. The following pieces of kepago are equivalent:
129 //
130 // @code
131 // InitFrame(0, 0, 1000, 2500)
132 // InitFrame(1, 1000, 0, 2500)
133 // @endcode
134 //
135 // @code
136 // InitFrames({0, 0, 1000, 2500}, {1, 1000, 0, 2500))
137 // @endcode
138 class MultiDispatch : public RLOp_SpecialCase {
139 public:
140 explicit MultiDispatch(RLOperation* op);
141 ~MultiDispatch();
142
143 virtual void ParseParameters(
144 const std::vector<std::string>& input,
145 libreallive::ExpressionPiecesVector& output) override;
146
147 virtual void operator()(RLMachine& machine,
148 const libreallive::CommandElement& ff);
149
150 private:
151 std::unique_ptr<RLOperation> handler_;
152 };
153
154 // Returns a Gameexe value to the store register.
155 class ReturnGameexeInt : public RLStoreOpcode<> {
156 public:
157 ReturnGameexeInt(const std::string& full_key, int en);
158
159 virtual int operator()(RLMachine& machine) override;
160
161 private:
162 std::string full_key_name_;
163 int entry_;
164 };
165
166 // Invokes a syscom command.
167 class InvokeSyscomAsOp : public RLOpcode<> {
168 public:
169 explicit InvokeSyscomAsOp(const int syscom);
170
171 virtual void operator()(RLMachine& machine) override;
172
173 private:
174 int syscom_;
175 };
176
177 // Class that exists simply to print out a prettier output message on
178 // unimplemented functions.
179 class UndefinedFunction : public RLOp_SpecialCase {
180 public:
181 UndefinedFunction(int modtype,
182 int module,
183 int opcode,
184 int overload);
185
186 // A note on UGLY HACKS: We need to override RLOp_SpecialCase::Dispatch()
187 // because that's the entry point when using ChildObjAdapter. So we overload
188 // all these methods so we error as early as possible when trying to use this
189 // invalid opcode.
190
191 // RLOp_SpecialCase:
192 virtual void Dispatch(
193 RLMachine& machine,
194 const libreallive::ExpressionPiecesVector& parameters) override;
195 virtual void DispatchFunction(RLMachine& machine,
196 const libreallive::CommandElement& f) override;
197 virtual void ParseParameters(
198 const std::vector<std::string>& input,
199 libreallive::ExpressionPiecesVector& output) override;
200 virtual void operator()(RLMachine&,
201 const libreallive::CommandElement&) override;
202
203 private:
204 int modtype_;
205 int module_;
206 int opcode_;
207 int overload_;
208 };
209
210 // extern template declarations; manually instantiated in the cc file.
211 extern template RLOperation* CallFunction<EventSystem>(void (EventSystem::*)(int));
212 extern template RLOperation* CallFunction<GraphicsSystem>(void (GraphicsSystem::*)(int));
213 extern template RLOperation* CallFunction<SoundSystem>(void (SoundSystem::*)(int));
214 extern template RLOperation* CallFunction<System>(void (System::*)(int));
215 extern template RLOperation* CallFunction<TextSystem>(void (TextSystem::*)(int));
216 extern template RLOperation* ReturnIntValue<EventSystem, int>(int (EventSystem::*)() const);
217 extern template RLOperation* ReturnIntValue<GraphicsSystem, int>(int (GraphicsSystem::*)() const);
218 extern template RLOperation* ReturnIntValue<RLMachine, int>(int (RLMachine::*)() const);
219 extern template RLOperation* ReturnIntValue<SoundSystem, int>(int (SoundSystem::*)() const);
220 extern template RLOperation* ReturnIntValue<TextSystem, int>(int (TextSystem::*)() const);
221
222 #endif // SRC_MACHINE_GENERAL_OPERATIONS_H_
223