Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Introduced separate attribute manager.

An additional public XML parser class, AttributeManager, has been
introduced to share code between DocumentVisitContext and Q3D::SubNode,
which needs to deal with attributes.
  • Loading branch information...
commit 022957a5b1b58c03a650c8081103d04bdcc32c34 1 parent d8b0717
@intelfx authored
View
9 DocumentationProcessor/Hierarchy.h
@@ -3,6 +3,8 @@
#include "build.h"
+#include "Reader.h"
+
// A base-class retrieval visitor for boost::variant, which upcasts all objects to a specified type
template <typename Base>
class BaseVisitor : public boost::static_visitor<Base*>
@@ -50,7 +52,7 @@ class Seat;
class SubNode;
// Object representing any possible hierarchy node.
-typedef boost::variant<SubNode, ChannelGroup, Channel, Link, ChannelType, Seat> Entity;
+typedef boost::variant<Node, SubNode, ChannelGroup, Channel, Link, ChannelType, Seat> Entity;
// -------------------------------------------------------------------------------------
@@ -88,12 +90,13 @@ class Node // copyable
Document* document_; // reference to the document this node belongs to
- Node();
Node (Document* document); // constructor for the document node
- virtual ~Node();
void UnregisterDocument (Document* document);
public:
+ Node();
+ virtual ~Node();
+
typedef BaseChildLinker<Node> ChildLinker;
Node (const Node&) = default;
View
56 DocumentationProcessor/Reader.cpp
@@ -4,6 +4,33 @@
namespace XML
{
+void AttributeManager::insert_attribute (std::string&& key, std::string&& data)
+{
+ std::pair<std::unordered_map<std::string, Attribute>::iterator, bool> insert_result =
+ attributes_.insert (std::pair<std::string, Attribute> (std::move (key), AttributeReader::Read (data.c_str())));
+
+ s_cassert (insert_result.second, "Attribute specified twice: \"%s\"", insert_result.first ->first.c_str());
+}
+
+const Attribute* AttributeManager::attribute_opt (const char* name) const
+{
+ std::string attribute_key = Glib::ustring (name).casefold_collate_key();
+
+ std::unordered_map<std::string, Attribute>::const_iterator attribute_iterator = attributes_.find (attribute_key);
+
+ if (attribute_iterator == attributes_.end())
+ return 0;
+ return &attribute_iterator ->second;
+}
+
+const Attribute& AttributeManager::attribute_raw (const char* name) const
+{
+ const Attribute* attr = attribute_opt (name);
+ s_cassert (attr, "Inexistent attribute: \"%s\"", name);
+ return *attr;
+}
+
+
void DocumentVisitContext::PreparseNode()
{
s_cassert (element_, "Uninitialised context");
@@ -11,15 +38,7 @@ void DocumentVisitContext::PreparseNode()
std::list<xmlpp::Attribute*> attrs = element_ ->get_attributes();
smsg (E_INFO, E_DEBUG, "Parsing %zu attributes", attrs.size());
for (xmlpp::Attribute* attribute_node: attrs)
- {
- std::string attribute_key = attribute_node ->get_name().casefold_collate_key();
- Attribute attribute_data = AttributeReader::Read (attribute_node ->get_value().c_str());
-
- std::pair<std::unordered_map<std::string, Attribute>::iterator, bool> insert_result =
- attributes_.insert (std::pair<std::string, Attribute> (std::move (attribute_key), std::move (attribute_data)));
-
- s_cassert (insert_result.second, "Attribute specified twice: \"%s\"", attribute_node ->get_name().c_str());
- }
+ insert_attribute (attribute_node ->get_name().casefold_collate_key(), attribute_node ->get_value());
}
const xmlpp::Element* DocumentVisitContext::child (const char* name) const
@@ -48,25 +67,6 @@ std::string DocumentVisitContext::get_node_text (const xmlpp::Element* element)
}
-const Attribute* DocumentVisitContext::attribute_opt (const char* name) const
-{
- std::string attribute_key = Glib::ustring (name).casefold_collate_key();
-
- std::unordered_map<std::string, Attribute>::const_iterator attribute_iterator = attributes_.find (attribute_key);
-
- if (attribute_iterator == attributes_.end())
- return 0;
- return &attribute_iterator ->second;
-}
-
-const Attribute& DocumentVisitContext::attribute_raw (const char* name) const
-{
- const Attribute* attr = attribute_opt (name);
- s_cassert (attr, "Inexistent attribute: \"%s\"", name);
- return *attr;
-}
-
-
Q3D::Document* Read (const char* filename)
{
xmlpp::DomParser parser;
View
29 DocumentationProcessor/Reader.h
@@ -3,7 +3,14 @@
#include "build.h"
-#include "Hierarchy.h"
+// -------------------------------------------------------------------------------------
+// Predeclarations
+// -------------------------------------------------------------------------------------
+
+namespace Q3D
+{
+ class Document;
+}
// -------------------------------------------------------------------------------------
// Types used in parameter parsing
@@ -23,6 +30,26 @@ namespace XML
typedef VariantTools::Reader<guid_t, integer_t, fp_t, string_t> AttributeReader;
typedef AttributeReader::variant_type Attribute;
+typedef std::unordered_map<std::string, Attribute> AttributeMap;
+
+class AttributeManager
+{
+protected:
+ AttributeMap attributes_;
+
+ void insert_attribute (std::string&& key, std::string&& data);
+
+public:
+ const Attribute& attribute_raw (const char* name) const;
+ const Attribute* attribute_opt (const char* name) const;
+ template <typename T>
+ const T& attribute (const char* name) const { return boost::get<T> (attribute_raw (name)); }
+ template <typename T>
+ static const T& attribute (const Attribute&
+ attr) { return boost::get<T> (attr); }
+
+ const AttributeMap& attribute_map ( ) const { return attributes_; }
+};
// Main read interface (libxml++-encapsulation)
Q3D::Document* Read (const char* filename);
View
14 DocumentationProcessor/ReaderPrivate.h
@@ -1,7 +1,10 @@
#ifndef _READER_PRIVATE_H
#define _READER_PRIVATE_H
+#include "build.h"
+
#include "Reader.h"
+#include "Hierarchy.h"
// -------------------------------------------------------------------------------------
// Helper definitions
@@ -16,7 +19,7 @@
namespace XML
{
-class DocumentVisitContext
+class DocumentVisitContext : public AttributeManager
{
public:
enum Flags
@@ -27,7 +30,6 @@ class DocumentVisitContext
private:
const xmlpp::Element* element_;
- std::unordered_map<std::string, XML::Attribute> attributes_;
mask_t flags_;
void PreparseNode();
@@ -44,14 +46,6 @@ class DocumentVisitContext
std::string text ( ) const { return get_node_text (element_); }
std::string child_text (const char* name) const { return get_node_text (child (name)); }
- const Attribute& attribute_raw (const char* name) const;
- const Attribute* attribute_opt (const char* name) const;
- template <typename T>
- const T& attribute (const char* name) const { return boost::get<T> (attribute_raw (name)); }
- template <typename T>
- static const T& attribute (const Attribute&
- attr) { return boost::get<T> (attr); }
-
DocumentVisitContext temp_context (const char* name) const { return DocumentVisitContext (child (name)); }
void disable (Flags flag) { flags_ &= ~MASK (flag); }
View
2  DocumentationProcessor/main.cpp
@@ -2,6 +2,8 @@
#include <uXray/fxlog_console.h>
#include <uXray/encap.h>
+#include <uXray/fxcrc32.h>
+
#include "Reader.h"
int main (int argc, char** argv)
Please sign in to comment.
Something went wrong with that request. Please try again.