ug4
archivar.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016: G-CSC, Goethe University Frankfurt
3  * Author: Sebastian Reiter
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef __H__UG_archivar
34 #define __H__UG_archivar
35 
36 #include <boost/static_assert.hpp>
37 #include <boost/mpl/for_each.hpp>
38 #include <boost/type_traits/is_base_of.hpp>
39 #include "../boost_serialization.h"
41 
42 namespace ug{
43 
44 
45 namespace detail{
46 namespace archivar{
47  template <typename TArchive, typename TBase, typename TDerived>
48  void CallArchiveOnDerivedClass (TArchive& ar, TBase& base, const char* name)
49  {
50  BOOST_STATIC_ASSERT((boost::is_base_of<TBase, TDerived>::value));
51  TDerived& derived = dynamic_cast<TDerived&>(base);
52  ar & make_nvp(name, derived);
53  }
54 }
55 }
56 
57 template <class TArchive, class TBase, class TPairSeq>
58 class Archivar
59 {
60 public:
62  {
64  boost::mpl::for_each<TPairSeq>(func);
65  }
66 
67  template <class T>
68  void archive(TArchive& ar, T& t)
69  {
70  archive(ar, t, "");
71  }
72 
73  template <class T>
74  void archive(TArchive& ar, T& t, const char* name)
75  {
76  std::string typeName(typeid(t).name());
77 
78  typename callback_map_t::iterator icallback = m_callbackMap.find(typeName);
79  UG_COND_THROW(icallback == m_callbackMap.end(),
80  "Unregistered class used in 'Archivar::archive': " << typeName);
81 
82  icallback->second(ar, t, name);
83  }
84 
85  template <class TDerived>
86  void register_class(const char* name)
87  {
88  BOOST_STATIC_ASSERT((boost::is_base_of<TBase, TDerived>::value));
89  std::string typeName(typeid(TDerived).name());
90  m_callbackMap[typeName] =
91  &detail::archivar::CallArchiveOnDerivedClass<TArchive, TBase, TDerived>;
92  }
93 
94 private:
95  typedef void (*archive_sig)(TArchive&, TBase&, const char* name);
96 
97  typedef std::map<std::string, archive_sig> callback_map_t;
99 };
100 
101 }// end of namespace
102 
103 #endif //__H__UG_archivar
location name
Definition: checkpoint_util.lua:128
Definition: archivar.h:59
void register_class(const char *name)
Definition: archivar.h:86
void archive(TArchive &ar, T &t)
Definition: archivar.h:68
std::map< std::string, archive_sig > callback_map_t
Definition: archivar.h:97
callback_map_t m_callbackMap
Definition: archivar.h:98
Archivar()
Definition: archivar.h:61
void archive(TArchive &ar, T &t, const char *name)
Definition: archivar.h:74
void(* archive_sig)(TArchive &, TBase &, const char *name)
Definition: archivar.h:95
#define UG_COND_THROW(cond, msg)
UG_COND_THROW(cond, msg) : performs a UG_THROW(msg) if cond == true.
Definition: error.h:61
void CallArchiveOnDerivedClass(TArchive &ar, TBase &base, const char *name)
Definition: archivar.h:48
the ug namespace
function func(x, y, z, t, si)
Definition: register_type_pair_functor.h:43