namekeys.h

Go to the documentation of this file.
00001 /*
00002  Copyright (C) 2001-2006, William Joseph.
00003  All Rights Reserved.
00004 
00005  This file is part of GtkRadiant.
00006 
00007  GtkRadiant is free software; you can redistribute it and/or modify
00008  it under the terms of the GNU General Public License as published by
00009  the Free Software Foundation; either version 2 of the License, or
00010  (at your option) any later version.
00011 
00012  GtkRadiant is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU General Public License for more details.
00016 
00017  You should have received a copy of the GNU General Public License
00018  along with GtkRadiant; if not, write to the Free Software
00019  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020  */
00021 
00022 #if !defined(INCLUDED_NAMEKEYS_H)
00023 #define INCLUDED_NAMEKEYS_H
00024 
00025 #include <stdio.h>
00026 #include <map>
00027 #include "generic/static.h"
00028 #include "entitylib.h"
00029 #include "namespace.h"
00030 
00031 static inline bool keyIsName (const std::string& key)
00032 {
00033     return key == "target" || key == "targetname";
00034 }
00035 
00036 typedef MemberCaller1<EntityKeyValue, const std::string&, &EntityKeyValue::assign> KeyValueAssignCaller;
00037 typedef MemberCaller1<EntityKeyValue, const KeyObserver&, &EntityKeyValue::attach> KeyValueAttachCaller;
00038 typedef MemberCaller1<EntityKeyValue, const KeyObserver&, &EntityKeyValue::detach> KeyValueDetachCaller;
00039 
00040 class NameKeys: public Entity::Observer, public Namespaced
00041 {
00042         Namespace* m_namespace;
00043         EntityKeyValues& m_entity;
00044         NameKeys (const NameKeys& other);
00045         NameKeys& operator= (const NameKeys& other);
00046 
00047         typedef std::map<std::string, EntityKeyValue*> KeyValues;
00048         KeyValues m_keyValues;
00049 
00050         void insertName (const std::string& key, EntityKeyValue& value)
00051         {
00052             if (m_namespace != 0 && keyIsName(key)) {
00053                 m_namespace->attach(KeyValueAssignCaller(value), KeyValueAttachCaller(value));
00054             }
00055         }
00056         void eraseName (const std::string& key, EntityKeyValue& value)
00057         {
00058             if (m_namespace != 0 && keyIsName(key)) {
00059                 m_namespace->detach(KeyValueAssignCaller(value), KeyValueDetachCaller(value));
00060             }
00061         }
00062         void insertAll ()
00063         {
00064             for (KeyValues::iterator i = m_keyValues.begin(); i != m_keyValues.end(); ++i) {
00065                 insertName((*i).first, *(*i).second);
00066             }
00067         }
00068         void eraseAll ()
00069         {
00070             for (KeyValues::iterator i = m_keyValues.begin(); i != m_keyValues.end(); ++i) {
00071                 eraseName((*i).first, *(*i).second);
00072             }
00073         }
00074     public:
00075         NameKeys (EntityKeyValues& entity) :
00076             m_namespace(0), m_entity(entity)
00077         {
00078             m_entity.attach(*this);
00079         }
00080         ~NameKeys ()
00081         {
00082             m_entity.detach(*this);
00083         }
00084         void setNamespace (Namespace& space)
00085         {
00086             eraseAll();
00087             m_namespace = &space;
00088             insertAll();
00089         }
00090         void insert (const std::string& key, EntityKeyValue& value)
00091         {
00092             m_keyValues.insert(KeyValues::value_type(key, &value));
00093             insertName(key, value);
00094         }
00095         void erase (const std::string& key, EntityKeyValue& value)
00096         {
00097             eraseName(key, value);
00098             m_keyValues.erase(key);
00099         }
00100 };
00101 
00102 #endif

Generated by  doxygen 1.6.2