Skip to content

Commit 1e47f43

Browse files
Goober5000claude
andcommitted
use std::exchange in move ctors/assignment operators
Replaces the copy-then-reset idiom with std::exchange in five steal-and-reset move constructors and assignment operators. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent e4c7c73 commit 1e47f43

4 files changed

Lines changed: 19 additions & 45 deletions

File tree

code/io/cursor.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,10 @@ namespace io
7878

7979
std::swap(this->mAnimationFrames, other.mAnimationFrames);
8080

81-
this->mBitmapHandle = other.mBitmapHandle;
82-
this->mBeginTimeStamp = other.mBeginTimeStamp;
83-
this->mLastFrame = other.mLastFrame;
84-
85-
other.mBitmapHandle = -1;
86-
other.mBeginTimeStamp = UI_TIMESTAMP::invalid();
87-
other.mLastFrame = static_cast<size_t>(-1);
88-
81+
this->mBitmapHandle = std::exchange(other.mBitmapHandle, -1);
82+
this->mBeginTimeStamp = std::exchange(other.mBeginTimeStamp, UI_TIMESTAMP::invalid());
83+
this->mLastFrame = std::exchange(other.mLastFrame, static_cast<size_t>(-1));
84+
8985
return *this;
9086
}
9187

code/jumpnode/jumpnode.cpp

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,8 @@ CJumpNode::CJumpNode(const vec3d* position)
7171
}
7272

7373
CJumpNode::CJumpNode(CJumpNode&& other) noexcept
74-
: m_radius(other.m_radius), m_modelnum(other.m_modelnum), m_objnum(other.m_objnum), m_polymodel_instance_num(other.m_polymodel_instance_num), m_flags(other.m_flags), m_fred_layer(std::move(other.m_fred_layer))
74+
: m_radius(std::exchange(other.m_radius, 0.0f)), m_modelnum(std::exchange(other.m_modelnum, -1)), m_objnum(std::exchange(other.m_objnum, -1)), m_polymodel_instance_num(std::exchange(other.m_polymodel_instance_num, -1)), m_flags(std::exchange(other.m_flags, 0)), m_fred_layer(std::exchange(other.m_fred_layer, "Default"))
7575
{
76-
other.m_radius = 0.0f;
77-
other.m_modelnum = -1;
78-
other.m_objnum = -1;
79-
other.m_polymodel_instance_num = -1;
80-
other.m_flags = 0;
81-
other.m_fred_layer = "Default";
82-
8376
m_display_color = other.m_display_color;
8477
m_pos = other.m_pos;
8578

@@ -91,19 +84,12 @@ CJumpNode& CJumpNode::operator=(CJumpNode&& other) noexcept
9184
{
9285
if (this != &other)
9386
{
94-
m_radius = other.m_radius;
95-
m_modelnum = other.m_modelnum;
96-
m_objnum = other.m_objnum;
97-
m_flags = other.m_flags;
98-
m_polymodel_instance_num = other.m_polymodel_instance_num;
99-
m_fred_layer = std::move(other.m_fred_layer);
100-
101-
other.m_radius = 0.0f;
102-
other.m_modelnum = -1;
103-
other.m_objnum = -1;
104-
other.m_flags = 0;
105-
other.m_polymodel_instance_num = -1;
106-
other.m_fred_layer = "Default";
87+
m_radius = std::exchange(other.m_radius, 0.0f);
88+
m_modelnum = std::exchange(other.m_modelnum, -1);
89+
m_objnum = std::exchange(other.m_objnum, -1);
90+
m_flags = std::exchange(other.m_flags, 0);
91+
m_polymodel_instance_num = std::exchange(other.m_polymodel_instance_num, -1);
92+
m_fred_layer = std::exchange(other.m_fred_layer, "Default");
10793

10894
m_display_color = other.m_display_color;
10995
m_pos = other.m_pos;

code/model/model.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "globalincs/globals.h" // for NAME_LENGTH
1616
#include "globalincs/pstypes.h"
1717
#include <array>
18+
#include <utility>
1819

1920
#include "actions/Program.h"
2021
#include "gamesnd/gamesnd.h"
@@ -566,12 +567,9 @@ struct w_bank
566567
w_bank& operator=(w_bank&& other) {
567568
this->~w_bank();
568569
num_slots = other.num_slots;
569-
pnt = other.pnt;
570-
norm = other.norm;
571-
external_model_angle_offset = other.external_model_angle_offset;
572-
other.pnt = nullptr;
573-
other.norm = nullptr;
574-
other.external_model_angle_offset = nullptr;
570+
pnt = std::exchange(other.pnt, nullptr);
571+
norm = std::exchange(other.norm, nullptr);
572+
external_model_angle_offset = std::exchange(other.external_model_angle_offset, nullptr);
575573
return *this;
576574
}
577575
w_bank(const w_bank& other) = default;

code/scripting/api/objs/player.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,17 @@ player_h::~player_h()
4040
delete _plr;
4141
}
4242
player_h::player_h(player_h&& other) noexcept
43-
: _owned(other._owned), _plr(other._plr)
44-
{
45-
other._owned = false;
46-
other._plr = nullptr;
47-
}
43+
: _owned(std::exchange(other._owned, false)), _plr(std::exchange(other._plr, nullptr))
44+
{}
4845
player_h& player_h::operator=(player_h&& other) noexcept
4946
{
5047
if (this != &other)
5148
{
5249
if (_owned)
5350
delete _plr;
5451

55-
_owned = other._owned;
56-
_plr = other._plr;
57-
58-
other._owned = false;
59-
other._plr = nullptr;
52+
_owned = std::exchange(other._owned, false);
53+
_plr = std::exchange(other._plr, nullptr);
6054
}
6155

6256
return *this;

0 commit comments

Comments
 (0)