]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Allow serialization of the TableHandler class.
authorbangerth <bangerth@0785d39b-7218-0410-832d-ea1e28bc413d>
Mon, 17 Oct 2011 21:09:14 +0000 (21:09 +0000)
committerbangerth <bangerth@0785d39b-7218-0410-832d-ea1e28bc413d>
Mon, 17 Oct 2011 21:09:14 +0000 (21:09 +0000)
git-svn-id: https://svn.dealii.org/trunk@24620 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/doc/news/changes.h
deal.II/include/deal.II/base/table_handler.h
tests/serialization/table_handler.cc [new file with mode: 0644]
tests/serialization/table_handler/cmp/generic [new file with mode: 0644]
tests/serialization/table_handler/table_1/cmp/generic [new file with mode: 0644]

index 2f66473725a29ba2ab32b12ccd5aa0fe0e361d53..174cda9d6b663018313b95abcdda5f76093c1037 100644 (file)
@@ -50,10 +50,11 @@ Debian. This is now fixed.
 <br>
 (Wolfgang Bangerth, 2011/10/17)
 
-<li> Changed: The TableHandler class has been changed significantly internally.
-It could previously store arbitrary values (though in practice, only int, unsigned int,
-double and std::string were implemented. The class is now restricted to this particular
-set of types.
+<li> Changed: The TableHandler class has been changed significantly
+internally.  It could previously store arbitrary values (though in practice,
+only int, unsigned int, double and std::string were implemented. The class is
+now restricted to this particular set of types. On the other hand, the
+TableHandler class can now be serialized.
 <br>
 (Wolfgang Bangerth, 2011/10/17)
 
index 04f7d9afb4697a507850ec69f2f8b6c20a0487f2..b985ff47666e7396ea72f4d815c0b2c2d4be47e5 100644 (file)
 #endif
 
 #include <boost/variant.hpp>
+#include <boost/serialization/map.hpp>
+#include <boost/serialization/string.hpp>
+#include <boost/serialization/vector.hpp>
+#include <boost/serialization/split_member.hpp>
 
 
 DEAL_II_NAMESPACE_OPEN
@@ -40,10 +44,10 @@ namespace internal
   /**
    * A <tt>TableEntry</tt> stores the value of a table entry.
    * It can either be of type int, unsigned int, double or std::string.
-   * In essence, this structure is the same as 
+   * In essence, this structure is the same as
    * <code>boost::variant<int,unsigned int,double,std::string></code>
    * but we wrap this object in a structure for which we can write
-   * a function that can serialize it. This is also why the function 
+   * a function that can serialize it. This is also why the function
    * is not in fact of type boost::any.
    */
   struct TableEntry
@@ -52,7 +56,12 @@ namespace internal
      * Abbreviation for the data type stored by this object.
      */
     typedef boost::variant<int,unsigned int,double,std::string> value_type;
-    
+
+    /**
+     * Stored value.
+     */
+    value_type value;
+
     /**
      * Return the value stored by this object. The template type
      * T must be one of <code>int,unsigned int,double,std::string</code>
@@ -61,7 +70,7 @@ namespace internal
      */
     template <typename T>
     T get () const;
-    
+
     /**
      * Return the numeric value of this object if data has been stored in
      * it either as an integer, an unsigned integer, or a double.
@@ -69,11 +78,24 @@ namespace internal
      * @return double
      **/
     double get_numeric_value () const;
-    
+
     /**
-     * Stored value.
+     * Write the data of this object to a
+     * stream for the purpose of
+     * serialization.
      */
-    value_type value;
+    template <class Archive>
+    void save (Archive & ar, const unsigned int version) const;
+
+    /**
+     * Read the data of this object from a
+     * stream for the purpose of
+     * serialization.
+     */
+    template <class Archive>
+    void load (Archive & ar, const unsigned int version);
+
+    BOOST_SERIALIZATION_SPLIT_MEMBER()
   };
 }
 
