ScummVM API documentation
lingo-object.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (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, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef DIRECTOR_LINGO_OBJECT_H
23 #define DIRECTOR_LINGO_OBJECT_H
24 
25 #include "director/lingo/lingo.h"
26 
27 namespace Director {
28 
29 struct MethodProto {
30  const char *name;
31  void (*func)(int);
32  int minArgs; // -1 -- arglist
33  int maxArgs;
34  int version;
35 };
36 
38 public:
39  virtual ~AbstractObject() {};
40 
41  virtual Common::String getName() const = 0;
42  virtual ObjectType getObjType() const = 0;
43  virtual bool isDisposed() const = 0;
44  virtual int *getRefCount() const = 0;
45  virtual void incRefCount() = 0;
46  virtual void decRefCount() = 0;
47  virtual int getInheritanceLevel() const = 0;
48 
49  virtual void setName(const Common::String &name) = 0;
50  virtual void dispose() = 0;
51 
52  virtual Common::String asString() = 0;
53  virtual AbstractObject *clone() = 0;
54  virtual Symbol getMethod(const Common::String &methodName) = 0;
55  virtual bool hasProp(const Common::String &propName) = 0;
56  virtual Datum getProp(const Common::String &propName) = 0;
57  virtual Common::String getPropAt(uint32 index) = 0;
58  virtual uint32 getPropCount() = 0;
59  virtual bool setProp(const Common::String &propName, const Datum &value, bool force = false) = 0;
60  virtual bool hasField(int field) = 0;
61  virtual Datum getField(int field) = 0;
62  virtual bool setField(int field, const Datum &value) = 0;
63 };
64 
65 template <typename Derived>
66 class Object : public AbstractObject {
67 public:
68  int *_refCount;
69 
70 protected:
71  Object(Common::String objName) {
72  _name = objName;
73  _objType = kNoneObj;
74  _disposed = false;
75  _inheritanceLevel = 1;
76  _refCount = new int;
77  *_refCount = 0;
78  };
79 
80  Object(const Object &obj) {
81  _name = obj._name;
82  _objType = obj._objType;
83  _disposed = obj._disposed;
84  _inheritanceLevel = obj._inheritanceLevel + 1;
85  _refCount = new int;
86  *_refCount = 0;
87  };
88 
89 public:
90  static void initMethods(MethodProto protos[]) {
91  if (_methods) {
92  warning("Object::initMethods: Methods already initialized");
93  return;
94  }
95 
96  _methods = new SymbolHash;
97  for (MethodProto *mtd = protos; mtd->name; mtd++) {
98  if (mtd->version > g_lingo->_vm->getVersion())
99  continue;
100 
101  Symbol sym;
102  sym.name = new Common::String(mtd->name);
103  sym.type = HBLTIN;
104  sym.nargs = mtd->minArgs;
105  sym.maxArgs = mtd->maxArgs;
106  sym.u.bltin = mtd->func;
107  (*_methods)[mtd->name] = sym;
108  }
109  }
110 
111  static void cleanupMethods() {
112  delete _methods;
113  _methods = nullptr;
114  }
115 
116  virtual ~Object() {
117  delete _refCount;
118  };
119 
120  Common::String getName() const override { return _name; };
121  ObjectType getObjType() const override { return _objType; };
122  bool isDisposed() const override { return _disposed; };
123  int *getRefCount() const override { return _refCount; };
124  void incRefCount() override { *_refCount += 1; };
125  virtual void decRefCount() override {
126  *_refCount -= 1;
127  if (*_refCount <= 0)
128  delete this;
129  };
130  int getInheritanceLevel() const override { return _inheritanceLevel; };
131 
132  void setName(const Common::String &name) override { _name = name; };
133  void dispose() override { _disposed = true; };
134 
135  Common::String asString() override {
136  return Common::String::format("object: #%s %d %p", _name.c_str(), _inheritanceLevel, (void *)this);
137  };
138 
139  AbstractObject *clone() override {
140  return new Derived(static_cast<Derived const &>(*this));
141  };
142 
143  Symbol getMethod(const Common::String &methodName) override {
144  Symbol sym;
145 
146  if (_disposed) {
147  warning("Method '%s' called on disposed object <%s>, returning VOID", methodName.c_str(), asString().c_str());
148  return sym;
149  }
150 
151  Common::String methodId;
152  if ((_objType & (kFactoryObj | kXObj)) && methodName.hasPrefixIgnoreCase("m")) {
153  methodId = methodName.substr(1);
154  } else {
155  methodId = methodName;
156  }
157 
158  if (_methods && _methods->contains(methodId)) {
159  sym = (*_methods)[methodId];
160  sym.target = this;
161  return sym;
162  }
163  if (g_lingo->_methods.contains(methodId) && (g_lingo->_methods[methodId].targetType & _objType)) {
164  sym = g_lingo->_methods[methodId];
165  sym.target = this;
166  return sym;
167  }
168 
169  return sym;
170  };
171 
172  bool hasProp(const Common::String &propName) override {
173  return false;
174  };
175  Datum getProp(const Common::String &propName) override {
176  return Datum();
177  };
178  Common::String getPropAt(uint32 index) override {
179  return Common::String();
180  };
181  uint32 getPropCount() override {
182  return 0;
183  };
184  bool setProp(const Common::String &propName, const Datum &value, bool force = false) override {
185  return false;
186  };
187  bool hasField(int field) override {
188  return false;
189  };
190  Datum getField(int field) override {
191  return Datum();
192  };
193  bool setField(int field, const Datum &value) override {
194  return false;
195  };
196 
197 protected:
198  static SymbolHash *_methods;
199  Common::String _name;
200  ObjectType _objType;
201  bool _disposed;
202  int _inheritanceLevel; // 1 for original object
203 };
204 
205 template<typename Derived>
207 
208 class ScriptContext : public Object<ScriptContext> {
209 public:
210  ScriptType _scriptType;
211  int _id;
212  Common::Array<Common::String> _functionNames; // used by cb_localcall
213  SymbolHash _functionHandlers;
214  Common::HashMap<uint32, Symbol> _eventHandlers;
215  Common::Array<Datum> _constants;
217  MethodHash _methodNames;
218 
219 private:
220  DatumHash _properties;
221  Common::Array<Common::String> _propertyNames;
222  bool _onlyInLctxContexts = false;
223 
224 public:
225  ScriptContext(Common::String name, ScriptType type = kNoneScript, int id = 0);
226  ScriptContext(const ScriptContext &sc);
227  ~ScriptContext() override;
228 
229  bool isFactory() const { return _objType == kFactoryObj; };
230  void setFactory(bool flag) { _objType = flag ? kFactoryObj : kScriptObj; }
231 
232  void setOnlyInLctxContexts() { _onlyInLctxContexts = true; }
233  bool getOnlyInLctxContexts() { return _onlyInLctxContexts; }
234 
235  Common::String asString() override;
236  Symbol getMethod(const Common::String &methodName) override;
237  bool hasProp(const Common::String &propName) override;
238  Datum getProp(const Common::String &propName) override;
239  Common::String getPropAt(uint32 index) override;
240  uint32 getPropCount() override;
241  bool setProp(const Common::String &propName, const Datum &value, bool force = false) override;
242 
243  Symbol define(const Common::String &name, ScriptData *code, Common::Array<Common::String> *argNames, Common::Array<Common::String> *varNames);
244 
245  Common::String formatFunctionList(const char *prefix);
246 };
247 
248 namespace LM {
249 
250 // predefined methods
251 void m_describe(int nargs);
252 void m_dispose(int nargs);
253 void m_get(int nargs);
254 void m_instanceRespondsTo(int nargs);
255 void m_messageList(int nargs);
256 void m_name(int nargs);
257 void m_new(int nargs);
258 void m_perform(int nargs);
259 void m_put(int nargs);
260 void m_respondsTo(int nargs);
261 
262 // window
263 void m_close(int nargs);
264 void m_forget(int nargs);
265 void m_moveToBack(int nargs);
266 void m_moveToFront(int nargs);
267 void m_open(int nargs);
268 
269 } // End of namespace LM
270 
271 } // End of namespace Director
272 
273 #endif
Definition: lingo.h:80
Definition: str.h:59
static String format(MSVC_PRINTF const char *fmt,...) GCC_PRINTF(1
void warning(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
Definition: archive.h:35
Definition: lingo-object.h:66
Definition: lingo-object.h:29
String substr(size_t pos=0, size_t len=npos) const
Definition: lingo.h:129
bool contains(const Key &key) const
Definition: hashmap.h:592
Definition: lingo-object.h:37
Definition: lingo-object.h:208