Make the game action callback type safe

This commit is contained in:
Ted John 2017-07-21 19:44:44 +01:00 committed by Michał Janiszewski
parent aa30859ab7
commit 4ac8f1dc35
8 changed files with 24 additions and 16 deletions

View File

@ -191,10 +191,10 @@ namespace GameActions
}
// Call callback for asynchronous events
const GameAction::GameActionCallback_t& cb = action->GetCallback();
if (cb)
auto cb = action->GetCallback();
if (cb != nullptr)
{
cb(action, result);
cb(action, result.get());
}
if (result->Error != GA_ERROR::OK && !(flags & GAME_COMMAND_FLAG_GHOST))

View File

@ -83,7 +83,7 @@ struct GameAction
{
public:
typedef std::unique_ptr<GameAction> Ptr;
typedef std::function<void(const struct GameAction *, const GameActionResult::Ptr&)> GameActionCallback_t;
typedef std::function<void(const struct GameAction *, const GameActionResult *)> Callback_t;
private:
uint32 const _type;
@ -92,7 +92,7 @@ private:
uint32 _playerId = 0; // Callee
uint32 _flags = 0; // GAME_COMMAND_FLAGS
uint32 _networkId = 0;
GameActionCallback_t _callback;
Callback_t _callback;
public:
GameAction(uint32 type, uint16 actionFlags)
@ -137,12 +137,12 @@ public:
return _type;
}
void SetCallback(const GameActionCallback_t& cb)
void SetCallback(Callback_t cb)
{
_callback = cb;
}
const GameActionCallback_t& GetCallback() const
const Callback_t & GetCallback() const
{
return _callback;
}
@ -181,7 +181,7 @@ public:
virtual GameActionResult::Ptr Execute() const abstract;
};
template<uint32 TType, uint16 TActionFlags>
template<uint32 TType, uint16 TActionFlags, typename TResultType>
struct GameActionBase : GameAction
{
public:
@ -191,6 +191,14 @@ public:
: GameAction(TYPE, TActionFlags)
{
}
void SetCallback(std::function<void(const struct GameAction *, const TResultType *)> typedCallback)
{
GameAction::SetCallback([typedCallback](const GameAction * ga, const GameActionResult * result)
{
typedCallback(ga, static_cast<const TResultType *>(result));
});
}
};
typedef GameAction *(*GameActionFactory)();

View File

@ -109,12 +109,12 @@ extern "C"
auto gameAction = RideCreateAction();
gameAction.rideType = listItem.type;
gameAction.rideSubType = listItem.entry_index;
gameAction.SetCallback([](const GameAction *ga, const GameActionResult::Ptr& res)
gameAction.SetCallback([](const GameAction *ga, const RideCreateGameActionResult * result)
{
if (res->Error != GA_ERROR::OK)
if (result->Error != GA_ERROR::OK)
return;
ride_construct(static_cast<const RideCreateGameActionResult*>(res.get())->rideIndex);
ride_construct(result->rideIndex);
});
GameActions::Execute(&gameAction);

View File

@ -37,7 +37,7 @@ struct PlaceParkEntranceGameActionResult : public GameActionResult
}
};
struct PlaceParkEntranceAction : public GameActionBase<GAME_COMMAND_PLACE_PARK_ENTRANCE, GA_FLAGS::EDITOR_ONLY>
struct PlaceParkEntranceAction : public GameActionBase<GAME_COMMAND_PLACE_PARK_ENTRANCE, GA_FLAGS::EDITOR_ONLY, PlaceParkEntranceGameActionResult>
{
public:
sint16 x;

View File

@ -41,7 +41,7 @@ struct RideCreateGameActionResult : public GameActionResult
uint32 rideColor;
};
struct RideCreateAction : public GameActionBase<GAME_COMMAND_CREATE_RIDE, GA_FLAGS::ALLOW_WHILE_PAUSED>
struct RideCreateAction : public GameActionBase<GAME_COMMAND_CREATE_RIDE, GA_FLAGS::ALLOW_WHILE_PAUSED, RideCreateGameActionResult>
{
public:
sint32 rideType;

View File

@ -28,7 +28,7 @@ extern "C"
#include "../ride/ride.h"
}
struct RideSetStatusAction : public GameActionBase<GAME_COMMAND_SET_RIDE_STATUS, GA_FLAGS::ALLOW_WHILE_PAUSED>
struct RideSetStatusAction : public GameActionBase<GAME_COMMAND_SET_RIDE_STATUS, GA_FLAGS::ALLOW_WHILE_PAUSED, GameActionResult>
{
public:
uint8 RideIndex;

View File

@ -27,7 +27,7 @@ extern "C"
#include "../world/park.h"
}
struct SetParkEntranceFeeAction : public GameActionBase<GAME_COMMAND_SET_PARK_ENTRANCE_FEE, GA_FLAGS::ALLOW_WHILE_PAUSED>
struct SetParkEntranceFeeAction : public GameActionBase<GAME_COMMAND_SET_PARK_ENTRANCE_FEE, GA_FLAGS::ALLOW_WHILE_PAUSED, GameActionResult>
{
public:
money16 Fee;

View File

@ -169,7 +169,7 @@ public:
std::vector<std::unique_ptr<NetworkGroup>> group_list;
NetworkKey _key;
std::vector<uint8> _challenge;
std::map<uint32, GameAction::GameActionCallback_t> _gameActionCallbacks;
std::map<uint32, GameAction::Callback_t> _gameActionCallbacks;
NetworkUserManager _userManager;
std::string ServerName;