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 // Call callback for asynchronous events
const GameAction::GameActionCallback_t& cb = action->GetCallback(); auto cb = action->GetCallback();
if (cb) if (cb != nullptr)
{ {
cb(action, result); cb(action, result.get());
} }
if (result->Error != GA_ERROR::OK && !(flags & GAME_COMMAND_FLAG_GHOST)) if (result->Error != GA_ERROR::OK && !(flags & GAME_COMMAND_FLAG_GHOST))

View File

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

View File

@ -109,12 +109,12 @@ extern "C"
auto gameAction = RideCreateAction(); auto gameAction = RideCreateAction();
gameAction.rideType = listItem.type; gameAction.rideType = listItem.type;
gameAction.rideSubType = listItem.entry_index; 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; return;
ride_construct(static_cast<const RideCreateGameActionResult*>(res.get())->rideIndex); ride_construct(result->rideIndex);
}); });
GameActions::Execute(&gameAction); 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: public:
sint16 x; sint16 x;

View File

@ -41,7 +41,7 @@ struct RideCreateGameActionResult : public GameActionResult
uint32 rideColor; 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: public:
sint32 rideType; sint32 rideType;

View File

@ -28,7 +28,7 @@ extern "C"
#include "../ride/ride.h" #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: public:
uint8 RideIndex; uint8 RideIndex;

View File

@ -27,7 +27,7 @@ extern "C"
#include "../world/park.h" #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: public:
money16 Fee; money16 Fee;

View File

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