xdrpp
RFC4506 XDR compiler and message library
cereal.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 
3 /** \file cereal.h Interface for
4  * [cereal](http://uscilab.github.io/cereal/) serealization back ends.
5  * By including this file, you can archive any XDR data structure
6  * using any format supported by [cereal]. Note that only the binary
7  * structures actually sanity-check the lengths of strings. You don't
8  * need to do anything with this file other than include it. Simply
9  * follow the [cereal] archive documentation to proceed, without the
10  * need to implement your own `archive` (or `save`/`load`) methods.
11  *
12  * Note you still need to include the cereal archive headers you want
13  * to use, e.g., <tt>#include &lt;cereal/archives/json.hpp&gt;</tt>.
14  *
15  * [cereal]: http://uscilab.github.io/cereal/
16  */
17 
18 #ifndef _XDRPP_CEREAL_H_HEADER_INCLUDED_
19 #define _XDRPP_CEREAL_H_HEADER_INCLUDED_ 1
20 
21 #include <type_traits>
22 #include <cereal/cereal.hpp>
23 #include <cereal/types/array.hpp>
24 #include <cereal/types/string.hpp>
25 #include <cereal/types/memory.hpp>
26 #include <cereal/types/vector.hpp>
27 #include <cereal/details/traits.hpp>
28 #include <xdrpp/types.h>
29 
30 namespace cereal {
31 class JSONInputArchive;
32 class JSONOutputArchive;
33 class XMLOutputArchive;
34 class XMLInputArchive;
35 }
36 
37 namespace xdr {
38 
39 namespace detail {
40 template<typename Archive, typename T> typename
41 std::enable_if<xdr_traits<T>::is_class>::type
42 save(Archive &ar, const T &t)
43 {
44  xdr_traits<T>::save(ar, t);
45 }
46 
47 template<typename Archive, typename T> typename
48 std::enable_if<xdr_traits<T>::is_class>::type
49 load(Archive &ar, T &t)
50 {
51  xdr_traits<T>::load(ar, t);
52 }
53 
54 template<typename Archive, typename T> typename
55 std::enable_if<cereal::traits::is_output_serializable<
56  cereal::BinaryData<char *>,Archive>::value
57  && xdr_traits<T>::is_bytes>::type
58 save(Archive &ar, const T &t)
59 {
60  if (xdr_traits<T>::variable_length)
61  ar(cereal::make_size_tag(static_cast<cereal::size_type>(t.size())));
62  ar(cereal::binary_data(const_cast<char *>(
63  reinterpret_cast<const char *>(t.data())), t.size()));
64 }
65 
66 template<typename Archive, typename T> typename
67 std::enable_if<cereal::traits::is_input_serializable<
68  cereal::BinaryData<char *>,Archive>::value
69  && xdr_traits<T>::is_bytes>::type
70 load(Archive &ar, T &t)
71 {
72  cereal::size_type size;
73  if (xdr_traits<T>::variable_length)
74  ar(cereal::make_size_tag(size));
75  else
76  size = t.size();
77  t.check_size(size);
78  t.resize(static_cast<std::uint32_t>(size));
79  ar(cereal::binary_data(t.data(), size));
80 }
81 
82 
83 template<typename Archive> struct nvp_adapter {
84  template<typename T> static void
85  apply(Archive &ar, T &&t, const char *field) {
86  if (field)
87  ar(cereal::make_nvp(field, std::forward<T>(t)));
88  else
89  ar(std::forward<T>(t));
90  }
91 
92  template<uint32_t N> static void
93  apply(Archive &ar, xstring<N> &s, const char *field) {
94  apply(ar, field, static_cast<std::string &>(s));
95  }
96  template<uint32_t N> static void
97  apply(Archive &ar, const xstring<N> &s, const char *field) {
98  apply(ar, field, static_cast<const std::string &>(s));
99  }
100 };
101 }
102 
103 //! \hideinitializer \cond
104 #define CEREAL_ARCHIVE_TAKES_NAME(archive) \
105 template<> struct archive_adapter<cereal::archive> \
106  : detail::nvp_adapter<cereal::archive> {}
107 CEREAL_ARCHIVE_TAKES_NAME(JSONInputArchive);
108 CEREAL_ARCHIVE_TAKES_NAME(JSONOutputArchive);
109 CEREAL_ARCHIVE_TAKES_NAME(XMLOutputArchive);
110 CEREAL_ARCHIVE_TAKES_NAME(XMLInputArchive);
111 #undef CEREAL_ARCHIVE_TAKES_NAME
112 //! \endcond
113 
114 
115 }
116 
117 namespace cereal {
118 using xdr::detail::load;
119 using xdr::detail::save;
120 }
121 
122 #endif // !_XDRPP_CEREAL_H_HEADER_INCLUDED_
Type definitions for xdrc compiler output.
Most of the xdrpp library is encapsulated in the xdr namespace.
Definition: arpc.cc:4
Definition: cereal.h:30