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 namespace bs {
9 class Matrix4;
10 class Color;
11 } // namespace bs
12 
13 class NewtonCollision;
14 
15 class asIScriptFunction;
16 class asIScriptObject;
17 
18 namespace Leviathan {
19 
20 class ScriptExecutor;
21 
23 constexpr auto ANGELSCRIPT_VOID_TYPEID = 0;
24 
27 
30 DLLEXPORT int ResolveProxy(const char* type, ScriptExecutor* resolver, bool constversion);
31 
33 template<class T>
35  static constexpr const char* Type()
36  {
37  if constexpr(std::is_pointer_v<T>) {
38  return std::remove_pointer_t<T>::ANGELSCRIPT_TYPE;
39  } else {
40 
41  return T::ANGELSCRIPT_TYPE;
42  }
43  }
44 };
45 
48 template<class T>
50 public:
51  static int Get(ScriptExecutor* resolver)
52  {
53  static int cached = ResolveProxy(TypeToAngelScriptTypeString<T>::Type(), resolver,
54  std::is_const_v<std::remove_pointer_t<T>>);
55  return cached;
56  }
57 };
58 
59 #define TYPE_RESOLVER_AS_PREDEFINED(x, astype) \
60  template<> \
61  struct TypeToAngelScriptTypeString<x> { \
62  static constexpr const char* Type() \
63  { \
64  return astype; \
65  } \
66  };
67 
68 // Special cases of AngelScriptTypeIDResolver for fundamental types //
69 TYPE_RESOLVER_AS_PREDEFINED(int, "int");
70 TYPE_RESOLVER_AS_PREDEFINED(unsigned int, "uint");
71 TYPE_RESOLVER_AS_PREDEFINED(int64_t, "int64");
72 TYPE_RESOLVER_AS_PREDEFINED(uint64_t, "uint64");
73 TYPE_RESOLVER_AS_PREDEFINED(int16_t, "int16");
75 TYPE_RESOLVER_AS_PREDEFINED(int8_t, "int8");
77 TYPE_RESOLVER_AS_PREDEFINED(double, "double");
78 TYPE_RESOLVER_AS_PREDEFINED(float, "float");
79 TYPE_RESOLVER_AS_PREDEFINED(bool, "bool");
80 // Char isn't actually signed or unsigned in c++ but this allows basically using char as alias
81 // for int8_t without explicit casts to angelscript
82 TYPE_RESOLVER_AS_PREDEFINED(char, "int8");
83 
84 // And other inbuilt types that can't have ANGELSCRIPT_TYPE in their class
85 TYPE_RESOLVER_AS_PREDEFINED(std::string, "string");
86 
87 // And other types
88 TYPE_RESOLVER_AS_PREDEFINED(bs::Color, "bs::Color@");
89 TYPE_RESOLVER_AS_PREDEFINED(bs::Matrix4, "bs::Matrix4@");
90 
91 TYPE_RESOLVER_AS_PREDEFINED(NewtonCollision, "NewtonCollision@");
92 
93 // Special case void //
94 template<>
96  static int Get(ScriptExecutor* resolver)
97  {
99  }
100 };
101 
102 // Also these angelscript types that can have a bunch of different
103 // types is a special case
104 template<>
105 struct AngelScriptTypeIDResolver<asIScriptFunction*> {
106  static int Get(ScriptExecutor* resolver)
107  {
108  return -1;
109  }
110 };
111 
112 template<>
113 struct AngelScriptTypeIDResolver<asIScriptObject*> {
114  static int Get(ScriptExecutor* resolver)
115  {
116  return -1;
117  }
118 };
119 } // 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
Definition: SFMLPackets.h:18
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")