File: MessagesSetup.h

package info (click to toggle)
0ad 0.0.23.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 78,292 kB
  • sloc: cpp: 245,166; ansic: 200,249; python: 13,754; sh: 6,104; perl: 4,620; makefile: 977; xml: 810; java: 533; ruby: 229; erlang: 46; pascal: 30; sql: 21; tcl: 4
file content (215 lines) | stat: -rw-r--r-- 6,450 bytes parent folder | download | duplicates (7)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* Copyright (C) 2009 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

// Used by Messages.h, so that file stays relatively clean.

#ifndef MESSAGESSETUP_NOTFIRST
#define MESSAGESSETUP_NOTFIRST

#include "MessagePasser.h"
#include "SharedTypes.h"
#include "Shareable.h"

// Structures in this file are passed over the DLL boundary, so some
// carefulness and/or luck is required...

class CMutex;

namespace AtlasMessage
{

struct IMessage
{
	virtual const char* GetName() const = 0;
	virtual ~IMessage() {}

	enum Type { Message, Query };
	virtual Type GetType() const = 0;
};


#define MESSAGESTRUCT(t) \
	struct m##t : public IMessage { \
		virtual const char* GetName() const { return #t; } \
		virtual Type GetType() const { return IMessage::Message; } \
	private: \
		const m##t& operator=(const m##t&); \
	public:

// Messages for doing/undoing/etc world-altering commands

MESSAGESTRUCT(WorldCommand)
	mWorldCommand() {}
	virtual void* CloneData() const = 0;
	virtual bool IsMergeable() const = 0;
};
MESSAGESTRUCT(DoCommand)
	mDoCommand(mWorldCommand* c) : name(c->GetName()), data(c->CloneData()) {}
	const Shareable<std::string> name;
	const Shareable<void*> data;
		// 'data' gets deallocated by ~cWhatever in the game thread
};
MESSAGESTRUCT(UndoCommand)  };
MESSAGESTRUCT(RedoCommand)  };
MESSAGESTRUCT(MergeCommand) };


struct QueryMessage : public IMessage
{
	Type GetType() const { return IMessage::Query; }
	void Post(); // defined in ScenarioEditor.cpp

	void* m_Semaphore; // for use by MessagePasser implementations (yay encapsulation)
};


