Colobot
CBotExternalCall.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/CBotUtils.h"
23 #include "CBot/CBotEnums.h"
24 #include "CBot/CBotDefines.h"
25 
26 #include <string>
27 #include <map>
28 #include <memory>
29 
30 namespace CBot
31 {
32 
33 class CBotStack;
34 class CBotCStack;
35 class CBotVar;
36 class CBotTypResult;
37 class CBotToken;
38 
46 {
47 public:
53 
57  virtual ~CBotExternalCall();
58 
66  virtual CBotTypResult Compile(CBotVar* thisVar, CBotVar* args, void* user) = 0;
67 
75  virtual bool Run(CBotVar* thisVar, CBotStack* pStack) = 0;
76 };
77 
82 {
83 public:
84  typedef bool (*RuntimeFunc)(CBotVar* args, CBotVar* result, int& exception, void* user);
85  typedef CBotTypResult (*CompileFunc)(CBotVar*& args, void* user);
86 
93  CBotExternalCallDefault(RuntimeFunc rExec, CompileFunc rCompile);
94 
98  virtual ~CBotExternalCallDefault();
99 
100  virtual CBotTypResult Compile(CBotVar* thisVar, CBotVar* args, void* user) override;
101  virtual bool Run(CBotVar* thisVar, CBotStack* pStack) override;
102 
103 private:
104  RuntimeFunc m_rExec;
105  CompileFunc m_rComp;
106 };
107 
108 
115 {
116 public:
123  bool AddFunction(const std::string& name, std::unique_ptr<CBotExternalCall> call);
124 
136  CBotTypResult CompileCall(CBotToken*& p, CBotVar* thisVar, CBotVar** ppVars, CBotCStack* pStack);
137 
143  bool CheckCall(const std::string& name);
144 
157  int DoCall(CBotToken* token, CBotVar* thisVar, CBotVar** ppVars, CBotStack* pStack, const CBotTypResult& rettype);
158 
168  bool RestoreCall(CBotToken* token, CBotVar* thisVar, CBotVar** ppVar, CBotStack* pStack);
169 
177  void SetUserPtr(void* pUser);
178 
182  void Clear();
183 
184 private:
185  std::map<std::string, std::unique_ptr<CBotExternalCall>> m_list{};
186  void* m_user = nullptr;
187 };
188 
189 } // namespace CBot
Class for mangaging CBot external calls.
Definition: CBotExternalCall.h:114
The execution stack.
Definition: CBotStack.h:44
Interface for external CBot calls.
Definition: CBotExternalCall.h:45
virtual CBotTypResult Compile(CBotVar *thisVar, CBotVar *args, void *user)=0
Compile the function.
A CBot variable.
Definition: CBotVar.h:42
Some enum values used across the CBot engine.
The CBotCStack class Management of the stack of compilation.
Definition: CBotCStack.h:35
CBotExternalCall()
Constructor.
Definition: CBotExternalCall.cpp:119
Default implementation of CBot external call, using compilation and runtime functions.
Definition: CBotExternalCall.h:81
Class to define the complete type of a variable.
Definition: CBotTypResult.h:47
CBot engine.
Definition: CBotCallMethode.cpp:28
virtual ~CBotExternalCall()
Destructor.
Definition: CBotExternalCall.cpp:123
virtual bool Run(CBotVar *thisVar, CBotStack *pStack)=0
Execute the function.
Class representing one token of a program.
Definition: CBotToken.h:80