Broken beginnings of ref counting attachments in C/C++.

This commit is contained in:
badlogic 2019-05-28 11:12:33 +02:00
parent a19331f843
commit 28b4694dbe
6 changed files with 13 additions and 7 deletions

View File

@ -52,13 +52,15 @@ typedef struct spAttachment {
const char* const name; const char* const name;
const spAttachmentType type; const spAttachmentType type;
const void* const vtable; const void* const vtable;
int refCount;
struct spAttachmentLoader* attachmentLoader; struct spAttachmentLoader* attachmentLoader;
#ifdef __cplusplus #ifdef __cplusplus
spAttachment() : spAttachment() :
name(0), name(0),
type(SP_ATTACHMENT_REGION), type(SP_ATTACHMENT_REGION),
vtable(0) { vtable(0),
refCount(0) {
} }
#endif #endif
} spAttachment; } spAttachment;

View File

@ -77,7 +77,7 @@ SP_API spSkin* spSkin_create (const char* name);
SP_API void spSkin_dispose (spSkin* self); SP_API void spSkin_dispose (spSkin* self);
/* The Skin owns the attachment. */ /* The Skin owns the attachment. */
SP_API void spSkin_addAttachment (spSkin* self, int slotIndex, const char* name, spAttachment* attachment); SP_API void spSkin_setAttachment (spSkin* self, int slotIndex, const char* name, spAttachment* attachment);
/* Returns 0 if the attachment was not found. */ /* Returns 0 if the attachment was not found. */
SP_API spAttachment* spSkin_getAttachment (const spSkin* self, int slotIndex, const char* name); SP_API spAttachment* spSkin_getAttachment (const spSkin* self, int slotIndex, const char* name);
@ -91,7 +91,7 @@ SP_API void spSkin_attachAll (const spSkin* self, struct spSkeleton* skeleton, c
typedef spSkin Skin; typedef spSkin Skin;
#define Skin_create(...) spSkin_create(__VA_ARGS__) #define Skin_create(...) spSkin_create(__VA_ARGS__)
#define Skin_dispose(...) spSkin_dispose(__VA_ARGS__) #define Skin_dispose(...) spSkin_dispose(__VA_ARGS__)
#define Skin_addAttachment(...) spSkin_addAttachment(__VA_ARGS__) #define Skin_setAttachment(...) spSkin_addAttachment(__VA_ARGS__)
#define Skin_getAttachment(...) spSkin_getAttachment(__VA_ARGS__) #define Skin_getAttachment(...) spSkin_getAttachment(__VA_ARGS__)
#define Skin_getAttachmentName(...) spSkin_getAttachmentName(__VA_ARGS__) #define Skin_getAttachmentName(...) spSkin_getAttachmentName(__VA_ARGS__)
#define Skin_attachAll(...) spSkin_attachAll(__VA_ARGS__) #define Skin_attachAll(...) spSkin_attachAll(__VA_ARGS__)

View File

@ -43,6 +43,8 @@ void _spAttachment_init (spAttachment* self, const char* name, spAttachmentType
MALLOC_STR(self->name, name); MALLOC_STR(self->name, name);
CONST_CAST(spAttachmentType, self->type) = type; CONST_CAST(spAttachmentType, self->type) = type;
self->refCount++;
} }
void _spAttachment_deinit (spAttachment* self) { void _spAttachment_deinit (spAttachment* self) {
@ -52,5 +54,7 @@ void _spAttachment_deinit (spAttachment* self) {
} }
void spAttachment_dispose (spAttachment* self) { void spAttachment_dispose (spAttachment* self) {
VTABLE(spAttachment, self) ->dispose(self); self->refCount--;
if (self->refCount == 0)
VTABLE(spAttachment, self) ->dispose(self);
} }

View File

@ -831,7 +831,7 @@ spSkin* spSkeletonBinary_readSkin(spSkeletonBinary* self, _dataInput* input,
for (ii = 0, nn = readVarint(input, 1); ii < nn; ++ii) { for (ii = 0, nn = readVarint(input, 1); ii < nn; ++ii) {
const char* name = readString(input); const char* name = readString(input);
spAttachment* attachment = spSkeletonBinary_readAttachment(self, input, skin, slotIndex, name, skeletonData, nonessential); spAttachment* attachment = spSkeletonBinary_readAttachment(self, input, skin, slotIndex, name, skeletonData, nonessential);
if (attachment) spSkin_addAttachment(skin, slotIndex, name, attachment); if (attachment) spSkin_setAttachment(skin, slotIndex, name, attachment);
FREE(name); FREE(name);
} }
} }

View File

@ -1054,7 +1054,7 @@ spSkeletonData* spSkeletonJson_readSkeletonData (spSkeletonJson* self, const cha
} }
} }
spSkin_addAttachment(skin, slotIndex, skinAttachmentName, attachment); spSkin_setAttachment(skin, slotIndex, skinAttachmentName, attachment);
} }
} }
} }

View File

@ -90,7 +90,7 @@ void spSkin_dispose (spSkin* self) {
FREE(self); FREE(self);
} }
void spSkin_addAttachment (spSkin* self, int slotIndex, const char* name, spAttachment* attachment) { void spSkin_setAttachment (spSkin* self, int slotIndex, const char* name, spAttachment* attachment) {
_Entry* newEntry = _Entry_create(slotIndex, name, attachment); _Entry* newEntry = _Entry_create(slotIndex, name, attachment);
newEntry->next = SUB_CAST(_spSkin, self)->entries; newEntry->next = SUB_CAST(_spSkin, self)->entries;
SUB_CAST(_spSkin, self)->entries = newEntry; SUB_CAST(_spSkin, self)->entries = newEntry;