Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #ifndef INCLUDED_COMMANDPROC 19 : #define INCLUDED_COMMANDPROC 20 : 21 : #include "lib/code_annotation.h" 22 : #include "lib/debug.h" 23 : 24 : #include <cstddef> 25 : #include <list> 26 : #include <map> 27 : #include <string> 28 : 29 : namespace AtlasMessage 30 : { 31 : 32 0 : struct Command 33 : { 34 0 : virtual ~Command() {} 35 : virtual void Do() = 0; 36 : virtual void Undo() = 0; 37 : virtual void Redo() = 0; 38 : virtual void Merge(Command* prev) = 0; 39 : virtual const char* GetType() const = 0; 40 : }; 41 : 42 : class CommandProc 43 : { 44 : public: 45 : CommandProc(); 46 : ~CommandProc(); 47 : 48 : // Should be called before shutting down, so it can free 49 : // references to entities/etc that are stored in commands 50 : void Destroy(); 51 : 52 : void Submit(Command* cmd); 53 : 54 : void Undo(); 55 : void Redo(); 56 : void Merge(); 57 : 58 : private: 59 : std::list<Command*> m_Commands; 60 : typedef std::list<Command*>::iterator cmdIt; 61 : // The 'current' command is the latest one which has been executed 62 : // (ignoring any that have been undone) 63 : cmdIt m_CurrentCommand; 64 : }; 65 : 66 : typedef Command* (*cmdHandler)(const void*); 67 : typedef std::map<std::string, cmdHandler> cmdHandlers; 68 : extern cmdHandlers& GetCmdHandlers(); 69 : 70 : CommandProc& GetCommandProc(); 71 : 72 : /* 73 : We want the command handler implementations to be as pretty as possible - so let people write 74 : 75 : BEGIN_COMMAND(CommandName) 76 : { 77 : int member; 78 : cCommandName() { ... } // } both are optional; neither may 79 : ~cCommandName() { ... } // } make use of this->msg 80 : 81 : void Do() { ... interact with this->msg ... } 82 : void Undo() { ... } 83 : void Redo() { ... } 84 : 85 : void MergeIntoPrevious(cCommandName* prev) { ... } // iff this command is a mergeable one 86 : }; 87 : END_COMMAND(CommandName) 88 : 89 : which looks almost exactly like a class definition, and is about the simplest 90 : system I can find. 91 : 92 : 93 : The following macros convert that into: 94 : 95 : class cCommandName_base : public Command 96 : { 97 : protected: 98 : // Storage for data passed into this command 99 : dCommandName* msg; 100 : public: 101 : // Ensure msg is initialised to something 'safe' 102 : cCommandName_base : msg(NULL) {} 103 : 104 : // MergeIntoPrevious should be overridden by mergeable commands, and implemented 105 : // to update 'prev' to include the effects of 'this'. (A subclass overriding 106 : // any function named 'MergeIntoPrevious' will hide this function, even if 107 : // the types differ.) 108 : void MergeIntoPrevious(void*) { ...error - needs to be overridden... } 109 : }; 110 : 111 : // Use 'struct' for automatic publicness - we want users to write as little as possible 112 : struct cCommandName : public cCommandName_base 113 : { 114 : // (user's command code - mostly overriding virtual methods from Command) 115 : } 116 : 117 : // Subclass the command to add things which require knowledge of the 118 : // complete class definition 119 : class cCommandName_sub : public cCommandName 120 : { 121 : public: 122 : cCommandName_sub(dCommandName *data) { ...set msg... } 123 : ~cCommandName_sub() { ...clear msg... } 124 : 125 : // Implement the relevant virtual methods from the Command base class, 126 : // with automatic casting to the correct types and stuff 127 : virtual void Merge(Command* prev) { ...call MergeIntoPrevious... } 128 : virtual const char* GetType() const { return "CommandName"; } 129 : 130 : // Factory method for creating an instance of this class, casting the 131 : // data pointer into the right form (to avoid forcing the generic 132 : // command-handling support code to worry about the types) 133 : static Command* Create(const void* data) { ...return new cCommandName_sub... } 134 : }; 135 : 136 : // Register.cpp wants to get that Create method, but it doesn't want to 137 : // load all the class definitions; so define a simple method that just 138 : // returns the address of Create 139 : cmdHandler cCommandName_create() 140 : { 141 : return &cCommandName_sub::Create; 142 : } 143 : 144 : // (TODO: make sure this stays in sync with the code below) 145 : 146 : */ 147 : 148 : #define BEGIN_COMMAND(t) \ 149 : class c##t##_base : public Command \ 150 : { \ 151 : protected: \ 152 : d##t* msg; \ 153 : public: \ 154 : c##t##_base() : msg(NULL) {} \ 155 : void MergeIntoPrevious(void*) { debug_warn(L"MergeIntoPrevious unimplemented in command " WIDEN(#t)); } \ 156 : }; \ 157 : struct c##t : public c##t##_base 158 : 159 : #define END_COMMAND(t) \ 160 : class c##t##_sub : public c##t \ 161 : { \ 162 : public: \ 163 : c##t##_sub(d##t* data) \ 164 : { \ 165 : msg = data; \ 166 : } \ 167 : ~c##t##_sub() \ 168 : { \ 169 : /* (msg was allocated in mDoCommand(), using SHAREABLE_NEW) */ \ 170 : AtlasMessage::ShareableDelete(msg); \ 171 : msg = NULL; \ 172 : } \ 173 : virtual void Merge(Command* prev) { MergeIntoPrevious(static_cast<c##t##_sub*>(prev)); } \ 174 : virtual const char* GetType() const { return #t; } \ 175 : static Command* Create(const void* data) \ 176 : { \ 177 : return new c##t##_sub (reinterpret_cast<d##t*>(const_cast<void*>(data))); \ 178 : } \ 179 : }; \ 180 : cmdHandler c##t##_create() \ 181 : { \ 182 : return &c##t##_sub ::Create; \ 183 : } 184 : 185 : 186 : } 187 : 188 : #endif // INCLUDED_COMMANDPROC