Files
@ r13060:e57594b0ca84
Branch filter:
Location: cpp/openttd-patchpack/source/src/script/squirrel_helper.hpp
r13060:e57594b0ca84
32.7 KiB
text/x-c++hdr
(svn r17570) -Fix: a number of Doxygen warnings about missing parameters, which were sometimes missing and sometimes just typos
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 | /* $Id$ */
/*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file squirrel_helper.hpp declarations and parts of the implementation of the class for convert code */
#ifndef SQUIRREL_HELPER_HPP
#define SQUIRREL_HELPER_HPP
#include <squirrel.h>
#include "../core/math_func.hpp"
#include "../core/smallvec_type.hpp"
#include "../economy_type.h"
#include "../string_func.h"
#include "squirrel_helper_type.hpp"
/**
* The Squirrel convert routines
*/
namespace SQConvert {
/**
* Pointers assigned to this class will be free'd when this instance
* comes out of scope. Useful to make sure you can use strdup(),
* without leaking memory.
*/
struct SQAutoFreePointers : SmallVector<void *, 1> {
~SQAutoFreePointers()
{
for (uint i = 0; i < this->items; i++) free(this->data[i]);
}
};
template <bool Y> struct YesT {
static const bool Yes = Y;
static const bool No = !Y;
};
/**
* Helper class to recognize if the given type is void. Usage: 'IsVoidT<T>::Yes'
*/
template <typename T> struct IsVoidT : YesT<false> {};
template <> struct IsVoidT<void> : YesT<true> {};
/**
* Helper class to recognize if the function/method return type is void.
*/
template <typename Tfunc> struct HasVoidReturnT;
/* functions */
template <typename Tretval> struct HasVoidReturnT<Tretval (*)()> : IsVoidT<Tretval> {};
template <typename Tretval, typename Targ1> struct HasVoidReturnT<Tretval (*)(Targ1)> : IsVoidT<Tretval> {};
template <typename Tretval, typename Targ1, typename Targ2> struct HasVoidReturnT<Tretval (*)(Targ1, Targ2)> : IsVoidT<Tretval> {};
template <typename Tretval, typename Targ1, typename Targ2, typename Targ3> struct HasVoidReturnT<Tretval (*)(Targ1, Targ2, Targ3)> : IsVoidT<Tretval> {};
template <typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4> struct HasVoidReturnT<Tretval (*)(Targ1, Targ2, Targ3, Targ4)> : IsVoidT<Tretval> {};
template <typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5> struct HasVoidReturnT<Tretval (*)(Targ1, Targ2, Targ3, Targ4, Targ5)> : IsVoidT<Tretval> {};
template <typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5, typename Targ6, typename Targ7, typename Targ8, typename Targ9, typename Targ10> struct HasVoidReturnT<Tretval (*)(Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10)> : IsVoidT<Tretval> {};
/* methods */
template <class Tcls, typename Tretval> struct HasVoidReturnT<Tretval (Tcls::*)()> : IsVoidT<Tretval> {};
template <class Tcls, typename Tretval, typename Targ1> struct HasVoidReturnT<Tretval (Tcls::*)(Targ1)> : IsVoidT<Tretval> {};
template <class Tcls, typename Tretval, typename Targ1, typename Targ2> struct HasVoidReturnT<Tretval (Tcls::*)(Targ1, Targ2)> : IsVoidT<Tretval> {};
template <class Tcls, typename Tretval, typename Targ1, typename Targ2, typename Targ3> struct HasVoidReturnT<Tretval (Tcls::*)(Targ1, Targ2, Targ3)> : IsVoidT<Tretval> {};
template <class Tcls, typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4> struct HasVoidReturnT<Tretval (Tcls::*)(Targ1, Targ2, Targ3, Targ4)> : IsVoidT<Tretval> {};
template <class Tcls, typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5> struct HasVoidReturnT<Tretval (Tcls::*)(Targ1, Targ2, Targ3, Targ4, Targ5)> : IsVoidT<Tretval> {};
template <class Tcls, typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5, typename Targ6, typename Targ7, typename Targ8, typename Targ9, typename Targ10> struct HasVoidReturnT<Tretval (Tcls::*)(Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10)> : IsVoidT<Tretval> {};
/**
* Special class to make it possible for the compiler to pick the correct GetParam().
*/
template <typename T> class ForceType { };
/**
* To return a value to squirrel, we call this function. It converts to the right format.
*/
template <typename T> static int Return(HSQUIRRELVM vm, T t);
template <> inline int Return<uint8> (HSQUIRRELVM vm, uint8 res) { sq_pushinteger(vm, (int32)res); return 1; }
template <> inline int Return<uint16> (HSQUIRRELVM vm, uint16 res) { sq_pushinteger(vm, (int32)res); return 1; }
template <> inline int Return<uint32> (HSQUIRRELVM vm, uint32 res) { sq_pushinteger(vm, (int32)res); return 1; }
template <> inline int Return<int8> (HSQUIRRELVM vm, int8 res) { sq_pushinteger(vm, res); return 1; }
template <> inline int Return<int16> (HSQUIRRELVM vm, int16 res) { sq_pushinteger(vm, res); return 1; }
template <> inline int Return<int32> (HSQUIRRELVM vm, int32 res) { sq_pushinteger(vm, res); return 1; }
template <> inline int Return<int64> (HSQUIRRELVM vm, int64 res) { sq_pushinteger(vm, ClampToI32(res)); return 1; }
template <> inline int Return<Money> (HSQUIRRELVM vm, Money res) { sq_pushinteger(vm, ClampToI32(res)); return 1; }
template <> inline int Return<bool> (HSQUIRRELVM vm, bool res) { sq_pushbool (vm, res); return 1; }
template <> inline int Return<char *> (HSQUIRRELVM vm, char *res) { if (res == NULL) sq_pushnull(vm); else { sq_pushstring(vm, OTTD2FS(res), -1); free(res); } return 1; }
template <> inline int Return<const char *>(HSQUIRRELVM vm, const char *res) { if (res == NULL) sq_pushnull(vm); else { sq_pushstring(vm, OTTD2FS(res), -1); } return 1; }
template <> inline int Return<void *> (HSQUIRRELVM vm, void *res) { sq_pushuserpointer(vm, res); return 1; }
/**
* To get a param from squirrel, we call this function. It converts to the right format.
*/
template <typename T> static T GetParam(ForceType<T>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr);
template <> inline uint8 GetParam(ForceType<uint8> , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; }
template <> inline uint16 GetParam(ForceType<uint16> , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; }
template <> inline uint32 GetParam(ForceType<uint32> , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; }
template <> inline int8 GetParam(ForceType<int8> , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; }
template <> inline int16 GetParam(ForceType<int16> , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; }
template <> inline int32 GetParam(ForceType<int32> , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; }
template <> inline bool GetParam(ForceType<bool> , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQBool tmp; sq_getbool (vm, index, &tmp); return tmp != 0; }
template <> inline void *GetParam(ForceType<void *> , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer tmp; sq_getuserpointer(vm, index, &tmp); return tmp; }
template <> inline const char *GetParam(ForceType<const char *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr)
{
sq_tostring(vm, index);
const SQChar *tmp;
sq_getstring(vm, -1, &tmp);
char *tmp_str = strdup(FS2OTTD(tmp));
sq_poptop(vm);
*ptr->Append() = (void *)tmp_str;
str_validate(tmp_str, tmp_str + strlen(tmp_str));
return tmp_str;
}
template <> inline Array *GetParam(ForceType<Array *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr)
{
SQObject obj;
sq_getstackobj(vm, index, &obj);
sq_pushobject(vm, obj);
sq_pushnull(vm);
SmallVector<int32, 2> data;
while (SQ_SUCCEEDED(sq_next(vm, -2))) {
SQInteger tmp;
if (SQ_SUCCEEDED(sq_getinteger(vm, -1, &tmp))) {
*data.Append() = (int32)tmp;
} else {
sq_pop(vm, 4);
throw sq_throwerror(vm, _SC("a member of an array used as parameter to a function is not numeric"));
}
sq_pop(vm, 2);
}
sq_pop(vm, 2);
Array *arr = (Array*)MallocT<byte>(sizeof(Array) + sizeof(int32) * data.Length());
arr->size = data.Length();
memcpy(arr->array, data.Begin(), sizeof(int32) * data.Length());
*ptr->Append() = arr;
return arr;
}
/**
* Helper class to recognize the function type (retval type, args) and use the proper specialization
* for SQ callback. The partial specializations for the second arg (Tis_void_retval) are not possible
* on the function. Therefore the class is used instead.
*/
template <typename Tfunc, bool Tis_void_retval = HasVoidReturnT<Tfunc>::Yes> struct HelperT;
/**
* The real C++ caller for function with return value and 0 params.
*/
template <typename Tretval>
struct HelperT<Tretval (*)(), false> {
static int SQCall(void *instance, Tretval (*func)(), HSQUIRRELVM vm)
{
return Return(vm, (*func)());
}
};
/**
* The real C++ caller for function with no return value and 0 params.
*/
template <typename Tretval>
struct HelperT<Tretval (*)(), true> {
static int SQCall(void *instance, Tretval (*func)(), HSQUIRRELVM vm)
{
(*func)();
return 0;
}
};
/**
* The real C++ caller for method with return value and 0 params.
*/
template <class Tcls, typename Tretval>
struct HelperT<Tretval (Tcls::*)(), false> {
static int SQCall(Tcls *instance, Tretval (Tcls::*func)(), HSQUIRRELVM vm)
{
return Return(vm, (instance->*func)());
}
};
/**
* The real C++ caller for method with no return value and 0 params.
*/
template <class Tcls, typename Tretval>
struct HelperT<Tretval (Tcls::*)(), true> {
static int SQCall(Tcls *instance, Tretval (Tcls::*func)(), HSQUIRRELVM vm)
{
(instance->*func)();
return 0;
}
static Tcls *SQConstruct(Tcls *instance, Tretval (Tcls::*func)(), HSQUIRRELVM vm)
{
return new Tcls();
}
};
/**
* The real C++ caller for function with return value and 1 param.
*/
template <typename Tretval, typename Targ1>
struct HelperT<Tretval (*)(Targ1), false> {
static int SQCall(void *instance, Tretval (*func)(Targ1), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
Tretval ret = (*func)(
GetParam(ForceType<Targ1>(), vm, 2, &ptr)
);
sq_pop(vm, 1);
return Return(vm, ret);
}
};
/**
* The real C++ caller for function with no return value and 1 param.
*/
template <typename Tretval, typename Targ1>
struct HelperT<Tretval (*)(Targ1), true> {
static int SQCall(void *instance, Tretval (*func)(Targ1), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
(*func)(
GetParam(ForceType<Targ1>(), vm, 2, &ptr)
);
sq_pop(vm, 1);
return 0;
}
};
/**
* The real C++ caller for method with return value and 1 param.
*/
template <class Tcls, typename Tretval, typename Targ1>
struct HelperT<Tretval (Tcls::*)(Targ1), false> {
static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
Tretval ret = (instance->*func)(
GetParam(ForceType<Targ1>(), vm, 2, &ptr)
);
sq_pop(vm, 1);
return Return(vm, ret);
}
};
/**
* The real C++ caller for method with no return value and 1 param.
*/
template <class Tcls, typename Tretval, typename Targ1>
struct HelperT<Tretval (Tcls::*)(Targ1), true> {
static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
(instance->*func)(
GetParam(ForceType<Targ1>(), vm, 2, &ptr)
);
sq_pop(vm, 1);
return 0;
}
static Tcls *SQConstruct(Tcls *instance, Tretval (Tcls::*func)(Targ1), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
Tcls *inst = new Tcls(
GetParam(ForceType<Targ1>(), vm, 2, &ptr)
);
return inst;
}
};
/**
* The real C++ caller for function with return value and 2 params.
*/
template <typename Tretval, typename Targ1, typename Targ2>
struct HelperT<Tretval (*)(Targ1, Targ2), false> {
static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
Tretval ret = (*func)(
GetParam(ForceType<Targ1>(), vm, 2, &ptr),
GetParam(ForceType<Targ2>(), vm, 3, &ptr)
);
sq_pop(vm, 2);
return Return(vm, ret);
}
};
/**
* The real C++ caller for function with no return value and 2 params.
*/
template <typename Tretval, typename Targ1, typename Targ2>
struct HelperT<Tretval (*)(Targ1, Targ2), true> {
static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
(*func)(
GetParam(ForceType<Targ1>(), vm, 2, &ptr),
GetParam(ForceType<Targ2>(), vm, 3, &ptr)
);
sq_pop(vm, 2);
return 0;
}
};
/**
* The real C++ caller for method with return value and 2 params.
*/
template <class Tcls, typename Tretval, typename Targ1, typename Targ2>
struct HelperT<Tretval (Tcls::*)(Targ1, Targ2), false> {
static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
Tretval ret = (instance->*func)(
GetParam(ForceType<Targ1>(), vm, 2, &ptr),
GetParam(ForceType<Targ2>(), vm, 3, &ptr)
);
sq_pop(vm, 2);
return Return(vm, ret);
}
};
/**
* The real C++ caller for method with no return value and 2 params.
*/
template <class Tcls, typename Tretval, typename Targ1, typename Targ2>
struct HelperT<Tretval (Tcls::*)(Targ1, Targ2), true> {
static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
(instance->*func)(
GetParam(ForceType<Targ1>(), vm, 2, &ptr),
GetParam(ForceType<Targ2>(), vm, 3, &ptr)
);
sq_pop(vm, 2);
return 0;
}
static Tcls *SQConstruct(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
Tcls *inst = new Tcls(
GetParam(ForceType<Targ1>(), vm, 2, &ptr),
GetParam(ForceType<Targ2>(), vm, 3, &ptr)
);
return inst;
}
};
/**
* The real C++ caller for function with return value and 3 params.
*/
template <typename Tretval, typename Targ1, typename Targ2, typename Targ3>
struct HelperT<Tretval (*)(Targ1, Targ2, Targ3), false> {
static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2, Targ3), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
Tretval ret = (*func)(
GetParam(ForceType<Targ1>(), vm, 2, &ptr),
GetParam(ForceType<Targ2>(), vm, 3, &ptr),
GetParam(ForceType<Targ3>(), vm, 4, &ptr)
);
sq_pop(vm, 3);
return Return(vm, ret);
}
};
/**
* The real C++ caller for function with no return value and 3 params.
*/
template <typename Tretval, typename Targ1, typename Targ2, typename Targ3>
struct HelperT<Tretval (*)(Targ1, Targ2, Targ3), true> {
static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2, Targ3), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
(*func)(
GetParam(ForceType<Targ1>(), vm, 2, &ptr),
GetParam(ForceType<Targ2>(), vm, 3, &ptr),
GetParam(ForceType<Targ3>(), vm, 4, &ptr)
);
sq_pop(vm, 3);
return 0;
}
};
/**
* The real C++ caller for method with return value and 3 params.
*/
template <class Tcls, typename Tretval, typename Targ1, typename Targ2, typename Targ3>
struct HelperT<Tretval (Tcls::*)(Targ1, Targ2, Targ3), false> {
static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
Tretval ret = (instance->*func)(
GetParam(ForceType<Targ1>(), vm, 2, &ptr),
GetParam(ForceType<Targ2>(), vm, 3, &ptr),
GetParam(ForceType<Targ3>(), vm, 4, &ptr)
);
sq_pop(vm, 3);
return Return(vm, ret);
}
};
/**
* The real C++ caller for method with no return value and 3 params.
*/
template <class Tcls, typename Tretval, typename Targ1, typename Targ2, typename Targ3>
struct HelperT<Tretval (Tcls::*)(Targ1, Targ2, Targ3), true> {
static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
(instance->*func)(
GetParam(ForceType<Targ1>(), vm, 2, &ptr),
GetParam(ForceType<Targ2>(), vm, 3, &ptr),
GetParam(ForceType<Targ3>(), vm, 4, &ptr)
);
sq_pop(vm, 3);
return 0;
}
static Tcls *SQConstruct(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
Tcls *inst = new Tcls(
GetParam(ForceType<Targ1>(), vm, 2, &ptr),
GetParam(ForceType<Targ2>(), vm, 3, &ptr),
GetParam(ForceType<Targ3>(), vm, 4, &ptr)
);
return inst;
}
};
/**
* The real C++ caller for function with return value and 4 params.
*/
template <typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4>
struct HelperT<Tretval (*)(Targ1, Targ2, Targ3, Targ4), false> {
static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2, Targ3, Targ4), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
Tretval ret = (*func)(
GetParam(ForceType<Targ1>(), vm, 2, &ptr),
GetParam(ForceType<Targ2>(), vm, 3, &ptr),
GetParam(ForceType<Targ3>(), vm, 4, &ptr),
GetParam(ForceType<Targ4>(), vm, 5, &ptr)
);
sq_pop(vm, 4);
return Return(vm, ret);
}
};
/**
* The real C++ caller for function with no return value and 4 params.
*/
template <typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4>
struct HelperT<Tretval (*)(Targ1, Targ2, Targ3, Targ4), true> {
static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2, Targ3, Targ4), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
(*func)(
GetParam(ForceType<Targ1>(), vm, 2, &ptr),
GetParam(ForceType<Targ2>(), vm, 3, &ptr),
GetParam(ForceType<Targ3>(), vm, 4, &ptr),
GetParam(ForceType<Targ4>(), vm, 5, &ptr)
);
sq_pop(vm, 4);
return 0;
}
};
/**
* The real C++ caller for method with return value and 4 params.
*/
template <class Tcls, typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4>
struct HelperT<Tretval (Tcls::*)(Targ1, Targ2, Targ3, Targ4), false> {
static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3, Targ4), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
Tretval ret = (instance->*func)(
GetParam(ForceType<Targ1>(), vm, 2, &ptr),
GetParam(ForceType<Targ2>(), vm, 3, &ptr),
GetParam(ForceType<Targ3>(), vm, 4, &ptr),
GetParam(ForceType<Targ4>(), vm, 5, &ptr)
);
sq_pop(vm, 4);
return Return(vm, ret);
}
};
/**
* The real C++ caller for method with no return value and 4 params.
*/
template <class Tcls, typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4>
struct HelperT<Tretval (Tcls::*)(Targ1, Targ2, Targ3, Targ4), true> {
static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3, Targ4), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
(instance->*func)(
GetParam(ForceType<Targ1>(), vm, 2, &ptr),
GetParam(ForceType<Targ2>(), vm, 3, &ptr),
GetParam(ForceType<Targ3>(), vm, 4, &ptr),
GetParam(ForceType<Targ4>(), vm, 5, &ptr)
);
sq_pop(vm, 4);
return 0;
}
static Tcls *SQConstruct(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3, Targ4), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
Tcls *inst = new Tcls(
GetParam(ForceType<Targ1>(), vm, 2, &ptr),
GetParam(ForceType<Targ2>(), vm, 3, &ptr),
GetParam(ForceType<Targ3>(), vm, 4, &ptr),
GetParam(ForceType<Targ4>(), vm, 5, &ptr)
);
return inst;
}
};
/**
* The real C++ caller for function with return value and 5 params.
*/
template <typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5>
struct HelperT<Tretval (*)(Targ1, Targ2, Targ3, Targ4, Targ5), false> {
static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2, Targ3, Targ4, Targ5), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
Tretval ret = (*func)(
GetParam(ForceType<Targ1>(), vm, 2, &ptr),
GetParam(ForceType<Targ2>(), vm, 3, &ptr),
GetParam(ForceType<Targ3>(), vm, 4, &ptr),
GetParam(ForceType<Targ4>(), vm, 5, &ptr),
GetParam(ForceType<Targ5>(), vm, 6, &ptr)
);
sq_pop(vm, 5);
return Return(vm, ret);
}
};
/**
* The real C++ caller for function with no return value and 5 params.
*/
template <typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5>
struct HelperT<Tretval (*)(Targ1, Targ2, Targ3, Targ4, Targ5), true> {
static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2, Targ3, Targ4, Targ5), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
(*func)(
GetParam(ForceType<Targ1>(), vm, 2, &ptr),
GetParam(ForceType<Targ2>(), vm, 3, &ptr),
GetParam(ForceType<Targ3>(), vm, 4, &ptr),
GetParam(ForceType<Targ4>(), vm, 5, &ptr),
GetParam(ForceType<Targ5>(), vm, 6, &ptr)
);
sq_pop(vm, 5);
return 0;
}
};
/**
* The real C++ caller for method with return value and 5 params.
*/
template <class Tcls, typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5>
struct HelperT<Tretval (Tcls::*)(Targ1, Targ2, Targ3, Targ4, Targ5), false> {
static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3, Targ4, Targ5), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
Tretval ret = (instance->*func)(
GetParam(ForceType<Targ1>(), vm, 2, &ptr),
GetParam(ForceType<Targ2>(), vm, 3, &ptr),
GetParam(ForceType<Targ3>(), vm, 4, &ptr),
GetParam(ForceType<Targ4>(), vm, 5, &ptr),
GetParam(ForceType<Targ5>(), vm, 6, &ptr)
);
sq_pop(vm, 5);
return Return(vm, ret);
}
};
/**
* The real C++ caller for method with no return value and 5 params.
*/
template <class Tcls, typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5>
struct HelperT<Tretval (Tcls::*)(Targ1, Targ2, Targ3, Targ4, Targ5), true> {
static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3, Targ4, Targ5), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
(instance->*func)(
GetParam(ForceType<Targ1>(), vm, 2, &ptr),
GetParam(ForceType<Targ2>(), vm, 3, &ptr),
GetParam(ForceType<Targ3>(), vm, 4, &ptr),
GetParam(ForceType<Targ4>(), vm, 5, &ptr),
GetParam(ForceType<Targ5>(), vm, 6, &ptr)
);
sq_pop(vm, 5);
return 0;
}
static Tcls *SQConstruct(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3, Targ4, Targ5), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
Tcls *inst = new Tcls(
GetParam(ForceType<Targ1>(), vm, 2, &ptr),
GetParam(ForceType<Targ2>(), vm, 3, &ptr),
GetParam(ForceType<Targ3>(), vm, 4, &ptr),
GetParam(ForceType<Targ4>(), vm, 5, &ptr),
GetParam(ForceType<Targ5>(), vm, 6, &ptr)
);
return inst;
}
};
/**
* The real C++ caller for function with return value and 10 params.
*/
template <typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5, typename Targ6, typename Targ7, typename Targ8, typename Targ9, typename Targ10>
struct HelperT<Tretval (*)(Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10), false> {
static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
Tretval ret = (*func)(
GetParam(ForceType<Targ1>(), vm, 2, &ptr),
GetParam(ForceType<Targ2>(), vm, 3, &ptr),
GetParam(ForceType<Targ3>(), vm, 4, &ptr),
GetParam(ForceType<Targ4>(), vm, 5, &ptr),
GetParam(ForceType<Targ5>(), vm, 6, &ptr),
GetParam(ForceType<Targ6>(), vm, 7, &ptr),
GetParam(ForceType<Targ7>(), vm, 8, &ptr),
GetParam(ForceType<Targ8>(), vm, 9, &ptr),
GetParam(ForceType<Targ9>(), vm, 10, &ptr),
GetParam(ForceType<Targ10>(), vm, 11, &ptr)
);
sq_pop(vm, 10);
return Return(vm, ret);
}
};
/**
* The real C++ caller for function with no return value and 10 params.
*/
template <typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5, typename Targ6, typename Targ7, typename Targ8, typename Targ9, typename Targ10>
struct HelperT<Tretval (*)(Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10), true> {
static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
(*func)(
GetParam(ForceType<Targ1>(), vm, 2, &ptr),
GetParam(ForceType<Targ2>(), vm, 3, &ptr),
GetParam(ForceType<Targ3>(), vm, 4, &ptr),
GetParam(ForceType<Targ4>(), vm, 5, &ptr),
GetParam(ForceType<Targ5>(), vm, 6, &ptr),
GetParam(ForceType<Targ6>(), vm, 7, &ptr),
GetParam(ForceType<Targ7>(), vm, 8, &ptr),
GetParam(ForceType<Targ8>(), vm, 9, &ptr),
GetParam(ForceType<Targ9>(), vm, 10, &ptr),
GetParam(ForceType<Targ10>(), vm, 11, &ptr)
);
sq_pop(vm, 10);
return 0;
}
};
/**
* The real C++ caller for method with return value and 10 params.
*/
template <class Tcls, typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5, typename Targ6, typename Targ7, typename Targ8, typename Targ9, typename Targ10>
struct HelperT<Tretval (Tcls::*)(Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10), false> {
static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
Tretval ret = (instance->*func)(
GetParam(ForceType<Targ1>(), vm, 2, &ptr),
GetParam(ForceType<Targ2>(), vm, 3, &ptr),
GetParam(ForceType<Targ3>(), vm, 4, &ptr),
GetParam(ForceType<Targ4>(), vm, 5, &ptr),
GetParam(ForceType<Targ5>(), vm, 6, &ptr),
GetParam(ForceType<Targ6>(), vm, 7, &ptr),
GetParam(ForceType<Targ7>(), vm, 8, &ptr),
GetParam(ForceType<Targ8>(), vm, 9, &ptr),
GetParam(ForceType<Targ9>(), vm, 10, &ptr),
GetParam(ForceType<Targ10>(), vm, 11, &ptr)
);
sq_pop(vm, 10);
return Return(vm, ret);
}
};
/**
* The real C++ caller for method with no return value and 10 params.
*/
template <class Tcls, typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5, typename Targ6, typename Targ7, typename Targ8, typename Targ9, typename Targ10>
struct HelperT<Tretval (Tcls::*)(Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10), true> {
static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
(instance->*func)(
GetParam(ForceType<Targ1>(), vm, 2, &ptr),
GetParam(ForceType<Targ2>(), vm, 3, &ptr),
GetParam(ForceType<Targ3>(), vm, 4, &ptr),
GetParam(ForceType<Targ4>(), vm, 5, &ptr),
GetParam(ForceType<Targ5>(), vm, 6, &ptr),
GetParam(ForceType<Targ6>(), vm, 7, &ptr),
GetParam(ForceType<Targ7>(), vm, 8, &ptr),
GetParam(ForceType<Targ8>(), vm, 9, &ptr),
GetParam(ForceType<Targ9>(), vm, 10, &ptr),
GetParam(ForceType<Targ10>(), vm, 11, &ptr)
);
sq_pop(vm, 10);
return 0;
}
static Tcls *SQConstruct(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10), HSQUIRRELVM vm)
{
SQAutoFreePointers ptr;
Tcls *inst = new Tcls(
GetParam(ForceType<Targ1>(), vm, 2, &ptr),
GetParam(ForceType<Targ2>(), vm, 3, &ptr),
GetParam(ForceType<Targ3>(), vm, 4, &ptr),
GetParam(ForceType<Targ4>(), vm, 5, &ptr),
GetParam(ForceType<Targ5>(), vm, 6, &ptr),
GetParam(ForceType<Targ6>(), vm, 7, &ptr),
GetParam(ForceType<Targ7>(), vm, 8, &ptr),
GetParam(ForceType<Targ8>(), vm, 9, &ptr),
GetParam(ForceType<Targ9>(), vm, 10, &ptr),
GetParam(ForceType<Targ10>(), vm, 11, &ptr)
);
return inst;
}
};
/**
* A general template for all non-static method callbacks from Squirrel.
* In here the function_proc is recovered, and the SQCall is called that
* can handle this exact amount of params.
*/
template <typename Tcls, typename Tmethod>
inline SQInteger DefSQNonStaticCallback(HSQUIRRELVM vm)
{
/* Find the amount of params we got */
int nparam = sq_gettop(vm);
SQUserPointer ptr = NULL;
SQUserPointer real_instance = NULL;
HSQOBJECT instance;
/* Get the 'SQ' instance of this class */
Squirrel::GetInstance(vm, &instance);
/* Protect against calls to a non-static method in a static way */
sq_pushroottable(vm);
sq_pushstring(vm, OTTD2FS(Tcls::GetClassName()), -1);
sq_get(vm, -2);
sq_pushobject(vm, instance);
if (sq_instanceof(vm) != SQTrue) return sq_throwerror(vm, _SC("class method is non-static"));
sq_pop(vm, 3);
/* Get the 'real' instance of this class */
sq_getinstanceup(vm, 1, &real_instance, 0);
/* Get the real function pointer */
sq_getuserdata(vm, nparam, &ptr, 0);
if (real_instance == NULL) return sq_throwerror(vm, _SC("couldn't detect real instance of class for non-static call"));
/* Remove the userdata from the stack */
sq_pop(vm, 1);
try {
/* Delegate it to a template that can handle this specific function */
return HelperT<Tmethod>::SQCall((Tcls *)real_instance, *(Tmethod *)ptr, vm);
} catch (SQInteger e) {
sq_pop(vm, nparam);
return e;
}
}
/**
* A general template for all non-static advanced method callbacks from Squirrel.
* In here the function_proc is recovered, and the SQCall is called that
* can handle this exact amount of params.
*/
template <typename Tcls, typename Tmethod>
inline SQInteger DefSQAdvancedNonStaticCallback(HSQUIRRELVM vm)
{
/* Find the amount of params we got */
int nparam = sq_gettop(vm);
SQUserPointer ptr = NULL;
SQUserPointer real_instance = NULL;
HSQOBJECT instance;
/* Get the 'SQ' instance of this class */
Squirrel::GetInstance(vm, &instance);
/* Protect against calls to a non-static method in a static way */
sq_pushroottable(vm);
sq_pushstring(vm, OTTD2FS(Tcls::GetClassName()), -1);
sq_get(vm, -2);
sq_pushobject(vm, instance);
if (sq_instanceof(vm) != SQTrue) return sq_throwerror(vm, _SC("class method is non-static"));
sq_pop(vm, 3);
/* Get the 'real' instance of this class */
sq_getinstanceup(vm, 1, &real_instance, 0);
/* Get the real function pointer */
sq_getuserdata(vm, nparam, &ptr, 0);
if (real_instance == NULL) return sq_throwerror(vm, _SC("couldn't detect real instance of class for non-static call"));
/* Remove the userdata from the stack */
sq_pop(vm, 1);
/* Call the function, which its only param is always the VM */
return (SQInteger)(((Tcls *)real_instance)->*(*(Tmethod *)ptr))(vm);
}
/**
* A general template for all function/static method callbacks from Squirrel.
* In here the function_proc is recovered, and the SQCall is called that
* can handle this exact amount of params.
*/
template <typename Tcls, typename Tmethod>
inline SQInteger DefSQStaticCallback(HSQUIRRELVM vm)
{
/* Find the amount of params we got */
int nparam = sq_gettop(vm);
SQUserPointer ptr = NULL;
/* Get the real function pointer */
sq_getuserdata(vm, nparam, &ptr, 0);
try {
/* Delegate it to a template that can handle this specific function */
return HelperT<Tmethod>::SQCall((Tcls *)NULL, *(Tmethod *)ptr, vm);
} catch (SQInteger e) {
sq_pop(vm, nparam);
return e;
}
}
/**
* A general template for the destructor of SQ instances. This is needed
* here as it has to be in the same scope as DefSQConstructorCallback.
*/
template <typename Tcls>
static SQInteger DefSQDestructorCallback(SQUserPointer p, SQInteger size)
{
/* Remove the real instance too */
if (p != NULL) ((Tcls *)p)->Release();
return 0;
}
/**
* A general template to handle creating of instance with any amount of
* params. It creates the instance in C++, and it sets all the needed
* settings in SQ to register the instance.
*/
template <typename Tcls, typename Tmethod, int Tnparam>
inline SQInteger DefSQConstructorCallback(HSQUIRRELVM vm)
{
/* Find the amount of params we got */
int nparam = sq_gettop(vm);
try {
/* Create the real instance */
Tcls *instance = HelperT<Tmethod>::SQConstruct((Tcls *)NULL, (Tmethod)NULL, vm);
sq_setinstanceup(vm, -Tnparam, instance);
sq_setreleasehook(vm, -Tnparam, DefSQDestructorCallback<Tcls>);
instance->AddRef();
return 0;
} catch (SQInteger e) {
sq_pop(vm, nparam);
return e;
}
}
}; // namespace SQConvert
#endif /* SQUIRREL_HELPER_HPP */
|