Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add destructor support #62

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion example/Dll.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ void onUnitMorph(AIModule* module, Unit* unit) {}
void onUnitRenegade(AIModule* module, Unit* unit) {}
void onSaveGame(AIModule* module, const char* gameName) {}
void onUnitComplete(AIModule* module, Unit* unit) {}
void drop(AIModule* module) {}

static AIModule_vtable module_vtable = {
MikailBag marked this conversation as resolved.
Show resolved Hide resolved
onStart,
Expand All @@ -224,7 +225,8 @@ static AIModule_vtable module_vtable = {
onUnitMorph,
onUnitRenegade,
onSaveGame,
onUnitComplete
onUnitComplete,
drop
};

DLLEXPORT void gameInit(BWAPI_Game* game) {
Expand Down
6 changes: 5 additions & 1 deletion include/AIModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ extern "C" {
///
/// Example:
/// typedef struct MyModule {
/// truct AIModule; // base
/// struct AIModule; // base
/// int my_field;
/// ...
/// } MyModule;
Expand All @@ -49,6 +49,7 @@ typedef struct AIModule {

struct AIModule_vtable
{
// Game lifecycle hooks
void (*onStart)(AIModule* module);
void (*onEnd)(AIModule* module, bool isWinner);
void (*onFrame)(AIModule* module);
Expand All @@ -66,6 +67,9 @@ struct AIModule_vtable
void (*onUnitRenegade)(AIModule* module, Unit* unit);
void (*onSaveGame)(AIModule* module, const char* gameName);
void (*onUnitComplete)(AIModule* module, Unit* unit);
// Destructor
// It is guaranteed drop() will be called exactly once, and after all other calls
void (*drop)(AIModule* module);
};

/* BWAPI::AIModule* */ void* createAIModuleWrapper(AIModule* module);
Expand Down
32 changes: 32 additions & 0 deletions src/AIModule.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#include <AIModule.h>
#include "Cast.hpp"

template<typename T>
void checkPointer(T* pointer) {
if (pointer == nullptr) {
printf("[BWAPI-C] FATAL ERROR: incorrect pointer");
abort();
}
}

class AIModuleWrapper : public BWAPI::AIModule
{
protected:
Expand Down Expand Up @@ -58,10 +66,34 @@ class AIModuleWrapper : public BWAPI::AIModule
virtual void onUnitComplete(BWAPI::Unit unit) override {
module->vtable->onUnitComplete(module, reinterpret_cast<Unit*>(unit));
}
virtual ~AIModuleWrapper() override {
module->vtable->drop(module);
}

AIModuleWrapper(::AIModule* module)
: module(module)
{
checkPointer(module);
checkPointer(module->vtable);
checkPointer(module->vtable->onStart);
kpp marked this conversation as resolved.
Show resolved Hide resolved
checkPointer(module->vtable->onEnd);
checkPointer(module->vtable->onFrame);
checkPointer(module->vtable->onSendText);
checkPointer(module->vtable->onReceiveText);
checkPointer(module->vtable->onPlayerLeft);
checkPointer(module->vtable->onNukeDetect);
checkPointer(module->vtable->onUnitDiscover);
checkPointer(module->vtable->onUnitEvade);
checkPointer(module->vtable->onUnitShow);
checkPointer(module->vtable->onUnitHide);
checkPointer(module->vtable->onUnitCreate);
checkPointer(module->vtable->onUnitDestroy);
checkPointer(module->vtable->onUnitMorph);
checkPointer(module->vtable->onUnitRenegade);
checkPointer(module->vtable->onSaveGame);
checkPointer(module->vtable->onUnitComplete);
checkPointer(module->vtable->drop);

}
};

Expand Down