Skip to content

Commit 66f6417

Browse files
Implement copy-and-swap idiom, add noexcept to appropriate methods
1 parent a385159 commit 66f6417

File tree

2 files changed

+32
-56
lines changed

2 files changed

+32
-56
lines changed

Code/Source/solver/BoundaryCondition.cpp

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <iostream>
3737
#include <cstdio>
3838
#include <vector>
39+
#include <utility>
3940

4041
#define n_debug_bc
4142

@@ -106,27 +107,24 @@ BoundaryCondition::BoundaryCondition(const BoundaryCondition& other)
106107
}
107108
}
108109

109-
BoundaryCondition& BoundaryCondition::operator=(const BoundaryCondition& other)
110-
{
111-
if (this != &other) {
112-
face_ = other.face_;
113-
global_num_nodes_ = other.global_num_nodes_;
114-
local_num_nodes_ = other.local_num_nodes_;
115-
array_names_ = other.array_names_;
116-
local_data_ = other.local_data_;
117-
global_data_ = other.global_data_;
118-
spatially_variable = other.spatially_variable;
119-
vtp_file_path_ = other.vtp_file_path_;
120-
flags_ = other.flags_;
121-
global_node_map_ = other.global_node_map_;
122-
defined_ = other.defined_;
123-
124-
if (other.vtp_data_) {
125-
vtp_data_ = std::make_unique<VtkVtpData>(*other.vtp_data_);
126-
} else {
127-
vtp_data_.reset();
128-
}
129-
}
110+
void swap(BoundaryCondition& lhs, BoundaryCondition& rhs) noexcept {
111+
using std::swap;
112+
swap(lhs.face_, rhs.face_);
113+
swap(lhs.global_num_nodes_, rhs.global_num_nodes_);
114+
swap(lhs.local_num_nodes_, rhs.local_num_nodes_);
115+
swap(lhs.array_names_, rhs.array_names_);
116+
swap(lhs.local_data_, rhs.local_data_);
117+
swap(lhs.global_data_, rhs.global_data_);
118+
swap(lhs.spatially_variable, rhs.spatially_variable);
119+
swap(lhs.vtp_file_path_, rhs.vtp_file_path_);
120+
swap(lhs.flags_, rhs.flags_);
121+
swap(lhs.global_node_map_, rhs.global_node_map_);
122+
swap(lhs.vtp_data_, rhs.vtp_data_);
123+
swap(lhs.defined_, rhs.defined_);
124+
}
125+
126+
BoundaryCondition& BoundaryCondition::operator=(BoundaryCondition other) {
127+
swap(*this, other);
130128
return *this;
131129
}
132130

@@ -151,30 +149,6 @@ BoundaryCondition::BoundaryCondition(BoundaryCondition&& other) noexcept
151149
other.defined_ = false;
152150
}
153151

154-
BoundaryCondition& BoundaryCondition::operator=(BoundaryCondition&& other) noexcept
155-
{
156-
if (this != &other) {
157-
face_ = other.face_;
158-
global_num_nodes_ = other.global_num_nodes_;
159-
local_num_nodes_ = other.local_num_nodes_;
160-
array_names_ = std::move(other.array_names_);
161-
local_data_ = std::move(other.local_data_);
162-
global_data_ = std::move(other.global_data_);
163-
spatially_variable = other.spatially_variable;
164-
vtp_file_path_ = std::move(other.vtp_file_path_);
165-
flags_ = std::move(other.flags_);
166-
global_node_map_ = std::move(other.global_node_map_);
167-
vtp_data_ = std::move(other.vtp_data_);
168-
defined_ = other.defined_;
169-
170-
other.face_ = nullptr;
171-
other.global_num_nodes_ = 0;
172-
other.local_num_nodes_ = 0;
173-
other.spatially_variable = false;
174-
other.defined_ = false;
175-
}
176-
return *this;
177-
}
178152

179153
BoundaryCondition::StringArrayMap BoundaryCondition::read_data_from_vtp_file(const std::string& vtp_file_path, const std::vector<std::string>& array_names)
180154
{

Code/Source/solver/BoundaryCondition.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <map>
4141
#include <vector>
4242
#include <stdexcept>
43+
#include <utility>
4344

4445
// Forward declarations. These are needed because including ComMod.h causes a
4546
// circular header dependency.
@@ -113,23 +114,24 @@ class BoundaryCondition {
113114
/// @brief Copy constructor
114115
BoundaryCondition(const BoundaryCondition& other);
115116

116-
/// @brief Copy assignment operator
117-
BoundaryCondition& operator=(const BoundaryCondition& other);
117+
/// @brief Unified assignment operator (handles both copy and move)
118+
BoundaryCondition& operator=(BoundaryCondition other);
118119

119120
/// @brief Move constructor
120121
BoundaryCondition(BoundaryCondition&& other) noexcept;
121122

122-
/// @brief Move assignment operator
123-
BoundaryCondition& operator=(BoundaryCondition&& other) noexcept;
124-
125123
/// @brief Virtual destructor
126-
virtual ~BoundaryCondition() = default;
124+
virtual ~BoundaryCondition() noexcept = default;
125+
126+
/// @brief Swap function for copy-and-swap idiom (friend function)
127+
friend void swap(BoundaryCondition& lhs, BoundaryCondition& rhs) noexcept;
127128

128129

129130
/// @brief Get value for a specific array and node
130131
/// @param array_name Name of the array
131132
/// @param node_id Node index on the face
132133
/// @return Value for the array at the specified node
134+
/// @throws std::runtime_error if array_name is not found
133135
double get_value(const std::string& array_name, int node_id) const;
134136

135137
/// @brief Get a boolean flag by name
@@ -144,13 +146,13 @@ class BoundaryCondition {
144146

145147
/// @brief Get global number of nodes
146148
/// @return Global number of nodes on the face
147-
int get_global_num_nodes() const {
149+
int get_global_num_nodes() const noexcept {
148150
return global_num_nodes_;
149151
}
150152

151153
/// @brief Get local number of nodes
152154
/// @return Local number of nodes on the face on this processor
153-
int get_local_num_nodes() const {
155+
int get_local_num_nodes() const noexcept {
154156
return local_num_nodes_;
155157
}
156158

@@ -162,19 +164,19 @@ class BoundaryCondition {
162164

163165
/// @brief Check if data is loaded from VTP file
164166
/// @return true if loaded from VTP, false if using uniform values
165-
bool is_from_vtp() const {
167+
bool is_from_vtp() const noexcept {
166168
return spatially_variable;
167169
}
168170

169171
/// @brief Get the VTP file path (empty if using uniform values)
170172
/// @return VTP file path
171-
const std::string& get_vtp_path() const {
173+
const std::string& get_vtp_path() const noexcept {
172174
return vtp_file_path_;
173175
}
174176

175177
/// @brief Check if this BC is properly defined
176178
/// @return true if BC has been initialized with either VTP data or uniform values
177-
bool is_defined() const {
179+
bool is_defined() const noexcept {
178180
return defined_;
179181
}
180182

0 commit comments

Comments
 (0)