Skip to content

Commit ca3fda4

Browse files
committed
chore: Array, Object special members
1 parent 735ef37 commit ca3fda4

File tree

2 files changed

+107
-2
lines changed

2 files changed

+107
-2
lines changed

include/mrdox/Support/Dom.hpp

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,15 @@ class MRDOX_DECL
300300
empty array which is distinct from every
301301
other empty array.
302302
*/
303-
private:
304303
Array();
305-
public:
304+
305+
/** Constructor.
306+
307+
Ownership of the contents is transferred
308+
to the new object. The moved-from array
309+
will behave as if default-constructed.
310+
*/
311+
Array(Array&& other);
306312

307313
/** Constructor.
308314
@@ -320,6 +326,23 @@ class MRDOX_DECL
320326
*/
321327
Array(impl_type impl) noexcept;
322328

329+
/** Assignment.
330+
331+
Ownership of the array is transferred
332+
to this, and ownership of the previous
333+
contents is released. The moved-from
334+
array behaves as if default constructed.
335+
*/
336+
Array& operator=(Array&&);
337+
338+
/** Assignment.
339+
340+
This acquires shared ownership of the copied
341+
array, and ownership of the previous contents
342+
is released.
343+
*/
344+
Array& operator=(Array const&) = default;
345+
323346
/** Return the implementation used by this object.
324347
*/
325348
auto impl() const noexcept -> impl_type const&;
@@ -355,6 +378,20 @@ class MRDOX_DECL
355378
*/
356379
void emplace_back(value_type value);
357380

381+
/** Swap two arrays.
382+
*/
383+
void swap(Array& other) noexcept
384+
{
385+
std::swap(impl_, other.impl_);
386+
}
387+
388+
/** Swap two arrays.
389+
*/
390+
friend void swap(Array& lhs, Array& rhs) noexcept
391+
{
392+
lhs.swap(rhs);
393+
}
394+
358395
/** Return a diagnostic string.
359396
*/
360397
friend
@@ -508,6 +545,14 @@ class MRDOX_DECL
508545
*/
509546
Object();
510547

548+
/** Constructor.
549+
550+
Ownership of the contents is transferred
551+
to the new object. The moved-from object
552+
will behave as if default-constructed.
553+
*/
554+
Object(Object&& other);
555+
511556
/** Constructor.
512557
513558
The newly constructed array will contain
@@ -541,6 +586,15 @@ class MRDOX_DECL
541586
MRDOX_ASSERT(impl_);
542587
}
543588

589+
/** Assignment.
590+
591+
Ownership of the object is transferred
592+
to this, and ownership of the previous
593+
contents is released. The moved-from
594+
object behaves as if default constructed.
595+
*/
596+
Object& operator=(Object&&);
597+
544598
/** Return the implementation used by this object.
545599
*/
546600
auto
@@ -601,6 +655,20 @@ class MRDOX_DECL
601655
*/
602656
void set(String key, Value value) const;
603657

658+
/** Swap two objects.
659+
*/
660+
void swap(Object& other) noexcept
661+
{
662+
std::swap(impl_, other.impl_);
663+
}
664+
665+
/** Swap two objects.
666+
*/
667+
friend void swap(Object& lhs, Object& rhs) noexcept
668+
{
669+
lhs.swap(rhs);
670+
}
671+
604672
/** Return an iterator to the beginning of the range of elements.
605673
*/
606674
iterator begin() const;

source/Support/Dom.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,32 @@ Array()
261261
{
262262
}
263263

264+
Array::
265+
Array(
266+
Array&& other)
267+
: Array()
268+
{
269+
swap(other);
270+
}
271+
264272
Array::
265273
Array(
266274
Array const& other)
267275
: impl_(other.impl_)
268276
{
269277
}
270278

279+
Array&
280+
Array::
281+
operator=(
282+
Array&& other)
283+
{
284+
Array temp;
285+
swap(temp);
286+
swap(other);
287+
return *this;
288+
}
289+
271290
std::string
272291
toString(
273292
Array const& arr)
@@ -358,6 +377,14 @@ Object()
358377
{
359378
}
360379

380+
Object::
381+
Object(
382+
Object&& other)
383+
: Object()
384+
{
385+
swap(other);
386+
}
387+
361388
Object::
362389
Object(
363390
Object const& other) noexcept
@@ -373,6 +400,16 @@ Object(
373400
{
374401
}
375402

403+
Object&
404+
Object::
405+
operator=(Object&& other)
406+
{
407+
Object temp;
408+
swap(temp);
409+
swap(other);
410+
return *this;
411+
}
412+
376413
bool
377414
Object::
378415
exists(std::string_view key) const

0 commit comments

Comments
 (0)