1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#ifndef I_GAME_COMMANDS_H
#define I_GAME_COMMANDS_H
#include "System/StringUtil.h"
// These two are required for the destructors
#include "SyncedActionExecutor.h"
#include "UnsyncedActionExecutor.h"
#include "WordCompletion.h"
#include <map>
#include <string>
#include <stdexcept>
template<class actionExecutor_t>
class IGameCommands
{
protected:
IGameCommands() {}
virtual ~IGameCommands();
public:
typedef std::map<std::string, actionExecutor_t*> actionExecutorsMap_t;
/**
* Registers the default action-executors for chat commands.
* These are all the ones that are not logically tied to a specific
* part/sub-module of the engine (for example rendering related switches).
* @see AddActionExecutor
*/
virtual void AddDefaultActionExecutors() = 0;
/**
* Registers a new action-executor for a chat command.
* @param executor has to be new'ed, will be delete'ed internally.
* @see RemoveActionExecutor
*/
void AddActionExecutor(actionExecutor_t* executor);
/**
* Deregisters an action-executor for a chat command.
* The action-executor corresponding to the given command
* (case-insensitive) will be removed and delete'ed internally.
* @param command used to lookup the action-executor to be removed.
* @see AddActionExecutor
*/
void RemoveActionExecutor(const std::string& command);
/**
* Deregisters all currently registered action-executor for chat commands.
* @see RemoveActionExecutor
*/
void RemoveAllActionExecutors();
/**
* Returns the action-executor for the given command (case-insensitive).
* @param command used to lookup the action-executor to be removed.
* @return the action-executor for the given command, or NULL, if none is
* registered.
*/
const actionExecutor_t* GetActionExecutor(const std::string& command) const;
/**
* Returns the map of currently registered lower-case commands with their
* respective action-executors.
*/
const actionExecutorsMap_t& GetActionExecutors() const { return actionExecutors; }
private:
// XXX maybe use a hash_map here, for faster lookup
actionExecutorsMap_t actionExecutors;
};
/*
* Because this is a template enabled class,
* the implementations have to be in the same file.
*/
template<class actionExecutor_t>
IGameCommands<actionExecutor_t>::~IGameCommands() {
RemoveAllActionExecutors();
}
template<class actionExecutor_t>
void IGameCommands<actionExecutor_t>::AddActionExecutor(actionExecutor_t* executor) {
const std::string commandLower = StringToLower(executor->GetCommand());
const typename actionExecutorsMap_t::const_iterator aei
= actionExecutors.find(commandLower);
if (aei != actionExecutors.end()) {
throw std::logic_error("Tried to register a duplicate action-executor for command: " + commandLower);
} else {
actionExecutors[commandLower] = executor;
wordCompletion->AddWord("/" + commandLower + " ", true, false, false);
}
}
template<class actionExecutor_t>
void IGameCommands<actionExecutor_t>::RemoveActionExecutor(const std::string& command) {
const std::string commandLower = StringToLower(command);
const typename actionExecutorsMap_t::iterator aei
= actionExecutors.find(commandLower);
if (aei != actionExecutors.end()) {
// an executor for this command is registered
// -> remove and delete
actionExecutor_t* executor = aei->second;
actionExecutors.erase(aei);
wordCompletion->RemoveWord("/" + commandLower + " ");
delete executor;
}
}
template<class actionExecutor_t>
void IGameCommands<actionExecutor_t>::RemoveAllActionExecutors() {
while (!actionExecutors.empty()) {
RemoveActionExecutor(actionExecutors.begin()->first);
}
}
template<class actionExecutor_t>
const actionExecutor_t* IGameCommands<actionExecutor_t>::GetActionExecutor(const std::string& command) const {
const actionExecutor_t* executor = NULL;
const std::string commandLower = StringToLower(command);
const typename actionExecutorsMap_t::const_iterator aei
= actionExecutors.find(commandLower);
if (aei != actionExecutors.end()) {
// an executor for this command is registered
executor = aei->second;
}
return executor;
}
#endif // I_GAME_COMMANDS_H
|