6#include <bond/core/config.h>
10#include "runtime_schema.h"
11#include "select_protocol_fwd.h"
13#ifdef BOND_NO_CXX14_GENERIC_LAMBDAS
25template <
typename Protocols,
typename T,
typename U,
typename Reader>
27Apply(
const boost::reference_wrapper<bonded<T> >& ref,
const bonded<U, Reader>& value)
29 value.template Deserialize<Protocols>(ref.get());
34template <
typename Protocols,
typename F>
35inline auto TryEachProtocol(F&& f)
36#ifdef BOND_NO_CXX14_RETURN_TYPE_DEDUCTION
37 ->
decltype(mpl::try_apply<typename Protocols::type>(std::forward<F>(f)))
40 return mpl::try_apply<typename Protocols::type>(std::forward<F>(f));
44template <
typename T,
typename Protocols>
45struct NextProtocolFunctor
47 template <
typename Buffer,
typename Transform,
typename Reader>
48 boost::optional<std::pair<ProtocolType, bool> >
49 operator()(Buffer& input,
const Transform& transform,
const mpl::identity<Reader>&)
const
53 if (reader.ReadVersion())
55 return std::make_pair(
56 static_cast<ProtocolType
>(Reader::magic),
57 Apply<Protocols>(transform, bonded<T, ProtocolReader>(reader)));
63 template <
typename Buffer,
typename Transform,
typename Reader>
65 operator()(Buffer& input,
const Transform& transform, uint16_t protocol,
const mpl::identity<Reader>&)
const
67 if (Reader::magic == protocol)
70 return Apply<Protocols>(transform, bonded<T, Reader&>(reader));
77 template <
template <
typename Writer,
typename ProtocolsT>
class Transform>
78 struct TransformFunctor
80 template <
typename Buffer,
typename Reader>
82 operator()(
const T& value, Buffer& output, uint16_t protocol,
const mpl::identity<Reader>&)
const
84 if (Reader::magic == protocol)
86 using Writer =
typename get_protocol_writer<Reader, Buffer>::type;
88 Writer writer(output);
89 return Apply<Protocols>(Transform<Writer, Protocols>(writer), value);
98template <
typename Protocols>
99struct NextProtocolFunctor<void, Protocols>
101 template <
typename Buffer,
typename Transform,
typename Reader>
102 boost::optional<std::pair<ProtocolType, bool> >
103 operator()(
const RuntimeSchema& schema, Buffer& input,
const Transform& transform,
const mpl::identity<Reader>&)
const
105 Reader reader(input);
107 if (reader.ReadVersion())
109 return std::make_pair(
110 static_cast<ProtocolType
>(Reader::magic),
111 Apply<Protocols>(transform, bonded<void, ProtocolReader>(reader, schema)));
117 template <
typename Buffer,
typename Transform,
typename Reader>
118 boost::optional<bool>
119 operator()(
const RuntimeSchema& schema, Buffer& input,
const Transform& transform, uint16_t protocol,
const mpl::identity<Reader>&)
const
121 if (Reader::magic == protocol)
123 Reader reader(input);
124 return Apply<Protocols>(transform, bonded<void, Reader&>(reader, schema));
133template <
typename T,
typename Protocols,
typename Buffer,
typename Transform>
134inline std::pair<ProtocolType, bool> NextProtocol(Buffer& input,
const Transform& transform)
137#ifndef BOND_NO_CXX14_GENERIC_LAMBDAS
138 [&](
const auto& identity) {
return NextProtocolFunctor<T, Protocols>{}(input, transform, identity); };
140 std::bind(NextProtocolFunctor<T, Protocols>{}, std::ref(input), std::cref(transform), std::placeholders::_1);
143 if (
auto&& result = TryEachProtocol<
typename Protocols::template FilterBuffer<Buffer> >(std::move(visitor)))
148 UnknownProtocolException();
153template <
typename Protocols,
typename Buffer,
typename Transform>
154inline std::pair<ProtocolType, bool> NextProtocol(
const RuntimeSchema& schema, Buffer& input,
const Transform& transform)
157#ifndef BOND_NO_CXX14_GENERIC_LAMBDAS
158 [&](
const auto& identity) {
return NextProtocolFunctor<void, Protocols>{}(schema, input, transform, identity); };
160 std::bind(NextProtocolFunctor<void, Protocols>{}, std::cref(schema), std::ref(input), std::cref(transform), std::placeholders::_1);
163 if (
auto&& result = TryEachProtocol<
typename Protocols::template FilterBuffer<Buffer> >(std::move(visitor)))
168 UnknownProtocolException();
173template <
typename T,
typename Protocols,
typename Buffer,
typename Transform>
174inline bool NextProtocol(Buffer& input,
const Transform& transform, uint16_t protocol)
177#ifndef BOND_NO_CXX14_GENERIC_LAMBDAS
178 [&](
const auto& identity) {
return NextProtocolFunctor<T, Protocols>{}(input, transform, protocol, identity); };
180 std::bind(NextProtocolFunctor<T, Protocols>{}, std::ref(input), std::cref(transform), protocol, std::placeholders::_1);
183 if (
auto&& result = TryEachProtocol<
typename Protocols::template FilterBuffer<Buffer> >(std::move(visitor)))
188 UnknownProtocolException(protocol);
193template <
typename Protocols,
typename Buffer,
typename Transform>
194inline bool NextProtocol(
const RuntimeSchema& schema, Buffer& input,
const Transform& transform, uint16_t protocol)
197#ifndef BOND_NO_CXX14_GENERIC_LAMBDAS
198 [&](
const auto& identity) {
return NextProtocolFunctor<void, Protocols>{}(schema, input, transform, protocol, identity); };
200 std::bind(NextProtocolFunctor<void, Protocols>{}, std::cref(schema), std::ref(input), std::cref(transform), protocol, std::placeholders::_1);
203 if (
auto&& result = TryEachProtocol<
typename Protocols::template FilterBuffer<Buffer> >(std::move(visitor)))
208 UnknownProtocolException(protocol);
213template <
template <
typename Writer,
typename ProtocolsT>
class Transform,
typename Protocols,
typename T,
typename Buffer>
214inline bool NextProtocol(
const T& value, Buffer& output, uint16_t protocol)
216 using TransformFunctor =
typename NextProtocolFunctor<T, Protocols>::template TransformFunctor<Transform>;
219#ifndef BOND_NO_CXX14_GENERIC_LAMBDAS
220 [&](
const auto& identity) {
return TransformFunctor{}(value, output, protocol, identity); };
222 std::bind(TransformFunctor{}, std::cref(value), std::ref(output), protocol, std::placeholders::_1);
225 if (
auto&& result = TryEachProtocol<typename Protocols::FilterEnabled>(std::move(visitor)))
230 UnknownProtocolException(protocol);
243template <
typename T,
typename Protocols,
typename Buffer,
typename Transform>
244inline std::pair<ProtocolType, bool> SelectProtocolAndApply(Buffer& input,
const Transform& transform)
246 return detail::NextProtocol<T, Protocols>(input, transform);
251template <
typename Protocols,
typename Buffer,
typename Transform>
252inline std::pair<ProtocolType, bool> SelectProtocolAndApply(
253 const RuntimeSchema& schema,
255 const Transform& transform)
257 return detail::NextProtocol<Protocols>(schema, input, transform);
263template <
typename T,
typename Protocols = BuiltInProtocols,
typename Transform,
typename Buffer>
264inline bool Apply(
const Transform& transform, Buffer& input, uint16_t protocol)
266 return detail::NextProtocol<T, Protocols>(input, transform, protocol);
271template <
typename Protocols = BuiltInProtocols,
typename Transform,
typename Buffer>
272inline bool Apply(
const Transform& transform,
const RuntimeSchema& schema, Buffer& input, uint16_t protocol)
274 return detail::NextProtocol<Protocols>(schema, input, transform, protocol);
279template <
template <
typename Writer,
typename ProtocolsT>
class Transform,
typename Protocols = BuiltInProtocols,
typename T,
typename Buffer>
280inline bool Apply(
const T& value, Buffer& output, uint16_t protocol)
282 return detail::NextProtocol<Transform, Protocols>(value, output, protocol);
290#if BOND_LIB_TYPE != BOND_LIB_TYPE_HEADER
291#include "detail/select_protocol_extern.h"
294#error BOND_LIB_TYPE is undefined
namespace bond
Definition: apply.h:17