Utility library for providing native functionality to the Squirrel environment.
include/sqbind.h@4:13939f98fe5f, 2014-12-16 (annotated)
- Committer:
- jhnwkmn
- Date:
- Tue Dec 16 11:38:54 2014 +0000
- Revision:
- 4:13939f98fe5f
- Parent:
- 3:22ae7e5068e4
Another pointer initialized.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jhnwkmn | 0:a9a5c12f2d30 | 1 | /****** |
jhnwkmn | 0:a9a5c12f2d30 | 2 | |
jhnwkmn | 0:a9a5c12f2d30 | 3 | Copyright (c) 2009 Juan Linietsky |
jhnwkmn | 0:a9a5c12f2d30 | 4 | |
jhnwkmn | 0:a9a5c12f2d30 | 5 | Permission is hereby granted, free of charge, to any person |
jhnwkmn | 0:a9a5c12f2d30 | 6 | obtaining a copy of this software and associated documentation |
jhnwkmn | 0:a9a5c12f2d30 | 7 | files (the "Software"), to deal in the Software without |
jhnwkmn | 0:a9a5c12f2d30 | 8 | restriction, including without limitation the rights to use, |
jhnwkmn | 0:a9a5c12f2d30 | 9 | copy, modify, merge, publish, distribute, sublicense, and/or sell |
jhnwkmn | 0:a9a5c12f2d30 | 10 | copies of the Software, and to permit persons to whom the |
jhnwkmn | 0:a9a5c12f2d30 | 11 | Software is furnished to do so, subject to the following |
jhnwkmn | 0:a9a5c12f2d30 | 12 | conditions: |
jhnwkmn | 0:a9a5c12f2d30 | 13 | |
jhnwkmn | 0:a9a5c12f2d30 | 14 | The above copyright notice and this permission notice shall be |
jhnwkmn | 0:a9a5c12f2d30 | 15 | included in all copies or substantial portions of the Software. |
jhnwkmn | 0:a9a5c12f2d30 | 16 | |
jhnwkmn | 0:a9a5c12f2d30 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
jhnwkmn | 0:a9a5c12f2d30 | 18 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
jhnwkmn | 0:a9a5c12f2d30 | 19 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
jhnwkmn | 0:a9a5c12f2d30 | 20 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
jhnwkmn | 0:a9a5c12f2d30 | 21 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
jhnwkmn | 0:a9a5c12f2d30 | 22 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
jhnwkmn | 0:a9a5c12f2d30 | 23 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
jhnwkmn | 0:a9a5c12f2d30 | 24 | OTHER DEALINGS IN THE SOFTWARE. |
jhnwkmn | 0:a9a5c12f2d30 | 25 | |
jhnwkmn | 0:a9a5c12f2d30 | 26 | ******/ |
jhnwkmn | 0:a9a5c12f2d30 | 27 | |
jhnwkmn | 0:a9a5c12f2d30 | 28 | #ifndef SQBIND_H |
jhnwkmn | 0:a9a5c12f2d30 | 29 | #define SQBIND_H |
jhnwkmn | 0:a9a5c12f2d30 | 30 | |
jhnwkmn | 0:a9a5c12f2d30 | 31 | |
jhnwkmn | 0:a9a5c12f2d30 | 32 | #include "squirrel.h" |
jhnwkmn | 0:a9a5c12f2d30 | 33 | #include <stdarg.h> |
jhnwkmn | 0:a9a5c12f2d30 | 34 | #include <stdio.h> |
jhnwkmn | 0:a9a5c12f2d30 | 35 | |
jhnwkmn | 0:a9a5c12f2d30 | 36 | |
jhnwkmn | 0:a9a5c12f2d30 | 37 | /** |
jhnwkmn | 0:a9a5c12f2d30 | 38 | @author Juan Linietsky <reduzio@gmail.com> |
jhnwkmn | 0:a9a5c12f2d30 | 39 | */ |
jhnwkmn | 0:a9a5c12f2d30 | 40 | |
jhnwkmn | 0:a9a5c12f2d30 | 41 | |
jhnwkmn | 0:a9a5c12f2d30 | 42 | #ifdef SQBIND_CUSTOMIZE |
jhnwkmn | 0:a9a5c12f2d30 | 43 | |
jhnwkmn | 0:a9a5c12f2d30 | 44 | #include SQBIND_CUSTOMIZE |
jhnwkmn | 0:a9a5c12f2d30 | 45 | |
jhnwkmn | 0:a9a5c12f2d30 | 46 | #else |
jhnwkmn | 0:a9a5c12f2d30 | 47 | |
jhnwkmn | 0:a9a5c12f2d30 | 48 | #define SQBIND_NEW( m_type ) new m_type |
jhnwkmn | 0:a9a5c12f2d30 | 49 | #define SQBIND_DELETE( m_instance) delete m_instance |
jhnwkmn | 0:a9a5c12f2d30 | 50 | #define SQBIND_INLINE inline |
jhnwkmn | 0:a9a5c12f2d30 | 51 | #define SQBIND_DEBUG |
jhnwkmn | 0:a9a5c12f2d30 | 52 | #endif |
jhnwkmn | 0:a9a5c12f2d30 | 53 | |
jhnwkmn | 0:a9a5c12f2d30 | 54 | #ifdef SQBIND_NAMESPACE |
jhnwkmn | 0:a9a5c12f2d30 | 55 | |
jhnwkmn | 0:a9a5c12f2d30 | 56 | namespace SQBIND_NAMESPACE { |
jhnwkmn | 0:a9a5c12f2d30 | 57 | |
jhnwkmn | 0:a9a5c12f2d30 | 58 | #endif |
jhnwkmn | 0:a9a5c12f2d30 | 59 | |
jhnwkmn | 0:a9a5c12f2d30 | 60 | /* |
jhnwkmn | 0:a9a5c12f2d30 | 61 | |
jhnwkmn | 0:a9a5c12f2d30 | 62 | Templates for obtaining the "simple type" of a type. |
jhnwkmn | 0:a9a5c12f2d30 | 63 | if T*, const T, const T& or T& is passed, , SqBindSimpleType<T>::type_t will always be T |
jhnwkmn | 0:a9a5c12f2d30 | 64 | |
jhnwkmn | 0:a9a5c12f2d30 | 65 | */ |
jhnwkmn | 0:a9a5c12f2d30 | 66 | template<class T> |
jhnwkmn | 0:a9a5c12f2d30 | 67 | struct SqBindSimpleType { |
jhnwkmn | 0:a9a5c12f2d30 | 68 | |
jhnwkmn | 0:a9a5c12f2d30 | 69 | typedef T type_t; |
jhnwkmn | 0:a9a5c12f2d30 | 70 | }; |
jhnwkmn | 0:a9a5c12f2d30 | 71 | |
jhnwkmn | 0:a9a5c12f2d30 | 72 | template<class T> |
jhnwkmn | 0:a9a5c12f2d30 | 73 | struct SqBindSimpleType<T&> { |
jhnwkmn | 0:a9a5c12f2d30 | 74 | |
jhnwkmn | 0:a9a5c12f2d30 | 75 | typedef T type_t; |
jhnwkmn | 0:a9a5c12f2d30 | 76 | }; |
jhnwkmn | 0:a9a5c12f2d30 | 77 | |
jhnwkmn | 0:a9a5c12f2d30 | 78 | template<class T> |
jhnwkmn | 0:a9a5c12f2d30 | 79 | struct SqBindSimpleType<const T> { |
jhnwkmn | 0:a9a5c12f2d30 | 80 | |
jhnwkmn | 0:a9a5c12f2d30 | 81 | typedef T type_t; |
jhnwkmn | 0:a9a5c12f2d30 | 82 | }; |
jhnwkmn | 0:a9a5c12f2d30 | 83 | |
jhnwkmn | 0:a9a5c12f2d30 | 84 | template<class T> |
jhnwkmn | 0:a9a5c12f2d30 | 85 | struct SqBindSimpleType<const T&> { |
jhnwkmn | 0:a9a5c12f2d30 | 86 | |
jhnwkmn | 0:a9a5c12f2d30 | 87 | typedef T type_t; |
jhnwkmn | 0:a9a5c12f2d30 | 88 | }; |
jhnwkmn | 0:a9a5c12f2d30 | 89 | |
jhnwkmn | 0:a9a5c12f2d30 | 90 | template<class T> |
jhnwkmn | 0:a9a5c12f2d30 | 91 | struct SqBindSimpleType<T*> { |
jhnwkmn | 0:a9a5c12f2d30 | 92 | |
jhnwkmn | 0:a9a5c12f2d30 | 93 | typedef T type_t; |
jhnwkmn | 0:a9a5c12f2d30 | 94 | }; |
jhnwkmn | 0:a9a5c12f2d30 | 95 | |
jhnwkmn | 0:a9a5c12f2d30 | 96 | template<class T> |
jhnwkmn | 0:a9a5c12f2d30 | 97 | struct SqBindSimpleType<const T*> { |
jhnwkmn | 0:a9a5c12f2d30 | 98 | |
jhnwkmn | 0:a9a5c12f2d30 | 99 | typedef T type_t; |
jhnwkmn | 0:a9a5c12f2d30 | 100 | }; |
jhnwkmn | 0:a9a5c12f2d30 | 101 | |
jhnwkmn | 0:a9a5c12f2d30 | 102 | |
jhnwkmn | 0:a9a5c12f2d30 | 103 | static SQInteger sqbind_throwerror(HSQUIRRELVM v, const char* p_message, ... ) { |
jhnwkmn | 0:a9a5c12f2d30 | 104 | |
jhnwkmn | 0:a9a5c12f2d30 | 105 | #ifdef SQUNICODE |
jhnwkmn | 0:a9a5c12f2d30 | 106 | |
jhnwkmn | 0:a9a5c12f2d30 | 107 | |
jhnwkmn | 0:a9a5c12f2d30 | 108 | #else |
jhnwkmn | 0:a9a5c12f2d30 | 109 | |
jhnwkmn | 0:a9a5c12f2d30 | 110 | va_list argp; |
jhnwkmn | 0:a9a5c12f2d30 | 111 | char errbuff[256]; |
jhnwkmn | 0:a9a5c12f2d30 | 112 | va_start(argp, p_message); |
jhnwkmn | 0:a9a5c12f2d30 | 113 | vsnprintf(errbuff,255,p_message, argp ); |
jhnwkmn | 0:a9a5c12f2d30 | 114 | va_end(argp); |
jhnwkmn | 0:a9a5c12f2d30 | 115 | vfprintf(stderr,p_message, argp ); |
jhnwkmn | 0:a9a5c12f2d30 | 116 | |
jhnwkmn | 0:a9a5c12f2d30 | 117 | return sq_throwerror(v,errbuff); |
jhnwkmn | 0:a9a5c12f2d30 | 118 | #endif |
jhnwkmn | 0:a9a5c12f2d30 | 119 | |
jhnwkmn | 0:a9a5c12f2d30 | 120 | } |
jhnwkmn | 0:a9a5c12f2d30 | 121 | |
jhnwkmn | 0:a9a5c12f2d30 | 122 | /* |
jhnwkmn | 0:a9a5c12f2d30 | 123 | |
jhnwkmn | 0:a9a5c12f2d30 | 124 | default userdata release hook template for bound types, |
jhnwkmn | 0:a9a5c12f2d30 | 125 | deletes the type correctly |
jhnwkmn | 0:a9a5c12f2d30 | 126 | |
jhnwkmn | 0:a9a5c12f2d30 | 127 | */ |
jhnwkmn | 0:a9a5c12f2d30 | 128 | |
jhnwkmn | 0:a9a5c12f2d30 | 129 | template<class T> |
jhnwkmn | 0:a9a5c12f2d30 | 130 | struct SqBindAllocator { |
jhnwkmn | 0:a9a5c12f2d30 | 131 | |
jhnwkmn | 1:1f66106257a0 | 132 | static SQBIND_INLINE T *construct(HSQUIRRELVM) { |
jhnwkmn | 0:a9a5c12f2d30 | 133 | |
jhnwkmn | 0:a9a5c12f2d30 | 134 | return SQBIND_NEW(T); |
jhnwkmn | 0:a9a5c12f2d30 | 135 | } |
jhnwkmn | 0:a9a5c12f2d30 | 136 | static SQBIND_INLINE T *copy_construct(const T* p_from) { |
jhnwkmn | 0:a9a5c12f2d30 | 137 | |
jhnwkmn | 0:a9a5c12f2d30 | 138 | return SQBIND_NEW(T(*p_from)); |
jhnwkmn | 0:a9a5c12f2d30 | 139 | } |
jhnwkmn | 0:a9a5c12f2d30 | 140 | static SQBIND_INLINE bool assign(T* p_val, const T* p_from) { |
jhnwkmn | 0:a9a5c12f2d30 | 141 | |
jhnwkmn | 0:a9a5c12f2d30 | 142 | *p_val=*p_from; |
jhnwkmn | 0:a9a5c12f2d30 | 143 | return true; |
jhnwkmn | 0:a9a5c12f2d30 | 144 | } |
jhnwkmn | 0:a9a5c12f2d30 | 145 | static SQBIND_INLINE void destruct(T* p_instance) { |
jhnwkmn | 0:a9a5c12f2d30 | 146 | |
jhnwkmn | 0:a9a5c12f2d30 | 147 | SQBIND_DELETE(p_instance); |
jhnwkmn | 0:a9a5c12f2d30 | 148 | } |
jhnwkmn | 0:a9a5c12f2d30 | 149 | |
jhnwkmn | 0:a9a5c12f2d30 | 150 | static SQBIND_INLINE T& get_empty() { |
jhnwkmn | 0:a9a5c12f2d30 | 151 | |
jhnwkmn | 0:a9a5c12f2d30 | 152 | static T empty; |
jhnwkmn | 0:a9a5c12f2d30 | 153 | return empty; |
jhnwkmn | 0:a9a5c12f2d30 | 154 | } |
jhnwkmn | 0:a9a5c12f2d30 | 155 | }; |
jhnwkmn | 0:a9a5c12f2d30 | 156 | |
jhnwkmn | 0:a9a5c12f2d30 | 157 | |
jhnwkmn | 0:a9a5c12f2d30 | 158 | template<class T, class A=SqBindAllocator<T> > |
jhnwkmn | 0:a9a5c12f2d30 | 159 | class SqBind { |
jhnwkmn | 0:a9a5c12f2d30 | 160 | public: |
jhnwkmn | 0:a9a5c12f2d30 | 161 | |
jhnwkmn | 0:a9a5c12f2d30 | 162 | typedef T Type; |
jhnwkmn | 0:a9a5c12f2d30 | 163 | typedef T* (*Constructor)(HSQUIRRELVM v); |
jhnwkmn | 0:a9a5c12f2d30 | 164 | |
jhnwkmn | 0:a9a5c12f2d30 | 165 | private: |
jhnwkmn | 0:a9a5c12f2d30 | 166 | static HSQOBJECT class_id; |
jhnwkmn | 0:a9a5c12f2d30 | 167 | static bool initialized; |
jhnwkmn | 0:a9a5c12f2d30 | 168 | static const char *name; |
jhnwkmn | 0:a9a5c12f2d30 | 169 | static bool instantiable; |
jhnwkmn | 0:a9a5c12f2d30 | 170 | static HSQOBJECT string_constructor; |
jhnwkmn | 0:a9a5c12f2d30 | 171 | static HSQOBJECT custom_member_var_table_set; |
jhnwkmn | 0:a9a5c12f2d30 | 172 | static HSQOBJECT custom_member_var_table_get; |
jhnwkmn | 0:a9a5c12f2d30 | 173 | static HSQOBJECT custom_member_var_table_offset; |
jhnwkmn | 0:a9a5c12f2d30 | 174 | static Constructor constructor; |
jhnwkmn | 0:a9a5c12f2d30 | 175 | |
jhnwkmn | 0:a9a5c12f2d30 | 176 | |
jhnwkmn | 0:a9a5c12f2d30 | 177 | |
jhnwkmn | 0:a9a5c12f2d30 | 178 | static SQInteger default_release_hook(SQUserPointer p_ptr,SQInteger p_size) { |
jhnwkmn | 0:a9a5c12f2d30 | 179 | |
jhnwkmn | 0:a9a5c12f2d30 | 180 | T *instance = reinterpret_cast<T*>(p_ptr); |
jhnwkmn | 0:a9a5c12f2d30 | 181 | A::destruct(instance); |
jhnwkmn | 0:a9a5c12f2d30 | 182 | return 0; // is it right? |
jhnwkmn | 0:a9a5c12f2d30 | 183 | } |
jhnwkmn | 0:a9a5c12f2d30 | 184 | |
jhnwkmn | 0:a9a5c12f2d30 | 185 | static SQInteger default_constructor(HSQUIRRELVM v) { |
jhnwkmn | 0:a9a5c12f2d30 | 186 | |
jhnwkmn | 0:a9a5c12f2d30 | 187 | if (!instantiable) { |
jhnwkmn | 0:a9a5c12f2d30 | 188 | return sqbind_throwerror(v,"Type '%s' is not instantiable.",name); |
jhnwkmn | 0:a9a5c12f2d30 | 189 | } |
jhnwkmn | 0:a9a5c12f2d30 | 190 | |
jhnwkmn | 4:13939f98fe5f | 191 | T *instance = 0; |
jhnwkmn | 0:a9a5c12f2d30 | 192 | |
jhnwkmn | 0:a9a5c12f2d30 | 193 | int params=sq_gettop(v); |
jhnwkmn | 0:a9a5c12f2d30 | 194 | |
jhnwkmn | 0:a9a5c12f2d30 | 195 | if (params==0) { |
jhnwkmn | 0:a9a5c12f2d30 | 196 | return sqbind_throwerror(v,"Type '%s' got a strange amount of params.",name); |
jhnwkmn | 0:a9a5c12f2d30 | 197 | |
jhnwkmn | 0:a9a5c12f2d30 | 198 | } |
jhnwkmn | 0:a9a5c12f2d30 | 199 | if (params==1) { |
jhnwkmn | 0:a9a5c12f2d30 | 200 | |
jhnwkmn | 1:1f66106257a0 | 201 | instance=A::construct(v); |
jhnwkmn | 0:a9a5c12f2d30 | 202 | if (!instance) |
jhnwkmn | 0:a9a5c12f2d30 | 203 | return sqbind_throwerror(v,"Type '%s' does not support default constructor.\n",name); |
jhnwkmn | 0:a9a5c12f2d30 | 204 | |
jhnwkmn | 0:a9a5c12f2d30 | 205 | } else if (params>=2) { |
jhnwkmn | 0:a9a5c12f2d30 | 206 | |
jhnwkmn | 0:a9a5c12f2d30 | 207 | bool call_constructor=true; |
jhnwkmn | 0:a9a5c12f2d30 | 208 | |
jhnwkmn | 0:a9a5c12f2d30 | 209 | if (params==2) { |
jhnwkmn | 0:a9a5c12f2d30 | 210 | // handle copyconstructor |
jhnwkmn | 0:a9a5c12f2d30 | 211 | SQUserPointer tt=NULL; |
jhnwkmn | 0:a9a5c12f2d30 | 212 | sq_gettypetag(v,2,&tt); |
jhnwkmn | 0:a9a5c12f2d30 | 213 | if (tt && tt==(SQUserPointer)&class_id) { |
jhnwkmn | 0:a9a5c12f2d30 | 214 | |
jhnwkmn | 0:a9a5c12f2d30 | 215 | instance=A::copy_construct(&get(v,2)); |
jhnwkmn | 0:a9a5c12f2d30 | 216 | if (!instance) |
jhnwkmn | 0:a9a5c12f2d30 | 217 | return sqbind_throwerror(v,"Type '%s' does not support copy constructor.\n",name); |
jhnwkmn | 0:a9a5c12f2d30 | 218 | call_constructor=false; |
jhnwkmn | 0:a9a5c12f2d30 | 219 | } |
jhnwkmn | 0:a9a5c12f2d30 | 220 | } |
jhnwkmn | 0:a9a5c12f2d30 | 221 | |
jhnwkmn | 0:a9a5c12f2d30 | 222 | if (call_constructor) { |
jhnwkmn | 0:a9a5c12f2d30 | 223 | |
jhnwkmn | 0:a9a5c12f2d30 | 224 | if (constructor) { |
jhnwkmn | 0:a9a5c12f2d30 | 225 | |
jhnwkmn | 0:a9a5c12f2d30 | 226 | instance=constructor(v); |
jhnwkmn | 0:a9a5c12f2d30 | 227 | if (!instance) { |
jhnwkmn | 0:a9a5c12f2d30 | 228 | |
jhnwkmn | 0:a9a5c12f2d30 | 229 | return sqbind_throwerror(v,"Wrong parameters for type '%s' constructor.",name); |
jhnwkmn | 0:a9a5c12f2d30 | 230 | } |
jhnwkmn | 0:a9a5c12f2d30 | 231 | } else { |
jhnwkmn | 0:a9a5c12f2d30 | 232 | |
jhnwkmn | 0:a9a5c12f2d30 | 233 | return sqbind_throwerror(v,"Wrong parameter count for type '%s' constructor.",name); |
jhnwkmn | 0:a9a5c12f2d30 | 234 | } |
jhnwkmn | 0:a9a5c12f2d30 | 235 | } |
jhnwkmn | 0:a9a5c12f2d30 | 236 | } |
jhnwkmn | 0:a9a5c12f2d30 | 237 | |
jhnwkmn | 0:a9a5c12f2d30 | 238 | |
jhnwkmn | 0:a9a5c12f2d30 | 239 | sq_setinstanceup(v, 1, instance); |
jhnwkmn | 0:a9a5c12f2d30 | 240 | sq_setreleasehook(v,1, default_release_hook); |
jhnwkmn | 0:a9a5c12f2d30 | 241 | |
jhnwkmn | 0:a9a5c12f2d30 | 242 | return 0; |
jhnwkmn | 0:a9a5c12f2d30 | 243 | } |
jhnwkmn | 0:a9a5c12f2d30 | 244 | |
jhnwkmn | 0:a9a5c12f2d30 | 245 | static SQInteger default_cloned(HSQUIRRELVM v) { |
jhnwkmn | 0:a9a5c12f2d30 | 246 | |
jhnwkmn | 0:a9a5c12f2d30 | 247 | if (!instantiable) { |
jhnwkmn | 0:a9a5c12f2d30 | 248 | return sqbind_throwerror(v,"Type '%s' is not instantiable/clonable.",name); |
jhnwkmn | 0:a9a5c12f2d30 | 249 | } |
jhnwkmn | 0:a9a5c12f2d30 | 250 | |
jhnwkmn | 0:a9a5c12f2d30 | 251 | T *self = &SqBind<T>::get(v,1); |
jhnwkmn | 0:a9a5c12f2d30 | 252 | T *from = &SqBind<T>::get(v,2); |
jhnwkmn | 0:a9a5c12f2d30 | 253 | A::assign(self,from); |
jhnwkmn | 0:a9a5c12f2d30 | 254 | |
jhnwkmn | 0:a9a5c12f2d30 | 255 | return 0; |
jhnwkmn | 0:a9a5c12f2d30 | 256 | } |
jhnwkmn | 0:a9a5c12f2d30 | 257 | |
jhnwkmn | 0:a9a5c12f2d30 | 258 | /* HANDLERS FOR DIRECT TO MEMORY MEMBER VARIABLE SET AND GET */ |
jhnwkmn | 0:a9a5c12f2d30 | 259 | |
jhnwkmn | 0:a9a5c12f2d30 | 260 | typedef void (*MemberVarGetFunction)(HSQUIRRELVM,int,int); |
jhnwkmn | 0:a9a5c12f2d30 | 261 | |
jhnwkmn | 0:a9a5c12f2d30 | 262 | template<class I> |
jhnwkmn | 0:a9a5c12f2d30 | 263 | static void sqbind_member_var_get(HSQUIRRELVM v,int p_idx,int p_offset) { |
jhnwkmn | 0:a9a5c12f2d30 | 264 | |
jhnwkmn | 0:a9a5c12f2d30 | 265 | unsigned char *data = (unsigned char*)&get(v,p_idx); |
jhnwkmn | 0:a9a5c12f2d30 | 266 | I *i=(I*)&data[p_offset]; |
jhnwkmn | 0:a9a5c12f2d30 | 267 | SqBind<I>::push(v,*i); |
jhnwkmn | 0:a9a5c12f2d30 | 268 | } |
jhnwkmn | 0:a9a5c12f2d30 | 269 | |
jhnwkmn | 0:a9a5c12f2d30 | 270 | |
jhnwkmn | 0:a9a5c12f2d30 | 271 | static SQInteger default_member_variable_get(HSQUIRRELVM v) { |
jhnwkmn | 0:a9a5c12f2d30 | 272 | |
jhnwkmn | 0:a9a5c12f2d30 | 273 | sq_pushobject(v,custom_member_var_table_get); |
jhnwkmn | 0:a9a5c12f2d30 | 274 | |
jhnwkmn | 0:a9a5c12f2d30 | 275 | sq_push(v,2); // copy the key |
jhnwkmn | 0:a9a5c12f2d30 | 276 | if (SQ_FAILED( sq_get(v,-2) )) { |
jhnwkmn | 0:a9a5c12f2d30 | 277 | return sq_throwerror(v,"Member Variable not found"); |
jhnwkmn | 0:a9a5c12f2d30 | 278 | } |
jhnwkmn | 0:a9a5c12f2d30 | 279 | MemberVarGetFunction *get_func; |
jhnwkmn | 0:a9a5c12f2d30 | 280 | sq_getuserdata(v,-1,(SQUserPointer*)&get_func,NULL); |
jhnwkmn | 0:a9a5c12f2d30 | 281 | |
jhnwkmn | 0:a9a5c12f2d30 | 282 | sq_pop(v,2); // pop userdata and get table |
jhnwkmn | 0:a9a5c12f2d30 | 283 | |
jhnwkmn | 0:a9a5c12f2d30 | 284 | sq_pushobject(v,custom_member_var_table_offset); |
jhnwkmn | 0:a9a5c12f2d30 | 285 | sq_push(v,2); // copy the key |
jhnwkmn | 0:a9a5c12f2d30 | 286 | if (SQ_FAILED( sq_get(v,-2) )) { |
jhnwkmn | 0:a9a5c12f2d30 | 287 | return sq_throwerror(v,"Member Variable not found"); |
jhnwkmn | 0:a9a5c12f2d30 | 288 | } |
jhnwkmn | 0:a9a5c12f2d30 | 289 | SQInteger offset; |
jhnwkmn | 0:a9a5c12f2d30 | 290 | sq_getinteger(v,-1,&offset); |
jhnwkmn | 0:a9a5c12f2d30 | 291 | |
jhnwkmn | 0:a9a5c12f2d30 | 292 | (*get_func)(v,1,offset); |
jhnwkmn | 0:a9a5c12f2d30 | 293 | return 1; |
jhnwkmn | 0:a9a5c12f2d30 | 294 | } |
jhnwkmn | 0:a9a5c12f2d30 | 295 | |
jhnwkmn | 0:a9a5c12f2d30 | 296 | typedef void (*MemberVarSetFunction)(HSQUIRRELVM,int,int,int); |
jhnwkmn | 0:a9a5c12f2d30 | 297 | |
jhnwkmn | 0:a9a5c12f2d30 | 298 | template<class I> |
jhnwkmn | 0:a9a5c12f2d30 | 299 | static void sqbind_member_var_set(HSQUIRRELVM v,int p_idx,int p_from,int p_offset) { |
jhnwkmn | 0:a9a5c12f2d30 | 300 | |
jhnwkmn | 0:a9a5c12f2d30 | 301 | unsigned char *data = (unsigned char*)&SqBind<T>::get(v,p_idx); |
jhnwkmn | 0:a9a5c12f2d30 | 302 | I *i=(I*)&data[p_offset]; |
jhnwkmn | 0:a9a5c12f2d30 | 303 | *i=SqBind<I>::get(v,p_from); |
jhnwkmn | 0:a9a5c12f2d30 | 304 | } |
jhnwkmn | 0:a9a5c12f2d30 | 305 | |
jhnwkmn | 0:a9a5c12f2d30 | 306 | static SQInteger default_member_variable_set(HSQUIRRELVM v) { |
jhnwkmn | 0:a9a5c12f2d30 | 307 | |
jhnwkmn | 0:a9a5c12f2d30 | 308 | sq_pushobject(v,custom_member_var_table_set); |
jhnwkmn | 0:a9a5c12f2d30 | 309 | sq_push(v,2); // copy the key |
jhnwkmn | 0:a9a5c12f2d30 | 310 | if (SQ_FAILED( sq_get(v,-2) )) { |
jhnwkmn | 0:a9a5c12f2d30 | 311 | return sqbind_throwerror(v,"Member Variable not found"); |
jhnwkmn | 0:a9a5c12f2d30 | 312 | } |
jhnwkmn | 0:a9a5c12f2d30 | 313 | MemberVarSetFunction *set_func; |
jhnwkmn | 0:a9a5c12f2d30 | 314 | sq_getuserdata(v,-1,(SQUserPointer*)&set_func,NULL); |
jhnwkmn | 0:a9a5c12f2d30 | 315 | |
jhnwkmn | 0:a9a5c12f2d30 | 316 | sq_pop(v,2); // pop userdata and get table |
jhnwkmn | 0:a9a5c12f2d30 | 317 | sq_pushobject(v,custom_member_var_table_offset); |
jhnwkmn | 0:a9a5c12f2d30 | 318 | sq_push(v,2); // copy the key |
jhnwkmn | 0:a9a5c12f2d30 | 319 | if (SQ_FAILED( sq_get(v,-2) )) { |
jhnwkmn | 0:a9a5c12f2d30 | 320 | return sq_throwerror(v,"Member Variable not found"); |
jhnwkmn | 0:a9a5c12f2d30 | 321 | } |
jhnwkmn | 0:a9a5c12f2d30 | 322 | |
jhnwkmn | 0:a9a5c12f2d30 | 323 | SQInteger offset; |
jhnwkmn | 0:a9a5c12f2d30 | 324 | sq_getinteger(v,-1,&offset); |
jhnwkmn | 0:a9a5c12f2d30 | 325 | |
jhnwkmn | 0:a9a5c12f2d30 | 326 | (*set_func)(v,1,3,offset); |
jhnwkmn | 0:a9a5c12f2d30 | 327 | return 0; |
jhnwkmn | 0:a9a5c12f2d30 | 328 | } |
jhnwkmn | 0:a9a5c12f2d30 | 329 | |
jhnwkmn | 0:a9a5c12f2d30 | 330 | static SQInteger default_full_comparator(HSQUIRRELVM v) { |
jhnwkmn | 0:a9a5c12f2d30 | 331 | |
jhnwkmn | 0:a9a5c12f2d30 | 332 | if (!is_type(v,3)) { |
jhnwkmn | 0:a9a5c12f2d30 | 333 | |
jhnwkmn | 0:a9a5c12f2d30 | 334 | return sqbind_throwerror(v,"_cmp: rvalue not of type %s\n",name); |
jhnwkmn | 0:a9a5c12f2d30 | 335 | } |
jhnwkmn | 0:a9a5c12f2d30 | 336 | |
jhnwkmn | 0:a9a5c12f2d30 | 337 | T * a = &get(v,2); |
jhnwkmn | 0:a9a5c12f2d30 | 338 | T * b = &get(v,3); |
jhnwkmn | 0:a9a5c12f2d30 | 339 | |
jhnwkmn | 0:a9a5c12f2d30 | 340 | if (*a < *b ) |
jhnwkmn | 0:a9a5c12f2d30 | 341 | return -1; |
jhnwkmn | 0:a9a5c12f2d30 | 342 | else if (*a > *b ) |
jhnwkmn | 0:a9a5c12f2d30 | 343 | return 1; |
jhnwkmn | 0:a9a5c12f2d30 | 344 | else return 0; |
jhnwkmn | 0:a9a5c12f2d30 | 345 | } |
jhnwkmn | 0:a9a5c12f2d30 | 346 | |
jhnwkmn | 0:a9a5c12f2d30 | 347 | static SQInteger default_boolean_comparator(HSQUIRRELVM v) { |
jhnwkmn | 0:a9a5c12f2d30 | 348 | |
jhnwkmn | 0:a9a5c12f2d30 | 349 | if (!is_type(v,3)) { |
jhnwkmn | 0:a9a5c12f2d30 | 350 | |
jhnwkmn | 0:a9a5c12f2d30 | 351 | return sqbind_throwerror(v,"_cmp: rvalue not of type %s\n",name); |
jhnwkmn | 0:a9a5c12f2d30 | 352 | } |
jhnwkmn | 0:a9a5c12f2d30 | 353 | |
jhnwkmn | 0:a9a5c12f2d30 | 354 | T * a = &get(v,2); |
jhnwkmn | 0:a9a5c12f2d30 | 355 | T * b = &get(v,3); |
jhnwkmn | 0:a9a5c12f2d30 | 356 | |
jhnwkmn | 0:a9a5c12f2d30 | 357 | if (*a == *b ) |
jhnwkmn | 0:a9a5c12f2d30 | 358 | return 0; |
jhnwkmn | 0:a9a5c12f2d30 | 359 | else |
jhnwkmn | 0:a9a5c12f2d30 | 360 | return 1; |
jhnwkmn | 0:a9a5c12f2d30 | 361 | } |
jhnwkmn | 0:a9a5c12f2d30 | 362 | |
jhnwkmn | 0:a9a5c12f2d30 | 363 | static void instance_type(HSQUIRRELVM v,HSQOBJECT p_type) { |
jhnwkmn | 0:a9a5c12f2d30 | 364 | #if 0 |
jhnwkmn | 0:a9a5c12f2d30 | 365 | sq_pushobject(v,class_id); |
jhnwkmn | 0:a9a5c12f2d30 | 366 | sq_pushobject(v,string_constructor); |
jhnwkmn | 0:a9a5c12f2d30 | 367 | sq_get(v,-2); |
jhnwkmn | 0:a9a5c12f2d30 | 368 | sq_createinstance(v,-1); |
jhnwkmn | 0:a9a5c12f2d30 | 369 | sq_call(v,1,true,true); |
jhnwkmn | 0:a9a5c12f2d30 | 370 | sq_remove(v,-2); // remove object |
jhnwkmn | 0:a9a5c12f2d30 | 371 | #else |
jhnwkmn | 0:a9a5c12f2d30 | 372 | |
jhnwkmn | 0:a9a5c12f2d30 | 373 | sq_pushobject(v,class_id); |
jhnwkmn | 0:a9a5c12f2d30 | 374 | sq_pushroottable(v); |
jhnwkmn | 0:a9a5c12f2d30 | 375 | sq_call(v,1,true,true); |
jhnwkmn | 0:a9a5c12f2d30 | 376 | |
jhnwkmn | 0:a9a5c12f2d30 | 377 | #endif |
jhnwkmn | 0:a9a5c12f2d30 | 378 | |
jhnwkmn | 0:a9a5c12f2d30 | 379 | } |
jhnwkmn | 0:a9a5c12f2d30 | 380 | |
jhnwkmn | 0:a9a5c12f2d30 | 381 | |
jhnwkmn | 0:a9a5c12f2d30 | 382 | public: |
jhnwkmn | 0:a9a5c12f2d30 | 383 | |
jhnwkmn | 0:a9a5c12f2d30 | 384 | // PUBLIC |
jhnwkmn | 0:a9a5c12f2d30 | 385 | |
jhnwkmn | 0:a9a5c12f2d30 | 386 | |
jhnwkmn | 0:a9a5c12f2d30 | 387 | |
jhnwkmn | 0:a9a5c12f2d30 | 388 | static void push(HSQUIRRELVM v, const T& p_value) { |
jhnwkmn | 0:a9a5c12f2d30 | 389 | |
jhnwkmn | 0:a9a5c12f2d30 | 390 | if (!initialized) { |
jhnwkmn | 0:a9a5c12f2d30 | 391 | sqbind_throwerror(v,"Type '%s', has not been initialized.",name); |
jhnwkmn | 0:a9a5c12f2d30 | 392 | return; |
jhnwkmn | 0:a9a5c12f2d30 | 393 | } |
jhnwkmn | 0:a9a5c12f2d30 | 394 | // create the instance, then assign the value! |
jhnwkmn | 0:a9a5c12f2d30 | 395 | |
jhnwkmn | 0:a9a5c12f2d30 | 396 | instance_type(v,class_id); |
jhnwkmn | 0:a9a5c12f2d30 | 397 | T* up=NULL; |
jhnwkmn | 0:a9a5c12f2d30 | 398 | sq_getinstanceup(v,-1,(SQUserPointer*)&up,NULL); |
jhnwkmn | 0:a9a5c12f2d30 | 399 | A::assign(up,&p_value); |
jhnwkmn | 0:a9a5c12f2d30 | 400 | } |
jhnwkmn | 0:a9a5c12f2d30 | 401 | |
jhnwkmn | 0:a9a5c12f2d30 | 402 | static void push(HSQUIRRELVM v, const T* p_value) { |
jhnwkmn | 0:a9a5c12f2d30 | 403 | |
jhnwkmn | 0:a9a5c12f2d30 | 404 | if (!initialized) { |
jhnwkmn | 0:a9a5c12f2d30 | 405 | sqbind_throwerror(v,"Type '%s', has not been initialized.",name); |
jhnwkmn | 0:a9a5c12f2d30 | 406 | return; |
jhnwkmn | 0:a9a5c12f2d30 | 407 | } |
jhnwkmn | 0:a9a5c12f2d30 | 408 | |
jhnwkmn | 0:a9a5c12f2d30 | 409 | // create the instance manually and asign the pointer to the exdisting value.. |
jhnwkmn | 0:a9a5c12f2d30 | 410 | // note: no release hook is created, so this is created unmanaged. |
jhnwkmn | 0:a9a5c12f2d30 | 411 | |
jhnwkmn | 0:a9a5c12f2d30 | 412 | sq_pushobject(v,class_id); |
jhnwkmn | 0:a9a5c12f2d30 | 413 | sq_createinstance(v,sq_gettop(v)); |
jhnwkmn | 0:a9a5c12f2d30 | 414 | sq_remove(v,-2); |
jhnwkmn | 0:a9a5c12f2d30 | 415 | sq_setinstanceup(v,-1,reinterpret_cast<SQUserPointer>(p_value)); |
jhnwkmn | 0:a9a5c12f2d30 | 416 | |
jhnwkmn | 0:a9a5c12f2d30 | 417 | } |
jhnwkmn | 0:a9a5c12f2d30 | 418 | |
jhnwkmn | 0:a9a5c12f2d30 | 419 | static T& get(HSQUIRRELVM v, int p_idx) { |
jhnwkmn | 0:a9a5c12f2d30 | 420 | |
jhnwkmn | 0:a9a5c12f2d30 | 421 | if (!initialized) { |
jhnwkmn | 0:a9a5c12f2d30 | 422 | // will crash anwyay, so who cares.. |
jhnwkmn | 0:a9a5c12f2d30 | 423 | sqbind_throwerror(v,"Type '%s', has not been initialized.",name); |
jhnwkmn | 0:a9a5c12f2d30 | 424 | // not the best solution ever |
jhnwkmn | 0:a9a5c12f2d30 | 425 | return A::get_empty(); |
jhnwkmn | 0:a9a5c12f2d30 | 426 | } |
jhnwkmn | 0:a9a5c12f2d30 | 427 | |
jhnwkmn | 0:a9a5c12f2d30 | 428 | T* up=NULL; |
jhnwkmn | 0:a9a5c12f2d30 | 429 | if (SQ_FAILED(sq_getinstanceup(v,p_idx,(SQUserPointer*)&up,&class_id))) { |
jhnwkmn | 0:a9a5c12f2d30 | 430 | |
jhnwkmn | 0:a9a5c12f2d30 | 431 | sqbind_throwerror(v,"Value at index is not of type '%s'!!.",name); |
jhnwkmn | 0:a9a5c12f2d30 | 432 | // not the best solution ever, again |
jhnwkmn | 0:a9a5c12f2d30 | 433 | return A::get_empty(); |
jhnwkmn | 0:a9a5c12f2d30 | 434 | } |
jhnwkmn | 0:a9a5c12f2d30 | 435 | return *up; |
jhnwkmn | 0:a9a5c12f2d30 | 436 | } |
jhnwkmn | 0:a9a5c12f2d30 | 437 | |
jhnwkmn | 0:a9a5c12f2d30 | 438 | static bool is_type(HSQUIRRELVM v,int p_idx) { |
jhnwkmn | 0:a9a5c12f2d30 | 439 | |
jhnwkmn | 0:a9a5c12f2d30 | 440 | if (!initialized) { |
jhnwkmn | 0:a9a5c12f2d30 | 441 | // will crash anwyay, so who cares.. |
jhnwkmn | 0:a9a5c12f2d30 | 442 | sqbind_throwerror(v,"Type '%s', has not been initialized.",name); |
jhnwkmn | 0:a9a5c12f2d30 | 443 | // not the best solution ever |
jhnwkmn | 0:a9a5c12f2d30 | 444 | return false; |
jhnwkmn | 0:a9a5c12f2d30 | 445 | } |
jhnwkmn | 0:a9a5c12f2d30 | 446 | |
jhnwkmn | 0:a9a5c12f2d30 | 447 | SQUserPointer tt=NULL; |
jhnwkmn | 0:a9a5c12f2d30 | 448 | sq_gettypetag(v,p_idx,&tt); |
jhnwkmn | 0:a9a5c12f2d30 | 449 | return (tt && tt==(SQUserPointer)&class_id); |
jhnwkmn | 0:a9a5c12f2d30 | 450 | } |
jhnwkmn | 0:a9a5c12f2d30 | 451 | |
jhnwkmn | 0:a9a5c12f2d30 | 452 | struct Getter { |
jhnwkmn | 0:a9a5c12f2d30 | 453 | |
jhnwkmn | 0:a9a5c12f2d30 | 454 | SQBIND_INLINE T& get(HSQUIRRELVM v, int p_idx) { |
jhnwkmn | 0:a9a5c12f2d30 | 455 | |
jhnwkmn | 0:a9a5c12f2d30 | 456 | return SqBind<T>::get(v,p_idx); |
jhnwkmn | 0:a9a5c12f2d30 | 457 | } |
jhnwkmn | 0:a9a5c12f2d30 | 458 | }; |
jhnwkmn | 0:a9a5c12f2d30 | 459 | |
jhnwkmn | 0:a9a5c12f2d30 | 460 | struct GetterPtr { |
jhnwkmn | 0:a9a5c12f2d30 | 461 | |
jhnwkmn | 0:a9a5c12f2d30 | 462 | SQBIND_INLINE T* get(HSQUIRRELVM v, int p_idx) { |
jhnwkmn | 0:a9a5c12f2d30 | 463 | |
jhnwkmn | 0:a9a5c12f2d30 | 464 | return &SqBind<T>::get(v,p_idx); |
jhnwkmn | 0:a9a5c12f2d30 | 465 | } |
jhnwkmn | 0:a9a5c12f2d30 | 466 | }; |
jhnwkmn | 0:a9a5c12f2d30 | 467 | |
jhnwkmn | 0:a9a5c12f2d30 | 468 | static void bind_method(HSQUIRRELVM v, const SQChar *p_name, SQFUNCTION p_function,bool p_static=false) { |
jhnwkmn | 0:a9a5c12f2d30 | 469 | |
jhnwkmn | 0:a9a5c12f2d30 | 470 | sq_pushobject(v,class_id); |
jhnwkmn | 0:a9a5c12f2d30 | 471 | sq_pushstring(v,p_name,-1); |
jhnwkmn | 0:a9a5c12f2d30 | 472 | sq_newclosure(v,p_function,0); |
jhnwkmn | 0:a9a5c12f2d30 | 473 | sq_newslot(v,-3,p_static); |
jhnwkmn | 0:a9a5c12f2d30 | 474 | sq_pop(v,1);// pop class |
jhnwkmn | 0:a9a5c12f2d30 | 475 | } |
jhnwkmn | 0:a9a5c12f2d30 | 476 | |
jhnwkmn | 0:a9a5c12f2d30 | 477 | |
jhnwkmn | 0:a9a5c12f2d30 | 478 | template<class I> |
jhnwkmn | 0:a9a5c12f2d30 | 479 | static void bind_member_variable(HSQUIRRELVM v,const SQChar *p_name,int p_offset) { |
jhnwkmn | 0:a9a5c12f2d30 | 480 | |
jhnwkmn | 0:a9a5c12f2d30 | 481 | if (sq_isnull(custom_member_var_table_get)) { |
jhnwkmn | 0:a9a5c12f2d30 | 482 | // no member var table and no set/get functions, huh? |
jhnwkmn | 0:a9a5c12f2d30 | 483 | sq_pushobject(v,class_id); |
jhnwkmn | 0:a9a5c12f2d30 | 484 | |
jhnwkmn | 0:a9a5c12f2d30 | 485 | // override _set/_get for the custom ones |
jhnwkmn | 0:a9a5c12f2d30 | 486 | sq_pushstring(v,_SC("_set"),-1); |
jhnwkmn | 0:a9a5c12f2d30 | 487 | sq_newclosure(v,default_member_variable_set,0); |
jhnwkmn | 0:a9a5c12f2d30 | 488 | sq_newslot(v,-3,false); |
jhnwkmn | 0:a9a5c12f2d30 | 489 | |
jhnwkmn | 0:a9a5c12f2d30 | 490 | sq_pushstring(v,_SC("_get"),-1); |
jhnwkmn | 0:a9a5c12f2d30 | 491 | sq_newclosure(v,default_member_variable_get,0); |
jhnwkmn | 0:a9a5c12f2d30 | 492 | sq_newslot(v,-3,false); |
jhnwkmn | 0:a9a5c12f2d30 | 493 | |
jhnwkmn | 0:a9a5c12f2d30 | 494 | sq_pushstring(v,_SC("__member_variables_set"),-1); |
jhnwkmn | 0:a9a5c12f2d30 | 495 | sq_newtable(v); |
jhnwkmn | 0:a9a5c12f2d30 | 496 | sq_getstackobj(v,-1,&custom_member_var_table_set); |
jhnwkmn | 0:a9a5c12f2d30 | 497 | sq_newslot(v,-3,true); |
jhnwkmn | 0:a9a5c12f2d30 | 498 | |
jhnwkmn | 0:a9a5c12f2d30 | 499 | sq_pushstring(v,_SC("__member_variables_get"),-1); |
jhnwkmn | 0:a9a5c12f2d30 | 500 | sq_newtable(v); |
jhnwkmn | 0:a9a5c12f2d30 | 501 | sq_getstackobj(v,-1,&custom_member_var_table_get); |
jhnwkmn | 0:a9a5c12f2d30 | 502 | sq_newslot(v,-3,true); |
jhnwkmn | 0:a9a5c12f2d30 | 503 | |
jhnwkmn | 0:a9a5c12f2d30 | 504 | sq_pushstring(v,_SC("__member_variables_offset"),-1); |
jhnwkmn | 0:a9a5c12f2d30 | 505 | sq_newtable(v); |
jhnwkmn | 0:a9a5c12f2d30 | 506 | sq_getstackobj(v,-1,&custom_member_var_table_offset); |
jhnwkmn | 0:a9a5c12f2d30 | 507 | sq_newslot(v,-3,true); |
jhnwkmn | 0:a9a5c12f2d30 | 508 | |
jhnwkmn | 0:a9a5c12f2d30 | 509 | sq_pop(v,1);// pop class |
jhnwkmn | 0:a9a5c12f2d30 | 510 | |
jhnwkmn | 0:a9a5c12f2d30 | 511 | } |
jhnwkmn | 0:a9a5c12f2d30 | 512 | |
jhnwkmn | 0:a9a5c12f2d30 | 513 | // store the offset |
jhnwkmn | 0:a9a5c12f2d30 | 514 | |
jhnwkmn | 0:a9a5c12f2d30 | 515 | sq_pushobject(v,custom_member_var_table_offset); |
jhnwkmn | 0:a9a5c12f2d30 | 516 | sq_pushstring(v,p_name,-1); |
jhnwkmn | 0:a9a5c12f2d30 | 517 | sq_pushinteger(v,p_offset); |
jhnwkmn | 0:a9a5c12f2d30 | 518 | sq_newslot(v,-3,false); // set the get function |
jhnwkmn | 0:a9a5c12f2d30 | 519 | sq_pop(v,1); // pop get table |
jhnwkmn | 0:a9a5c12f2d30 | 520 | |
jhnwkmn | 0:a9a5c12f2d30 | 521 | // store the get function |
jhnwkmn | 0:a9a5c12f2d30 | 522 | |
jhnwkmn | 0:a9a5c12f2d30 | 523 | sq_pushobject(v,custom_member_var_table_get); |
jhnwkmn | 0:a9a5c12f2d30 | 524 | |
jhnwkmn | 0:a9a5c12f2d30 | 525 | sq_pushstring(v,p_name,-1); |
jhnwkmn | 0:a9a5c12f2d30 | 526 | unsigned char* _get_addr=(unsigned char*)sq_newuserdata(v,sizeof(MemberVarGetFunction)); |
jhnwkmn | 0:a9a5c12f2d30 | 527 | |
jhnwkmn | 0:a9a5c12f2d30 | 528 | union { |
jhnwkmn | 0:a9a5c12f2d30 | 529 | MemberVarGetFunction _getfunc; |
jhnwkmn | 0:a9a5c12f2d30 | 530 | unsigned char _bytes[sizeof(MemberVarGetFunction)]; |
jhnwkmn | 0:a9a5c12f2d30 | 531 | |
jhnwkmn | 0:a9a5c12f2d30 | 532 | } _get_union; |
jhnwkmn | 0:a9a5c12f2d30 | 533 | |
jhnwkmn | 0:a9a5c12f2d30 | 534 | _get_union._getfunc=sqbind_member_var_get<I>; |
jhnwkmn | 0:a9a5c12f2d30 | 535 | |
jhnwkmn | 0:a9a5c12f2d30 | 536 | for (size_t i=0;i<sizeof(MemberVarGetFunction);i++) |
jhnwkmn | 0:a9a5c12f2d30 | 537 | _get_addr[i]=_get_union._bytes[i]; |
jhnwkmn | 0:a9a5c12f2d30 | 538 | |
jhnwkmn | 0:a9a5c12f2d30 | 539 | sq_newslot(v,-3,false); // set the get function |
jhnwkmn | 0:a9a5c12f2d30 | 540 | sq_pop(v,1); // pop get table |
jhnwkmn | 0:a9a5c12f2d30 | 541 | |
jhnwkmn | 0:a9a5c12f2d30 | 542 | //store the set function |
jhnwkmn | 0:a9a5c12f2d30 | 543 | |
jhnwkmn | 0:a9a5c12f2d30 | 544 | sq_pushobject(v,custom_member_var_table_set); |
jhnwkmn | 0:a9a5c12f2d30 | 545 | |
jhnwkmn | 0:a9a5c12f2d30 | 546 | sq_pushstring(v,p_name,-1); |
jhnwkmn | 0:a9a5c12f2d30 | 547 | unsigned char* _set_addr=(unsigned char*)sq_newuserdata(v,sizeof(MemberVarSetFunction)); |
jhnwkmn | 0:a9a5c12f2d30 | 548 | |
jhnwkmn | 0:a9a5c12f2d30 | 549 | union { |
jhnwkmn | 0:a9a5c12f2d30 | 550 | MemberVarSetFunction _setfunc; |
jhnwkmn | 0:a9a5c12f2d30 | 551 | unsigned char _bytes[sizeof(MemberVarSetFunction)]; |
jhnwkmn | 0:a9a5c12f2d30 | 552 | |
jhnwkmn | 0:a9a5c12f2d30 | 553 | } _set_union; |
jhnwkmn | 0:a9a5c12f2d30 | 554 | |
jhnwkmn | 0:a9a5c12f2d30 | 555 | _set_union._setfunc=sqbind_member_var_set<I>; |
jhnwkmn | 0:a9a5c12f2d30 | 556 | |
jhnwkmn | 0:a9a5c12f2d30 | 557 | for (size_t i=0;i<sizeof(MemberVarSetFunction);i++) |
jhnwkmn | 0:a9a5c12f2d30 | 558 | _set_addr[i]=_set_union._bytes[i]; |
jhnwkmn | 0:a9a5c12f2d30 | 559 | |
jhnwkmn | 0:a9a5c12f2d30 | 560 | sq_newslot(v,-3,false); // set the get function |
jhnwkmn | 0:a9a5c12f2d30 | 561 | sq_pop(v,1); // pop get table |
jhnwkmn | 0:a9a5c12f2d30 | 562 | |
jhnwkmn | 0:a9a5c12f2d30 | 563 | } |
jhnwkmn | 0:a9a5c12f2d30 | 564 | |
jhnwkmn | 0:a9a5c12f2d30 | 565 | static HSQOBJECT get_id() { |
jhnwkmn | 0:a9a5c12f2d30 | 566 | |
jhnwkmn | 0:a9a5c12f2d30 | 567 | return class_id; |
jhnwkmn | 0:a9a5c12f2d30 | 568 | } |
jhnwkmn | 0:a9a5c12f2d30 | 569 | |
jhnwkmn | 0:a9a5c12f2d30 | 570 | static void set_custom_constructor(Constructor p_constructor) { |
jhnwkmn | 0:a9a5c12f2d30 | 571 | |
jhnwkmn | 0:a9a5c12f2d30 | 572 | constructor=p_constructor; |
jhnwkmn | 0:a9a5c12f2d30 | 573 | } |
jhnwkmn | 0:a9a5c12f2d30 | 574 | |
jhnwkmn | 0:a9a5c12f2d30 | 575 | static void bind_full_comparator(HSQUIRRELVM v) { |
jhnwkmn | 0:a9a5c12f2d30 | 576 | |
jhnwkmn | 0:a9a5c12f2d30 | 577 | bind_method(v,"_cmp",default_full_comparator,false); |
jhnwkmn | 0:a9a5c12f2d30 | 578 | |
jhnwkmn | 0:a9a5c12f2d30 | 579 | } |
jhnwkmn | 0:a9a5c12f2d30 | 580 | |
jhnwkmn | 0:a9a5c12f2d30 | 581 | static void bind_bool_comparator(HSQUIRRELVM v) { |
jhnwkmn | 0:a9a5c12f2d30 | 582 | |
jhnwkmn | 0:a9a5c12f2d30 | 583 | bind_method(v,"_cmp",default_boolean_comparator,false); |
jhnwkmn | 0:a9a5c12f2d30 | 584 | |
jhnwkmn | 0:a9a5c12f2d30 | 585 | } |
jhnwkmn | 0:a9a5c12f2d30 | 586 | |
jhnwkmn | 0:a9a5c12f2d30 | 587 | static void bind_constant(HSQUIRRELVM v,const SQChar *p_name,SQInteger p_value) { |
jhnwkmn | 0:a9a5c12f2d30 | 588 | |
jhnwkmn | 0:a9a5c12f2d30 | 589 | sq_pushobject(v,class_id); |
jhnwkmn | 0:a9a5c12f2d30 | 590 | sq_pushstring(v,p_name,-1); |
jhnwkmn | 0:a9a5c12f2d30 | 591 | sq_pushinteger(v,p_value); |
jhnwkmn | 0:a9a5c12f2d30 | 592 | sq_newslot(v,-3,true); |
jhnwkmn | 0:a9a5c12f2d30 | 593 | |
jhnwkmn | 0:a9a5c12f2d30 | 594 | } |
jhnwkmn | 0:a9a5c12f2d30 | 595 | |
jhnwkmn | 0:a9a5c12f2d30 | 596 | static void init(HSQUIRRELVM v,const SQChar * p_name,const SQChar *p_base_class_name, bool p_instantiable=true) { |
jhnwkmn | 0:a9a5c12f2d30 | 597 | |
jhnwkmn | 0:a9a5c12f2d30 | 598 | sq_pushroottable(v); |
jhnwkmn | 0:a9a5c12f2d30 | 599 | sq_pushstring(v,p_base_class_name,-1); |
jhnwkmn | 0:a9a5c12f2d30 | 600 | if (SQ_FAILED( sq_get(v,-2) ) ) { |
jhnwkmn | 0:a9a5c12f2d30 | 601 | |
jhnwkmn | 0:a9a5c12f2d30 | 602 | fprintf(stderr,"SqBind Error - Base Class %s not in root table (doesn't exist?).\n",name); |
jhnwkmn | 0:a9a5c12f2d30 | 603 | sq_pop(v,1); |
jhnwkmn | 0:a9a5c12f2d30 | 604 | return; |
jhnwkmn | 0:a9a5c12f2d30 | 605 | |
jhnwkmn | 0:a9a5c12f2d30 | 606 | } |
jhnwkmn | 0:a9a5c12f2d30 | 607 | |
jhnwkmn | 0:a9a5c12f2d30 | 608 | HSQOBJECT base_class; |
jhnwkmn | 0:a9a5c12f2d30 | 609 | sq_resetobject(&base_class); |
jhnwkmn | 0:a9a5c12f2d30 | 610 | sq_getstackobj(v,-1,&base_class); |
jhnwkmn | 0:a9a5c12f2d30 | 611 | init(v,p_name,&base_class,p_instantiable); |
jhnwkmn | 0:a9a5c12f2d30 | 612 | sq_pop(v,2); // pop base class and root table |
jhnwkmn | 0:a9a5c12f2d30 | 613 | |
jhnwkmn | 0:a9a5c12f2d30 | 614 | } |
jhnwkmn | 0:a9a5c12f2d30 | 615 | |
jhnwkmn | 0:a9a5c12f2d30 | 616 | static void init(HSQUIRRELVM v,const SQChar * p_name,HSQOBJECT *p_base_class=NULL, bool p_instantiable=true) { |
jhnwkmn | 0:a9a5c12f2d30 | 617 | |
jhnwkmn | 0:a9a5c12f2d30 | 618 | // already bound |
jhnwkmn | 0:a9a5c12f2d30 | 619 | if (initialized) { |
jhnwkmn | 0:a9a5c12f2d30 | 620 | |
jhnwkmn | 0:a9a5c12f2d30 | 621 | fprintf(stderr,"SqBind Error - Class %s already initialized\n",name); |
jhnwkmn | 0:a9a5c12f2d30 | 622 | return; |
jhnwkmn | 0:a9a5c12f2d30 | 623 | } |
jhnwkmn | 0:a9a5c12f2d30 | 624 | |
jhnwkmn | 0:a9a5c12f2d30 | 625 | /* CREATE CLASS TABLE */ |
jhnwkmn | 0:a9a5c12f2d30 | 626 | name=p_name; |
jhnwkmn | 0:a9a5c12f2d30 | 627 | instantiable=p_instantiable; |
jhnwkmn | 0:a9a5c12f2d30 | 628 | initialized=true; |
jhnwkmn | 0:a9a5c12f2d30 | 629 | |
jhnwkmn | 0:a9a5c12f2d30 | 630 | // preparate for adding it to globals |
jhnwkmn | 0:a9a5c12f2d30 | 631 | sq_pushroottable(v); |
jhnwkmn | 0:a9a5c12f2d30 | 632 | sq_pushstring(v,p_name,-1); |
jhnwkmn | 0:a9a5c12f2d30 | 633 | if (p_base_class) { |
jhnwkmn | 0:a9a5c12f2d30 | 634 | sq_pushobject(v,*p_base_class); |
jhnwkmn | 0:a9a5c12f2d30 | 635 | } |
jhnwkmn | 0:a9a5c12f2d30 | 636 | sq_newclass(v,p_base_class!=NULL); |
jhnwkmn | 0:a9a5c12f2d30 | 637 | sq_getstackobj(v,-1,&class_id); |
jhnwkmn | 0:a9a5c12f2d30 | 638 | sq_settypetag(v,-1,&class_id); // use the address of classid (which is static) as typetag |
jhnwkmn | 0:a9a5c12f2d30 | 639 | |
jhnwkmn | 0:a9a5c12f2d30 | 640 | // create the default constructor |
jhnwkmn | 0:a9a5c12f2d30 | 641 | |
jhnwkmn | 0:a9a5c12f2d30 | 642 | sq_pushstring(v,_SC("constructor"),-1); |
jhnwkmn | 0:a9a5c12f2d30 | 643 | sq_resetobject(&string_constructor); |
jhnwkmn | 0:a9a5c12f2d30 | 644 | sq_getstackobj(v,-1,&string_constructor); |
jhnwkmn | 0:a9a5c12f2d30 | 645 | |
jhnwkmn | 0:a9a5c12f2d30 | 646 | sq_newclosure(v,default_constructor,0); |
jhnwkmn | 0:a9a5c12f2d30 | 647 | sq_newslot(v,-3,false); // add the default constructor |
jhnwkmn | 0:a9a5c12f2d30 | 648 | |
jhnwkmn | 0:a9a5c12f2d30 | 649 | sq_pushstring(v,_SC("_cloned"),-1); |
jhnwkmn | 0:a9a5c12f2d30 | 650 | sq_newclosure(v,default_cloned,0); |
jhnwkmn | 0:a9a5c12f2d30 | 651 | sq_newslot(v,-3,false); // add the default cloned |
jhnwkmn | 0:a9a5c12f2d30 | 652 | |
jhnwkmn | 0:a9a5c12f2d30 | 653 | sq_newslot(v,-3,false); // add class to the root table |
jhnwkmn | 0:a9a5c12f2d30 | 654 | |
jhnwkmn | 0:a9a5c12f2d30 | 655 | sq_pop(v,1); // pop root table |
jhnwkmn | 0:a9a5c12f2d30 | 656 | |
jhnwkmn | 0:a9a5c12f2d30 | 657 | // reset these so they are null |
jhnwkmn | 0:a9a5c12f2d30 | 658 | sq_resetobject(&custom_member_var_table_set); |
jhnwkmn | 0:a9a5c12f2d30 | 659 | sq_resetobject(&custom_member_var_table_get); |
jhnwkmn | 0:a9a5c12f2d30 | 660 | } |
jhnwkmn | 0:a9a5c12f2d30 | 661 | |
jhnwkmn | 0:a9a5c12f2d30 | 662 | |
jhnwkmn | 0:a9a5c12f2d30 | 663 | }; |
jhnwkmn | 0:a9a5c12f2d30 | 664 | |
jhnwkmn | 0:a9a5c12f2d30 | 665 | // init static variables. |
jhnwkmn | 0:a9a5c12f2d30 | 666 | |
jhnwkmn | 0:a9a5c12f2d30 | 667 | template<class T,class A> |
jhnwkmn | 0:a9a5c12f2d30 | 668 | HSQOBJECT SqBind<T,A>::class_id; |
jhnwkmn | 0:a9a5c12f2d30 | 669 | template<class T,class A> |
jhnwkmn | 0:a9a5c12f2d30 | 670 | bool SqBind<T,A>::initialized=false; |
jhnwkmn | 0:a9a5c12f2d30 | 671 | template<class T,class A> |
jhnwkmn | 0:a9a5c12f2d30 | 672 | const char *SqBind<T,A>::name="<undefined>"; |
jhnwkmn | 0:a9a5c12f2d30 | 673 | template<class T,class A> |
jhnwkmn | 0:a9a5c12f2d30 | 674 | bool SqBind<T,A>::instantiable=false; |
jhnwkmn | 0:a9a5c12f2d30 | 675 | template<class T,class A> |
jhnwkmn | 0:a9a5c12f2d30 | 676 | HSQOBJECT SqBind<T,A>::custom_member_var_table_set; |
jhnwkmn | 0:a9a5c12f2d30 | 677 | template<class T,class A> |
jhnwkmn | 0:a9a5c12f2d30 | 678 | HSQOBJECT SqBind<T,A>::custom_member_var_table_get; |
jhnwkmn | 0:a9a5c12f2d30 | 679 | template<class T,class A> |
jhnwkmn | 0:a9a5c12f2d30 | 680 | HSQOBJECT SqBind<T,A>::custom_member_var_table_offset; |
jhnwkmn | 0:a9a5c12f2d30 | 681 | template<class T,class A> |
jhnwkmn | 0:a9a5c12f2d30 | 682 | HSQOBJECT SqBind<T,A>::string_constructor; |
jhnwkmn | 0:a9a5c12f2d30 | 683 | template<class T,class A> |
jhnwkmn | 0:a9a5c12f2d30 | 684 | typename SqBind<T,A>::Constructor SqBind<T,A>::constructor=NULL; |
jhnwkmn | 0:a9a5c12f2d30 | 685 | |
jhnwkmn | 0:a9a5c12f2d30 | 686 | |
jhnwkmn | 0:a9a5c12f2d30 | 687 | #define SQBIND_CLASS_CONSTANT(m_vm,m_class,m_constant) \ |
jhnwkmn | 0:a9a5c12f2d30 | 688 | SqBind<m_class>::bind_constant( m_vm, _SC( #m_constant ), m_class::m_constant) |
jhnwkmn | 0:a9a5c12f2d30 | 689 | |
jhnwkmn | 0:a9a5c12f2d30 | 690 | |
jhnwkmn | 0:a9a5c12f2d30 | 691 | static SQBIND_INLINE void sqbind_bind_constant(HSQUIRRELVM v, const SQChar *p_constant, SQInteger p_value) { |
jhnwkmn | 0:a9a5c12f2d30 | 692 | |
jhnwkmn | 0:a9a5c12f2d30 | 693 | sq_pushroottable(v); |
jhnwkmn | 0:a9a5c12f2d30 | 694 | sq_pushstring(v,p_constant,-1); |
jhnwkmn | 0:a9a5c12f2d30 | 695 | sq_pushinteger(v,p_value); |
jhnwkmn | 0:a9a5c12f2d30 | 696 | sq_newslot(v,-3,false); |
jhnwkmn | 0:a9a5c12f2d30 | 697 | sq_pop(v,1); // pop roottable |
jhnwkmn | 0:a9a5c12f2d30 | 698 | |
jhnwkmn | 0:a9a5c12f2d30 | 699 | } |
jhnwkmn | 0:a9a5c12f2d30 | 700 | |
jhnwkmn | 0:a9a5c12f2d30 | 701 | #define SQBIND_CONSTANT(m_vm,m_constant) \ |
jhnwkmn | 0:a9a5c12f2d30 | 702 | sqbind_bind_constant(m_vm,_SC( #m_constant ), m_constant) |
jhnwkmn | 0:a9a5c12f2d30 | 703 | |
jhnwkmn | 0:a9a5c12f2d30 | 704 | |
jhnwkmn | 0:a9a5c12f2d30 | 705 | /* C++ native type binding specializations */ |
jhnwkmn | 0:a9a5c12f2d30 | 706 | |
jhnwkmn | 0:a9a5c12f2d30 | 707 | // integer binding. This is also very useful to bind enumerations. |
jhnwkmn | 0:a9a5c12f2d30 | 708 | |
jhnwkmn | 0:a9a5c12f2d30 | 709 | #define SQBIND_INTEGER( m_type )\ |
jhnwkmn | 0:a9a5c12f2d30 | 710 | template<>\ |
jhnwkmn | 0:a9a5c12f2d30 | 711 | class SqBind<m_type> {\ |
jhnwkmn | 0:a9a5c12f2d30 | 712 | public:\ |
jhnwkmn | 0:a9a5c12f2d30 | 713 | struct Getter {\ |
jhnwkmn | 0:a9a5c12f2d30 | 714 | SQBIND_INLINE m_type get(HSQUIRRELVM v, int p_idx) {\ |
jhnwkmn | 0:a9a5c12f2d30 | 715 | return SqBind<m_type>::get(v,p_idx);\ |
jhnwkmn | 0:a9a5c12f2d30 | 716 | }\ |
jhnwkmn | 0:a9a5c12f2d30 | 717 | };\ |
jhnwkmn | 0:a9a5c12f2d30 | 718 | struct GetterPtr {\ |
jhnwkmn | 0:a9a5c12f2d30 | 719 | m_type temp;\ |
jhnwkmn | 0:a9a5c12f2d30 | 720 | SQBIND_INLINE m_type* get(HSQUIRRELVM v, int p_idx) {\ |
jhnwkmn | 0:a9a5c12f2d30 | 721 | temp=SqBind<m_type>::get(v,p_idx);\ |
jhnwkmn | 0:a9a5c12f2d30 | 722 | return &temp;\ |
jhnwkmn | 0:a9a5c12f2d30 | 723 | }\ |
jhnwkmn | 0:a9a5c12f2d30 | 724 | };\ |
jhnwkmn | 0:a9a5c12f2d30 | 725 | typedef m_type Type;\ |
jhnwkmn | 0:a9a5c12f2d30 | 726 | static bool is_type(HSQUIRRELVM v, int p_idx) {\ |
jhnwkmn | 0:a9a5c12f2d30 | 727 | return (sq_gettype(v,p_idx)&SQOBJECT_NUMERIC);\ |
jhnwkmn | 0:a9a5c12f2d30 | 728 | }\ |
jhnwkmn | 0:a9a5c12f2d30 | 729 | static m_type get(HSQUIRRELVM v, int p_idx) {\ |
jhnwkmn | 0:a9a5c12f2d30 | 730 | if (!(sq_gettype(v,p_idx)&SQOBJECT_NUMERIC)) {\ |
jhnwkmn | 0:a9a5c12f2d30 | 731 | sqbind_throwerror(v,"Type is not numeric!");\ |
jhnwkmn | 0:a9a5c12f2d30 | 732 | return (m_type)0;\ |
jhnwkmn | 0:a9a5c12f2d30 | 733 | }\ |
jhnwkmn | 0:a9a5c12f2d30 | 734 | SQInteger ret;\ |
jhnwkmn | 0:a9a5c12f2d30 | 735 | sq_getinteger(v,p_idx,&ret);\ |
jhnwkmn | 0:a9a5c12f2d30 | 736 | return (m_type)ret;\ |
jhnwkmn | 0:a9a5c12f2d30 | 737 | }\ |
jhnwkmn | 0:a9a5c12f2d30 | 738 | \ |
jhnwkmn | 0:a9a5c12f2d30 | 739 | static void push(HSQUIRRELVM v, const m_type& p_value) {\ |
jhnwkmn | 0:a9a5c12f2d30 | 740 | sq_pushinteger(v,p_value);\ |
jhnwkmn | 0:a9a5c12f2d30 | 741 | }\ |
jhnwkmn | 0:a9a5c12f2d30 | 742 | static void push(HSQUIRRELVM v, const m_type* p_value) {\ |
jhnwkmn | 0:a9a5c12f2d30 | 743 | if (!p_value)\ |
jhnwkmn | 0:a9a5c12f2d30 | 744 | sq_pushnull(v);\ |
jhnwkmn | 0:a9a5c12f2d30 | 745 | else\ |
jhnwkmn | 0:a9a5c12f2d30 | 746 | sq_pushinteger(v,*p_value);\ |
jhnwkmn | 0:a9a5c12f2d30 | 747 | }\ |
jhnwkmn | 0:a9a5c12f2d30 | 748 | }; |
jhnwkmn | 0:a9a5c12f2d30 | 749 | |
jhnwkmn | 0:a9a5c12f2d30 | 750 | SQBIND_INTEGER(unsigned char); |
jhnwkmn | 0:a9a5c12f2d30 | 751 | SQBIND_INTEGER(signed char); |
jhnwkmn | 0:a9a5c12f2d30 | 752 | SQBIND_INTEGER(unsigned short); |
jhnwkmn | 0:a9a5c12f2d30 | 753 | SQBIND_INTEGER(signed short); |
jhnwkmn | 0:a9a5c12f2d30 | 754 | SQBIND_INTEGER(unsigned int); |
jhnwkmn | 0:a9a5c12f2d30 | 755 | SQBIND_INTEGER(signed int); |
jhnwkmn | 0:a9a5c12f2d30 | 756 | SQBIND_INTEGER(unsigned long); |
jhnwkmn | 0:a9a5c12f2d30 | 757 | SQBIND_INTEGER(signed long); |
jhnwkmn | 0:a9a5c12f2d30 | 758 | |
jhnwkmn | 0:a9a5c12f2d30 | 759 | #ifdef _MSVC |
jhnwkmn | 0:a9a5c12f2d30 | 760 | |
jhnwkmn | 0:a9a5c12f2d30 | 761 | SQBIND_INTEGER(__int64); |
jhnwkmn | 0:a9a5c12f2d30 | 762 | SQBIND_INTEGER(unsigned __int64); |
jhnwkmn | 0:a9a5c12f2d30 | 763 | |
jhnwkmn | 0:a9a5c12f2d30 | 764 | #else |
jhnwkmn | 0:a9a5c12f2d30 | 765 | SQBIND_INTEGER(long long); |
jhnwkmn | 0:a9a5c12f2d30 | 766 | SQBIND_INTEGER(unsigned long long); |
jhnwkmn | 0:a9a5c12f2d30 | 767 | |
jhnwkmn | 0:a9a5c12f2d30 | 768 | #endif |
jhnwkmn | 0:a9a5c12f2d30 | 769 | |
jhnwkmn | 0:a9a5c12f2d30 | 770 | // floating point binding |
jhnwkmn | 0:a9a5c12f2d30 | 771 | |
jhnwkmn | 0:a9a5c12f2d30 | 772 | #define SQBIND_FLOAT( m_type )\ |
jhnwkmn | 0:a9a5c12f2d30 | 773 | template<>\ |
jhnwkmn | 0:a9a5c12f2d30 | 774 | class SqBind<m_type> {\ |
jhnwkmn | 0:a9a5c12f2d30 | 775 | public:\ |
jhnwkmn | 0:a9a5c12f2d30 | 776 | struct Getter {\ |
jhnwkmn | 0:a9a5c12f2d30 | 777 | SQBIND_INLINE m_type get(HSQUIRRELVM v, int p_idx) {\ |
jhnwkmn | 0:a9a5c12f2d30 | 778 | return SqBind<m_type>::get(v,p_idx);\ |
jhnwkmn | 0:a9a5c12f2d30 | 779 | }\ |
jhnwkmn | 0:a9a5c12f2d30 | 780 | };\ |
jhnwkmn | 0:a9a5c12f2d30 | 781 | struct GetterPtr {\ |
jhnwkmn | 0:a9a5c12f2d30 | 782 | m_type temp;\ |
jhnwkmn | 0:a9a5c12f2d30 | 783 | SQBIND_INLINE m_type* get(HSQUIRRELVM v, int p_idx) {\ |
jhnwkmn | 0:a9a5c12f2d30 | 784 | temp=SqBind<m_type>::get(v,p_idx);\ |
jhnwkmn | 0:a9a5c12f2d30 | 785 | return &temp;\ |
jhnwkmn | 0:a9a5c12f2d30 | 786 | }\ |
jhnwkmn | 0:a9a5c12f2d30 | 787 | };\ |
jhnwkmn | 0:a9a5c12f2d30 | 788 | typedef m_type Type;\ |
jhnwkmn | 0:a9a5c12f2d30 | 789 | static bool is_type(HSQUIRRELVM v, int p_idx) {\ |
jhnwkmn | 0:a9a5c12f2d30 | 790 | return (sq_gettype(v,p_idx)&SQOBJECT_NUMERIC);\ |
jhnwkmn | 0:a9a5c12f2d30 | 791 | }\ |
jhnwkmn | 0:a9a5c12f2d30 | 792 | static m_type get(HSQUIRRELVM v, int p_idx) {\ |
jhnwkmn | 0:a9a5c12f2d30 | 793 | if (!(sq_gettype(v,p_idx)&SQOBJECT_NUMERIC)) {\ |
jhnwkmn | 0:a9a5c12f2d30 | 794 | sqbind_throwerror(v,"Type is not numeric!");\ |
jhnwkmn | 0:a9a5c12f2d30 | 795 | return 0;\ |
jhnwkmn | 0:a9a5c12f2d30 | 796 | }\ |
jhnwkmn | 0:a9a5c12f2d30 | 797 | SQFloat ret;\ |
jhnwkmn | 0:a9a5c12f2d30 | 798 | sq_getfloat(v,p_idx,&ret);\ |
jhnwkmn | 0:a9a5c12f2d30 | 799 | return (m_type)ret;\ |
jhnwkmn | 0:a9a5c12f2d30 | 800 | }\ |
jhnwkmn | 0:a9a5c12f2d30 | 801 | \ |
jhnwkmn | 0:a9a5c12f2d30 | 802 | static void push(HSQUIRRELVM v, const m_type& p_value) {\ |
jhnwkmn | 0:a9a5c12f2d30 | 803 | sq_pushfloat(v,p_value);\ |
jhnwkmn | 0:a9a5c12f2d30 | 804 | }\ |
jhnwkmn | 0:a9a5c12f2d30 | 805 | static void push(HSQUIRRELVM v, const m_type* p_value) {\ |
jhnwkmn | 0:a9a5c12f2d30 | 806 | if (!p_value)\ |
jhnwkmn | 0:a9a5c12f2d30 | 807 | sq_pushnull(v);\ |
jhnwkmn | 0:a9a5c12f2d30 | 808 | else\ |
jhnwkmn | 0:a9a5c12f2d30 | 809 | sq_pushfloat(v,*p_value);\ |
jhnwkmn | 0:a9a5c12f2d30 | 810 | }\ |
jhnwkmn | 0:a9a5c12f2d30 | 811 | }; |
jhnwkmn | 0:a9a5c12f2d30 | 812 | |
jhnwkmn | 0:a9a5c12f2d30 | 813 | |
jhnwkmn | 0:a9a5c12f2d30 | 814 | SQBIND_FLOAT(float); |
jhnwkmn | 0:a9a5c12f2d30 | 815 | SQBIND_FLOAT(double); |
jhnwkmn | 0:a9a5c12f2d30 | 816 | SQBIND_FLOAT(long double); |
jhnwkmn | 0:a9a5c12f2d30 | 817 | |
jhnwkmn | 0:a9a5c12f2d30 | 818 | // bool binding |
jhnwkmn | 0:a9a5c12f2d30 | 819 | |
jhnwkmn | 0:a9a5c12f2d30 | 820 | #define SQBIND_BOOL( m_type )\ |
jhnwkmn | 0:a9a5c12f2d30 | 821 | template<>\ |
jhnwkmn | 0:a9a5c12f2d30 | 822 | class SqBind<m_type> {\ |
jhnwkmn | 0:a9a5c12f2d30 | 823 | public:\ |
jhnwkmn | 0:a9a5c12f2d30 | 824 | struct Getter {\ |
jhnwkmn | 0:a9a5c12f2d30 | 825 | SQBIND_INLINE m_type get(HSQUIRRELVM v, int p_idx) {\ |
jhnwkmn | 0:a9a5c12f2d30 | 826 | return SqBind<m_type>::get(v,p_idx);\ |
jhnwkmn | 0:a9a5c12f2d30 | 827 | }\ |
jhnwkmn | 0:a9a5c12f2d30 | 828 | };\ |
jhnwkmn | 0:a9a5c12f2d30 | 829 | struct GetterPtr {\ |
jhnwkmn | 0:a9a5c12f2d30 | 830 | m_type temp;\ |
jhnwkmn | 0:a9a5c12f2d30 | 831 | SQBIND_INLINE m_type* get(HSQUIRRELVM v, int p_idx) {\ |
jhnwkmn | 0:a9a5c12f2d30 | 832 | temp=SqBind<m_type>::get(v,p_idx);\ |
jhnwkmn | 0:a9a5c12f2d30 | 833 | return &temp;\ |
jhnwkmn | 0:a9a5c12f2d30 | 834 | }\ |
jhnwkmn | 0:a9a5c12f2d30 | 835 | };\ |
jhnwkmn | 0:a9a5c12f2d30 | 836 | typedef m_type Type;\ |
jhnwkmn | 0:a9a5c12f2d30 | 837 | static bool is_type(HSQUIRRELVM v, int p_idx) {\ |
jhnwkmn | 0:a9a5c12f2d30 | 838 | if (sq_gettype(v,p_idx)&SQOBJECT_NUMERIC) {\ |
jhnwkmn | 0:a9a5c12f2d30 | 839 | return true;\ |
jhnwkmn | 0:a9a5c12f2d30 | 840 | } else if (sq_gettype(v,p_idx)==OT_BOOL) {\ |
jhnwkmn | 0:a9a5c12f2d30 | 841 | return true;\ |
jhnwkmn | 0:a9a5c12f2d30 | 842 | } \ |
jhnwkmn | 0:a9a5c12f2d30 | 843 | return false;\ |
jhnwkmn | 0:a9a5c12f2d30 | 844 | }\ |
jhnwkmn | 0:a9a5c12f2d30 | 845 | static m_type get(HSQUIRRELVM v, int p_idx) {\ |
jhnwkmn | 0:a9a5c12f2d30 | 846 | if (sq_gettype(v,p_idx)&SQOBJECT_NUMERIC) {\ |
jhnwkmn | 0:a9a5c12f2d30 | 847 | SQInteger ret;\ |
jhnwkmn | 0:a9a5c12f2d30 | 848 | sq_getinteger(v,p_idx,&ret);\ |
jhnwkmn | 0:a9a5c12f2d30 | 849 | return (m_type)ret;\ |
jhnwkmn | 0:a9a5c12f2d30 | 850 | } else if (sq_gettype(v,p_idx)==OT_BOOL) {\ |
jhnwkmn | 0:a9a5c12f2d30 | 851 | SQBool ret;\ |
jhnwkmn | 0:a9a5c12f2d30 | 852 | sq_getbool(v,p_idx,&ret);\ |
jhnwkmn | 0:a9a5c12f2d30 | 853 | return (m_type)ret;\ |
jhnwkmn | 0:a9a5c12f2d30 | 854 | } else { \ |
jhnwkmn | 0:a9a5c12f2d30 | 855 | sqbind_throwerror(v,"Type is not numeric!");\ |
jhnwkmn | 0:a9a5c12f2d30 | 856 | return false;\ |
jhnwkmn | 0:a9a5c12f2d30 | 857 | }\ |
jhnwkmn | 0:a9a5c12f2d30 | 858 | }\ |
jhnwkmn | 0:a9a5c12f2d30 | 859 | \ |
jhnwkmn | 0:a9a5c12f2d30 | 860 | static void push(HSQUIRRELVM v, const m_type& p_value) {\ |
jhnwkmn | 0:a9a5c12f2d30 | 861 | sq_pushbool(v,p_value);\ |
jhnwkmn | 0:a9a5c12f2d30 | 862 | }\ |
jhnwkmn | 0:a9a5c12f2d30 | 863 | static void push(HSQUIRRELVM v, const m_type* p_value) {\ |
jhnwkmn | 0:a9a5c12f2d30 | 864 | if (!p_value)\ |
jhnwkmn | 0:a9a5c12f2d30 | 865 | sq_pushnull(v);\ |
jhnwkmn | 0:a9a5c12f2d30 | 866 | else\ |
jhnwkmn | 0:a9a5c12f2d30 | 867 | sq_pushbool(v,*p_value);\ |
jhnwkmn | 0:a9a5c12f2d30 | 868 | }\ |
jhnwkmn | 0:a9a5c12f2d30 | 869 | }; |
jhnwkmn | 0:a9a5c12f2d30 | 870 | |
jhnwkmn | 0:a9a5c12f2d30 | 871 | SQBIND_BOOL(bool); |
jhnwkmn | 0:a9a5c12f2d30 | 872 | |
jhnwkmn | 0:a9a5c12f2d30 | 873 | #if 0 |
jhnwkmn | 0:a9a5c12f2d30 | 874 | |
jhnwkmn | 0:a9a5c12f2d30 | 875 | // this is an example class on how to bind your own string type to native |
jhnwkmn | 0:a9a5c12f2d30 | 876 | // in this example, std::string is used. |
jhnwkmn | 0:a9a5c12f2d30 | 877 | |
jhnwkmn | 0:a9a5c12f2d30 | 878 | template<> |
jhnwkmn | 0:a9a5c12f2d30 | 879 | class SqBind<std::string> { |
jhnwkmn | 0:a9a5c12f2d30 | 880 | public: |
jhnwkmn | 0:a9a5c12f2d30 | 881 | struct Getter { |
jhnwkmn | 0:a9a5c12f2d30 | 882 | SQBIND_INLINE std::string get(HSQUIRRELVM v, int p_idx) { |
jhnwkmn | 0:a9a5c12f2d30 | 883 | return SqBind<std::string>::get(v,2); |
jhnwkmn | 0:a9a5c12f2d30 | 884 | } |
jhnwkmn | 0:a9a5c12f2d30 | 885 | }; |
jhnwkmn | 0:a9a5c12f2d30 | 886 | struct GetterPtr { |
jhnwkmn | 0:a9a5c12f2d30 | 887 | std::string temp; |
jhnwkmn | 0:a9a5c12f2d30 | 888 | SQBIND_INLINE std::string* get(HSQUIRRELVM v, int p_idx) { |
jhnwkmn | 0:a9a5c12f2d30 | 889 | temp=SqBind<std::string>::get(v,2); |
jhnwkmn | 0:a9a5c12f2d30 | 890 | return &temp; |
jhnwkmn | 0:a9a5c12f2d30 | 891 | } |
jhnwkmn | 0:a9a5c12f2d30 | 892 | }; |
jhnwkmn | 0:a9a5c12f2d30 | 893 | static std::string get(HSQUIRRELVM v, int p_idx) { |
jhnwkmn | 0:a9a5c12f2d30 | 894 | if (sq_gettype(v,p_idx)!=OT_STRING) { |
jhnwkmn | 0:a9a5c12f2d30 | 895 | sqbind_throwerror(v,"Type is not string!"); |
jhnwkmn | 0:a9a5c12f2d30 | 896 | return std::string(); |
jhnwkmn | 0:a9a5c12f2d30 | 897 | } |
jhnwkmn | 0:a9a5c12f2d30 | 898 | const SQChar * str; |
jhnwkmn | 0:a9a5c12f2d30 | 899 | sq_getstring(v,p_idx,&str); |
jhnwkmn | 0:a9a5c12f2d30 | 900 | return std::string(str); |
jhnwkmn | 0:a9a5c12f2d30 | 901 | } |
jhnwkmn | 0:a9a5c12f2d30 | 902 | |
jhnwkmn | 0:a9a5c12f2d30 | 903 | static void push(HSQUIRRELVM v, const std::string& p_value) { |
jhnwkmn | 0:a9a5c12f2d30 | 904 | sq_pushstring(v,p_value.c_str(),-1); |
jhnwkmn | 0:a9a5c12f2d30 | 905 | } |
jhnwkmn | 0:a9a5c12f2d30 | 906 | }; |
jhnwkmn | 0:a9a5c12f2d30 | 907 | |
jhnwkmn | 0:a9a5c12f2d30 | 908 | #endif |
jhnwkmn | 0:a9a5c12f2d30 | 909 | |
jhnwkmn | 0:a9a5c12f2d30 | 910 | |
jhnwkmn | 0:a9a5c12f2d30 | 911 | |
jhnwkmn | 0:a9a5c12f2d30 | 912 | /* HELPERS FOR sqbind_method */ |
jhnwkmn | 0:a9a5c12f2d30 | 913 | |
jhnwkmn | 0:a9a5c12f2d30 | 914 | template<class M> |
jhnwkmn | 0:a9a5c12f2d30 | 915 | void sqbind_push_method_userdata(HSQUIRRELVM v, M method ) { |
jhnwkmn | 0:a9a5c12f2d30 | 916 | |
jhnwkmn | 0:a9a5c12f2d30 | 917 | union { |
jhnwkmn | 0:a9a5c12f2d30 | 918 | |
jhnwkmn | 0:a9a5c12f2d30 | 919 | M m; |
jhnwkmn | 0:a9a5c12f2d30 | 920 | unsigned char b[sizeof(M)]; |
jhnwkmn | 0:a9a5c12f2d30 | 921 | } _mu; |
jhnwkmn | 0:a9a5c12f2d30 | 922 | |
jhnwkmn | 0:a9a5c12f2d30 | 923 | _mu.m=method; |
jhnwkmn | 0:a9a5c12f2d30 | 924 | unsigned char *ptr =(unsigned char*)sq_newuserdata(v, sizeof(M)); |
jhnwkmn | 0:a9a5c12f2d30 | 925 | for (size_t i=0;i<sizeof(M);i++) |
jhnwkmn | 0:a9a5c12f2d30 | 926 | ptr[i]=_mu.b[i]; |
jhnwkmn | 0:a9a5c12f2d30 | 927 | } |
jhnwkmn | 0:a9a5c12f2d30 | 928 | |
jhnwkmn | 0:a9a5c12f2d30 | 929 | template<class T> |
jhnwkmn | 0:a9a5c12f2d30 | 930 | struct SqCParam { |
jhnwkmn | 0:a9a5c12f2d30 | 931 | typename SqBind<T>::Getter getter; |
jhnwkmn | 0:a9a5c12f2d30 | 932 | }; |
jhnwkmn | 0:a9a5c12f2d30 | 933 | |
jhnwkmn | 0:a9a5c12f2d30 | 934 | template<class T> |
jhnwkmn | 0:a9a5c12f2d30 | 935 | struct SqCParam<T&> { |
jhnwkmn | 0:a9a5c12f2d30 | 936 | typename SqBind<T>::Getter getter; |
jhnwkmn | 0:a9a5c12f2d30 | 937 | }; |
jhnwkmn | 0:a9a5c12f2d30 | 938 | |
jhnwkmn | 0:a9a5c12f2d30 | 939 | template<class T> |
jhnwkmn | 0:a9a5c12f2d30 | 940 | struct SqCParam<const T&> { |
jhnwkmn | 0:a9a5c12f2d30 | 941 | typename SqBind<T>::Getter getter; |
jhnwkmn | 0:a9a5c12f2d30 | 942 | }; |
jhnwkmn | 0:a9a5c12f2d30 | 943 | |
jhnwkmn | 0:a9a5c12f2d30 | 944 | template<class T> |
jhnwkmn | 0:a9a5c12f2d30 | 945 | struct SqCParam<const T> { |
jhnwkmn | 0:a9a5c12f2d30 | 946 | typename SqBind<T>::Getter getter; |
jhnwkmn | 0:a9a5c12f2d30 | 947 | }; |
jhnwkmn | 0:a9a5c12f2d30 | 948 | |
jhnwkmn | 0:a9a5c12f2d30 | 949 | template<class T> |
jhnwkmn | 0:a9a5c12f2d30 | 950 | struct SqCParam<T*> { |
jhnwkmn | 0:a9a5c12f2d30 | 951 | |
jhnwkmn | 0:a9a5c12f2d30 | 952 | typename SqBind<T>::GetterPtr getter; |
jhnwkmn | 0:a9a5c12f2d30 | 953 | }; |
jhnwkmn | 0:a9a5c12f2d30 | 954 | |
jhnwkmn | 0:a9a5c12f2d30 | 955 | template<class T> |
jhnwkmn | 0:a9a5c12f2d30 | 956 | struct SqCParam<const T*> { |
jhnwkmn | 0:a9a5c12f2d30 | 957 | |
jhnwkmn | 0:a9a5c12f2d30 | 958 | typename SqBind<T>::GetterPtr getter; |
jhnwkmn | 0:a9a5c12f2d30 | 959 | }; |
jhnwkmn | 0:a9a5c12f2d30 | 960 | |
jhnwkmn | 0:a9a5c12f2d30 | 961 | template<class T, class M> |
jhnwkmn | 0:a9a5c12f2d30 | 962 | static void sqbind_push_method_userdata(HSQUIRRELVM v, M method ) { |
jhnwkmn | 0:a9a5c12f2d30 | 963 | |
jhnwkmn | 0:a9a5c12f2d30 | 964 | union { |
jhnwkmn | 0:a9a5c12f2d30 | 965 | |
jhnwkmn | 0:a9a5c12f2d30 | 966 | M m; |
jhnwkmn | 0:a9a5c12f2d30 | 967 | unsigned char b[sizeof(M)]; |
jhnwkmn | 0:a9a5c12f2d30 | 968 | } _mu; |
jhnwkmn | 0:a9a5c12f2d30 | 969 | |
jhnwkmn | 0:a9a5c12f2d30 | 970 | _mu.m=method; |
jhnwkmn | 0:a9a5c12f2d30 | 971 | unsigned char *ptr =(unsigned char*)sq_newuserdata(v,sizeof(M)); |
jhnwkmn | 0:a9a5c12f2d30 | 972 | for (size_t i=0;i<sizeof(M);i++) |
jhnwkmn | 0:a9a5c12f2d30 | 973 | ptr[i]=_mu.b[i]; |
jhnwkmn | 0:a9a5c12f2d30 | 974 | |
jhnwkmn | 0:a9a5c12f2d30 | 975 | |
jhnwkmn | 0:a9a5c12f2d30 | 976 | } |
jhnwkmn | 0:a9a5c12f2d30 | 977 | |
jhnwkmn | 0:a9a5c12f2d30 | 978 | // little helper |
jhnwkmn | 0:a9a5c12f2d30 | 979 | |
jhnwkmn | 0:a9a5c12f2d30 | 980 | |
jhnwkmn | 0:a9a5c12f2d30 | 981 | #define _SQBC( m_type ) SqBind<typename SqBindSimpleType<m_type>::type_t> |
jhnwkmn | 0:a9a5c12f2d30 | 982 | |
jhnwkmn | 0:a9a5c12f2d30 | 983 | #include "sqmethods.inc" |
jhnwkmn | 0:a9a5c12f2d30 | 984 | |
jhnwkmn | 0:a9a5c12f2d30 | 985 | #ifdef SQBIND_NAMESPACE |
jhnwkmn | 0:a9a5c12f2d30 | 986 | |
jhnwkmn | 0:a9a5c12f2d30 | 987 | }; // end of namespace |
jhnwkmn | 0:a9a5c12f2d30 | 988 | #endif |
jhnwkmn | 0:a9a5c12f2d30 | 989 | |
jhnwkmn | 0:a9a5c12f2d30 | 990 | |
jhnwkmn | 0:a9a5c12f2d30 | 991 | #endif |