Skip to content

Commit 1310eac

Browse files
committed
Implement ParsedFEMFunction copy ctor/assignment operator using copy-and-swap idiom
1 parent 00cd55d commit 1310eac

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

include/numerics/parsed_fem_function.h

+23-20
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,26 @@ ParsedFEMFunction<Output>::ParsedFEMFunction (const System & sys,
221221
template <typename Output>
222222
inline
223223
ParsedFEMFunction<Output>::ParsedFEMFunction (const ParsedFEMFunction<Output> & other) :
224-
FEMFunctionBase<Output>(),
225-
_sys(other._sys)
224+
FEMFunctionBase<Output>(other),
225+
_sys(other._sys),
226+
_expression(other._expression),
227+
_subexpressions(other._subexpressions),
228+
_n_vars(other._n_vars),
229+
_n_requested_vars(other._n_requested_vars),
230+
_n_requested_grad_components(other._n_requested_grad_components),
231+
_n_requested_hess_components(other._n_requested_hess_components),
232+
_requested_normals(other._requested_normals),
233+
_spacetime(other._spacetime),
234+
_need_var(other._need_var),
235+
_need_var_grad(other._need_var_grad),
236+
#ifdef LIBMESH_ENABLE_SECOND_DERIVATIVES
237+
_need_var_hess(other._need_var_hess),
238+
#endif // LIBMESH_ENABLE_SECOND_DERIVATIVES
239+
variables(other.variables),
240+
_additional_vars(other._additional_vars),
241+
_initial_vals(other._initial_vals)
226242
{
227-
*this = other;
243+
this->reparse(_expression);
228244
}
229245

230246

@@ -233,25 +249,12 @@ inline
233249
ParsedFEMFunction<Output> &
234250
ParsedFEMFunction<Output>::operator= (const ParsedFEMFunction<Output> & other)
235251
{
252+
// We can only be assigned another ParsedFEMFunction defined on the same System
236253
libmesh_assert(&_sys == &other._sys);
237254

238-
this->_expression = other._expression;
239-
this->_n_vars = other._n_vars;
240-
this->_n_requested_vars = other._n_requested_vars;
241-
this->_n_requested_grad_components = other._n_requested_grad_components;
242-
this->_n_requested_hess_components = other._n_requested_hess_components;
243-
this->_requested_normals = other._requested_normals;
244-
this->_need_var = other._need_var;
245-
this->_need_var_grad = other._need_var_grad;
246-
#ifdef LIBMESH_ENABLE_SECOND_DERIVATIVES
247-
this->_need_var_hess = other._need_var_hess;
248-
#endif // LIBMESH_ENABLE_SECOND_DERIVATIVES
249-
this->_additional_vars = other._additional_vars;
250-
this->_initial_vals = other._initial_vals;
251-
252-
// parsers can be generated from scratch by reparsing expression
253-
this->reparse(_expression);
254-
255+
// Use copy-and-swap idiom
256+
ParsedFEMFunction<Output> tmp(other);
257+
std::swap(tmp, *this);
255258
return *this;
256259
}
257260

0 commit comments

Comments
 (0)