Line data Source code
1 : /* Copyright (C) 2019 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 : // Used by Messages.h, so that file stays relatively clean.
19 :
20 : #ifndef MESSAGESSETUP_NOTFIRST
21 : #define MESSAGESSETUP_NOTFIRST
22 :
23 : #include "MessagePasser.h"
24 : #include "SharedTypes.h"
25 : #include "Shareable.h"
26 :
27 : // Structures in this file are passed over the DLL boundary, so some
28 : // carefulness and/or luck is required...
29 :
30 : namespace AtlasMessage
31 : {
32 :
33 : struct IMessage
34 : {
35 : virtual const char* GetName() const = 0;
36 : virtual ~IMessage() {}
37 :
38 : enum Type { Message, Query };
39 : virtual Type GetType() const = 0;
40 : };
41 :
42 :
43 : #define MESSAGESTRUCT(t) \
44 : struct m##t : public IMessage { \
45 : virtual const char* GetName() const { return #t; } \
46 : virtual Type GetType() const { return IMessage::Message; } \
47 : private: \
48 : const m##t& operator=(const m##t&); \
49 : public:
50 :
51 : // Messages for doing/undoing/etc world-altering commands
52 :
53 0 : MESSAGESTRUCT(WorldCommand)
54 : mWorldCommand() {}
55 : virtual void* CloneData() const = 0;
56 : virtual bool IsMergeable() const = 0;
57 : };
58 0 : MESSAGESTRUCT(DoCommand)
59 : mDoCommand(mWorldCommand* c) : name(c->GetName()), data(c->CloneData()) {}
60 : const Shareable<std::string> name;
61 : const Shareable<void*> data;
62 : // 'data' gets deallocated by ~cWhatever in the game thread
63 : };
64 0 : MESSAGESTRUCT(UndoCommand) };
65 0 : MESSAGESTRUCT(RedoCommand) };
66 0 : MESSAGESTRUCT(MergeCommand) };
67 :
68 :
69 : struct QueryMessage : public IMessage
70 : {
71 0 : Type GetType() const { return IMessage::Query; }
72 : void Post(); // defined in ScenarioEditor.cpp
73 :
74 : void* m_Semaphore; // for use by MessagePasser implementations (yay encapsulation)
75 : };
76 :
77 :
78 : #define QUERYSTRUCT(t) \
79 : struct q##t : public QueryMessage { \
80 : const char* GetName() const { return #t; } \
81 : private: \
82 : const q##t& operator=(const q##t&); \
83 : public:
84 :
85 :
86 :
87 : const bool MERGE = true;
88 : const bool NOMERGE = false;
89 :
90 :
91 : #define COMMANDDATASTRUCT(t) \
92 : struct d##t { \
93 : private: \
94 : const d##t& operator=(const d##t&); \
95 : public: \
96 : d##t(const d##t& d) = default;
97 :
98 : #define COMMANDSTRUCT(t, merge) \
99 : struct m##t : public mWorldCommand, public d##t { \
100 : m##t(const d##t& d) : d##t(d) {} \
101 : const char* GetName() const { return #t; } \
102 : virtual bool IsMergeable() const { return merge; } \
103 : void* CloneData() const { return SHAREABLE_NEW(d##t, (*this)); } \
104 : private: \
105 : const m##t& operator=(const m##t&);\
106 : }
107 :
108 :
109 :
110 : #include <boost/preprocessor/tuple/elem.hpp>
111 : #include <boost/preprocessor/punctuation/comma_if.hpp>
112 : #include <boost/preprocessor/seq/for_each_i.hpp>
113 : #include <boost/preprocessor/comparison/equal.hpp>
114 :
115 : #define B_TYPE(elem) BOOST_PP_TUPLE_ELEM(2, 0, elem)
116 : #define B_NAME(elem) BOOST_PP_TUPLE_ELEM(2, 1, elem)
117 : #define B_CONSTRUCTORARGS(r, data, n, elem) BOOST_PP_COMMA_IF(n) B_TYPE(elem) BOOST_PP_CAT(B_NAME(elem),_)
118 : #define B_CONSTRUCTORTYPES(r, data, n, elem) BOOST_PP_COMMA_IF(n) B_TYPE(elem)
119 : #define B_CONSTRUCTORINIT(r, data, n, elem) BOOST_PP_COMMA_IF(n) B_NAME(elem)(BOOST_PP_CAT(B_NAME(elem),_))
120 : #define B_CONSTMEMBERS(r, data, n, elem) const Shareable< B_TYPE(elem) > B_NAME(elem);
121 : #define B_MEMBERS(r, data, n, elem) Shareable< B_TYPE(elem) > B_NAME(elem);
122 :
123 : /* For each message type, generate something roughly like:
124 : struct mBlah : public IMessage {
125 : const char* GetName() const { return "Blah"; }
126 : mBlah(int in0_, bool in1_) : in0(in0_), in1(in1_) {}
127 : static mBlah* CtorType (int, bool) { return NULL; } // This doesn't do anything useful - it's just to make template-writing easier
128 : const Shareable<int> in0;
129 : const Shareable<bool> in1;
130 : }
131 : */
132 :
133 : #define MESSAGE_WITH_INPUTS(name, vals) \
134 : MESSAGESTRUCT(name) \
135 : m##name( BOOST_PP_SEQ_FOR_EACH_I(B_CONSTRUCTORARGS, ~, vals) ) \
136 : : BOOST_PP_SEQ_FOR_EACH_I(B_CONSTRUCTORINIT, ~, vals) {} \
137 : static m##name* CtorType( BOOST_PP_SEQ_FOR_EACH_I(B_CONSTRUCTORTYPES, ~, vals) ) { return NULL; } \
138 : BOOST_PP_SEQ_FOR_EACH_I(B_CONSTMEMBERS, ~, vals) \
139 : }
140 :
141 : #define MESSAGE_WITHOUT_INPUTS(name, vals) \
142 : MESSAGESTRUCT(name) \
143 : m##name() {} \
144 : static m##name* CtorType() { return NULL; } \
145 : }
146 :
147 : #define MESSAGE(name, vals) \
148 : BOOST_PP_IIF( \
149 : BOOST_PP_EQUAL(BOOST_PP_SEQ_SIZE((~)vals), 1), \
150 : MESSAGE_WITHOUT_INPUTS, \
151 : MESSAGE_WITH_INPUTS) \
152 : (name, vals)
153 :
154 :
155 :
156 : #define COMMAND(name, merge, vals) \
157 : COMMANDDATASTRUCT(name) \
158 : d##name( BOOST_PP_SEQ_FOR_EACH_I(B_CONSTRUCTORARGS, ~, vals) ) \
159 : : BOOST_PP_SEQ_FOR_EACH_I(B_CONSTRUCTORINIT, ~, vals) {} \
160 : BOOST_PP_SEQ_FOR_EACH_I(B_CONSTMEMBERS, ~, vals) \
161 : }; \
162 : COMMANDSTRUCT(name, merge)
163 :
164 :
165 : // Need different syntax depending on whether there are some input values in the query:
166 :
167 : #define QUERY_WITHOUT_INPUTS(name, in_vals, out_vals) \
168 : QUERYSTRUCT(name) \
169 : q##name() {} \
170 : static q##name* CtorType() { return NULL; } \
171 : BOOST_PP_SEQ_FOR_EACH_I(B_MEMBERS, ~, out_vals) /* other members */ \
172 : }
173 :
174 : #define QUERY_WITH_INPUTS(name, in_vals, out_vals) \
175 : QUERYSTRUCT(name) \
176 : q##name( BOOST_PP_SEQ_FOR_EACH_I(B_CONSTRUCTORARGS, ~, in_vals) ) \
177 : : BOOST_PP_SEQ_FOR_EACH_I(B_CONSTRUCTORINIT, ~, in_vals) {} \
178 : static q##name* CtorType( BOOST_PP_SEQ_FOR_EACH_I(B_CONSTRUCTORTYPES, ~, in_vals) ) { return NULL; } \
179 : BOOST_PP_SEQ_FOR_EACH_I(B_CONSTMEMBERS, ~, in_vals) \
180 : BOOST_PP_SEQ_FOR_EACH_I(B_MEMBERS, ~, out_vals) \
181 : }
182 :
183 : #define QUERY(name, in_vals, out_vals) \
184 : BOOST_PP_IIF( \
185 : BOOST_PP_EQUAL(BOOST_PP_SEQ_SIZE((~)in_vals), 1), \
186 : QUERY_WITHOUT_INPUTS, \
187 : QUERY_WITH_INPUTS) \
188 : (name, in_vals, out_vals)
189 :
190 : //////////////////////////////////////////////////////////////////////////
191 : //////////////////////////////////////////////////////////////////////////
192 :
193 : #else // MESSAGESSETUP_NOTFIRST => clean up the mess
194 :
195 : #undef MESSAGESTRUCT
196 : #undef QUERYSTRUCT
197 : #undef COMMANDDATASTRUCT
198 : #undef COMMANDSTRUCT
199 : #undef B_TYPE
200 : #undef B_NAME
201 : #undef B_CONSTRUCTORARGS
202 : #undef B_CONSTRUCTORTYPES
203 : #undef B_CONSTRUCTORINIT
204 : #undef B_CONSTMEMBERS
205 : #undef B_MEMBERS
206 : #undef MESSAGE
207 : #undef COMMAND
208 : #undef QUERY_WITHOUT_INPUTS
209 : #undef QUERY_WITH_INPUTS
210 : #undef QUERY
211 :
212 : }
213 :
214 : #endif
|