| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 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 |
strtol( string, const_cast<char**>( &string ), 10 ); |
| 33 |
return *string == '\0'; |
| 34 |
} |
| 35 |
|
| 36 |
typedef bool ( *KeyIsNameFunc )( const char* key ); |
| 37 |
|
| 38 |
class KeyIsName |
| 39 |
{ |
| 40 |
public: |
| 41 |
KeyIsNameFunc m_keyIsName; |
| 42 |
const char* m_nameKey; |
| 43 |
|
| 44 |
KeyIsName(){ |
| 45 |
} |
| 46 |
}; |
| 47 |
|
| 48 |
|
| 49 |
typedef MemberCaller<EntityKeyValue, void(const char*), &EntityKeyValue::assign> KeyValueAssignCaller; |
| 50 |
typedef MemberCaller<EntityKeyValue, void(const KeyObserver&), &EntityKeyValue::attach> KeyValueAttachCaller; |
| 51 |
typedef MemberCaller<EntityKeyValue, void(const KeyObserver&), &EntityKeyValue::detach> KeyValueDetachCaller; |
| 52 |
|
| 53 |
class NameKeys : public Entity::Observer, public Namespaced |
| 54 |
{ |
| 55 |
Namespace* m_namespace; |
| 56 |
EntityKeyValues& m_entity; |
| 57 |
KeyIsNameFunc m_keyIsName; |
| 58 |
NameKeys( const NameKeys& other ); |
| 59 |
NameKeys& operator=( const NameKeys& other ); |
| 60 |
|
| 61 |
typedef std::map<CopiedString, EntityKeyValue*> KeyValues; |
| 62 |
KeyValues m_keyValues; |
| 63 |
|
| 64 |
void insertName( const char* key, EntityKeyValue& value ){ |
| 65 |
if ( m_namespace != 0 && m_keyIsName( key ) ) { |
| 66 |
|
| 67 |
m_namespace->attach( KeyValueAssignCaller( value ), KeyValueAttachCaller( value ) ); |
| 68 |
} |
| 69 |
} |
| 70 |
void eraseName( const char* key, EntityKeyValue& value ){ |
| 71 |
if ( m_namespace != 0 && m_keyIsName( key ) ) { |
| 72 |
|
| 73 |
m_namespace->detach( KeyValueAssignCaller( value ), KeyValueDetachCaller( value ) ); |
| 74 |
} |
| 75 |
} |
| 76 |
void insertAll(){ |
| 77 |
for ( KeyValues::iterator i = m_keyValues.begin(); i != m_keyValues.end(); ++i ) |
| 78 |
{ |
| 79 |
insertName( ( *i ).first.c_str(), *( *i ).second ); |
| 80 |
} |
| 81 |
} |
| 82 |
void eraseAll(){ |
| 83 |
for ( KeyValues::iterator i = m_keyValues.begin(); i != m_keyValues.end(); ++i ) |
| 84 |
{ |
| 85 |
eraseName( ( *i ).first.c_str(), *( *i ).second ); |
| 86 |
} |
| 87 |
} |
| 88 |
public: |
| 89 |
NameKeys( EntityKeyValues& entity ) : m_namespace( 0 ), m_entity( entity ), m_keyIsName( Static<KeyIsName>::instance().m_keyIsName ){ |
| 90 |
m_entity.attach( *this ); |
| 91 |
} |
| 92 |
~NameKeys(){ |
| 93 |
m_entity.detach( *this ); |
| 94 |
} |
| 95 |
void setNamespace( Namespace& space ){ |
| 96 |
eraseAll(); |
| 97 |
m_namespace = &space; |
| 98 |
insertAll(); |
| 99 |
} |
| 100 |
void setKeyIsName( KeyIsNameFunc keyIsName ){ |
| 101 |
eraseAll(); |
| 102 |
m_keyIsName = keyIsName; |
| 103 |
insertAll(); |
| 104 |
} |
| 105 |
void insert( const char* key, EntityKeyValue& value ){ |
| 106 |
m_keyValues.insert( KeyValues::value_type( key, &value ) ); |
| 107 |
insertName( key, value ); |
| 108 |
} |
| 109 |
void erase( const char* key, EntityKeyValue& value ){ |
| 110 |
eraseName( key, value ); |
| 111 |
m_keyValues.erase( key ); |
| 112 |
} |
| 113 |
}; |
| 114 |
|
| 115 |
inline bool keyIsNameDoom3( const char* key ){ |
| 116 |
return string_equal( key, "target" ) |
| 117 |
|| ( string_equal_n( key, "target", 6 ) && string_is_integer( key + 6 ) ) |
| 118 |
|| string_equal( key, "name" ); |
| 119 |
} |
| 120 |
|
| 121 |
inline bool keyIsNameDoom3Doom3Group( const char* key ){ |
| 122 |
return keyIsNameDoom3( key ) |
| 123 |
|| string_equal( key, "model" ); |
| 124 |
} |
| 125 |
|
| 126 |
inline bool keyIsNameQuake3( const char* key ){ |
| 127 |
return string_equal( key, "target" ) |
| 128 |
|| string_equal( key, "targetname" ) |
| 129 |
|| string_equal( key, "killtarget" ) |
| 130 |
|| ( string_equal_n( key, "target", 6 ) && string_is_integer( key + 6 ) ); |
| 131 |
} |
| 132 |
|
| 133 |
#endif |