Skip to content

Commit 9174204

Browse files
committed
Add/use DofObject::create() helper function
1 parent a31a4f6 commit 9174204

2 files changed

Lines changed: 25 additions & 10 deletions

File tree

include/base/dof_object.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,17 @@ class DofObject : public ReferenceCountedObject<DofObject>
501501
*/
502502
DofObject (const DofObject &);
503503

504+
/**
505+
* Convenient factory function that calls either the (deep) copy
506+
* constructor or the default constructor depending on the input
507+
* arg. Like the copy constructor, this function is also private. We
508+
* can't use std::make_unique to construct a DofObject since the
509+
* copy constructor is private, but we can at least encapsulate the
510+
* code which calls "new" directly.
511+
*/
512+
std::unique_ptr<DofObject>
513+
construct(const DofObject * other = nullptr);
514+
504515
/**
505516
* Deep-copying assignment operator
506517
*/
@@ -687,6 +698,15 @@ DofObject::DofObject () :
687698

688699

689700

701+
inline
702+
std::unique_ptr<DofObject>
703+
DofObject::construct(const DofObject * other)
704+
{
705+
return other
706+
? std::unique_ptr<DofObject>(new DofObject(*other))
707+
: std::unique_ptr<DofObject>(new DofObject());
708+
}
709+
690710

691711

692712
inline

src/base/dof_object.C

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,7 @@ DofObject & DofObject::operator= (const DofObject & dof_obj)
8181

8282
#ifdef LIBMESH_ENABLE_AMR
8383
this->clear_old_dof_object();
84-
85-
// Note: we can't use std::make_unique here because the DofObject
86-
// copy constructor is private, and std::make_unique effectively
87-
// calls "placement new" which requires a public constructor.
88-
this->old_dof_object = std::unique_ptr<DofObject>(new DofObject(*dof_obj.old_dof_object));
84+
this->old_dof_object = this->construct(dof_obj.old_dof_object.get());
8985
#endif
9086

9187
_id = dof_obj._id;
@@ -142,10 +138,9 @@ void DofObject::set_old_dof_object ()
142138

143139
libmesh_assert (!this->old_dof_object);
144140

145-
// Note: we can't use std::make_unique here because the DofObject
146-
// copy constructor is private, and std::make_unique effectively
147-
// calls "placement new" which requires a public constructor.
148-
this->old_dof_object = std::unique_ptr<DofObject>(new DofObject(*this));
141+
// Make a new DofObject, assign a copy of \p this.
142+
// Make sure the copy ctor for DofObject works!!
143+
this->old_dof_object = this->construct(this);
149144
}
150145

151146
#endif
@@ -634,7 +629,7 @@ void DofObject::unpack_indexing(std::vector<largest_id_type>::const_iterator beg
634629
#ifdef LIBMESH_ENABLE_AMR
635630
if (has_old_dof_object)
636631
{
637-
this->old_dof_object = std::unique_ptr<DofObject>(new DofObject());
632+
this->old_dof_object = this->construct();
638633
this->old_dof_object->unpack_indexing(begin+size);
639634
}
640635
#endif

0 commit comments

Comments
 (0)