OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
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  C4AulParSet() = default; // standard-constructor
33  template<class ...T> explicit C4AulParSet(T&& ...pars):
34  Par {C4Value(std::forward<T>(pars))...}
35  {
36  }
37  void Copy(const C4Value * Pars, int ParCount)
38  {
39  for (int i = 0; i < ParCount; ++i)
40  Par[i].Set(Pars[i]);
41  }
42  C4Value & operator[](int iIdx) { return Par[iIdx]; }
43  C4AulParSet * operator&() { return this; }
44 };
45 
46 // base function class
47 class C4AulFunc: public C4RefCnt
48 {
49  friend class C4AulScriptEngine;
50  friend class C4AulFuncMap;
51  friend class C4AulParse;
52  friend class C4ScriptHost;
53 public:
54  C4AulFunc(C4PropListStatic * Parent, const char *pName);
55 
57  const char * GetName() const { return Name ? Name->GetCStr() : nullptr; }
58  virtual StdStrBuf GetFullName() const; // get a fully classified name (C4ID::Name) for debug output
59 
60 protected:
61  C4RefCntPointer<C4String> Name; // function name
62  C4AulFunc *MapNext; // map member
63  ~C4AulFunc() override;
64 
65 public:
66  virtual C4AulScriptFunc *SFunc() { return nullptr; } // type check func...
67 
68  // Wether this function should be visible to players
69  virtual bool GetPublic() const { return false; }
70  virtual int GetParCount() const { return C4AUL_MAX_Par; }
71  virtual const C4V_Type* GetParType() const = 0;
72  virtual C4V_Type GetRetType() const = 0;
73  C4Value Exec(C4PropList * p = nullptr, C4AulParSet *pPars = nullptr, bool fPassErrors=false)
74  {
75  // Every parameter type allows conversion from nil, so no parameters are always allowed
76  if (!pPars)
77  return Exec(p, C4AulParSet().Par, fPassErrors);
78  if (!CheckParTypes(pPars->Par, fPassErrors)) return C4Value();
79  return Exec(p, pPars->Par, fPassErrors);
80  }
81  virtual C4Value Exec(C4PropList * p, C4Value pPars[], bool fPassErrors=false) = 0;
82  bool CheckParTypes(const C4Value pPars[], bool fPassErrors) const;
83 };
84 
85 #endif
86 
C4AulParSet * operator&()
Definition: C4AulFunc.h:43
virtual bool GetPublic() const
Definition: C4AulFunc.h:69
C4V_Type
Definition: C4Value.h:23
const char * GetName() const
Definition: C4AulFunc.h:57
const char * GetCStr() const
Definition: C4StringTable.h:49
C4Value Par[C4AUL_MAX_Par]
Definition: C4AulFunc.h:30
C4AulParSet()=default
virtual const C4V_Type * GetParType() const =0
~C4AulFunc() override
Definition: C4AulFunc.cpp:32
#define C4AUL_MAX_Par
Definition: C4AulFunc.h:26
C4AulFunc(C4PropListStatic *Parent, const char *pName)
Definition: C4AulFunc.cpp:22
virtual C4V_Type GetRetType() const =0
C4AulParSet(T &&...pars)
Definition: C4AulFunc.h:33
C4Value & operator[](int iIdx)
Definition: C4AulFunc.h:42
void Copy(const C4Value *Pars, int ParCount)
Definition: C4AulFunc.h:37
C4AulFunc * MapNext
Definition: C4AulFunc.h:62
virtual StdStrBuf GetFullName() const
Definition: C4AulFunc.cpp:38
C4Value Exec(C4PropList *p=nullptr, C4AulParSet *pPars=nullptr, bool fPassErrors=false)
Definition: C4AulFunc.h:73
bool CheckParTypes(const C4Value pPars[], bool fPassErrors) const
Definition: C4AulFunc.cpp:58
C4PropListStatic * Parent
Definition: C4AulFunc.h:56
C4RefCntPointer< C4String > Name
Definition: C4AulFunc.h:61
virtual C4AulScriptFunc * SFunc()
Definition: C4AulFunc.h:66
virtual int GetParCount() const
Definition: C4AulFunc.h:70