@@ -304,6 +326,14 @@ class TableHandler
                                       */
     void write_tex (std::ostream &file, const bool with_header=true) const;
 
+                                     /**
+                                     * Read or write the data of this
+                                     * object to or from a stream for
+                                     * the purpose of serialization.
+                                     */
+    template <class Archive>
+    void serialize(Archive & ar, const unsigned int version);
+
                                     /** @addtogroup Exceptions
                                      * @{ */
 
@@ -362,6 +392,13 @@ class TableHandler
                                           */
         Column (const std::string &tex_caption);
 
+                                        /**
+                                         * Read or write the data of this
+                                         * object to or from a stream for
+                                         * the purpose of serialization.
+                                         */
+       template <class Archive>
+       void serialize(Archive & ar, const unsigned int version);
 
                                          /**
                                           * List of entries within
@@ -531,8 +568,8 @@ namespace internal
       throw;
     }
   }
-  
-  
+
+
   inline
   double TableEntry::get_numeric_value () const
   {
@@ -549,7 +586,7 @@ namespace internal
     catch (...)
     {}
 
-    
+
     // ... then with unsigned int...
     try
     {
@@ -557,7 +594,7 @@ namespace internal
     }
     catch (...)
     {}
-    
+
     // ...and finally with double precision:
     try
     {
@@ -568,11 +605,121 @@ namespace internal
       Assert (false, ExcMessage ("The number stored by this element of the "
       "table is not a number."))
     }
-    
+
     return 0;
-  }    
+  }
+
+
+
+  template <class Archive>
+  void TableEntry::save (Archive & ar,
+                        const unsigned int) const
+  {
+                                    // write first an identifier for the kind
+                                    // of data stored and then the actual
+                                    // data, in its correct data type
+    if (const int *p = boost::get<int>(&value))
+      {
+       char c = 'i';
+       ar & c & *p;
+      }
+    else if (const unsigned int *p = boost::get<unsigned int>(&value))
+      {
+       char c = 'u';
+       ar & c & *p;
+      }
+    else if (const double *p = boost::get<double>(&value))
+      {
+       char c = 'd';
+       ar & c & *p;
+      }
+    else if (const std::string *p = boost::get<std::string>(&value))
+      {
+       char c = 's';
+       ar & c & *p;
+      }
+    else
+      Assert (false, ExcInternalError());
+  }
+
+
+
+  template <class Archive>
+  void TableEntry::load (Archive & ar,
+                        const unsigned int)
+  {
+                                    // following what we do in the save()
+                                    // function, first read in the data type
+                                    // as a one-character id, and then read
+                                    // the data
+    char c;
+    ar & c;
+
+    switch (c)
+      {
+       case 'i':
+       {
+         int val;
+         ar & val;
+         value = val;
+         break;
+       }
+
+       case 'u':
+       {
+         unsigned int val;
+         ar & val;
+         value = val;
+         break;
+       }
+
+       case 'd':
+       {
+         double val;
+         ar & val;
+         value = val;
+         break;
+       }
+
+       case 's':
+       {
+         std::string val;
+         ar & val;
+         value = val;
+         break;
+       }
+
+       default:
+             Assert (false, ExcInternalError());
+      }
+  }
 }
 
+
+template <class Archive>
+void
+TableHandler::Column::serialize(Archive & ar,
+                               const unsigned int)
+{
+  ar & entries & tex_caption
+    & tex_format & precision
+    & scientific & flag;
+}
+
+
+
+template <class Archive>
+void
+TableHandler::serialize(Archive & ar,
+                       const unsigned int)
+{
+  ar & column_order & columns
+    & supercolumns & tex_supercaptions
+    & tex_table_caption
+    & tex_table_label;
+}
+
+
 DEAL_II_NAMESPACE_CLOSE
 
 #endif
diff --git a/tests/serialization/table_handler.cc b/tests/serialization/table_handler.cc
new file mode 100644 (file)
index 0000000..29db272
--- /dev/null
@@ -0,0 +1,66 @@
+//----------------------------  table_1.cc  ---------------------------
+//    $Id$
+//    Version: $Name$
+//
+//    Copyright (C) 2010, 2011 by the deal.II authors
+//
+//    This file is subject to QPL and may not be  distributed
+//    without copyright and license information. Please refer
+//    to the file deal.II/doc/license.html for the  text  and
+//    further information on this license.
+//
+//----------------------------  table_1.cc  ---------------------------
+
+// check serialization for TableHandler
+
+#include "serialization.h"
+#include <deal.II/base/table_handler.h>
+#include <boost/serialization/vector.hpp>
+
+namespace dealii
+{
+  bool compare (const TableHandler &t1,
+               const TableHandler &t2)
+  {
+    std::ostringstream o1, o2;
+    t1.write_tex (o1);
+    t1.write_text (o1);
+    t2.write_tex (o2);
+    t2.write_text (o2);
+
+    return (o1.str() == o2.str());
+  }
+}
+
+
+void test ()
+{
+  TableHandler t1, t2;
+  std::string keys[4] = { "key1", "key2", "key3", "key4" };
+
+
+  for (unsigned int i=0; i<10; ++i)
+  {
+    t1.add_value(keys[(0+i)%4], i);
+    t1.add_value(keys[(1+i)%4], sqrt(i));
+    t1.add_value(keys[(2+i)%4], 'a'+i);
+    t1.add_value(keys[(3+i)%4], std::string("abc-")+"0123456789"[i]);
+  }
+  t1.set_tex_table_caption("This is a caption text with \\LaTeX{} symbols");
+
+  verify (t1, t2);
+}
+
+
+int main ()
+{
+  std::ofstream logfile("table_handler/output");
+  deallog << std::setprecision(3);
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10);
+
+  test ();
+
+  deallog << "OK" << std::endl;
+}
diff --git a/tests/serialization/table_handler/cmp/generic b/tests/serialization/table_handler/cmp/generic
new file mode 100644 (file)
index 0000000..96c4d72
--- /dev/null
@@ -0,0 +1,4 @@
+
+DEAL::22 serialization::archive 9 0 0 0 0 4 0 4 key1 4 key2 4 key3 4 key4 0 0 4 0 0 0 4 key1 0 0 0 0 10 0 0 0 117 0 115 5 abc-1 117 99 100 1.7320508075688772 117 4 115 5 abc-5 117 103 100 2.6457513110645907 117 8 115 5 abc-9 4 key1 1 c 4 0 0 4 key2 10 0 100 0 117 1 115 5 abc-2 117 100 100 2 117 5 115 5 abc-6 117 104 100 2.8284271247461903 117 9 4 key2 1 c 4 0 0 4 key3 10 0 117 97 100 1 117 2 115 5 abc-3 117 101 100 2.2360679774997898 117 6 115 5 abc-7 117 105 100 3 4 key3 1 c 4 0 0 4 key4 10 0 115 5 abc-0 117 98 100 1.4142135623730951 117 3 115 5 abc-4 117 102 100 2.4494897427831779 117 7 115 5 abc-8 117 106 4 key4 1 c 4 0 0 0 0 0 0 0 0 0 0 44 This is a caption text with \LaTeX{} symbols 0 
+
+DEAL::OK
diff --git a/tests/serialization/table_handler/table_1/cmp/generic b/tests/serialization/table_handler/table_1/cmp/generic
new file mode 100644 (file)
index 0000000..4e7d1c9
--- /dev/null
@@ -0,0 +1,6 @@
+
+DEAL::22 serialization::archive 9 0 0 0 0 3 0 1 2 3 0 0 1 3
+
+DEAL::22 serialization::archive 9 0 0 0 0 3 0 1 2 3 0 0 1 3
+
+DEAL::OK

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.