File: message_id.h

package info (click to toggle)
widelands 1:19+repack-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 370,608 kB
  • ctags: 20,609
  • sloc: cpp: 108,404; ansic: 18,695; python: 5,155; sh: 487; xml: 460; makefile: 233
file content (89 lines) | stat: -rw-r--r-- 2,899 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 2010 by the Widelands Development Team
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#ifndef WL_LOGIC_MESSAGE_ID_H
#define WL_LOGIC_MESSAGE_ID_H

#include <stdint.h>

#include "base/macros.h"

namespace Widelands {

/// Each message has an id that is unique per message queue. (Each player has
/// his own message queue.) Ids are not reused while the simulation is running.
/// But when the game is saved, Ids are of course not saved with it. Instead,
/// all messages are just saved in sequence. When the game is loaded again,
/// messages will get new ids starting at 1. The value 0 is a special null
/// value that can be used to represent an unset reference to a message.
///
/// Because of this renumbering, the code that saves a message must translate
/// the id that it has to the sequence number in the savegame of that message.
/// MapMessageSaver does that.
struct MessageId {
	MessageId() : id(0) {
	}
	explicit MessageId(uint32_t const init_id) : id(init_id) {
	}

	/// Constant value for no message.
	static MessageId null() {
		MessageId result;
		result.id = 0;
		return result;
	}

	bool operator==(const MessageId& other) const {
		return id == other.id;
	}
	bool operator!=(const MessageId& other) const {
		return id != other.id;
	}
	bool operator<(const MessageId& other) const {
		return id < other.id;
	}
	operator bool() const {
		return *this != null();
	}
	uint32_t value() const {
		return id;
	}

private:
	//  This is needed to prevent operator bool from being applied when someone
	//  tries to use a MessageId where an int*_t is needed. It will try to use
	//  this operator instead and fail because it is private. As an extra line
	//  of defense, it is marked as deprectated. In any case, the linking will
	//  fail because the function body is missing.
	operator int8_t() const __attribute__((deprecated));
	operator int16_t() const __attribute__((deprecated));
	operator int32_t() const __attribute__((deprecated));
	operator int64_t() const __attribute__((deprecated));

	friend struct MapMessageSaver;
	friend struct MessageQueue;
	MessageId operator++() {
		++id;
		return *this;
	}
	uint32_t id;
};
}

#endif  // end of include guard: WL_LOGIC_MESSAGE_ID_H