codec_value.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2020 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2019 Moskvin Aleksey <zerg1996@yandex.ru>
4 //
5 // MIT License
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in all
15 // copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 // SOFTWARE.
24 //---------------------------------------------------------------------------//
25 
26 #ifndef CRYPTO3_CODEC_VALUE_HPP
27 #define CRYPTO3_CODEC_VALUE_HPP
28 
29 #include <boost/assert.hpp>
30 #include <boost/concept_check.hpp>
31 
32 #include <boost/range/concepts.hpp>
33 
35 
36 namespace nil {
37  namespace crypto3 {
38  namespace codec {
39  namespace detail {
40  template<typename CodecAccumulatorSet>
41  struct ref_codec_impl {
42  typedef CodecAccumulatorSet accumulator_set_type;
43  typedef
44  typename boost::mpl::front<typename accumulator_set_type::features_type>::type accumulator_type;
45 
46  typedef typename accumulator_type::mode_type mode_type;
47  typedef typename mode_type::codec_type codec_type;
48 
50  }
51 
53  };
54 
55  template<typename CodecAccumulatorSet>
57  typedef CodecAccumulatorSet accumulator_set_type;
58  typedef
59  typename boost::mpl::front<typename accumulator_set_type::features_type>::type accumulator_type;
60 
61  typedef typename accumulator_type::mode_type mode_type;
62  typedef typename mode_type::codec_type codec_type;
63 
65  accumulator_set(std::forward<accumulator_set_type>(acc)) {
66  }
67 
69  };
70 
71  template<typename CodecStateImpl>
72  struct range_codec_impl : public CodecStateImpl {
73  typedef CodecStateImpl codec_state_impl_type;
74 
75  typedef typename codec_state_impl_type::accumulator_type accumulator_type;
76  typedef typename codec_state_impl_type::accumulator_set_type accumulator_set_type;
77 
78  typedef typename codec_state_impl_type::mode_type mode_type;
79  typedef typename codec_state_impl_type::codec_type codec_type;
80 
81  typedef typename boost::mpl::apply<accumulator_set_type, accumulator_type>::type::result_type
83 
84  template<typename SinglePassRange>
85  range_codec_impl(const SinglePassRange &range, accumulator_set_type &&ise) :
86  CodecStateImpl(std::forward<accumulator_set_type>(ise)) {
87  BOOST_RANGE_CONCEPT_ASSERT((boost::SinglePassRangeConcept<const SinglePassRange>));
88 
89  typedef
90  typename std::iterator_traits<typename SinglePassRange::iterator>::value_type value_type;
91  BOOST_STATIC_ASSERT(std::numeric_limits<value_type>::is_specialized);
92  typedef typename codec_type::template stream_processor<
94  std::numeric_limits<value_type>::digits + std::numeric_limits<value_type>::is_signed>::type
95  stream_processor;
96 
97  stream_processor(this->accumulator_set)(range.begin(), range.end());
98  }
99 
100  template<typename InputIterator>
101  range_codec_impl(InputIterator first, InputIterator last, accumulator_set_type &&ise) :
102  CodecStateImpl(std::forward<accumulator_set_type>(ise)) {
103  BOOST_CONCEPT_ASSERT((boost::InputIteratorConcept<InputIterator>));
104 
105  typedef typename std::iterator_traits<InputIterator>::value_type value_type;
106  BOOST_STATIC_ASSERT(std::numeric_limits<value_type>::is_specialized);
107  typedef typename codec_type::template stream_processor<
109  std::numeric_limits<value_type>::digits + std::numeric_limits<value_type>::is_signed>::type
110  stream_processor;
111 
112  stream_processor(this->accumulator_set)(first, last);
113  }
114 
115  template<typename OutputRange>
116  inline operator OutputRange() const {
117  result_type result =
118  boost::accumulators::extract_result<accumulator_type>(this->accumulator_set);
119  return OutputRange(result.begin(), result.end());
120  }
121 
122  inline operator result_type() const {
123  return boost::accumulators::extract_result<accumulator_type>(this->accumulator_set);
124  }
125 
126  inline operator accumulator_set_type &() const {
127  return this->accumulator_set;
128  }
129 
130 #ifdef CRYPTO3_ASCII_STRING_CODEC_OUTPUT
131 
132  template<typename Char, typename CharTraits, typename Alloc>
133  inline operator std::basic_string<Char, CharTraits, Alloc>() const {
134  return std::to_string(
135  boost::accumulators::extract_result<accumulator_type>(this->accumulator_set));
136  }
137 
138 #endif
139  };
140 
141  template<typename CodecStateImpl, typename OutputIterator>
142  struct itr_codec_impl : public CodecStateImpl {
143  private:
144  mutable OutputIterator out;
145 
146  public:
147  typedef CodecStateImpl codec_state_impl_type;
148 
149  typedef typename codec_state_impl_type::accumulator_type accumulator_type;
150  typedef typename codec_state_impl_type::accumulator_set_type accumulator_set_type;
151 
152  typedef typename codec_state_impl_type::mode_type mode_type;
153  typedef typename codec_state_impl_type::codec_type codec_type;
154 
155  typedef typename boost::mpl::apply<accumulator_set_type, accumulator_type>::type::result_type
157 
158  template<typename SinglePassRange>
159  itr_codec_impl(const SinglePassRange &range, OutputIterator out, accumulator_set_type &&ise) :
160  CodecStateImpl(std::forward<accumulator_set_type>(ise)), out(std::move(out)) {
161  BOOST_CONCEPT_ASSERT((boost::SinglePassRangeConcept<const SinglePassRange>));
162 
163  typedef
164  typename std::iterator_traits<typename SinglePassRange::iterator>::value_type value_type;
165  BOOST_STATIC_ASSERT(std::numeric_limits<value_type>::is_specialized);
166  typedef typename codec_type::template stream_processor<
168  std::numeric_limits<value_type>::digits + std::numeric_limits<value_type>::is_signed>::type
169  stream_processor;
170 
171  stream_processor(this->accumulator_set)(range.begin(), range.end());
172  }
173 
174  template<typename InputIterator>
175  itr_codec_impl(InputIterator first, InputIterator last, OutputIterator out,
176  accumulator_set_type &&ise) :
177  CodecStateImpl(std::forward<accumulator_set_type>(ise)),
178  out(std::move(out)) {
179  BOOST_CONCEPT_ASSERT((boost::InputIteratorConcept<InputIterator>));
180 
181  typedef typename std::iterator_traits<InputIterator>::value_type value_type;
182  BOOST_STATIC_ASSERT(std::numeric_limits<value_type>::is_specialized);
183  typedef typename codec_type::template stream_processor<
185  std::numeric_limits<value_type>::digits + std::numeric_limits<value_type>::is_signed>::type
186  stream_processor;
187 
188  stream_processor(this->accumulator_set)(first, last);
189  }
190 
191  inline operator OutputIterator() const {
192  result_type result =
193  boost::accumulators::extract_result<accumulator_type>(this->accumulator_set);
194 
195  return std::move(result.cbegin(), result.cend(), out);
196  }
197 
198  inline operator accumulator_set_type &() const {
199  return this->accumulator_set;
200  }
201  };
202  } // namespace detail
203  } // namespace codec
204  } // namespace crypto3
205 } // namespace nil
206 
207 #endif // CRYPTO3_CODEC_VALUE_HPP
boost::accumulators::accumulator_set< digest< ProcessingMode::output_block_bits >, boost::accumulators::features< accumulators::tag::codec< ProcessingMode > >, std::size_t > accumulator_set
Accumulator set with codec accumulator predefined params.
Definition: codec_state.hpp:67
boost::mpl::apply< AccumulatorSet, tag::codec< Mode > >::type::result_type codec(const AccumulatorSet &acc)
Definition: accumulators/codec.hpp:261
OutputIterator move(const SinglePassRange &rng, OutputIterator result)
Definition: move.hpp:45
Definition: pair.hpp:31
Definition: codec_value.hpp:142
itr_codec_impl(InputIterator first, InputIterator last, OutputIterator out, accumulator_set_type &&ise)
Definition: codec_value.hpp:175
boost::mpl::apply< accumulator_set_type, accumulator_type >::type::result_type result_type
Definition: codec_value.hpp:156
CodecStateImpl codec_state_impl_type
Definition: codec_value.hpp:147
codec_state_impl_type::accumulator_set_type accumulator_set_type
Definition: codec_value.hpp:150
codec_state_impl_type::codec_type codec_type
Definition: codec_value.hpp:153
itr_codec_impl(const SinglePassRange &range, OutputIterator out, accumulator_set_type &&ise)
Definition: codec_value.hpp:159
codec_state_impl_type::accumulator_type accumulator_type
Definition: codec_value.hpp:149
codec_state_impl_type::mode_type mode_type
Definition: codec_value.hpp:152
Definition: codec_value.hpp:72
range_codec_impl(InputIterator first, InputIterator last, accumulator_set_type &&ise)
Definition: codec_value.hpp:101
codec_state_impl_type::accumulator_set_type accumulator_set_type
Definition: codec_value.hpp:76
codec_state_impl_type::mode_type mode_type
Definition: codec_value.hpp:78
codec_state_impl_type::accumulator_type accumulator_type
Definition: codec_value.hpp:75
codec_state_impl_type::codec_type codec_type
Definition: codec_value.hpp:79
boost::mpl::apply< accumulator_set_type, accumulator_type >::type::result_type result_type
Definition: codec_value.hpp:82
range_codec_impl(const SinglePassRange &range, accumulator_set_type &&ise)
Definition: codec_value.hpp:85
CodecStateImpl codec_state_impl_type
Definition: codec_value.hpp:73
Definition: codec_value.hpp:41
CodecAccumulatorSet accumulator_set_type
Definition: codec_value.hpp:42
ref_codec_impl(accumulator_set_type &&acc)
Definition: codec_value.hpp:49
accumulator_set_type & accumulator_set
Definition: codec_value.hpp:52
boost::mpl::front< typename accumulator_set_type::features_type >::type accumulator_type
Definition: codec_value.hpp:44
mode_type::codec_type codec_type
Definition: codec_value.hpp:47
accumulator_type::mode_type mode_type
Definition: codec_value.hpp:46
Definition: codec_value.hpp:56
CodecAccumulatorSet accumulator_set_type
Definition: codec_value.hpp:57
mode_type::codec_type codec_type
Definition: codec_value.hpp:62
boost::mpl::front< typename accumulator_set_type::features_type >::type accumulator_type
Definition: codec_value.hpp:59
value_codec_impl(accumulator_set_type &&acc)
Definition: codec_value.hpp:64
accumulator_type::mode_type mode_type
Definition: codec_value.hpp:61
accumulator_set_type accumulator_set
Definition: codec_value.hpp:68