splib transition

This commit is contained in:
2025-07-10 16:34:28 +02:00
parent c0473e5a65
commit cc57e03bc4
29 changed files with 146 additions and 1127 deletions

View File

@@ -5,107 +5,59 @@
* \brief File containing the definitions of the lockstep commands
*/
#include <memory>
#include <sp/protocol/ConcreteMessage.h>
#include <sp/protocol/MessageDispatcher.h>
#include <sp/protocol/MessageFactory.h>
#include <sp/protocol/MessageHandler.h>
#include <td/Types.h>
#include <td/common/NonCopyable.h>
#include <td/protocol/command/CommandData.h>
#include <td/protocol/command/CommandDeclare.h>
#include <memory>
namespace td {
namespace protocol {
class CommandVisitor;
/** A Command id is 8 bits wide */
using CommandID = std::uint8_t;
#define DeclareCommand(CommandName, ...) /** CommandName */ CommandName,
/**
* \enum CommandType
* \brief Map a Command to an id
*/
enum class CommandType : CommandID {
DeclareAllCommand()
/** The number of Commands */
COMMAND_COUNT
};
#undef DeclareCommand
class Command : private NonCopyable {
public:
/**
* \return The real type of the Command
*/
virtual CommandType GetType() const = 0;
private:
/** Use a CommandVisitor to make double-dispatch possible */
virtual void Accept(CommandVisitor& a_Visitor) const = 0;
friend class CommandVisitor;
enum class CommandID : std::uint8_t {
End = 0,
PlaceTower,
PlayerJoin,
SpawnTroop,
TeamChange,
UpgradeTower,
UseItem,
};
class CommandHandler;
using CommandBase = sp::MessageBase<CommandID, CommandHandler>;
template <typename TData, CommandID ID>
using CommandMessage = sp::ConcreteMessage<TData, CommandID, ID, CommandHandler>;
namespace commands {
/**
* \class ConcreteCommand
* \brief A Command associated with an id and holding data
* \tparam PT The Command type
* \tparam Data The structure holding the data of the Command (in td::protocol::data namespace)
*/
template <CommandType CT, typename Data>
class ConcreteCommand : public Command {
public:
/** The type of the struct holding the data */
using CommandDataType = Data;
/** The structure holding the actual data */
CommandDataType m_Data;
/** Construct the Command with data of type CommandDataType */
ConcreteCommand(const CommandDataType& a_Data = {});
constexpr CommandType GetType() const override {
return CT;
};
private:
void Accept(CommandVisitor& a_Visitor) const override;
};
// define TD_INSTANCIATE_COMMANDS
// before including this file
// if you want to instantiate templates
#ifdef TD_INSTANCIATE_COMMANDS
#define DeclareCommand(CommandName, ...) \
using CommandName = ConcreteCommand<CommandType::CommandName, cdata::CommandName>; \
template class ConcreteCommand<CommandType::CommandName, cdata::CommandName>;
#else
#define DeclareCommand(CommandName, ...) /** Defines the CommandName Command */ \
using CommandName = ConcreteCommand<CommandType::CommandName, cdata::CommandName>;
#endif
DeclareAllCommand()
#undef DeclareCommand
using EndCommand = CommandMessage<cdata::End, CommandID::End>;
using PlaceTowerCommand = CommandMessage<cdata::PlaceTower, CommandID::PlaceTower>;
using PlayerJoinCommand = CommandMessage<cdata::PlayerJoin, CommandID::PlayerJoin>;
using SpawnTroopCommand = CommandMessage<cdata::SpawnTroop, CommandID::SpawnTroop>;
using TeamChangeCommand = CommandMessage<cdata::TeamChange, CommandID::TeamChange>;
using UpgradeTowerCommand = CommandMessage<cdata::UpgradeTower, CommandID::UpgradeTower>;
using UseItemCommand = CommandMessage<cdata::UseItem, CommandID::UseItem>;
} // namespace commands
using LockStep = std::vector<std::shared_ptr<protocol::Command>>;
using AllCommands = std::tuple<commands::EndCommand, commands::PlaceTowerCommand, commands::PlayerJoinCommand,
commands::SpawnTroopCommand, commands::TeamChangeCommand, commands::UpgradeTowerCommand, commands::UseItemCommand>;
class CommandHandler : public sp::MessageHandler<AllCommands> {};
using CommandDispatcher = sp::MessageDispatcher<CommandBase>;
using CommandFactory = sp::MessageFactory<CommandBase, AllCommands>;
using LockStep = std::vector<std::shared_ptr<CommandBase>>;
} // namespace protocol
} // namespace td