lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [4841] Refactor


From: Greg Chicares
Subject: [lmi-commits] [4841] Refactor
Date: Sat, 17 Apr 2010 12:45:24 +0000

Revision: 4841
          http://svn.sv.gnu.org/viewvc/?view=rev&root=lmi&revision=4841
Author:   chicares
Date:     2010-04-17 12:45:23 +0000 (Sat, 17 Apr 2010)
Log Message:
-----------
Refactor

Modified Paths:
--------------
    lmi/trunk/ChangeLog
    lmi/trunk/xml_serializable.hpp
    lmi/trunk/xml_serializable.tpp

Modified: lmi/trunk/ChangeLog
===================================================================
--- lmi/trunk/ChangeLog 2010-04-16 23:43:11 UTC (rev 4840)
+++ lmi/trunk/ChangeLog 2010-04-17 12:45:23 UTC (rev 4841)
@@ -24878,3 +24878,24 @@
   any_member.hpp
 Improve documentation.
 
+20100416T2343Z <address@hidden> [760]
+
+  antediluvian_stubs.cpp
+  input.hpp
+  input_xml_io.cpp
+  mec_input.cpp
+  mec_input.hpp
+  mec_state.cpp
+  mec_state.hpp
+  product_data.cpp
+  product_data.hpp
+  xml_serializable.hpp
+  xml_serializable.tpp
+Refactor.
+
+20100417T1245Z <address@hidden> [760]
+
+  xml_serializable.hpp
+  xml_serializable.tpp
+Refactor.
+

Modified: lmi/trunk/xml_serializable.hpp
===================================================================
--- lmi/trunk/xml_serializable.hpp      2010-04-16 23:43:11 UTC (rev 4840)
+++ lmi/trunk/xml_serializable.hpp      2010-04-17 12:45:23 UTC (rev 4841)
@@ -59,6 +59,19 @@
     virtual int         class_version() const = 0;
     virtual std::string xml_root_name() const = 0;
 
+    // Reading and writing.
+    virtual void read_element
+        (xml::element const& parent
+        ,std::string const&  name
+        ,T&
+        ,int                 file_version
+        );
+    virtual void write_element
+        (xml::element&       parent
+        ,std::string const&  name
+        ,T const&
+        ) const;
+
     // Backward compatibility.
     virtual bool is_detritus(std::string const&) const;
     virtual bool redintegrate_ex_ante

Modified: lmi/trunk/xml_serializable.tpp
===================================================================
--- lmi/trunk/xml_serializable.tpp      2010-04-16 23:43:11 UTC (rev 4840)
+++ lmi/trunk/xml_serializable.tpp      2010-04-17 12:45:23 UTC (rev 4841)
@@ -29,7 +29,7 @@
 #include "xml_serializable.hpp"
 
 #include "alert.hpp"
-#include "any_member.hpp" // MemberSymbolTable<>
+#include "any_member.hpp"         // MemberSymbolTable<>
 #include "platform_dependent.hpp" // access()
 #include "value_cast.hpp"
 #include "xml_lmi.hpp"
@@ -39,8 +39,8 @@
 
 #include <xmlwrapp/nodes_view.h>
 
-#include <algorithm>      // std::copy(), std::find()
-#include <iterator>       // std::back_inserter
+#include <algorithm>              // std::copy(), std::find()
+#include <iterator>               // std::back_inserter
 #include <vector>
 
 template<typename T>
@@ -140,10 +140,7 @@
             );
         if(residuary_names.end() != current_member)
             {
-            std::string s = xml_lmi::get_content(*child);
-            // Return value unused for the moment:
-            redintegrate_ex_ante(file_version, node_tag, s);
-            t[node_tag] = s;
+            read_element(x, node_tag, t, file_version);
             residuary_names.erase(current_member);
             }
         else if(is_detritus(node_tag))
@@ -187,9 +184,7 @@
     std::vector<std::string>::const_iterator i;
     for(i = t.member_names().begin(); i != t.member_names().end(); ++i)
         {
-        std::string node_tag(*i);
-        std::string value = t[node_tag].str();
-        root.push_back(xml::element(node_tag.c_str(), value.c_str()));
+        write_element(root, *i, t);
         }
 }
 
@@ -209,6 +204,59 @@
     throw "Unreachable--silences a compiler diagnostic.";
 }
 
+/// Read an xml element.
+///
+/// This default implementation is appropriate only for streamable
+/// types (for which any_member<T>::operator=(std::string const&) is
+/// valid). Override it wherever that precondition does not hold.
+///
+/// xml_serialize::get_element() does nearly the same thing, but in a
+/// type-dependent way; thus, it doesn't have the precondition above.
+/// However, the datum here has been subject to type erasure and its
+/// type is not readily unerased.
+///
+/// Calling retrieve_element() here imposes a speed penalty of
+/// seventeen percent (measured with the 'input_test' unit test), and
+/// is superfluous because the element is already available through
+/// an iterator in read().
+
+template<typename T>
+void xml_serializable<T>::read_element
+    (xml::element const& parent
+    ,std::string const&  name
+    ,T&                  t
+    ,int                 file_version
+    )
+{
+    xml::node::const_iterator i = xml_lmi::retrieve_element(parent, name);
+    std::string s = xml_lmi::get_content(*i);
+    redintegrate_ex_ante(file_version, name, s);
+    t[name] = s;
+}
+
+/// Write an xml element.
+///
+/// This default implementation is appropriate only for streamable
+/// types (for which any_member<T>::str() is valid). Override it
+/// wherever that precondition does not hold.
+///
+/// xml_serialize::set_element() does nearly the same thing, but it
+/// asserts a precondition that the parent has no element with the
+/// given tagname. Such an assertion here would impose a speed penalty
+/// of fourteen percent (measured with the 'input_test' unit test),
+/// yet would serve no purpose because immit_members_into() iterates
+/// across std::map keys, which are guaranteed to be unique.
+
+template<typename T>
+void xml_serializable<T>::write_element
+    (xml::element&        parent
+    ,std::string const&   name
+    ,T const&             t
+    ) const
+{
+    parent.push_back(xml::element(name.c_str(), t[name].str().c_str()));
+}
+
 /// Ascertain whether an element-tag is obsolete.
 ///
 /// Tags that were present in older versions and later removed are





reply via email to

[Prev in Thread] Current Thread [Next in Thread]