OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4AulDefFunc.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 // Template helper to export C++ functions to C4Script
17 
18 #ifndef INC_C4AulDefFunc
19 #define INC_C4AulDefFunc
20 
21 #include "script/C4Aul.h"
22 #include "object/C4Def.h"
23 #include "object/C4DefList.h"
24 #include "script/C4Effect.h"
25 
26 inline const static char *FnStringPar(C4String *pString)
27 {
28  return pString ? pString->GetCStr() : "";
29 }
30 inline C4String *String(const char * str)
31 {
32  return str ? ::Strings.RegString(str) : nullptr;
33 }
34 inline C4Object * Object(C4PropList * _this)
35 {
36  return _this ? _this->GetObject() : nullptr;
37 }
38 StdStrBuf FnStringFormat(C4PropList * _this, C4String *szFormatPar, C4Value * Pars, int ParCount);
39 C4Effect ** FnGetEffectsFor(C4PropList * pTarget);
40 
41 // Nillable: Allow integer and boolean parameters to be nil
42 // pointer parameters represent nil via plain nullptr
43 // other types can use C4Void
44 class C4Void { };
45 template <typename T> struct C4ValueConv;
46 template <typename T>
47 class Nillable
48 {
49  bool _nil;
50  T _val;
51 public:
52  inline Nillable(const T &value) : _nil(!value && !C4Value::IsNullableType(C4ValueConv<T>::Type)), _val(value) {}
53  inline Nillable() : _nil(true), _val(T()) {}
54  inline Nillable(std::nullptr_t) : _nil(true), _val(T()) {}
55  template <typename T2> inline Nillable(const Nillable<T2> & n2) : _nil(n2._nil), _val(n2._val) {}
56  inline Nillable(const C4Void &) : _nil(true), _val(T()) {}
57  inline bool IsNil() const { return _nil; }
58  inline operator T() const { return _val; }
59  inline Nillable<T> &operator =(const T &val)
60  {
61  _val = val;
63  return *this;
64  }
65  inline Nillable<T> &operator =(const Nillable<T> &val)
66  {
67  _val = val._val;
68  _nil = val._nil;
69  return *this;
70  }
71  // Some operators
72  inline Nillable<T> &operator ++() { ++_val; return *this; }
73  inline T operator ++(int) { T v(_val++); return v; }
74  inline Nillable<T> &operator --() { --_val; return *this; }
75  inline T operator --(int) { T v(_val--); return v; }
76 };
77 
78 // Other functions are callable in object context only.
79 // This exception gets thrown if they are called from anywhere else.
81 {
82 public:
83  NeedObjectContext(const char *function) : C4AulExecError(FormatString("%s: must be called from object context", function).getData()) {}
84 };
85 
86 // Then there's functions that don't care, but need either defn or object context.
87 // This exception gets thrown if those are called from global scripts.
89 {
90 public:
91  NeedNonGlobalContext(const char *function) : C4AulExecError(FormatString("%s: call must not be from global context", function).getData()) {}
92 };
93 
94 // For functions taking a C4Object as this, check that they got one
95 template <typename ThisType> struct ThisImpl;
96 template <> struct ThisImpl<C4Object>
97 {
98  static C4Object* Conv(C4PropList* _this, C4AulFunc* func)
99  {
100  C4Object* Obj = _this ? _this->GetObject() : nullptr;
101  if (Obj)
102  return Obj;
103  else
104  throw NeedObjectContext(func->GetName());
105  }
106 };
107 template <> struct ThisImpl<C4PropList>
108 {
109  static C4PropList* Conv(C4PropList* _this, C4AulFunc* func)
110  {
111  return _this;
112  }
113 };
114 
115 // Extracts the parameters from C4Values and wraps the return value in a C4Value
116 template <typename RType, typename ThisType, typename ...ParTypes>
117 struct ExecImpl
118 {
119  template <std::size_t... Is>
120  static C4Value Exec(RType (*pFunc)(ThisType *, ParTypes...), ThisType * _this, C4Value pPars[], std::index_sequence<Is...>)
121  {
122  return C4Value(pFunc(_this, C4ValueConv<ParTypes>::_FromC4V(pPars[Is])...));
123  (void) pPars;
124  }
125 };
126 template <typename ThisType, typename ...ParTypes>
127 struct ExecImpl<void, ThisType, ParTypes...>
128 {
129  template <std::size_t... Is>
130  static C4Value Exec(void (*pFunc)(ThisType *, ParTypes...), ThisType * _this, C4Value pPars[], std::index_sequence<Is...>)
131  {
132  pFunc(_this, C4ValueConv<ParTypes>::_FromC4V(pPars[Is])...);
133  return C4Value();
134  (void) pPars;
135  }
136 };
137 
138 // converter templates
139 template <typename T>
141 {
142  inline static Nillable<T> _FromC4V(C4Value &v) { if (v.GetType() == C4V_Nil) return C4Void(); else return C4ValueConv<T>::_FromC4V(v); }
143  static constexpr C4V_Type Type = C4ValueConv<T>::Type;
144 };
145 template <> struct C4ValueConv<void>
146 {
147  static constexpr C4V_Type Type = C4V_Nil;
148 };
149 template <> struct C4ValueConv<int>
150 {
151  static constexpr C4V_Type Type = C4V_Int;
152  inline static int _FromC4V(C4Value &v) { return v._getInt(); }
153 };
154 template <> struct C4ValueConv<long>: public C4ValueConv<int> { };
155 template <> struct C4ValueConv<bool>
156 {
157  static constexpr C4V_Type Type = C4V_Bool;
158  inline static bool _FromC4V(C4Value &v) { return v._getBool(); }
159 };
160 template <> struct C4ValueConv<C4ID>
161 {
162  static constexpr C4V_Type Type = C4V_PropList;
163  inline static C4ID _FromC4V(C4Value &v) { C4Def * def = v.getDef(); return def ? def->id : C4ID::None; }
164 };
165 template <> struct C4ValueConv<C4Object *>
166 {
167  static constexpr C4V_Type Type = C4V_Object;
168  inline static C4Object *_FromC4V(C4Value &v) { return v._getObj(); }
169 };
170 template <> struct C4ValueConv<C4String *>
171 {
172  static constexpr C4V_Type Type = C4V_String;
173  inline static C4String *_FromC4V(C4Value &v) { return v._getStr(); }
174 };
175 template <> struct C4ValueConv<C4ValueArray *>
176 {
177  static constexpr C4V_Type Type = C4V_Array;
178  inline static C4ValueArray *_FromC4V(C4Value &v) { return v._getArray(); }
179 };
180 template <> struct C4ValueConv<C4AulFunc *>
181 {
182  static constexpr C4V_Type Type = C4V_Function;
183  inline static C4AulFunc *_FromC4V(C4Value &v) { return v._getFunction(); }
184 };
185 template <> struct C4ValueConv<C4PropList *>
186 {
187  static constexpr C4V_Type Type = C4V_PropList;
188  inline static C4PropList *_FromC4V(C4Value &v) { return v._getPropList(); }
189 };
190 template <> struct C4ValueConv<C4Effect *>
191 {
192  static constexpr C4V_Type Type = C4V_Effect;
193  inline static C4Effect *_FromC4V(C4Value &v) { C4PropList * p = v._getPropList(); return p ? p->GetEffect() : nullptr; }
194 };
195 template <> struct C4ValueConv<C4Def *>
196 {
197  static constexpr C4V_Type Type = C4V_Def;
198  inline static C4Def *_FromC4V(C4Value &v) { return v._getDef(); }
199 };
200 template <> struct C4ValueConv<const C4Value &>
201 {
202  static constexpr C4V_Type Type = C4V_Any;
203  inline static const C4Value &_FromC4V(C4Value &v) { return v; }
204 };
205 template <> struct C4ValueConv<C4Value>
206 {
207  static constexpr C4V_Type Type = C4V_Any;
208  inline static C4Value _FromC4V(C4Value &v) { return v; }
209 };
210 
211 // Wrapper around an ordinary C++ function callable from C4Script
212 template <typename RType, typename ThisType, typename ...ParTypes>
214 {
215 public:
216  /* A pointer to the function which this class wraps */
217  typedef RType (*Func)(ThisType *, ParTypes...);
218 
219  C4AulEngineFunc(C4PropListStatic * Parent, const char *pName, Func pFunc, bool Public):
220  C4AulFunc(Parent, pName),
221  pFunc(pFunc), ParType {C4ValueConv<ParTypes>::Type...}, Public(Public)
222  {
224  for(int i = GetParCount(); i < C4AUL_MAX_Par; ++i)
225  ParType[i] = C4V_Any;
226  }
227 
228  int GetParCount() const override
229  {
230  return sizeof...(ParTypes);
231  }
232 
233  const C4V_Type* GetParType() const override
234  {
235  return ParType;
236  }
237 
238  C4V_Type GetRetType() const override
239  {
241  }
242 
243  bool GetPublic() const override
244  {
245  return Public;
246  }
247 
248  C4Value Exec(C4PropList * _this, C4Value pPars[], bool fPassErrors) override
249  {
251  pPars, std::index_sequence_for<ParTypes...>{});
252  }
253 protected:
255  C4V_Type ParType[C4AUL_MAX_Par];// type of the parameters
256  bool Public;
257 };
258 
259 template <typename RType, typename ThisType, typename ...ParTypes>
260 inline void AddFunc(C4PropListStatic * Parent, const char * Name, RType (*pFunc)(ThisType *, ParTypes...), bool Public=true)
261 {
262  new C4AulEngineFunc<RType, ThisType, ParTypes...>(Parent, Name, pFunc, Public);
263 }
264 
265 // a definition of a script constant
267 {
268  const char * Identifier; // constant name
269  C4V_Type ValType; // type value
270  long Data; // raw data
271 };
272 
273 // a definition of a function exported to script
275 {
276  const char * Identifier; // the name of the func in the script
277  bool Public;
278  C4V_Type RetType; // type returned. ignored when C4V
279  C4V_Type ParType[10];// type of the parameters. error when wrong parameter type.
281 };
282 
283 // defined function class
285 {
286 public:
288 
290  ~C4AulDefFunc() override;
291 
292  bool GetPublic() const override { return !!Def->Public; }
293  const C4V_Type* GetParType() const override { return Def->ParType; }
294  C4V_Type GetRetType() const override { return Def->RetType; }
295 
296  C4Value Exec(C4PropList * p, C4Value pPars[], bool fPassErrors=false) override;
297 };
298 
299 #endif
RType(* Func)(ThisType *, ParTypes...)
Definition: C4AulDefFunc.h:217
C4ID id
Definition: C4Def.h:101
C4V_Type
Definition: C4Value.h:23
const C4V_Type * GetParType() const override
Definition: C4AulDefFunc.h:293
static C4Def * _FromC4V(C4Value &v)
Definition: C4AulDefFunc.h:198
int GetParCount() const override
Definition: C4AulDefFunc.h:228
static C4Value Exec(RType(*pFunc)(ThisType *, ParTypes...), ThisType *_this, C4Value pPars[], std::index_sequence< Is...>)
Definition: C4AulDefFunc.h:120
C4Value Exec(C4PropList *_this, C4Value pPars[], bool fPassErrors) override
Definition: C4AulDefFunc.h:248
C4String * String(const char *str)
Definition: C4AulDefFunc.h:30
bool _getBool() const
Definition: C4Value.h:123
C4Value(* FunctionC4V)(C4PropList *_this, C4Value *)
Definition: C4AulDefFunc.h:280
C4AulDefFunc(C4PropListStatic *Parent, C4ScriptFnDef *pDef)
Definition: C4Script.cpp:110
const char * GetName() const
Definition: C4AulFunc.h:56
static C4ID _FromC4V(C4Value &v)
Definition: C4AulDefFunc.h:163
static const C4Value & _FromC4V(C4Value &v)
Definition: C4AulDefFunc.h:203
const char * GetCStr() const
Definition: C4StringTable.h:49
C4String * RegString(StdStrBuf String)
C4String * _getStr() const
Definition: C4Value.h:126
NeedObjectContext(const char *function)
Definition: C4AulDefFunc.h:83
static C4Value Exec(void(*pFunc)(ThisType *, ParTypes...), ThisType *_this, C4Value pPars[], std::index_sequence< Is...>)
Definition: C4AulDefFunc.h:130
C4V_Type ParType[C4AUL_MAX_Par]
Definition: C4AulDefFunc.h:255
virtual C4Object * GetObject()
Definition: C4PropList.cpp:667
C4V_Type ParType[10]
Definition: C4AulDefFunc.h:279
C4Value Exec(C4PropList *p, C4Value pPars[], bool fPassErrors=false) override
Definition: C4Script.cpp:118
static int _FromC4V(C4Value &v)
Definition: C4AulDefFunc.h:152
C4ValueArray * _getArray() const
Definition: C4Value.h:127
static bool _FromC4V(C4Value &v)
Definition: C4AulDefFunc.h:158
#define C4AUL_MAX_Par
Definition: C4AulFunc.h:26
static C4ValueArray * _FromC4V(C4Value &v)
Definition: C4AulDefFunc.h:178
bool IsNil() const
Definition: C4AulDefFunc.h:57
C4AulEngineFunc(C4PropListStatic *Parent, const char *pName, Func pFunc, bool Public)
Definition: C4AulDefFunc.h:219
Nillable(const Nillable< T2 > &n2)
Definition: C4AulDefFunc.h:55
static C4AulFunc * _FromC4V(C4Value &v)
Definition: C4AulDefFunc.h:183
~C4AulDefFunc() override
C4V_Type GetType() const
Definition: C4Value.h:161
C4StringTable Strings
Definition: C4Globals.cpp:42
C4Object * _getObj() const
Definition: C4Value.cpp:73
C4V_Type GetRetType() const override
Definition: C4AulDefFunc.h:238
const char * Identifier
Definition: C4AulDefFunc.h:276
C4V_Type GetRetType() const override
Definition: C4AulDefFunc.h:294
static const C4ID None
Definition: C4Id.h:39
C4Def * _getDef() const
Definition: C4Value.cpp:83
Nillable< T > & operator=(const T &val)
Definition: C4AulDefFunc.h:59
int32_t _getInt() const
Definition: C4Value.h:122
Nillable(std::nullptr_t)
Definition: C4AulDefFunc.h:54
Definition: C4Def.h:98
static C4PropList * _FromC4V(C4Value &v)
Definition: C4AulDefFunc.h:188
Definition: C4Id.h:25
virtual C4Effect * GetEffect()
Definition: C4PropList.cpp:709
NeedNonGlobalContext(const char *function)
Definition: C4AulDefFunc.h:91
static constexpr bool IsNullableType(C4V_Type Type)
Definition: C4Value.h:212
Nillable(const C4Void &)
Definition: C4AulDefFunc.h:56
C4Object * Object(C4PropList *_this)
Definition: C4AulDefFunc.h:34
static C4Object * Conv(C4PropList *_this, C4AulFunc *func)
Definition: C4AulDefFunc.h:98
const char * Identifier
Definition: C4AulDefFunc.h:268
C4AulFunc * _getFunction() const
Definition: C4Value.h:128
static Nillable< T > _FromC4V(C4Value &v)
Definition: C4AulDefFunc.h:142
C4ScriptFnDef * Def
Definition: C4AulDefFunc.h:287
C4V_Type RetType
Definition: C4AulDefFunc.h:278
C4Def * getDef() const
Definition: C4Value.cpp:78
bool GetPublic() const override
Definition: C4AulDefFunc.h:292
Nillable< T > & operator--()
Definition: C4AulDefFunc.h:74
static C4Object * _FromC4V(C4Value &v)
Definition: C4AulDefFunc.h:168
C4Value C4VFunction(C4AulFunc *pFn)
Definition: C4Value.h:250
virtual void SetPropertyByS(C4String *k, const C4Value &to)
Definition: C4PropList.cpp:952
C4PropListStatic * Parent
Definition: C4AulFunc.h:55
Nillable(const T &value)
Definition: C4AulDefFunc.h:52
C4Effect ** FnGetEffectsFor(C4PropList *pTarget)
static C4Effect * _FromC4V(C4Value &v)
Definition: C4AulDefFunc.h:193
C4RefCntPointer< C4String > Name
Definition: C4AulFunc.h:60
C4PropList * _getPropList() const
Definition: C4Value.h:129
StdStrBuf FnStringFormat(C4PropList *_this, C4String *szFormatPar, C4Value *Pars, int ParCount)
Definition: C4Script.cpp:29
static C4String * _FromC4V(C4Value &v)
Definition: C4AulDefFunc.h:173
static C4Value _FromC4V(C4Value &v)
Definition: C4AulDefFunc.h:208
void AddFunc(C4PropListStatic *Parent, const char *Name, RType(*pFunc)(ThisType *, ParTypes...), bool Public=true)
Definition: C4AulDefFunc.h:260
Nillable< T > & operator++()
Definition: C4AulDefFunc.h:72
bool GetPublic() const override
Definition: C4AulDefFunc.h:243
static C4PropList * Conv(C4PropList *_this, C4AulFunc *func)
Definition: C4AulDefFunc.h:109
const C4V_Type * GetParType() const override
Definition: C4AulDefFunc.h:233
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:270