Colobot
CBotClass.h
1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2016, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsitec.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
20 #pragma once
21 
22 #include "CBot/CBotDefines.h"
23 #include "CBot/CBotTypResult.h"
24 #include "CBot/CBotVar/CBotVar.h"
25 
26 #include <string>
27 #include <deque>
28 #include <set>
29 
30 namespace CBot
31 {
32 
33 class CBotCallMethode;
34 class CBotFunction;
35 class CBotProgram;
36 class CBotStack;
37 class CBotDefParam;
38 class CBotToken;
39 class CBotCStack;
40 
105 class CBotClass : public CBotLinkedList<CBotClass>
106 {
107 public:
115  CBotClass(const std::string& name,
116  CBotClass* parent,
117  bool bIntrinsic = false);
118 
122  ~CBotClass( );
123 
131  static CBotClass* Create(const std::string& name,
132  CBotClass* parent,
133  bool intrinsic = false);
134 
144  bool AddFunction(const std::string& name,
145  bool rExec(CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception, void* user),
146  CBotTypResult rCompile(CBotVar* pThis, CBotVar*& pVar));
147 
154  bool SetUpdateFunc(void rUpdate(CBotVar* thisVar, void* user));
155  //
156 
164  bool AddItem(std::string name, CBotTypResult type,
166 
173  bool AddItem(CBotVar* pVar);
174 
179  std::string GetName();
180 
185  CBotClass* GetParent();
186 
192  bool IsChildOf(CBotClass* pClass);
193 
199  static CBotClass* Find(CBotToken* &pToken);
200 
206  static CBotClass* Find(const std::string& name);
207 
212  CBotVar* GetVar();
218  CBotVar* GetItem(const std::string& name);
219 
225  CBotVar* GetItemRef(int nIdent);
226 
237  CBotTypResult CompileMethode(const std::string& name,
238  CBotVar* pThis,
239  CBotVar** ppParams,
240  CBotCStack* pStack,
241  long& nIdent);
242 
254  bool ExecuteMethode(long& nIdent,
255  const std::string& name,
256  CBotVar* pThis,
257  CBotVar** ppParams,
258  CBotVar*& pResult,
259  CBotStack*& pStack,
260  CBotToken* pToken);
261 
270  void RestoreMethode(long& nIdent,
271  const std::string& name,
272  CBotVar* pThis,
273  CBotVar** ppParams,
274  CBotStack*& pStack);
275 
282  static CBotClass* Compile(CBotToken* &p,
283  CBotCStack* pStack);
284 
291  static CBotClass* Compile1(CBotToken* &p,
292  CBotCStack* pStack);
293 
301  bool CompileDefItem(CBotToken* &p,
302  CBotCStack* pStack,
303  bool bSecond);
304 
309  bool IsIntrinsic();
310 
314  void Purge();
315 
319  static void ClearPublic();
320 
326  static bool SaveStaticState(FILE* pf);
327 
333  static bool RestoreStaticState(FILE* pf);
334 
340  bool Lock(CBotProgram* prog);
341 
346  void Unlock();
347 
352  static void FreeLock(CBotProgram* prog);
353 
361  bool CheckCall(CBotProgram* program, CBotDefParam* pParam, CBotToken*& pToken);
362 
363  void Update(CBotVar* var, void* user);
364 
365 private:
367  static std::set<CBotClass*> m_publicClasses;
368 
369 
371  bool m_IsDef;
373  std::string m_name;
375  CBotClass* m_parent;
377  int m_nbVar;
379  bool m_bIntrinsic;
381  CBotVar* m_pVar;
383  CBotCallMethode* m_pCalls;
385  CBotFunction* m_pMethod;
386  void (*m_rUpdate)(CBotVar* thisVar, void* user);
387 
389  int m_lockCurrentCount = 0;
391  std::deque<CBotProgram*> m_lockProg{};
392 };
393 
394 } // namespace CBot
The CBotCallMethode class Class managing the methods declared by AddFunction on a class...
Definition: CBotCallMethode.h:37
A function declaration in the code.
Definition: CBotFunction.h:42
static CBotClass * Compile(CBotToken *&p, CBotCStack *pStack)
Compile Compiles a class declared by the user.
Definition: CBotClass.cpp:768
The CBotDefParam class A list of parameters.
Definition: CBotDefParam.h:36
bool ExecuteMethode(long &nIdent, const std::string &name, CBotVar *pThis, CBotVar **ppParams, CBotVar *&pResult, CBotStack *&pStack, CBotToken *pToken)
ExecuteMethode Executes a method.
Definition: CBotClass.cpp:341
CBotClass(const std::string &name, CBotClass *parent, bool bIntrinsic=false)
CBotClass Constructor. Once a class is created, it is known around CBot intrinsic mode gives a class ...
Definition: CBotClass.cpp:51
Class that manages a CBot program. This is the main entry point into the CBot engine.
Definition: CBotProgram.h:83
std::string GetName()
GetName Gives the name of the class.
Definition: CBotClass.cpp:200
CBotVar * GetItem(const std::string &name)
GetItem One of the variables according to its name.
Definition: CBotClass.cpp:231
The execution stack.
Definition: CBotStack.h:44
bool IsChildOf(CBotClass *pClass)
IsChildOf True if a class is derived (Extends) of another.
Definition: CBotClass.cpp:213
void Purge()
Purge.
Definition: CBotClass.cpp:93
bool CheckCall(CBotProgram *program, CBotDefParam *pParam, CBotToken *&pToken)
CheckCall Test if a procedure name is already defined somewhere.
Definition: CBotClass.cpp:447
A CBot class definition.
Definition: CBotClass.h:105
static bool RestoreStaticState(FILE *pf)
RestoreStaticState.
Definition: CBotClass.cpp:409
bool IsIntrinsic()
IsIntrinsic.
Definition: CBotClass.cpp:259
bool AddItem(std::string name, CBotTypResult type, CBotVar::ProtectionLevel mPrivate=CBotVar::ProtectionLevel::Public)
AddItem Adds an element to the class.
Definition: CBotClass.cpp:164
A CBot variable.
Definition: CBotVar.h:42
bool SetUpdateFunc(void rUpdate(CBotVar *thisVar, void *user))
SetUpdateFunc Defines routine to be called to update the elements of the class.
Definition: CBotClass.cpp:312
ProtectionLevel
Class member protection level (public/protected/private)
Definition: CBotVar.h:306
The CBotCStack class Management of the stack of compilation.
Definition: CBotCStack.h:35
static void ClearPublic()
Free.
Definition: CBotClass.cpp:87
~CBotClass()
CBotClass Destructor.
Definition: CBotClass.cpp:69
static CBotClass * Compile1(CBotToken *&p, CBotCStack *pStack)
Compile1.
Definition: CBotClass.cpp:469
bool CompileDefItem(CBotToken *&p, CBotCStack *pStack, bool bSecond)
CompileDefItem.
Definition: CBotClass.cpp:525
CBotClass * GetParent()
GetParent Gives the parent class (or nullptr).
Definition: CBotClass.cpp:206
void RestoreMethode(long &nIdent, const std::string &name, CBotVar *pThis, CBotVar **ppParams, CBotStack *&pStack)
RestoreMethode Restored the execution stack.
Definition: CBotClass.cpp:365
bool AddFunction(const std::string &name, bool rExec(CBotVar *pThis, CBotVar *pVar, CBotVar *pResult, int &Exception, void *user), CBotTypResult rCompile(CBotVar *pThis, CBotVar *&pVar))
AddFunction This call allows to add as external new method used by the objects of this class...
Definition: CBotClass.cpp:282
static CBotClass * Create(const std::string &name, CBotClass *parent, bool intrinsic=false)
Create.
Definition: CBotClass.cpp:79
CBotVar * GetVar()
GetVar Return the list of variables.
Definition: CBotClass.cpp:225
bool Lock(CBotProgram *prog)
Request a lock on this class (for "synchronized" keyword)
Definition: CBotClass.cpp:112
CBotTypResult CompileMethode(const std::string &name, CBotVar *pThis, CBotVar **ppParams, CBotCStack *pStack, long &nIdent)
CompileMethode Compiles a method associated with an instance of class the method can be declared by t...
Definition: CBotClass.cpp:319
Definition: CBotUtils.h:99
CBotVar * GetItemRef(int nIdent)
GetItemRef.
Definition: CBotClass.cpp:245
static bool SaveStaticState(FILE *pf)
SaveStaticState.
Definition: CBotClass.cpp:375
static void FreeLock(CBotProgram *prog)
Release all locks in all classes held by this program.
Definition: CBotClass.cpp:145
Class to define the complete type of a variable.
Definition: CBotTypResult.h:47
CBot engine.
Definition: CBotCallMethode.cpp:28
Class representing one token of a program.
Definition: CBotToken.h:80
void Unlock()
Release the lock acquired in Lock() If you call Lock() multiple times for the same program...
Definition: CBotClass.cpp:137
static CBotClass * Find(CBotToken *&pToken)
Find Trouve une classe d&#39;après son nom.
Definition: CBotClass.cpp:265