OpenClonk
C4AulFunc.h
Go to the documentation of this file.
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 
29 {
31 
32  template<class ...T> explicit C4AulParSet(T&& ...pars):
33  Par {C4Value(std::forward<T>(pars))...}
34  {
35  }
36  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 
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:
65  virtual C4AulScriptFunc *SFunc() { return nullptr; } // type check func...
66 
67  // Wether this function should be visible to players
68  virtual bool GetPublic() const { return false; }
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 
#define C4AUL_MAX_Par
Definition: C4AulFunc.h:26
C4V_Type
Definition: C4Value.h:24
virtual bool GetPublic() const
Definition: C4AulFunc.h:68
virtual StdStrBuf GetFullName() const
Definition: C4AulFunc.cpp:38
C4RefCntPointer< C4String > Name
Definition: C4AulFunc.h:60
virtual C4V_Type GetRetType() const =0
C4AulFunc * MapNext
Definition: C4AulFunc.h:61
C4AulFunc(C4PropListStatic *Parent, const char *pName)
Definition: C4AulFunc.cpp:22
virtual C4Value Exec(C4PropList *p, C4Value pPars[], bool fPassErrors=false)=0
const char * GetName() const
Definition: C4AulFunc.h:56
virtual C4AulScriptFunc * SFunc()
Definition: C4AulFunc.h:65
~C4AulFunc() override
Definition: C4AulFunc.cpp:32
bool CheckParTypes(const C4Value pPars[], bool fPassErrors) const
Definition: C4AulFunc.cpp:58
C4Value Exec(C4PropList *p=nullptr, C4AulParSet *pPars=nullptr, bool fPassErrors=false)
Definition: C4AulFunc.h:72
C4PropListStatic * Parent
Definition: C4AulFunc.h:55
virtual const C4V_Type * GetParType() const =0
virtual int GetParCount() const
Definition: C4AulFunc.h:69
const char * GetCStr() const
Definition: C4StringTable.h:49
C4Value Par[C4AUL_MAX_Par]
Definition: C4AulFunc.h:30
C4Value & operator[](int iIdx)
Definition: C4AulFunc.h:41
C4AulParSet(T &&...pars)
Definition: C4AulFunc.h:32
C4AulParSet * operator&()
Definition: C4AulFunc.h:42
void Copy(const C4Value *Pars, int ParCount)
Definition: C4AulFunc.h:36