DUDS
Distributed Update of Data from Something
Int128.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the DUDS project. It is subject to the BSD-style
3  * license terms in the LICENSE file found in the top-level directory of this
4  * distribution and at https://github.com/jjackowski/duds/blob/master/LICENSE.
5  * No part of DUDS, including this file, may be copied, modified, propagated,
6  * or distributed except according to the terms contained in the LICENSE file.
7  *
8  * Copyright (C) 2017 Jeff Jackowski
9  */
17 #include <boost/serialization/split_member.hpp>
18 #include <boost/serialization/nvp.hpp>
19 #include <boost/multiprecision/cpp_int.hpp>
20 #include <duds/BuildConfig.h>
21 #include <cstdint>
22 #include <sstream>
23 
24 /*
25 #include <type_traits>
26 namespace std {
27  template <>
28  struct is_integral<__int128> : public true_type {
29  //static constexpr bool value = true;
30  };
31  template <>
32  struct is_integral<unsigned __int128> : public true_type {
33  //static constexpr bool value = true;
34  };
35  template <>
36  struct is_integral<boost::multiprecision::int128_t> : public true_type {
37  //static constexpr bool value = true;
38  };
39  template <>
40  struct is_integral<boost::multiprecision::uint128_t> : public true_type {
41  //static constexpr bool value = true;
42  };
43 }
44 */
45 
46 namespace duds { namespace data {
47 
59 #ifdef HAVE_INT128
60 typedef __int128 int128_t;
61 
62 std::istream &operator >> (std::istream &is, int128_t &b);
63 std::ostream &operator << (std::ostream &os, int128_t const &b);
64 
65 #else
67 #endif
68 
84 template <class I, class M>
86  /*
87 private:
88  LargeIntWrapper(const double &v);
89  LargeIntWrapper &operator = (double const &b);
90  */
91 public:
92  I value;
93  LargeIntWrapper() = default;
94  LargeIntWrapper(const LargeIntWrapper &liw) = default;
95  //template <class N, typename std::enable_if<std::is_integral<N>::value>::type >
96  //constexpr LargeIntWrapper(const N &v) : value(v) { }
97  constexpr LargeIntWrapper(const I &v) : value(v) { }
98  /*
99  operator I const & () const {
100  return value;
101  }
102  */
104  value = b.value;
105  return *this;
106  }
107  /*
108  template <class N, typename std::enable_if<std::is_integral<N>::value>::type >
109  LargeIntWrapper &operator = (N const &b) {
110  value = b;
111  return *this;
112  }
113  */
114  //template <>
116  value = b;
117  return *this;
118  }
119  bool operator == (LargeIntWrapper const &b) const {
120  return value == b.value;
121  }
122  bool operator != (LargeIntWrapper const &b) const {
123  return value != b.value;
124  }
125  bool operator > (LargeIntWrapper const &b) const {
126  return value > b.value;
127  }
128  bool operator < (LargeIntWrapper const &b) const {
129  return value < b.value;
130  }
131  bool operator >= (LargeIntWrapper const &b) const {
132  return value >= b.value;
133  }
134  bool operator <= (LargeIntWrapper const &b) const {
135  return value <= b.value;
136  }
138  return value + b.value;
139  }
141  return value - b.value;
142  }
144  return value * b.value;
145  }
147  return value / b.value;
148  }
150  return value % b.value;
151  }
153  return value += b.value;
154  }
156  return value -= b.value;
157  }
159  return value *= b.value;
160  }
162  return value /= b.value;
163  }
165  return value %= b.value;
166  }
167  friend std::istream &operator >> (std::istream &is, LargeIntWrapper &b) {
168  return is >> b.value;
169  }
170  friend std::ostream &operator << (std::ostream &os, LargeIntWrapper const &b) {
171  return os << b.value;
172  }
173 private:
183  template <class A>
184  void save(A &a, const unsigned int) const {
185  std::ostringstream os;
186  os << std::hex << *this;
187  std::string s(os.str());
188  a << boost::serialization::make_nvp("value", s);
189  }
190  template <class A>
191  void load(A &a, const unsigned int) {
192  std::string s;
193  a >> boost::serialization::make_nvp("value", s);
194  std::istringstream is(s);
195  is >> std::hex >> *this;
196  }
198 };
199 
200 
206 
207 } }
LargeIntWrapper & operator*=(LargeIntWrapper const &b)
Definition: Int128.hpp:158
LargeIntWrapper operator/(LargeIntWrapper const &b) const
Definition: Int128.hpp:146
friend std::ostream & operator<<(std::ostream &os, LargeIntWrapper const &b)
Definition: Int128.hpp:170
bool operator<(LargeIntWrapper const &b) const
Definition: Int128.hpp:128
Wraps an integer to allow the Boost multiprecision library to provide insertion and extraction operat...
Definition: Int128.hpp:85
LargeIntWrapper< int128_t, boost::multiprecision::int128_t > int128_w
A 128-bit integer wrapped by LargeIntWrapper to make the Boost serialized result interchangable betwe...
Definition: Int128.hpp:205
LargeIntWrapper & operator=(LargeIntWrapper const &b)
Definition: Int128.hpp:103
bool operator>=(LargeIntWrapper const &b) const
Definition: Int128.hpp:131
boost::multiprecision::int128_t int128_t
The type used for 128-bit integers.
Definition: Int128.hpp:66
void save(A &a, const unsigned int) const
Serialize the unit data.
Definition: Int128.hpp:184
LargeIntWrapper operator*(LargeIntWrapper const &b) const
Definition: Int128.hpp:143
LargeIntWrapper & operator%=(LargeIntWrapper const &b)
Definition: Int128.hpp:164
LargeIntWrapper & operator/=(LargeIntWrapper const &b)
Definition: Int128.hpp:161
void load(A &a, const unsigned int)
Definition: Int128.hpp:191
LargeIntWrapper operator%(LargeIntWrapper const &b) const
Definition: Int128.hpp:149
bool operator>(LargeIntWrapper const &b) const
Definition: Int128.hpp:125
LargeIntWrapper operator-(LargeIntWrapper const &b) const
Definition: Int128.hpp:140
ConversationExtractor & operator>>(ConversationExtractor &ce, Int &i)
Extraction operator to read an integer from a Conversation through a ConversationExtractor object...
LargeIntWrapper operator+(LargeIntWrapper const &b) const
Definition: Int128.hpp:137
constexpr LargeIntWrapper(const I &v)
Definition: Int128.hpp:97
bool operator<=(LargeIntWrapper const &b) const
Definition: Int128.hpp:134
LargeIntWrapper & operator+=(LargeIntWrapper const &b)
Definition: Int128.hpp:152
friend std::istream & operator>>(std::istream &is, LargeIntWrapper &b)
Definition: Int128.hpp:167
ConversationVector & operator<<(ConversationVector &cv, const Int &i)
Insertion operator to add an integer to a ConversationVector object.
bool operator==(LargeIntWrapper const &b) const
Definition: Int128.hpp:119
LargeIntWrapper & operator-=(LargeIntWrapper const &b)
Definition: Int128.hpp:155
bool operator!=(LargeIntWrapper const &b) const
Definition: Int128.hpp:122
friend class boost::serialization::access
Serialization support.
Definition: Int128.hpp:177