Skip to content

Commit a31a4f6

Browse files
committed
DofObject::old_dof_object can be a unique_ptr
* Make DofObject destructor default, non-protected. * Cannot use std::make_unique since DofObject copy ctor is private.
1 parent 7ba0889 commit a31a4f6

2 files changed

Lines changed: 16 additions & 41 deletions

File tree

include/base/dof_object.h

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <cstddef>
3333
#include <cstring>
3434
#include <vector>
35+
#include <memory>
3536

3637
namespace libMesh
3738
{
@@ -64,21 +65,20 @@ class DofObject : public ReferenceCountedObject<DofObject>
6465
*/
6566
DofObject ();
6667

68+
public:
69+
6770
/**
68-
* Destructor. Protected so that you can't destroy one of these
69-
* except as a part of a Node or Elem.
71+
* Destructor.
7072
*/
71-
~DofObject ();
72-
73-
public:
73+
~DofObject () = default;
7474

7575
#ifdef LIBMESH_ENABLE_AMR
7676

7777
/**
7878
* This object on the last mesh. Useful for projecting
7979
* solutions from one mesh to another.
8080
*/
81-
DofObject * old_dof_object;
81+
std::unique_ptr<DofObject> old_dof_object;
8282

8383
/**
8484
* Sets the \p old_dof_object to nullptr
@@ -676,9 +676,6 @@ class DofObject : public ReferenceCountedObject<DofObject>
676676
// Inline functions
677677
inline
678678
DofObject::DofObject () :
679-
#ifdef LIBMESH_ENABLE_AMR
680-
old_dof_object(nullptr),
681-
#endif
682679
#ifdef LIBMESH_ENABLE_UNIQUE_ID
683680
_unique_id (invalid_unique_id),
684681
#endif
@@ -692,18 +689,6 @@ DofObject::DofObject () :
692689

693690

694691

695-
inline
696-
DofObject::~DofObject ()
697-
{
698-
// Free all memory.
699-
#ifdef LIBMESH_ENABLE_AMR
700-
this->clear_old_dof_object ();
701-
#endif
702-
this->clear_dofs ();
703-
}
704-
705-
706-
707692
inline
708693
void DofObject::invalidate_dofs (const unsigned int sys_num)
709694
{

src/base/dof_object.C

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,12 @@
1717

1818

1919

20-
// C++ includes
21-
2220
// Local includes
2321
#include "libmesh/dof_object.h"
2422

25-
2623
namespace libMesh
2724
{
2825

29-
30-
3126
// ------------------------------------------------------------
3227
// DofObject class static member -now initialized in header
3328
const dof_id_type DofObject::invalid_id;
@@ -36,14 +31,9 @@ const processor_id_type DofObject::invalid_processor_id;
3631

3732

3833

39-
// ------------------------------------------------------------
40-
// DofObject class members
4134
// Copy Constructor
4235
DofObject::DofObject (const DofObject & dof_obj) :
4336
ReferenceCountedObject<DofObject>(),
44-
#ifdef LIBMESH_ENABLE_AMR
45-
old_dof_object (nullptr),
46-
#endif
4737
#ifdef LIBMESH_ENABLE_UNIQUE_ID
4838
_unique_id (dof_obj._unique_id),
4939
#endif
@@ -56,9 +46,6 @@ DofObject::DofObject (const DofObject & dof_obj) :
5646
// is intended to be used solely for internal construction of
5747
// old_dof_object, never for a true deep copy where the newly
5848
// created object really matches the source object.
59-
//
60-
// if (dof_obj.old_dof_object)
61-
// this->old_dof_object = new DofObject(*(dof_obj.old_dof_object));
6249

6350
// Check that everything worked
6451
#ifdef DEBUG
@@ -95,7 +82,10 @@ DofObject & DofObject::operator= (const DofObject & dof_obj)
9582
#ifdef LIBMESH_ENABLE_AMR
9683
this->clear_old_dof_object();
9784

98-
this->old_dof_object = new DofObject(*(dof_obj.old_dof_object));
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));
9989
#endif
10090

10191
_id = dof_obj._id;
@@ -141,8 +131,7 @@ DofObject & DofObject::operator= (const DofObject & dof_obj)
141131

142132
void DofObject::clear_old_dof_object ()
143133
{
144-
delete this->old_dof_object;
145-
this->old_dof_object = nullptr;
134+
this->old_dof_object.reset(nullptr);
146135
}
147136

148137

@@ -153,9 +142,10 @@ void DofObject::set_old_dof_object ()
153142

154143
libmesh_assert (!this->old_dof_object);
155144

156-
// Make a new DofObject, assign a copy of \p this.
157-
// Make sure the copy ctor for DofObject works!!
158-
this->old_dof_object = new DofObject(*this);
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));
159149
}
160150

161151
#endif
@@ -644,7 +634,7 @@ void DofObject::unpack_indexing(std::vector<largest_id_type>::const_iterator beg
644634
#ifdef LIBMESH_ENABLE_AMR
645635
if (has_old_dof_object)
646636
{
647-
this->old_dof_object = new DofObject();
637+
this->old_dof_object = std::unique_ptr<DofObject>(new DofObject());
648638
this->old_dof_object->unpack_indexing(begin+size);
649639
}
650640
#endif

0 commit comments

Comments
 (0)