Bond
 
Loading...
Searching...
No Matches
traits.h
1// Copyright (c) Microsoft. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4#pragma once
5
6#include <bond/core/config.h>
7
8#include "bond_fwd.h"
9#include "detail/mpl.h"
10#include "scalar_interface.h"
11
12#include <boost/static_assert.hpp>
13#include <boost/utility/enable_if.hpp>
14
15#include <type_traits>
16
17
18namespace bond
19{
20
21// is_signed_int
22template <typename T> struct
23is_signed_int
24 : std::integral_constant<bool,
25 std::is_signed<T>::value
26 && !std::is_floating_point<T>::value
27 && !std::is_enum<T>::value> {};
28
29
30// is_signed_int_or_enum
31template <typename T> struct
32is_signed_int_or_enum
33 : std::integral_constant<bool,
34 is_signed_int<T>::value
35 || std::is_enum<T>::value> {};
36
37
38// schema
39template <typename T, typename Enable = void> struct
40schema;
41
42template <typename T> struct
43schema<T, typename boost::enable_if<std::is_class<typename T::Schema::fields> >::type>
44{
45 typedef typename T::Schema type;
46};
47
48
49// has_schema
50template <typename T, typename Enable = void> struct
51has_schema
52 : std::false_type {};
53
54
55template <typename T> struct
56has_schema<T, typename boost::enable_if<std::is_class<typename schema<T>::type> >::type>
57 : std::true_type {};
58
59
60// is_protocol_same
61template <typename Reader, typename Writer> struct
62is_protocol_same
63 : std::false_type {};
64
65
66template <template <typename T> class Reader, typename Input, template <typename T> class Writer, typename Output> struct
67is_protocol_same<Reader<Input>, Writer<Output> >
68 : std::is_same<typename Reader<Output>::Writer, Writer<Output> > {};
69
70
71template <template <typename T, typename U> class Reader, typename Input, typename MarshaledBondedProtocols, template <typename T> class Writer, typename Output> struct
72is_protocol_same<Reader<Input, MarshaledBondedProtocols>, Writer<Output> >
73 : std::is_same<typename Reader<Output, MarshaledBondedProtocols>::Writer, Writer<Output> > {};
74
75
76// For protocols that have multiple versions, specialize this template...
77template <typename Reader> struct
78protocol_has_multiple_versions
79 : std::false_type {};
80
81
82// ... and overload this function.
83template <typename Reader, typename Writer>
84inline
85bool is_protocol_version_same(const Reader&, const Writer&)
86{
87 return true;
88}
89
90
91// By default if a protocol has multiple versions any of the versions can be
92// used by an application. This template can be specialized to fix protocol to
93// a single version specified by default_version<Reader>. This can enable some
94// optimizations, e.g. fast pass-through w/o checking version at runtime.
95template <typename Reader> struct
96enable_protocol_versions
97 : std::true_type {};
98
99
100// get_protocol_writer
101template <typename Reader, typename Output> struct
102get_protocol_writer;
103
104template <template <typename T> class Reader, typename I, typename Output> struct
105get_protocol_writer<Reader<I>, Output>
106{
107 typedef typename Reader<Output>::Writer type;
108};
109
110template <template <typename T, typename U> class Reader, typename Input, typename MarshaledBondedProtocols, typename Output> struct
111get_protocol_writer<Reader<Input, MarshaledBondedProtocols>, Output>
112{
113 typedef typename Reader<Output, MarshaledBondedProtocols>::Writer type;
114};
115
116
117// Used only for older compilers for which BOND_NO_SFINAE_EXPR is defined.
118template <typename T, T> struct
119check_method
120 : std::true_type {};
121
122
123template <typename T> struct
124is_bonded
125 : std::false_type {};
126
127
128template <typename T, typename Reader> struct
129is_bonded<bonded<T, Reader> >
130 : std::true_type {};
131
132
133template <typename Reader, typename Unused = void> struct
134uses_marshaled_bonded;
135
136
137// is_type_alias
138template <typename T> struct
139is_type_alias
140 : std::is_object<typename aliased_type<T>::type> {};
141
142
143// is_reader
144template <typename Input, typename T = void, typename Enable = void> struct
145is_reader
146 : std::false_type {};
147
148template <typename Input, typename T> struct
149is_reader<Input&, T>
150 : is_reader<Input, T> {};
151
152template <typename Input, typename T> struct
153is_reader<Input, T, typename boost::enable_if<std::is_class<typename Input::Parser> >::type>
154 : std::true_type {};
155
156
157template <typename T> struct
158buffer_magic
159{
160 BOOST_STATIC_ASSERT_MSG(
161 detail::mpl::always_false<T>::value,
162 "buffer_magic is undefined for this buffer. Make sure buffer_magic is specialized for this buffer type.");
163};
164
165template <typename T> struct
166buffer_magic<T&>
167 : buffer_magic<T> {};
168
169
170template <uint16_t Id> struct
171unique_buffer_magic_check;
172
173#define BOND_DEFINE_BUFFER_MAGIC(Buffer, Id) \
174 template <> struct unique_buffer_magic_check<Id> {}; \
175 template <> struct buffer_magic<Buffer> : std::integral_constant<uint16_t, Id> {}
176
177// uses_static_parser
178template <typename Reader, typename Enable = void> struct
179uses_static_parser
180 : std::false_type {};
181
182template <typename Reader> struct
183uses_static_parser<Reader, typename boost::enable_if<
184 std::is_same<typename Reader::Parser, StaticParser<Reader&> > >::type>
185 : std::true_type {};
186
187template <typename Reader> struct
188uses_static_parser<Reader&>
189 : uses_static_parser<Reader> {};
190
191} // namespace bond
namespace bond
Definition: apply.h:17