From: bangerth Date: Mon, 17 Oct 2011 21:09:14 +0000 (+0000) Subject: Allow serialization of the TableHandler class. X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b01f37299febeac0d30e3ccd86d53e04a3ba828d;p=dealii-svn.git Allow serialization of the TableHandler class. git-svn-id: https://svn.dealii.org/trunk@24620 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/doc/news/changes.h b/deal.II/doc/news/changes.h index 2f66473725..174cda9d6b 100644 --- a/deal.II/doc/news/changes.h +++ b/deal.II/doc/news/changes.h @@ -50,10 +50,11 @@ Debian. This is now fixed.
(Wolfgang Bangerth, 2011/10/17) -
  • 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. +
  • 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.
    (Wolfgang Bangerth, 2011/10/17) diff --git a/deal.II/include/deal.II/base/table_handler.h b/deal.II/include/deal.II/base/table_handler.h index 04f7d9afb4..b985ff4766 100644 --- a/deal.II/include/deal.II/base/table_handler.h +++ b/deal.II/include/deal.II/base/table_handler.h @@ -30,6 +30,10 @@ #endif #include +#include +#include +#include +#include DEAL_II_NAMESPACE_OPEN @@ -40,10 +44,10 @@ namespace internal /** * A TableEntry 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 * boost::variant * 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 value_type; - + + /** + * Stored value. + */ + value_type value; + /** * Return the value stored by this object. The template type * T must be one of int,unsigned int,double,std::string @@ -61,7 +70,7 @@ namespace internal */ template 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 + void save (Archive & ar, const unsigned int version) const; + + /** + * Read the data of this object from a + * stream for the purpose of + * serialization. + */ + template + 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 + 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 + 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 + 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(&value)) + { + char c = 'i'; + ar & c & *p; + } + else if (const unsigned int *p = boost::get(&value)) + { + char c = 'u'; + ar & c & *p; + } + else if (const double *p = boost::get(&value)) + { + char c = 'd'; + ar & c & *p; + } + else if (const std::string *p = boost::get(&value)) + { + char c = 's'; + ar & c & *p; + } + else + Assert (false, ExcInternalError()); + } + + + + template + 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 +void +TableHandler::Column::serialize(Archive & ar, + const unsigned int) +{ + ar & entries & tex_caption + & tex_format & precision + & scientific & flag; +} + + + +template +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 index 0000000000..29db272497 --- /dev/null +++ b/tests/serialization/table_handler.cc @@ -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 +#include + +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 index 0000000000..96c4d72426 --- /dev/null +++ b/tests/serialization/table_handler/cmp/generic @@ -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 index 0000000000..4e7d1c9828 --- /dev/null +++ b/tests/serialization/table_handler/table_1/cmp/generic @@ -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