Monero
variant.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2018, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30 
37 #pragma once
38 
39 #include <boost/variant/variant.hpp>
40 #include <boost/variant/apply_visitor.hpp>
41 #include <boost/variant/static_visitor.hpp>
42 #include <boost/mpl/empty.hpp>
43 #include <boost/mpl/if.hpp>
44 #include <boost/mpl/front.hpp>
45 #include <boost/mpl/pop_front.hpp>
46 #include "serialization.h"
47 
54 template <class Archive, class T>
56 {
57 };
58 
63 template <class Archive, class Variant, class TBegin, class TEnd>
65 {
66  typedef typename Archive::variant_tag_type variant_tag_type;
67  typedef typename boost::mpl::next<TBegin>::type TNext;
68  typedef typename boost::mpl::deref<TBegin>::type current_type;
69 
70  // A tail recursive inline function.... okay...
71  static inline bool read(Archive &ar, Variant &v, variant_tag_type t)
72  {
74  current_type x;
75  if(!::do_serialize(ar, x))
76  {
77  ar.stream().setstate(std::ios::failbit);
78  return false;
79  }
80  v = x;
81  } else {
82  // Tail recursive.... but no mutation is going on. Why?
84  }
85  return true;
86  }
87 };
88 
89 // This one just fails when you call it.... okay
90 // So the TEnd parameter must be specified/different from TBegin
91 template <class Archive, class Variant, class TBegin>
92 struct variant_reader<Archive, Variant, TBegin, TBegin>
93 {
94  typedef typename Archive::variant_tag_type variant_tag_type;
95 
96  static inline bool read(Archive &ar, Variant &v, variant_tag_type t)
97  {
98  ar.stream().setstate(std::ios::failbit);
99  return false;
100  }
101 };
102 
103 
104 template <template <bool> class Archive, BOOST_VARIANT_ENUM_PARAMS(typename T)>
105 struct serializer<Archive<false>, boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>>
106 {
107  typedef boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> variant_type;
108  typedef typename Archive<false>::variant_tag_type variant_tag_type;
109  typedef typename variant_type::types types;
110 
111  static bool serialize(Archive<false> &ar, variant_type &v) {
113  ar.begin_variant();
114  ar.read_variant_tag(t);
115  if(!variant_reader<Archive<false>, variant_type,
116  typename boost::mpl::begin<types>::type,
117  typename boost::mpl::end<types>::type>::read(ar, v, t))
118  {
119  ar.stream().setstate(std::ios::failbit);
120  return false;
121  }
122  ar.end_variant();
123  return true;
124  }
125 };
126 
127 template <template <bool> class Archive, BOOST_VARIANT_ENUM_PARAMS(typename T)>
128 struct serializer<Archive<true>, boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>>
129 {
130  typedef boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> variant_type;
131  //typedef typename Archive<true>::variant_tag_type variant_tag_type;
132 
133  struct visitor : public boost::static_visitor<bool>
134  {
135  Archive<true> &ar;
136 
137  visitor(Archive<true> &a) : ar(a) { }
138 
139  template <class T>
140  bool operator ()(T &rv) const
141  {
142  ar.begin_variant();
143  ar.write_variant_tag(variant_serialization_traits<Archive<true>, T>::get_tag());
144  if(!::do_serialize(ar, rv))
145  {
146  ar.stream().setstate(std::ios::failbit);
147  return false;
148  }
149  ar.end_variant();
150  return true;
151  }
152  };
153 
154  static bool serialize(Archive<true> &ar, variant_type &v) {
155  return boost::apply_visitor(visitor(ar), v);
156  }
157 };
Archive::variant_tag_type variant_tag_type
Definition: variant.h:66
const uint32_t T[512]
Definition: groestl_tables.h:33
boost::variant< BOOST_VARIANT_ENUM_PARAMS(T)> variant_type
Definition: variant.h:107
Archive::variant_tag_type variant_tag_type
Definition: variant.h:94
Definition: unordered_containers_boost_serialization.h:37
boost::variant< BOOST_VARIANT_ENUM_PARAMS(T)> variant_type
Definition: variant.h:130
reads a variant
Definition: variant.h:64
static bool serialize(Archive< false > &ar, variant_type &v)
Definition: variant.h:111
Definition: variant.h:55
Archive< false >::variant_tag_type variant_tag_type
Definition: variant.h:108
static bool read(Archive &ar, Variant &v, variant_tag_type t)
Definition: variant.h:71
void do_serialize(Archive &a, epee::net_utils::network_address &na, T local)
Definition: net_peerlist_boost_serialization.h:41
Simple DSL AAPI based on.
static bool serialize(Archive< true > &ar, variant_type &v)
Definition: variant.h:154
#define false
Definition: stdbool.h:37
boost::mpl::deref< TBegin >::type current_type
Definition: variant.h:68
static bool read(Archive &ar, Variant &v, variant_tag_type t)
Definition: variant.h:96
... wouldn&#39;t a class be better?
Definition: serialization.h:92
string a
Definition: MakeCryptoOps.py:15
boost::mpl::next< TBegin >::type TNext
Definition: variant.h:67
#define true
Definition: stdbool.h:36