/
mapelement.h
146 lines (124 loc) · 4.44 KB
/
mapelement.h
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
/** @file world/mapelement.h Base class for all world map elements.
*
* @authors Copyright © 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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, see:
* http://www.gnu.org/licenses</small>
*/
#ifndef DENG_WORLD_MAPELEMENT_H
#define DENG_WORLD_MAPELEMENT_H
#include <de/Error>
#include "dd_share.h"
#include "world/dmuargs.h"
namespace de {
/**
* Base class for all elements of a map. Provides runtime type information and
* safe dynamic casting to various derived types.
*
* Maps are composed out of vertices, lines, sectors, etc.
*
* Abstract handling of map elements is particularly helpful in the public Map
* Update (DMU) API, where objects can be referenced either by type and index
* or by an opaque pointer.
*
* @ingroup world
*/
class MapElement
{
public:
/// The referenced property does not exist. @ingroup errors
DENG2_ERROR(UnknownPropertyError);
/// The referenced property is not writeable. @ingroup errors
DENG2_ERROR(WritePropertyError);
/// Special identifier used to mark an invalid index.
enum { NoIndex = -1 };
public:
MapElement(int t = DMU_NONE)
: _type(t), _indexInArchive(NoIndex), _indexInMap(NoIndex) {}
virtual ~MapElement() {}
/**
* Returns the DMU_* type of the object.
*/
int type() const;
/**
* Returns the archive index for the map element. The archive index is
* the position of the relevant data or definition in the archived map.
* For example, in the case of a DMU_SIDE element that is produced from
* an id tech 1 format map, this should be the index of the definition
* in the SIDEDEFS data lump.
*
* @see setIndexInArchive()
*/
int indexInArchive() const;
/**
* Change the "archive index" of the map element to @a newIndex.
*
* @see indexInArchive()
*/
void setIndexInArchive(int newIndex = NoIndex);
int indexInMap() const;
void setIndexInMap(int newIndex = NoIndex);
template <typename Type>
inline Type *as()
{
Type *t = dynamic_cast<Type *>(this);
DENG2_ASSERT(t != 0);
return t;
}
template <typename Type>
inline Type const *as() const
{
Type const *t = dynamic_cast<Type const *>(this);
DENG_ASSERT(t != 0);
return t;
}
/**
* @note The Current index indices are retained.
*
* @see setIndexInArchive(), setIndexInMap()
*/
MapElement &operator = (MapElement const &other);
/**
* Get a property value, selected by DMU_* name.
*
* Derived classes can override this to implement read access for additional
* DMU properties. MapElement::property() must be called from an overridding
* method if the named property is unknown/not handled, returning the result.
* If the property is known and the read access is handled the overriding
* method should return @c false.
*
* @param args Property arguments.
* @return Always @c 0 (can be used as an iterator).
*/
virtual int property(DmuArgs &args) const;
/**
* Update a property value, selected by DMU_* name.
*
* Derived classes can override this to implement write access for additional
* DMU properties. MapElement::setProperty() must be called from an overridding
* method if the named property is unknown/not handled, returning the result.
* If the property is known and the write access is handled the overriding
* method should return @c false.
*
* @param args Property arguments.
* @return Always @c 0 (can be used as an iterator).
*/
virtual int setProperty(DmuArgs const &args);
private:
int _type;
int _indexInArchive;
int _indexInMap;
};
} // namespace de
#endif // DENG_WORLD_MAPELEMENT_H