Leviathan  0.8.0.0
Leviathan game engine
ScriptTypeResolver.h
Go to the documentation of this file.
1 // Leviathan Game Engine
2 // Copyright (c) 2012-2018 Henri Hyyryläinen
3 #pragma once
4 #include "Define.h"
5 // ------------------------------------ //
6 
7 // Forward declare things for type resolver specification
8 class NewtonCollision;
9 
10 class asIScriptFunction;
11 class asIScriptObject;
12 
13 namespace Leviathan {
14 
15 class ScriptExecutor;
16 
18 constexpr auto ANGELSCRIPT_VOID_TYPEID = 0;
19 
22 
25 DLLEXPORT int ResolveProxy(const char* type, ScriptExecutor* resolver, bool constversion);
26 
28 template<class T>
30  static constexpr const char* Type()
31  {
32  if constexpr(std::is_pointer_v<T>) {
33  return std::remove_pointer_t<T>::ANGELSCRIPT_TYPE;
34  } else {
35 
36  return T::ANGELSCRIPT_TYPE;
37  }
38  }
39 };
40 
43 template<class T>
45 public:
46  static int Get(ScriptExecutor* resolver)
47  {
48  static int cached = ResolveProxy(TypeToAngelScriptTypeString<T>::Type(), resolver,
49  std::is_const_v<std::remove_pointer_t<T>>);
50  return cached;
51  }
52 };
53 
54 #define TYPE_RESOLVER_AS_PREDEFINED(x, astype) \
55  template<> \
56  struct TypeToAngelScriptTypeString<x> { \
57  static constexpr const char* Type() \
58  { \
59  return astype; \
60  } \
61  };
62 
63 // Special cases of AngelScriptTypeIDResolver for fundamental types //
64 TYPE_RESOLVER_AS_PREDEFINED(int, "int");
65 TYPE_RESOLVER_AS_PREDEFINED(unsigned int, "uint");
66 TYPE_RESOLVER_AS_PREDEFINED(int64_t, "int64");
67 TYPE_RESOLVER_AS_PREDEFINED(uint64_t, "uint64");
68 TYPE_RESOLVER_AS_PREDEFINED(int16_t, "int16");
70 TYPE_RESOLVER_AS_PREDEFINED(int8_t, "int8");
72 TYPE_RESOLVER_AS_PREDEFINED(double, "double");
73 TYPE_RESOLVER_AS_PREDEFINED(float, "float");
74 TYPE_RESOLVER_AS_PREDEFINED(bool, "bool");
75 // Char isn't actually signed or unsigned in c++ but this allows basically using char as alias
76 // for int8_t without explicit casts to angelscript
77 TYPE_RESOLVER_AS_PREDEFINED(char, "int8");
78 
79 // And other inbuilt types that can't have ANGELSCRIPT_TYPE in their class
80 TYPE_RESOLVER_AS_PREDEFINED(std::string, "string");
81 TYPE_RESOLVER_AS_PREDEFINED(const std::string, "string");
82 
83 // And other types
84 
85 TYPE_RESOLVER_AS_PREDEFINED(NewtonCollision, "NewtonCollision@");
86 
87 // Special case void //
88 template<>
90  static int Get(ScriptExecutor* resolver)
91  {
93  }
94 };
95 
96 // Also these angelscript types that can have a bunch of different
97 // types is a special case
98 template<>
99 struct AngelScriptTypeIDResolver<asIScriptFunction*> {
100  static int Get(ScriptExecutor* resolver)
101  {
102  return -1;
103  }
104 };
105 
106 template<>
107 struct AngelScriptTypeIDResolver<asIScriptObject*> {
108  static int Get(ScriptExecutor* resolver)
109  {
110  return -1;
111  }
112 };
113 } // namespace Leviathan
static int Get(ScriptExecutor *resolver)
constexpr auto ANGELSCRIPT_VOID_TYPEID
This has to be constant (and luckily so far it has been)
Handles ScriptModule creation and AngelScript code execution.
DLLEXPORT ScriptExecutor * GetCurrentGlobalScriptExecutor()
Proxy for stuff that wants to only include this file.
DLLEXPORT int ResolveProxy(const char *type, ScriptExecutor *resolver, bool constversion)
static int Get(ScriptExecutor *resolver)
unsigned char uint8_t
Definition: core.h:38
static constexpr const char * Type()
unsigned short uint16_t
Definition: core.h:39
#define DLLEXPORT
Definition: Include.h:84
Converts type to AngelScript type string.
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
TYPE_RESOLVER_AS_PREDEFINED(int, "int")