]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/entity/namekeys.h
Merge branch 'fix-fast' into 'master'
[xonotic/netradiant.git] / plugins / entity / namekeys.h
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #if !defined( INCLUDED_NAMEKEYS_H )
23 #define INCLUDED_NAMEKEYS_H
24
25 #include <stdio.h>
26 #include <map>
27 #include "generic/static.h"
28 #include "entitylib.h"
29 #include "namespace.h"
30
31 inline bool string_is_integer(const char *string)
32 {
33     strtol(string, const_cast<char **>( &string ), 10);
34     return *string == '\0';
35 }
36
37 typedef bool ( *KeyIsNameFunc )(const char *key);
38
39 class KeyIsName {
40 public:
41     KeyIsNameFunc m_keyIsName;
42     const char *m_nameKey;
43
44     KeyIsName()
45     {
46     }
47 };
48
49
50 typedef MemberCaller<EntityKeyValue, void(const char *), &EntityKeyValue::assign> KeyValueAssignCaller;
51 typedef MemberCaller<EntityKeyValue, void(const KeyObserver &), &EntityKeyValue::attach> KeyValueAttachCaller;
52 typedef MemberCaller<EntityKeyValue, void(const KeyObserver &), &EntityKeyValue::detach> KeyValueDetachCaller;
53
54 class NameKeys : public Entity::Observer, public Namespaced {
55     Namespace *m_namespace;
56     EntityKeyValues &m_entity;
57     KeyIsNameFunc m_keyIsName;
58
59     NameKeys(const NameKeys &other);
60
61     NameKeys &operator=(const NameKeys &other);
62
63     typedef std::map<CopiedString, EntityKeyValue *> KeyValues;
64     KeyValues m_keyValues;
65
66     void insertName(const char *key, EntityKeyValue &value)
67     {
68         if (m_namespace != 0 && m_keyIsName(key)) {
69             //globalOutputStream() << "insert " << key << "\n";
70             m_namespace->attach(KeyValueAssignCaller(value), KeyValueAttachCaller(value));
71         }
72     }
73
74     void eraseName(const char *key, EntityKeyValue &value)
75     {
76         if (m_namespace != 0 && m_keyIsName(key)) {
77             //globalOutputStream() << "erase " << key << "\n";
78             m_namespace->detach(KeyValueAssignCaller(value), KeyValueDetachCaller(value));
79         }
80     }
81
82     void insertAll()
83     {
84         for (KeyValues::iterator i = m_keyValues.begin(); i != m_keyValues.end(); ++i) {
85             insertName((*i).first.c_str(), *(*i).second);
86         }
87     }
88
89     void eraseAll()
90     {
91         for (KeyValues::iterator i = m_keyValues.begin(); i != m_keyValues.end(); ++i) {
92             eraseName((*i).first.c_str(), *(*i).second);
93         }
94     }
95
96 public:
97     NameKeys(EntityKeyValues &entity) : m_namespace(0), m_entity(entity),
98                                         m_keyIsName(Static<KeyIsName>::instance().m_keyIsName)
99     {
100         m_entity.attach(*this);
101     }
102
103     ~NameKeys()
104     {
105         m_entity.detach(*this);
106     }
107
108     void setNamespace(Namespace &space)
109     {
110         eraseAll();
111         m_namespace = &space;
112         insertAll();
113     }
114
115     void setKeyIsName(KeyIsNameFunc keyIsName)
116     {
117         eraseAll();
118         m_keyIsName = keyIsName;
119         insertAll();
120     }
121
122     void insert(const char *key, EntityKeyValue &value)
123     {
124         m_keyValues.insert(KeyValues::value_type(key, &value));
125         insertName(key, value);
126     }
127
128     void erase(const char *key, EntityKeyValue &value)
129     {
130         eraseName(key, value);
131         m_keyValues.erase(key);
132     }
133 };
134
135 inline bool keyIsNameDoom3(const char *key)
136 {
137     return string_equal(key, "target")
138            || (string_equal_n(key, "target", 6) && string_is_integer(key + 6))
139            || string_equal(key, "name");
140 }
141
142 inline bool keyIsNameDoom3Doom3Group(const char *key)
143 {
144     return keyIsNameDoom3(key)
145            || string_equal(key, "model");
146 }
147
148 inline bool keyIsNameQuake3(const char *key)
149 {
150     return string_equal(key, "target")
151            || string_equal(key, "targetname")
152            || string_equal(key, "killtarget")
153            || (string_equal_n(key, "target", 6) && string_is_integer(key + 6)); // Nexuiz
154 }
155
156 #endif