Skip to content

Commit b19b4d3

Browse files
committed
feat: serializers are default constructible and assignable
1 parent d92c02d commit b19b4d3

3 files changed

Lines changed: 94 additions & 41 deletions

File tree

include/boost/http_proto/serializer.hpp

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,63 @@ class serializer
6666
using const_buffers_type =
6767
boost::span<buffers::const_buffer const>;
6868

69+
/** Destructor
70+
*/
71+
BOOST_HTTP_PROTO_DECL
72+
~serializer();
73+
74+
/** Constructor
75+
Default-constructed serializers do not reference any implementation;
76+
the only valid operations are destruction and assignment.
77+
*/
78+
serializer() = default;
79+
80+
/** Constructor.
81+
82+
The states of `other` are transferred
83+
to the newly constructed object,
84+
which includes the allocated buffer.
85+
After construction, the only valid
86+
operations on the moved-from object
87+
are destruction and assignment.
88+
89+
Buffer sequences previously obtained
90+
using @ref prepare or @ref stream::prepare
91+
remain valid.
92+
93+
@par Postconditions
94+
@code
95+
other.is_done() == true
96+
@endcode
97+
98+
@par Complexity
99+
Constant.
100+
101+
@param other The serializer to move from.
102+
*/
103+
BOOST_HTTP_PROTO_DECL
104+
serializer(
105+
serializer&& other) noexcept;
106+
107+
/** Assignment.
108+
The states of `other` are transferred
109+
to this object, which includes the
110+
allocated buffer. After assignment,
111+
the only valid operations on the
112+
moved-from object are destruction and
113+
assignment.
114+
Buffer sequences previously obtained
115+
using @ref prepare or @ref stream::prepare
116+
remain valid.
117+
@par Complexity
118+
Constant.
119+
@param other The serializer to move from.
120+
@return A reference to this object.
121+
*/
122+
BOOST_HTTP_PROTO_DECL
123+
serializer&
124+
operator=(serializer&& other) noexcept;
125+
69126
/** Constructor.
70127
71128
Constructs a serializer that uses the @ref
@@ -116,38 +173,6 @@ class serializer
116173
serializer(
117174
const rts::context& ctx);
118175

119-
/** Constructor.
120-
121-
The states of `other` are transferred
122-
to the newly constructed object,
123-
which includes the allocated buffer.
124-
After construction, the only valid
125-
operations on the moved-from object
126-
are destruction and assignment.
127-
128-
Buffer sequences previously obtained
129-
using @ref prepare or @ref stream::prepare
130-
remain valid.
131-
132-
@par Postconditions
133-
@code
134-
other.is_done() == true
135-
@endcode
136-
137-
@par Complexity
138-
Constant.
139-
140-
@param other The serializer to move from.
141-
*/
142-
BOOST_HTTP_PROTO_DECL
143-
serializer(
144-
serializer&& other) noexcept;
145-
146-
/** Destructor
147-
*/
148-
BOOST_HTTP_PROTO_DECL
149-
~serializer();
150-
151176
/** Reset the serializer for a new message.
152177
153178
Aborts any ongoing serialization and
@@ -536,7 +561,7 @@ class serializer
536561
message_base const&,
537562
source&);
538563

539-
impl* impl_;
564+
impl* impl_ = nullptr;
540565
};
541566

542567
/** Serializer configuration settings.

src/serializer.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -931,11 +931,9 @@ class serializer::impl
931931
//------------------------------------------------
932932

933933
serializer::
934-
serializer(const rts::context& ctx)
935-
: impl_(new impl(ctx))
934+
~serializer()
936935
{
937-
// TODO: use a single allocation for
938-
// impl and workspace buffer.
936+
delete impl_;
939937
}
940938

941939
serializer::
@@ -945,10 +943,25 @@ serializer(serializer&& other) noexcept
945943
other.impl_ = nullptr;
946944
}
947945

946+
serializer&
948947
serializer::
949-
~serializer()
948+
operator=(serializer&& other) noexcept
950949
{
951-
delete impl_;
950+
if(this != &other)
951+
{
952+
delete impl_;
953+
impl_ = other.impl_;
954+
other.impl_ = nullptr;
955+
}
956+
return *this;
957+
}
958+
959+
serializer::
960+
serializer(rts::context const& ctx)
961+
: impl_(new impl(ctx))
962+
{
963+
// TODO: use a single allocation for
964+
// impl and workspace buffer.
952965
}
953966

954967
void

test/unit/serializer.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ struct serializer_test
211211
}
212212

213213
void
214-
testSpecialMembers()
214+
testSpecial()
215215
{
216216
rts::context ctx;
217217
install_serializer_service(ctx, {});
@@ -222,6 +222,21 @@ struct serializer_test
222222
// empty body final chunk
223223
expected.append("0\r\n\r\n");
224224

225+
// serializer()
226+
{
227+
BOOST_TEST_NO_THROW(serializer());
228+
}
229+
230+
// operator=(serializer&&)
231+
{
232+
serializer sr;
233+
BOOST_TEST_NO_THROW(sr = serializer());
234+
}
235+
{
236+
serializer sr;
237+
BOOST_TEST_NO_THROW(sr = serializer(ctx));
238+
}
239+
225240
// serializer(serializer&&)
226241
{
227242
std::string message;
@@ -956,7 +971,7 @@ struct serializer_test
956971
run()
957972
{
958973
testSyntax();
959-
testSpecialMembers();
974+
testSpecial();
960975
testEmptyBody();
961976
testOutput();
962977
testExpect100Continue();

0 commit comments

Comments
 (0)