6#include <bond/core/config.h>
9#include <bond/core/containers.h>
10#include <bond/core/traits.h>
12#include <boost/static_assert.hpp>
20class RandomProtocolEngine
27 RandomProtocolEngine()
34 uint64_t s1 = state[0];
35 const uint64_t s0 = state[1];
38 return (state[1] = (s1 ^ s0 ^ (s1 >> 17) ^ (s0 >> 26))) + s0;
41 void Seed(uint64_t s0 = seed1, uint64_t s1 = seed2)
47 static uint64_t state[2];
48 const static uint64_t seed1 = 234578354U;
49 const static uint64_t seed2 = 753478U;
53uint64_t RandomProtocolEngine<T>::state[2] = {seed1, seed2};
62class RandomProtocolReader
63 :
public RandomProtocolEngine<RandomProtocolReader>
66 typedef bond::StaticParser<RandomProtocolReader&> Parser;
68 RandomProtocolReader(uint32_t max_string_length = 50, uint32_t max_list_size = 20,
bool json =
false)
69 : _max_string_length(max_string_length),
70 _max_list_size(max_list_size),
74 bool operator==(
const RandomProtocolReader&)
const
85 typename boost::disable_if<is_string_type<T> >::type
88 BOOST_STATIC_ASSERT(std::is_trivially_copyable<T>::value);
90 BOOST_STATIC_ASSERT(
sizeof(T) <=
sizeof(uint64_t));
92 uint64_t random = RandomProtocolEngine::Next();
93 std::memcpy(&value, &random,
sizeof(T));
96 void Read(uint64_t& value)
98 value = RandomProtocolEngine::Next();
108 void Read(
bool& value)
115 void Read(
double& value)
117 BOOST_STATIC_ASSERT(
sizeof(
double) ==
sizeof(uint64_t));
131 uint64_t bits = ((uint64_t)(sign) << 63) | ((uint64_t)(exponent) << (52 + 3)) | (uint64_t)mantissa;
132 std::memcpy(&value, &bits,
sizeof(bits));
135 void Read(
float& value)
137 BOOST_STATIC_ASSERT(
sizeof(
float) ==
sizeof(uint32_t));
148 if (exponent == 0 || exponent == 0xff)
151 uint32_t bits = ((uint32_t)(sign) << 31) | ((uint32_t)(exponent) << 23) | (uint32_t)mantissa;
152 std::memcpy(&value, &bits,
sizeof(bits));
155 template <
typename T>
156 typename boost::enable_if<is_string_type<T> >::type
163 length %= _max_string_length;
164 length = length ? length : 1;
166 resize_string(value, length);
168 typename element_type<T>::type* p = string_data(value);
169 typename element_type<T>::type*
const p_end = p + length;
171 for (; p != p_end; ++p)
177 *p =
typename element_type<T>::type(
' ') + c % (
'z' -
' ');
182 void Read(blob& value, uint32_t size)
184 boost::shared_ptr<char[]> buffer(boost::make_shared_noinit<
char[]>(size));
185 char* p = buffer.get();
186 char*
const p_end = p + size;
188 for (; p != p_end; ++p)
193 value.assign(buffer, size);
196 template <
typename T>
200 template <
typename T>
204 template <
typename T>
205 void ReadContainerBegin(uint32_t& size, T&)
209 size %= _max_list_size;
210 size = size ? size : 1;
213 void ReadContainerEnd()
const
218 uint32_t _max_string_length;
219 uint32_t _max_list_size;
224template <
typename Unused>
struct
225uses_marshaled_bonded<RandomProtocolReader, Unused>
226 : std::false_type {};
228template <
typename Unused>
struct
229uses_marshaled_bonded<RandomProtocolReader&, Unused>
230 : std::false_type {};
namespace bond
Definition: apply.h:17
boost::enable_if< is_signed_int_or_enum< SignedT >, bool >::type operator==(const Variant &variant, SignedT value)
Compares variant for equality against the provided signed integer or enum value.
Definition: metadata.h:24