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
|
#include <new>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h> // sprintf
#include "scriptgrid.h"
using namespace std;
BEGIN_AS_NAMESPACE
// Set the default memory routines
// Use the angelscript engine's memory routines by default
static asALLOCFUNC_t userAlloc = asAllocMem;
static asFREEFUNC_t userFree = asFreeMem;
// Allows the application to set which memory routines should be used by the array object
void CScriptGrid::SetMemoryFunctions(asALLOCFUNC_t allocFunc, asFREEFUNC_t freeFunc)
{
userAlloc = allocFunc;
userFree = freeFunc;
}
static void RegisterScriptGrid_Native(asIScriptEngine *engine);
struct SGridBuffer
{
asDWORD width;
asDWORD height;
asBYTE data[1];
};
CScriptGrid *CScriptGrid::Create(asITypeInfo *ti)
{
return CScriptGrid::Create(ti, 0, 0);
}
CScriptGrid *CScriptGrid::Create(asITypeInfo *ti, asUINT w, asUINT h)
{
// Allocate the memory
void *mem = userAlloc(sizeof(CScriptGrid));
if( mem == 0 )
{
asIScriptContext *ctx = asGetActiveContext();
if( ctx )
ctx->SetException("Out of memory");
return 0;
}
// Initialize the object
CScriptGrid *a = new(mem) CScriptGrid(w, h, ti);
return a;
}
CScriptGrid *CScriptGrid::Create(asITypeInfo *ti, void *initList)
{
// Allocate the memory
void *mem = userAlloc(sizeof(CScriptGrid));
if( mem == 0 )
{
asIScriptContext *ctx = asGetActiveContext();
if( ctx )
ctx->SetException("Out of memory");
return 0;
}
// Initialize the object
CScriptGrid *a = new(mem) CScriptGrid(ti, initList);
return a;
}
CScriptGrid *CScriptGrid::Create(asITypeInfo *ti, asUINT w, asUINT h, void *defVal)
{
// Allocate the memory
void *mem = userAlloc(sizeof(CScriptGrid));
if( mem == 0 )
{
asIScriptContext *ctx = asGetActiveContext();
if( ctx )
ctx->SetException("Out of memory");
return 0;
}
// Initialize the object
CScriptGrid *a = new(mem) CScriptGrid(w, h, defVal, ti);
return a;
}
// This optional callback is called when the template type is first used by the compiler.
// It allows the application to validate if the template can be instantiated for the requested
// subtype at compile time, instead of at runtime. The output argument dontGarbageCollect
// allow the callback to tell the engine if the template instance type shouldn't be garbage collected,
// i.e. no asOBJ_GC flag.
static bool ScriptGridTemplateCallback(asITypeInfo *ti, bool &dontGarbageCollect)
{
// Make sure the subtype can be instantiated with a default factory/constructor,
// otherwise we won't be able to instantiate the elements.
int typeId = ti->GetSubTypeId();
if( typeId == asTYPEID_VOID )
return false;
if( (typeId & asTYPEID_MASK_OBJECT) && !(typeId & asTYPEID_OBJHANDLE) )
{
asITypeInfo *subtype = ti->GetEngine()->GetTypeInfoById(typeId);
asQWORD flags = subtype->GetFlags();
if( (flags & asOBJ_VALUE) && !(flags & asOBJ_POD) )
{
// Verify that there is a default constructor
bool found = false;
for( asUINT n = 0; n < subtype->GetBehaviourCount(); n++ )
{
asEBehaviours beh;
asIScriptFunction *func = subtype->GetBehaviourByIndex(n, &beh);
if( beh != asBEHAVE_CONSTRUCT ) continue;
if( func->GetParamCount() == 0 )
{
// Found the default constructor
found = true;
break;
}
}
if( !found )
{
// There is no default constructor
ti->GetEngine()->WriteMessage("array", 0, 0, asMSGTYPE_ERROR, "The subtype has no default constructor");
return false;
}
}
else if( (flags & asOBJ_REF) )
{
bool found = false;
// If value assignment for ref type has been disabled then the array
// can be created if the type has a default factory function
if( !ti->GetEngine()->GetEngineProperty(asEP_DISALLOW_VALUE_ASSIGN_FOR_REF_TYPE) )
{
// Verify that there is a default factory
for( asUINT n = 0; n < subtype->GetFactoryCount(); n++ )
{
asIScriptFunction *func = subtype->GetFactoryByIndex(n);
if( func->GetParamCount() == 0 )
{
// Found the default factory
found = true;
break;
}
}
}
if( !found )
{
// No default factory
ti->GetEngine()->WriteMessage("array", 0, 0, asMSGTYPE_ERROR, "The subtype has no default factory");
return false;
}
}
// If the object type is not garbage collected then the array also doesn't need to be
if( !(flags & asOBJ_GC) )
dontGarbageCollect = true;
}
else if( !(typeId & asTYPEID_OBJHANDLE) )
{
// Arrays with primitives cannot form circular references,
// thus there is no need to garbage collect them
dontGarbageCollect = true;
}
else
{
assert( typeId & asTYPEID_OBJHANDLE );
// It is not necessary to set the array as garbage collected for all handle types.
// If it is possible to determine that the handle cannot refer to an object type
// that can potentially form a circular reference with the array then it is not
// necessary to make the array garbage collected.
asITypeInfo *subtype = ti->GetEngine()->GetTypeInfoById(typeId);
asQWORD flags = subtype->GetFlags();
if( !(flags & asOBJ_GC) )
{
if( (flags & asOBJ_SCRIPT_OBJECT) )
{
// Even if a script class is by itself not garbage collected, it is possible
// that classes that derive from it may be, so it is not possible to know
// that no circular reference can occur.
if( (flags & asOBJ_NOINHERIT) )
{
// A script class declared as final cannot be inherited from, thus
// we can be certain that the object cannot be garbage collected.
dontGarbageCollect = true;
}
}
else
{
// For application registered classes we assume the application knows
// what it is doing and don't mark the array as garbage collected unless
// the type is also garbage collected.
dontGarbageCollect = true;
}
}
}
// The type is ok
return true;
}
// Registers the template array type
void RegisterScriptGrid(asIScriptEngine *engine)
{
// TODO: Implement the generic calling convention
RegisterScriptGrid_Native(engine);
}
static void RegisterScriptGrid_Native(asIScriptEngine *engine)
{
int r;
// Register the grid type as a template
r = engine->RegisterObjectType("grid<class T>", 0, asOBJ_REF | asOBJ_GC | asOBJ_TEMPLATE); assert( r >= 0 );
// Register a callback for validating the subtype before it is used
r = engine->RegisterObjectBehaviour("grid<T>", asBEHAVE_TEMPLATE_CALLBACK, "bool f(int&in, bool&out)", asFUNCTION(ScriptGridTemplateCallback), asCALL_CDECL); assert( r >= 0 );
// Templates receive the object type as the first parameter. To the script writer this is hidden
r = engine->RegisterObjectBehaviour("grid<T>", asBEHAVE_FACTORY, "grid<T>@ f(int&in)", asFUNCTIONPR(CScriptGrid::Create, (asITypeInfo*), CScriptGrid*), asCALL_CDECL); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("grid<T>", asBEHAVE_FACTORY, "grid<T>@ f(int&in, uint, uint)", asFUNCTIONPR(CScriptGrid::Create, (asITypeInfo*, asUINT, asUINT), CScriptGrid*), asCALL_CDECL); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("grid<T>", asBEHAVE_FACTORY, "grid<T>@ f(int&in, uint, uint, const T &in)", asFUNCTIONPR(CScriptGrid::Create, (asITypeInfo*, asUINT, asUINT, void *), CScriptGrid*), asCALL_CDECL); assert( r >= 0 );
// Register the factory that will be used for initialization lists
r = engine->RegisterObjectBehaviour("grid<T>", asBEHAVE_LIST_FACTORY, "grid<T>@ f(int&in type, int&in list) {repeat {repeat_same T}}", asFUNCTIONPR(CScriptGrid::Create, (asITypeInfo*, void*), CScriptGrid*), asCALL_CDECL); assert( r >= 0 );
// The memory management methods
r = engine->RegisterObjectBehaviour("grid<T>", asBEHAVE_ADDREF, "void f()", asMETHOD(CScriptGrid,AddRef), asCALL_THISCALL); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("grid<T>", asBEHAVE_RELEASE, "void f()", asMETHOD(CScriptGrid,Release), asCALL_THISCALL); assert( r >= 0 );
// The index operator returns the template subtype
r = engine->RegisterObjectMethod("grid<T>", "T &opIndex(uint, uint)", asMETHODPR(CScriptGrid, At, (asUINT, asUINT), void*), asCALL_THISCALL); assert( r >= 0 );
r = engine->RegisterObjectMethod("grid<T>", "const T &opIndex(uint, uint) const", asMETHODPR(CScriptGrid, At, (asUINT, asUINT) const, const void*), asCALL_THISCALL); assert( r >= 0 );
// Other methods
r = engine->RegisterObjectMethod("grid<T>", "void resize(uint width, uint height)", asMETHODPR(CScriptGrid, Resize, (asUINT, asUINT), void), asCALL_THISCALL); assert( r >= 0 );
r = engine->RegisterObjectMethod("grid<T>", "uint width() const", asMETHOD(CScriptGrid, GetWidth), asCALL_THISCALL); assert( r >= 0 );
r = engine->RegisterObjectMethod("grid<T>", "uint height() const", asMETHOD(CScriptGrid, GetHeight), asCALL_THISCALL); assert( r >= 0 );
// Register GC behaviours in case the array needs to be garbage collected
r = engine->RegisterObjectBehaviour("grid<T>", asBEHAVE_GETREFCOUNT, "int f()", asMETHOD(CScriptGrid, GetRefCount), asCALL_THISCALL); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("grid<T>", asBEHAVE_SETGCFLAG, "void f()", asMETHOD(CScriptGrid, SetFlag), asCALL_THISCALL); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("grid<T>", asBEHAVE_GETGCFLAG, "bool f()", asMETHOD(CScriptGrid, GetFlag), asCALL_THISCALL); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("grid<T>", asBEHAVE_ENUMREFS, "void f(int&in)", asMETHOD(CScriptGrid, EnumReferences), asCALL_THISCALL); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("grid<T>", asBEHAVE_RELEASEREFS, "void f(int&in)", asMETHOD(CScriptGrid, ReleaseAllHandles), asCALL_THISCALL); assert( r >= 0 );
}
CScriptGrid::CScriptGrid(asITypeInfo *ti, void *buf)
{
refCount = 1;
gcFlag = false;
objType = ti;
objType->AddRef();
buffer = 0;
subTypeId = objType->GetSubTypeId();
asIScriptEngine *engine = ti->GetEngine();
// Determine element size
if( subTypeId & asTYPEID_MASK_OBJECT )
elementSize = sizeof(asPWORD);
else
elementSize = engine->GetSizeOfPrimitiveType(subTypeId);
// Determine the initial size from the buffer
asUINT height = *(asUINT*)buf;
asUINT width = height ? *(asUINT*)((char*)(buf)+4) : 0;
// Make sure the grid size isn't too large for us to handle
if( !CheckMaxSize(width, height) )
{
// Don't continue with the initialization
return;
}
// Skip the height value at the start of the buffer
buf = (asUINT*)(buf)+1;
// Copy the values of the grid elements from the buffer
if( (ti->GetSubTypeId() & asTYPEID_MASK_OBJECT) == 0 )
{
CreateBuffer(&buffer, width, height);
// Copy the values of the primitive type into the internal buffer
for( asUINT y = 0; y < height; y++ )
{
// Skip the length value at the start of each row
buf = (asUINT*)(buf)+1;
// Copy the line
if( width > 0 )
memcpy(At(0,y), buf, width*elementSize);
// Move to next line
buf = (char*)(buf) + width*elementSize;
// Align to 4 byte boundary
if( asPWORD(buf) & 0x3 )
buf = (char*)(buf) + 4 - (asPWORD(buf) & 0x3);
}
}
else if( ti->GetSubTypeId() & asTYPEID_OBJHANDLE )
{
CreateBuffer(&buffer, width, height);
// Copy the handles into the internal buffer
for( asUINT y = 0; y < height; y++ )
{
// Skip the length value at the start of each row
buf = (asUINT*)(buf)+1;
// Copy the line
if( width > 0 )
memcpy(At(0,y), buf, width*elementSize);
// With object handles it is safe to clear the memory in the received buffer
// instead of increasing the ref count. It will save time both by avoiding the
// call the increase ref, and also relieve the engine from having to release
// its references too
memset(buf, 0, width*elementSize);
// Move to next line
buf = (char*)(buf) + width*elementSize;
// Align to 4 byte boundary
if( asPWORD(buf) & 0x3 )
buf = (char*)(buf) + 4 - (asPWORD(buf) & 0x3);
}
}
else if( ti->GetSubType()->GetFlags() & asOBJ_REF )
{
// Only allocate the buffer, but not the objects
subTypeId |= asTYPEID_OBJHANDLE;
CreateBuffer(&buffer, width, height);
subTypeId &= ~asTYPEID_OBJHANDLE;
// Copy the handles into the internal buffer
for( asUINT y = 0; y < height; y++ )
{
// Skip the length value at the start of each row
buf = (asUINT*)(buf)+1;
// Copy the line
if( width > 0 )
memcpy(At(0,y), buf, width*elementSize);
// With object handles it is safe to clear the memory in the received buffer
// instead of increasing the ref count. It will save time both by avoiding the
// call the increase ref, and also relieve the engine from having to release
// its references too
memset(buf, 0, width*elementSize);
// Move to next line
buf = (char*)(buf) + width*elementSize;
// Align to 4 byte boundary
if( asPWORD(buf) & 0x3 )
buf = (char*)(buf) + 4 - (asPWORD(buf) & 0x3);
}
}
else
{
// TODO: Optimize by calling the copy constructor of the object instead of
// constructing with the default constructor and then assigning the value
// TODO: With C++11 ideally we should be calling the move constructor, instead
// of the copy constructor as the engine will just discard the objects in the
// buffer afterwards.
CreateBuffer(&buffer, width, height);
// For value types we need to call the opAssign for each individual object
asITypeInfo *subType = ti->GetSubType();
asUINT subTypeSize = subType->GetSize();
for( asUINT y = 0;y < height; y++ )
{
// Skip the length value at the start of each row
buf = (asUINT*)(buf)+1;
// Call opAssign for each of the objects on the row
for( asUINT x = 0; x < width; x++ )
{
void *obj = At(x,y);
asBYTE *srcObj = (asBYTE*)(buf) + x*subTypeSize;
engine->AssignScriptObject(obj, srcObj, subType);
}
// Move to next line
buf = (char*)(buf) + width*subTypeSize;
// Align to 4 byte boundary
if( asPWORD(buf) & 0x3 )
buf = (char*)(buf) + 4 - (asPWORD(buf) & 0x3);
}
}
// Notify the GC of the successful creation
if( objType->GetFlags() & asOBJ_GC )
objType->GetEngine()->NotifyGarbageCollectorOfNewObject(this, objType);
}
CScriptGrid::CScriptGrid(asUINT width, asUINT height, asITypeInfo *ti)
{
refCount = 1;
gcFlag = false;
objType = ti;
objType->AddRef();
buffer = 0;
subTypeId = objType->GetSubTypeId();
// Determine element size
if( subTypeId & asTYPEID_MASK_OBJECT )
elementSize = sizeof(asPWORD);
else
elementSize = objType->GetEngine()->GetSizeOfPrimitiveType(subTypeId);
// Make sure the array size isn't too large for us to handle
if( !CheckMaxSize(width, height) )
{
// Don't continue with the initialization
return;
}
CreateBuffer(&buffer, width, height);
// Notify the GC of the successful creation
if( objType->GetFlags() & asOBJ_GC )
objType->GetEngine()->NotifyGarbageCollectorOfNewObject(this, objType);
}
void CScriptGrid::Resize(asUINT width, asUINT height)
{
// Make sure the size isn't too large for us to handle
if( !CheckMaxSize(width, height) )
return;
// Create a new buffer
SGridBuffer *tmpBuffer = 0;
CreateBuffer(&tmpBuffer, width, height);
if( tmpBuffer == 0 )
return;
if( buffer )
{
// Copy the existing values to the new buffer
asUINT w = width > buffer->width ? buffer->width : width;
asUINT h = height > buffer->height ? buffer->height : height;
for( asUINT y = 0; y < h; y++ )
for( asUINT x = 0; x < w; x++ )
SetValue(tmpBuffer, x, y, At(buffer, x, y));
// Replace the internal buffer
DeleteBuffer(buffer);
}
buffer = tmpBuffer;
}
CScriptGrid::CScriptGrid(asUINT width, asUINT height, void *defVal, asITypeInfo *ti)
{
refCount = 1;
gcFlag = false;
objType = ti;
objType->AddRef();
buffer = 0;
subTypeId = objType->GetSubTypeId();
// Determine element size
if( subTypeId & asTYPEID_MASK_OBJECT )
elementSize = sizeof(asPWORD);
else
elementSize = objType->GetEngine()->GetSizeOfPrimitiveType(subTypeId);
// Make sure the array size isn't too large for us to handle
if( !CheckMaxSize(width, height) )
{
// Don't continue with the initialization
return;
}
CreateBuffer(&buffer, width, height);
// Notify the GC of the successful creation
if( objType->GetFlags() & asOBJ_GC )
objType->GetEngine()->NotifyGarbageCollectorOfNewObject(this, objType);
// Initialize the elements with the default value
for( asUINT y = 0; y < GetHeight(); y++ )
for( asUINT x = 0; x < GetWidth(); x++ )
SetValue(x, y, defVal);
}
void CScriptGrid::SetValue(asUINT x, asUINT y, void *value)
{
SetValue(buffer, x, y, value);
}
void CScriptGrid::SetValue(SGridBuffer *buf, asUINT x, asUINT y, void *value)
{
// At() will take care of the out-of-bounds checking, though
// if called from the application then nothing will be done
void *ptr = At(buf, x, y);
if( ptr == 0 ) return;
if( (subTypeId & ~asTYPEID_MASK_SEQNBR) && !(subTypeId & asTYPEID_OBJHANDLE) )
objType->GetEngine()->AssignScriptObject(ptr, value, objType->GetSubType());
else if( subTypeId & asTYPEID_OBJHANDLE )
{
void *tmp = *(void**)ptr;
*(void**)ptr = *(void**)value;
objType->GetEngine()->AddRefScriptObject(*(void**)value, objType->GetSubType());
if( tmp )
objType->GetEngine()->ReleaseScriptObject(tmp, objType->GetSubType());
}
else if( subTypeId == asTYPEID_BOOL ||
subTypeId == asTYPEID_INT8 ||
subTypeId == asTYPEID_UINT8 )
*(char*)ptr = *(char*)value;
else if( subTypeId == asTYPEID_INT16 ||
subTypeId == asTYPEID_UINT16 )
*(short*)ptr = *(short*)value;
else if( subTypeId == asTYPEID_INT32 ||
subTypeId == asTYPEID_UINT32 ||
subTypeId == asTYPEID_FLOAT ||
subTypeId > asTYPEID_DOUBLE ) // enums have a type id larger than doubles
*(int*)ptr = *(int*)value;
else if( subTypeId == asTYPEID_INT64 ||
subTypeId == asTYPEID_UINT64 ||
subTypeId == asTYPEID_DOUBLE )
*(double*)ptr = *(double*)value;
}
CScriptGrid::~CScriptGrid()
{
if( buffer )
{
DeleteBuffer(buffer);
buffer = 0;
}
if( objType ) objType->Release();
}
asUINT CScriptGrid::GetWidth() const
{
if( buffer )
return buffer->width;
return 0;
}
asUINT CScriptGrid::GetHeight() const
{
if( buffer )
return buffer->height;
return 0;
}
// internal
bool CScriptGrid::CheckMaxSize(asUINT width, asUINT height)
{
// This code makes sure the size of the buffer that is allocated
// for the array doesn't overflow and becomes smaller than requested
asUINT maxSize = 0xFFFFFFFFul - sizeof(SGridBuffer) + 1;
if( elementSize > 0 )
maxSize /= elementSize;
asINT64 numElements = width * height;
if( (numElements >> 32) || numElements > maxSize )
{
asIScriptContext *ctx = asGetActiveContext();
if( ctx )
ctx->SetException("Too large grid size");
return false;
}
// OK
return true;
}
asITypeInfo *CScriptGrid::GetGridObjectType() const
{
return objType;
}
int CScriptGrid::GetGridTypeId() const
{
return objType->GetTypeId();
}
int CScriptGrid::GetElementTypeId() const
{
return subTypeId;
}
void *CScriptGrid::At(asUINT x, asUINT y)
{
return At(buffer, x, y);
}
// Return a pointer to the array element. Returns 0 if the index is out of bounds
void *CScriptGrid::At(SGridBuffer *buf, asUINT x, asUINT y)
{
if( buf == 0 || x >= buf->width || y >= buf->height )
{
// If this is called from a script we raise a script exception
asIScriptContext *ctx = asGetActiveContext();
if( ctx )
ctx->SetException("Index out of bounds");
return 0;
}
asUINT index = x+y*buf->width;
if( (subTypeId & asTYPEID_MASK_OBJECT) && !(subTypeId & asTYPEID_OBJHANDLE) )
return *(void**)(buf->data + elementSize*index);
else
return buf->data + elementSize*index;
}
const void *CScriptGrid::At(asUINT x, asUINT y) const
{
return const_cast<CScriptGrid*>(this)->At(const_cast<SGridBuffer*>(buffer), x, y);
}
// internal
void CScriptGrid::CreateBuffer(SGridBuffer **buf, asUINT w, asUINT h)
{
asUINT numElements = w * h;
*buf = reinterpret_cast<SGridBuffer*>(userAlloc(sizeof(SGridBuffer)-1+elementSize*numElements));
if( *buf )
{
(*buf)->width = w;
(*buf)->height = h;
Construct(*buf);
}
else
{
// Oops, out of memory
asIScriptContext *ctx = asGetActiveContext();
if( ctx )
ctx->SetException("Out of memory");
}
}
// internal
void CScriptGrid::DeleteBuffer(SGridBuffer *buf)
{
assert( buf );
Destruct(buf);
// Free the buffer
userFree(buf);
}
// internal
void CScriptGrid::Construct(SGridBuffer *buf)
{
assert( buf );
if( subTypeId & asTYPEID_OBJHANDLE )
{
// Set all object handles to null
void *d = (void*)(buf->data);
memset(d, 0, (buf->width*buf->height)*sizeof(void*));
}
else if( subTypeId & asTYPEID_MASK_OBJECT )
{
void **max = (void**)(buf->data + (buf->width*buf->height) * sizeof(void*));
void **d = (void**)(buf->data);
asIScriptEngine *engine = objType->GetEngine();
asITypeInfo *subType = objType->GetSubType();
for( ; d < max; d++ )
{
*d = (void*)engine->CreateScriptObject(subType);
if( *d == 0 )
{
// Set the remaining entries to null so the destructor
// won't attempt to destroy invalid objects later
memset(d, 0, sizeof(void*)*(max-d));
// There is no need to set an exception on the context,
// as CreateScriptObject has already done that
return;
}
}
}
}
// internal
void CScriptGrid::Destruct(SGridBuffer *buf)
{
assert( buf );
if( subTypeId & asTYPEID_MASK_OBJECT )
{
asIScriptEngine *engine = objType->GetEngine();
void **max = (void**)(buf->data + (buf->width*buf->height) * sizeof(void*));
void **d = (void**)(buf->data);
for( ; d < max; d++ )
{
if( *d )
engine->ReleaseScriptObject(*d, objType->GetSubType());
}
}
}
// GC behaviour
void CScriptGrid::EnumReferences(asIScriptEngine *engine)
{
if( buffer == 0 ) return;
// If the grid is holding handles, then we need to notify the GC of them
if (subTypeId & asTYPEID_MASK_OBJECT)
{
asUINT numElements = buffer->width * buffer->height;
void **d = (void**)buffer->data;
asITypeInfo *subType = engine->GetTypeInfoById(subTypeId);
if ((subType->GetFlags() & asOBJ_REF))
{
// For reference types we need to notify the GC of each instance
for (asUINT n = 0; n < numElements; n++)
{
if (d[n])
engine->GCEnumCallback(d[n]);
}
}
else if ((subType->GetFlags() & asOBJ_VALUE) && (subType->GetFlags() & asOBJ_GC))
{
// For value types we need to forward the enum callback
// to the object so it can decide what to do
for (asUINT n = 0; n < numElements; n++)
{
if (d[n])
engine->ForwardGCEnumReferences(d[n], subType);
}
}
}
}
// GC behaviour
void CScriptGrid::ReleaseAllHandles(asIScriptEngine*)
{
if( buffer == 0 ) return;
DeleteBuffer(buffer);
buffer = 0;
}
void CScriptGrid::AddRef() const
{
// Clear the GC flag then increase the counter
gcFlag = false;
asAtomicInc(refCount);
}
void CScriptGrid::Release() const
{
// Clearing the GC flag then descrease the counter
gcFlag = false;
if( asAtomicDec(refCount) == 0 )
{
// When reaching 0 no more references to this instance
// exists and the object should be destroyed
this->~CScriptGrid();
userFree(const_cast<CScriptGrid*>(this));
}
}
// GC behaviour
int CScriptGrid::GetRefCount()
{
return refCount;
}
// GC behaviour
void CScriptGrid::SetFlag()
{
gcFlag = true;
}
// GC behaviour
bool CScriptGrid::GetFlag()
{
return gcFlag;
}
END_AS_NAMESPACE
|