#define QUERYSTRUCT(t) \
	struct q##t : public QueryMessage { \
		const char* GetName() const { return #t; } \
	private: \
		const q##t& operator=(const q##t&); \
	public:



const bool MERGE = true;
const bool NOMERGE = false;


#define COMMANDDATASTRUCT(t) \
	struct d##t { \
	private: \
		const d##t& operator=(const d##t&); \
	public:

#define COMMANDSTRUCT(t, merge) \
	struct m##t : public mWorldCommand, public d##t { \
		m##t(const d##t& d) : d##t(d) {} \
		const char* GetName() const { return #t; } \
		virtual bool IsMergeable() const { return merge; } \
		void* CloneData() const { return SHAREABLE_NEW(d##t, (*this)); } \
	private: \
		const m##t& operator=(const m##t&);\
	}



#include <boost/preprocessor/tuple/elem.hpp>
#include <boost/preprocessor/punctuation/comma_if.hpp>
#include <boost/preprocessor/seq/for_each_i.hpp>
#include <boost/preprocessor/comparison/equal.hpp>

#define B_TYPE(elem) BOOST_PP_TUPLE_ELEM(2, 0, elem)
#define B_NAME(elem) BOOST_PP_TUPLE_ELEM(2, 1, elem)
#define B_CONSTRUCTORARGS(r, data, n, elem) BOOST_PP_COMMA_IF(n) B_TYPE(elem) BOOST_PP_CAT(B_NAME(elem),_)
#define B_CONSTRUCTORTYPES(r, data, n, elem) BOOST_PP_COMMA_IF(n) B_TYPE(elem)
#define B_CONSTRUCTORINIT(r, data, n, elem) BOOST_PP_COMMA_IF(n) B_NAME(elem)(BOOST_PP_CAT(B_NAME(elem),_))
#define B_CONSTMEMBERS(r, data, n, elem) const Shareable< B_TYPE(elem) > B_NAME(elem);
#define B_MEMBERS(r, data, n, elem) Shareable< B_TYPE(elem) > B_NAME(elem);

/* For each message type, generate something roughly like:
	struct mBlah : public IMessage {
		const char* GetName() const { return "Blah"; }
		mBlah(int in0_, bool in1_) : in0(in0_), in1(in1_) {}
		static mBlah* CtorType (int, bool) { return NULL; } // This doesn't do anything useful - it's just to make template-writing easier
		const Shareable<int> in0;
		const Shareable<bool> in1;
	}
*/

#define MESSAGE_WITH_INPUTS(name, vals) \
	MESSAGESTRUCT(name) \
		m##name( BOOST_PP_SEQ_FOR_EACH_I(B_CONSTRUCTORARGS, ~, vals) ) \
			: BOOST_PP_SEQ_FOR_EACH_I(B_CONSTRUCTORINIT, ~, vals) {} \
		static m##name* CtorType( BOOST_PP_SEQ_FOR_EACH_I(B_CONSTRUCTORTYPES, ~, vals) ) { return NULL; } \
		BOOST_PP_SEQ_FOR_EACH_I(B_CONSTMEMBERS, ~, vals) \
	}

#define MESSAGE_WITHOUT_INPUTS(name, vals) \
	MESSAGESTRUCT(name) \
		m##name() {} \
		static m##name* CtorType() { return NULL; } \
	}

#define MESSAGE(name, vals) \
	BOOST_PP_IIF( \
		BOOST_PP_EQUAL(BOOST_PP_SEQ_SIZE((~)vals), 1), \
		MESSAGE_WITHOUT_INPUTS, \
		MESSAGE_WITH_INPUTS) \
	(name, vals)



#define COMMAND(name, merge, vals) \
	COMMANDDATASTRUCT(name) \
		d##name( BOOST_PP_SEQ_FOR_EACH_I(B_CONSTRUCTORARGS, ~, vals) ) \
			: BOOST_PP_SEQ_FOR_EACH_I(B_CONSTRUCTORINIT, ~, vals) {} \
		BOOST_PP_SEQ_FOR_EACH_I(B_CONSTMEMBERS, ~, vals) \
	}; \
	COMMANDSTRUCT(name, merge)


// Need different syntax depending on whether there are some input values in the query:

#define QUERY_WITHOUT_INPUTS(name, in_vals, out_vals) \
	QUERYSTRUCT(name) \
		q##name() {} \
		static q##name* CtorType() { return NULL; } \
		BOOST_PP_SEQ_FOR_EACH_I(B_MEMBERS, ~, out_vals) /* other members */ \
	}

#define QUERY_WITH_INPUTS(name, in_vals, out_vals) \
	QUERYSTRUCT(name) \
		q##name( BOOST_PP_SEQ_FOR_EACH_I(B_CONSTRUCTORARGS, ~, in_vals) ) \
			: BOOST_PP_SEQ_FOR_EACH_I(B_CONSTRUCTORINIT, ~, in_vals) {} \
		static q##name* CtorType( BOOST_PP_SEQ_FOR_EACH_I(B_CONSTRUCTORTYPES, ~, in_vals) ) { return NULL; } \
		BOOST_PP_SEQ_FOR_EACH_I(B_CONSTMEMBERS, ~, in_vals) \
		BOOST_PP_SEQ_FOR_EACH_I(B_MEMBERS, ~, out_vals) \
	}

#define QUERY(name, in_vals, out_vals) \
	BOOST_PP_IIF( \
		BOOST_PP_EQUAL(BOOST_PP_SEQ_SIZE((~)in_vals), 1), \
		QUERY_WITHOUT_INPUTS, \
		QUERY_WITH_INPUTS) \
	(name, in_vals, out_vals)

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////

#else // MESSAGESSETUP_NOTFIRST => clean up the mess

#undef MESSAGESTRUCT
#undef QUERYSTRUCT
#undef COMMANDDATASTRUCT
#undef COMMANDSTRUCT
#undef B_TYPE
#undef B_NAME
#undef B_CONSTRUCTORARGS
#undef B_CONSTRUCTORTYPES
#undef B_CONSTRUCTORINIT
#undef B_CONSTMEMBERS
#undef B_MEMBERS
#undef MESSAGE
#undef COMMAND
#undef QUERY_WITHOUT_INPUTS
#undef QUERY_WITH_INPUTS
#undef QUERY

}

#endif