1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 
17 #ifndef INC_C4AulFunc
18 #define INC_C4AulFunc
19 
20 #ifndef INC_C4Value
21 #error Include C4Value.h instead of C4AulFunc.h
22 #endif
23 
24 #include "script/C4StringTable.h"
25 
26 #define C4AUL_MAX_Par         10  // max number of parameters
27 
28 struct C4AulParSet
29 {
30 	C4Value Par[C4AUL_MAX_Par];
31 
C4AulParSetC4AulParSet32 	template<class ...T> explicit C4AulParSet(T&& ...pars):
33 			Par {C4Value(std::forward<T>(pars))...}
34 	{
35 	}
CopyC4AulParSet36 	void Copy(const C4Value * Pars, int ParCount)
37 	{
38 		for (int i = 0; i < ParCount; ++i)
39 			Par[i].Set(Pars[i]);
40 	}
41 	C4Value & operator[](int iIdx) { return Par[iIdx]; }
42 	C4AulParSet * operator&() { return this; }
43 };
44 
45 // base function class
46 class C4AulFunc: public C4RefCnt
47 {
48 	friend class C4AulScriptEngine;
49 	friend class C4AulFuncMap;
50 	friend class C4AulParse;
51 	friend class C4ScriptHost;
52 public:
53 	C4AulFunc(C4PropListStatic * Parent, const char *pName);
54 
55 	C4PropListStatic * Parent;
GetName()56 	const char * GetName() const { return Name ? Name->GetCStr() : nullptr; }
57 	virtual StdStrBuf GetFullName() const; // get a fully classified name (C4ID::Name) for debug output
58 
59 protected:
60 	C4RefCntPointer<C4String> Name; // function name
61 	C4AulFunc *MapNext; // map member
62 	~C4AulFunc() override;
63 
64 public:
SFunc()65 	virtual C4AulScriptFunc *SFunc() { return nullptr; } // type check func...
66 
67 	// Wether this function should be visible to players
GetPublic()68 	virtual bool GetPublic() const { return false; }
GetParCount()69 	virtual int GetParCount() const { return C4AUL_MAX_Par; }
70 	virtual const C4V_Type* GetParType() const = 0;
71 	virtual C4V_Type GetRetType() const = 0;
72 	C4Value Exec(C4PropList * p = nullptr, C4AulParSet *pPars = nullptr, bool fPassErrors=false)
73 	{
74 		// Every parameter type allows conversion from nil, so no parameters are always allowed
75 		if (!pPars)
76 			return Exec(p, C4AulParSet().Par, fPassErrors);
77 		if (!CheckParTypes(pPars->Par, fPassErrors)) return C4Value();
78 		return Exec(p, pPars->Par, fPassErrors);
79 	}
80 	virtual C4Value Exec(C4PropList * p, C4Value pPars[], bool fPassErrors=false) = 0;
81 	bool CheckParTypes(const C4Value pPars[], bool fPassErrors) const;
82 };
83 
84 #endif
85 
86