2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
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.
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.
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
22 #if !defined(INCLUDED_NAMEKEYS_H)
23 #define INCLUDED_NAMEKEYS_H
27 #include "generic/static.h"
28 #include "entitylib.h"
29 #include "namespace.h"
31 inline bool string_is_integer(const char* string)
33 strtol(string, const_cast<char**>(&string), 10);
34 return *string == '\0';
37 typedef bool (*KeyIsNameFunc)(const char* key);
42 KeyIsNameFunc m_keyIsName;
43 const char* m_nameKey;
51 typedef MemberCaller1<EntityKeyValue, const char*, &EntityKeyValue::assign> KeyValueAssignCaller;
52 typedef MemberCaller1<EntityKeyValue, const KeyObserver&, &EntityKeyValue::attach> KeyValueAttachCaller;
53 typedef MemberCaller1<EntityKeyValue, const KeyObserver&, &EntityKeyValue::detach> KeyValueDetachCaller;
55 class NameKeys : public Entity::Observer, public Namespaced
57 Namespace* m_namespace;
58 EntityKeyValues& m_entity;
59 KeyIsNameFunc m_keyIsName;
60 NameKeys(const NameKeys& other);
61 NameKeys& operator=(const NameKeys& other);
63 typedef std::map<CopiedString, EntityKeyValue*> KeyValues;
64 KeyValues m_keyValues;
66 void insertName(const char* key, EntityKeyValue& value)
68 if(m_namespace != 0 && m_keyIsName(key))
70 //globalOutputStream() << "insert " << key << "\n";
71 m_namespace->attach(KeyValueAssignCaller(value), KeyValueAttachCaller(value));
74 void eraseName(const char* key, EntityKeyValue& value)
76 if(m_namespace != 0 && m_keyIsName(key))
78 //globalOutputStream() << "erase " << key << "\n";
79 m_namespace->detach(KeyValueAssignCaller(value), KeyValueDetachCaller(value));
84 for(KeyValues::iterator i = m_keyValues.begin(); i != m_keyValues.end(); ++i)
86 insertName((*i).first.c_str(), *(*i).second);
91 for(KeyValues::iterator i = m_keyValues.begin(); i != m_keyValues.end(); ++i)
93 eraseName((*i).first.c_str(), *(*i).second);
97 NameKeys(EntityKeyValues& entity) : m_namespace(0), m_entity(entity), m_keyIsName(Static<KeyIsName>::instance().m_keyIsName)
99 m_entity.attach(*this);
103 m_entity.detach(*this);
105 void setNamespace(Namespace& space)
108 m_namespace = &space;
111 void setKeyIsName(KeyIsNameFunc keyIsName)
114 m_keyIsName = keyIsName;
117 void insert(const char* key, EntityKeyValue& value)
119 m_keyValues.insert(KeyValues::value_type(key, &value));
120 insertName(key, value);
122 void erase(const char* key, EntityKeyValue& value)
124 eraseName(key, value);
125 m_keyValues.erase(key);
129 inline bool keyIsNameDoom3(const char* key)
131 return string_equal(key, "target")
132 || (string_equal_n(key, "target", 6) && string_is_integer(key + 6))
133 || string_equal(key, "name");
136 inline bool keyIsNameDoom3Doom3Group(const char* key)
138 return keyIsNameDoom3(key)
139 || string_equal(key, "model");
142 inline bool keyIsNameQuake3(const char* key)
144 return string_equal(key, "target")
145 || string_equal(key, "targetname")
146 || string_equal(key, "killtarget")
147 || (string_equal_n(key, "target", 6) && string_is_integer(key + 6)); // Nexuiz