Interpret arguments of add actor frame opcode properly.
[scummvm-innocent.git] / engines / innocent / value.h
blob836916dd2b76242fdd6cfded54e9a997fd8a282f
1 #ifndef INNOCENT_VARIABLES_H
2 #define INNOCENT_VARIABLES_H
4 #include <vector>
6 #include "common/endian.h"
7 #include "config.h"
9 #include "innocent/debug.h"
11 namespace Innocent {
14 enum OpcodeMode {
15 kCodeInitial = 0,
16 kCodeNewRoom = 1,
17 kCodeRoomLoop = 2,
18 kCodeItem = 4,
19 kCodeNewBlock = 8
22 class Interpreter;
24 enum ValueType {
25 kValueVoid,
26 kValueConstant
29 class Value : public NumericInspectable<uint16> {
30 public:
31 virtual ~Value() {}
32 virtual ValueType type() const { return kValueVoid; }
34 virtual operator uint16() const { assert(false); }
35 virtual int16 signd() const { const uint16 v = *this; return *reinterpret_cast<const int16 *>(&v); }
36 virtual Value &operator=(uint16 value) { assert(false); }
37 virtual Value &operator=(const Value &) { assert(false); }
38 virtual bool operator==(const Value &other) { return uint16(*this) == other; }
39 virtual bool operator<(const Value &other) { return uint16(*this) < other; }
40 virtual bool operator>(const Value &other) { return other < *this; }
41 virtual Value &operator++() { return *this = uint16(*this) + 1; }
42 virtual uint16 operator++(int) { uint16 old = *this; ++*this; return old; }
43 virtual Value &operator--() { return *this = uint16(*this) - 1; }
44 virtual uint16 operator--(int) { uint16 old = *this; --*this; return old; }
45 template<typename T>
46 T operator-(T she) { T me = *this; return me - she; }
48 virtual bool holdsCode() const { assert(false); }
50 virtual operator byte *() { assert(false); }
52 Value() {}
54 private:
55 explicit Value(Value &) : NumericInspectable<uint16>() { assert(false); } // no copying
58 class Constant : public Value {
59 public:
60 Constant(uint16 value) : _value(value) {}
61 virtual ~Constant() {}
63 virtual operator uint16() const { return _value; }
64 virtual ValueType type() const { return kValueConstant; }
65 private:
66 uint16 _value;
69 class ByteVariable : public Value {
70 public:
71 ByteVariable(byte *ptr) : _ptr(ptr) {}
72 virtual Value &operator=(uint16 value) { assert(value < 256); *_ptr = value; return *this; }
73 virtual operator uint16() const { return *_ptr; }
74 private:
75 byte *_ptr;
78 class WordVariable : public Value {
79 public:
80 WordVariable(byte *ptr) : _ptr(ptr) {}
81 virtual operator uint16() const { return READ_LE_UINT16(_ptr); }
82 virtual Value &operator=(uint16 value);
83 virtual Value &operator=(const Value &other) { return *this = uint16(other); }
84 private:
85 byte *_ptr;
88 class ValueVector {
89 public:
90 ~ValueVector() {
91 for (std::vector<Value *>::iterator it = _values.begin(); it != _values.end(); ++it)
92 /*delete *it*/; // FIXME segfaults, why?
94 void push_back(Value *element) { _values.push_back(element); }
95 Value &operator[](uint8 idx) { return *_values[idx]; }
96 private:
97 std::vector<Value *> _values;
100 class CodePointer : public Value {
101 public:
102 CodePointer() : _interpreter(0) {}
103 CodePointer(const CodePointer &c) : Value(), _offset(c._offset), _interpreter(c._interpreter) { init(); }
104 CodePointer(uint16 offset, Interpreter *interpreter);
106 CodePointer &operator=(const CodePointer &cp) { _offset = cp._offset; _interpreter = cp._interpreter; return *this; }
108 virtual const char *operator+() const { return _inspect; }
109 virtual void run() const;
110 virtual void run(OpcodeMode mode) const;
111 uint16 offset() const { return _offset; }
112 byte *base() const;
113 virtual operator uint16() const { return _offset; }
114 virtual bool holdsCode() const { return true; }
115 virtual byte *code() const;
116 Interpreter *interpreter() const { return _interpreter; }
117 bool isEmpty() const { return _interpreter == 0; }
118 void reset() { _interpreter = 0; }
120 template<typename T> T &field(T &, int) const;
122 private:
123 void init();
124 char _inspect[40];
125 uint16 _offset;
126 Interpreter *_interpreter;
129 template<typename Enum, int N>
130 struct EnumName {
131 static const char *name() {
132 assert(false);
133 return 0;
136 static const char *findName(Enum a){
137 if (N == a)
138 return name();
139 else
140 return EnumName<Enum, N-1>::findName(a);
144 template<typename Enum>
145 struct EnumName<Enum, -1> {
146 static const char *findName(Enum a) {
147 assert(false);
148 return 0;
153 template<typename Enum>
154 class EnumPack : public Inspectable {
155 public:
156 EnumPack() {}
157 EnumPack(Enum a) : _a(a) { }
158 const char *operator+() const { return EnumName<Enum,40>::findName(Enum(_a)); }
159 operator Enum() const { return _a; }
160 private:
161 Enum _a;
164 #define ENAME(en, v, s) template<> const char *EnumName<en, v>::name() { return s; } enum {}
168 #endif