entityxml.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. Copyright (C) 2001-2006, William Joseph.
  3. All Rights Reserved.
  4. This file is part of GtkRadiant.
  5. GtkRadiant is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. GtkRadiant is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GtkRadiant; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #if !defined(INCLUDED_ENTITYXML_H)
  18. #define INCLUDED_ENTITYXML_H
  19. #include "ientity.h"
  20. #include "xml/ixml.h"
  21. #include "xml/xmlelement.h"
  22. class entity_import : public XMLImporter
  23. {
  24. Entity& m_entity;
  25. public:
  26. entity_import(Entity& entity)
  27. : m_entity(entity)
  28. {
  29. }
  30. void pushElement(const XMLElement& element)
  31. {
  32. if(strcmp(element.name(), "epair") == 0)
  33. m_entity.setKeyValue(element.attribute("key"), element.attribute("value"));
  34. }
  35. void popElement(const char* name)
  36. {
  37. }
  38. std::size_t write(const char* data, std::size_t length)
  39. {
  40. return length;
  41. }
  42. };
  43. class entity_export : public XMLExporter
  44. {
  45. class ExportXMLVisitor : public Entity::Visitor
  46. {
  47. XMLImporter& m_importer;
  48. public:
  49. ExportXMLVisitor(XMLImporter& importer) : m_importer(importer)
  50. {
  51. }
  52. void visit(const char* key, const char* value)
  53. {
  54. StaticElement element("epair");
  55. element.insertAttribute("key", key);
  56. element.insertAttribute("value", value);
  57. m_importer.pushElement(element);
  58. m_importer.popElement(element.name());
  59. }
  60. };
  61. const Entity& m_entity;
  62. public:
  63. entity_export(const Entity& entity) : m_entity(entity)
  64. {
  65. }
  66. void exportXML(XMLImporter& observer)
  67. {
  68. ExportXMLVisitor visitor(observer);
  69. m_entity.forEachKeyValue(visitor);
  70. }
  71. };
  72. inline void entity_copy(Entity& entity, const Entity& other)
  73. {
  74. entity_export exporter(other);
  75. entity_import importer(entity);
  76. exporter.exportXML(importer);
  77. }
  78. template<typename EntityType>
  79. class EntityConstruction
  80. {
  81. public:
  82. typedef EntityClass* type;
  83. static type get(const EntityType& entity)
  84. {
  85. return &entity.getEntity().getEntityClass();
  86. }
  87. static void copy(EntityType& entity, const EntityType& other)
  88. {
  89. entity_copy(entity.getEntity(), other.getEntity());
  90. }
  91. };
  92. #endif