Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change pointer to shared_ptr #671

Merged
merged 40 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
2808dc5
change point to uniq/shared ptr
bam241 Apr 7, 2020
a2d6f24
turned MBI into a unique_ptr
bam241 Apr 7, 2020
c5a954b
firace add c++ 11
bam241 Apr 7, 2020
b93dc9b
no need to force it
bam241 Apr 7, 2020
ab729dc
switching from unique to shared
bam241 Apr 7, 2020
d483256
forcing c++11
bam241 Apr 7, 2020
9d955aa
Add indent
bam241 Apr 7, 2020
f31eb12
removing it ?
bam241 Apr 7, 2020
2f17c8c
revseting the DAMC_macro
bam241 Apr 7, 2020
967353b
empty commit
bam241 Apr 7, 2020
67ffa6c
foircing back c++11
bam241 Apr 7, 2020
b78c085
add c++11 in test
bam241 Apr 7, 2020
e415a8f
convert from pointer to shared_ptr...
bam241 Apr 8, 2020
c147bd0
revert to std c++11 cmake setup
bam241 Apr 8, 2020
70de322
revert to force c++11
bam241 Apr 8, 2020
02c2a98
readd previous option
bam241 Apr 8, 2020
8332b18
and now ?
bam241 Apr 8, 2020
b4b3029
this is clean
bam241 Apr 8, 2020
2c48814
adding news
bam241 Apr 9, 2020
51a5048
Update src/dagmc/DagMC.cpp
bam241 Apr 9, 2020
4142a7f
Apply suggestions from code review
bam241 Apr 9, 2020
d9548da
Apply suggestions from code review
bam241 Apr 9, 2020
d559e51
add a deprecated constructor
bam241 Apr 10, 2020
dee0d47
vifx news name
bam241 Apr 10, 2020
5339237
adding new getter for share_ptr, and deprecate old ones
bam241 Apr 10, 2020
73b5eaf
changed test to shape_ptr
bam241 Apr 10, 2020
e5204af
change raw pointer for all shared_ptr in dagmc test
bam241 Apr 10, 2020
8e9a2bd
fixing style
bam241 Apr 10, 2020
075cec7
remove unnecessary includes
bam241 Apr 10, 2020
5715833
update news
bam241 Apr 10, 2020
2dd8c83
Update src/dagmc/DagMC.cpp
bam241 Apr 10, 2020
ff99edc
do not return shared_ptr anymore
bam241 Apr 10, 2020
407c83d
change to unique ptr
bam241 Apr 13, 2020
cb79661
attempt to fix unique_ptr
bam241 Apr 13, 2020
53f5110
thx @pshriwise for solving that one !
bam241 Apr 13, 2020
59dbae4
now return a shared_ptre for GTT and allows to retrieve MBI shared_ptr
bam241 Apr 16, 2020
96e9aca
set MBI_shared_ptr ass a nullptr when MBI passed as raw pointer
bam241 Apr 16, 2020
24317b0
return the correct MBI version
bam241 Apr 16, 2020
c7e7a8c
reorder incldues, add runtime_error when trying to return MBI as a sh…
bam241 Apr 27, 2020
3088944
update news details
bam241 Apr 27, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmake/test_config/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ project(Test_DAGMC_config)
cmake_minimum_required(VERSION 2.8)

set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR})
set(CMAKE_CXX_STANDARD 11)

find_package(DAGMC REQUIRED)

Expand Down
23 changes: 23 additions & 0 deletions news/PR-0671.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:** None

**Changed:**
- DagMC: updated pointer management to RAII ("Resource Allocation Is
Initialization") technique:
- MBI is now a shared_ptr unless passed as a raw pointer in the DagMC
constructor (can be returned as a shared_ptr if not provided as a raw
pointer)
- GTT is now a shared_ptr, and can only be returned as such
- GQT is now a uniq_ptr, (and can't be return - not change there)
- DagMC tests:
- DagMC instance is now a shared_ptr
- when used MBI instance is now a shared_ptr

**Deprecated:**
- DagMC: Deprecated constructor using a raw pointer for the MBI instance,
prefered way uses shared_ptr for MBI instance.

**Removed:** None

**Fixed:** None

**Security:** None
29 changes: 20 additions & 9 deletions src/dagmc/DagMC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,36 +39,47 @@ const bool counting = false; /* controls counts of ray casts and pt_in_vols */
const std::map<std::string, std::string> DagMC::no_synonyms;

// DagMC Constructor
DagMC::DagMC(Interface* mb_impl, double overlap_tolerance, double p_numerical_precision) {
DagMC::DagMC(std::shared_ptr<moab::Interface> mb_impl, double overlap_tolerance, double p_numerical_precision) {
moab_instance_created = false;
// if we arent handed a moab instance create one
if (NULL == mb_impl) {
mb_impl = new moab::Core();
if (nullptr == mb_impl) {
mb_impl = std::make_shared<Core>();
moab_instance_created = true;
}

MBI_shared_ptr = mb_impl;
// set the internal moab pointer
MBI = MBI_shared_ptr.get();

// make new GeomTopoTool and GeomQueryTool
GTT = std::make_shared<GeomTopoTool> (MBI, false);
GQT = std::unique_ptr<GeomQueryTool>(new GeomQueryTool(GTT.get(), overlap_tolerance, p_numerical_precision));

// This is the correct place to uniquely define default values for the dagmc settings
defaultFacetingTolerance = .001;
}

DagMC::DagMC(Interface* mb_impl, double overlap_tolerance, double p_numerical_precision) {
moab_instance_created = false;
// set the internal moab pointer
MBI = mb_impl;
MBI_shared_ptr = nullptr;

// make new GeomTopoTool and GeomQueryTool
GTT = new moab::GeomTopoTool(MBI, false);
GQT = new moab::GeomQueryTool(GTT, overlap_tolerance, p_numerical_precision);
GTT = std::make_shared<GeomTopoTool> (MBI, false);
GQT = std::unique_ptr<GeomQueryTool>(new GeomQueryTool(GTT.get(), overlap_tolerance, p_numerical_precision));

// This is the correct place to uniquely define default values for the dagmc settings
defaultFacetingTolerance = .001;
}

// Destructor
DagMC::~DagMC() {
// delete the GeomTopoTool and GeomQueryTool
delete GTT;
delete GQT;

// if we created the moab instance
// clear it
if (moab_instance_created) {
MBI->delete_mesh();
delete MBI;
}
}

Expand Down
26 changes: 20 additions & 6 deletions src/dagmc/DagMC.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
#include "moab/GeomQueryTool.hpp"
#include "DagMCVersion.hpp"

#include <vector>
#include <assert.h>
#include <map>
#include <memory>
#include <stdexcept>
#include <string>
#include <assert.h>
#include <vector>

class RefEntity;

Expand Down Expand Up @@ -57,7 +59,10 @@ class CartVect;
class DagMC {
public:
// Constructor
DagMC(Interface* mb_impl = NULL, double overlap_tolerance = 0., double numerical_precision = .001);
DagMC(std::shared_ptr<Interface> mb_impl = nullptr, double overlap_tolerance = 0., double numerical_precision = .001);
// Deprecated Constructor
[[ deprecated("Replaced by DagMC(std::shared_ptr<Interface> mb_impl, ... )") ]]
DagMC(Interface* mb_impl, double overlap_tolerance = 0., double numerical_precision = .001);
// Destructor
~DagMC();

Expand Down Expand Up @@ -365,7 +370,7 @@ class DagMC {
public:
OrientedBoxTreeTool* obb_tree() {return GTT->obb_tree();}

GeomTopoTool* geom_tool() {return GTT;}
std::shared_ptr<GeomTopoTool> geom_tool() {return GTT;}

ErrorCode write_mesh(const char* ffile,
const int flen);
Expand All @@ -382,16 +387,25 @@ class DagMC {

/** Get the instance of MOAB used by functions in this file. */
Interface* moab_instance() {return MBI;}
std::shared_ptr<Interface> moab_instance_sptr() {
if (nullptr == MBI_shared_ptr)
std::runtime_error("MBI instance is not defined as a shared pointer !");
return MBI_shared_ptr;
}
Comment on lines +390 to +394
Copy link
Member Author

@bam241 bam241 Apr 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a runtime_error when trying to return the MBI instance as a shared_ptr when it has been provided as a direct pointer. (It has been implemented in the Header, as it is fairly simple, I can move it to the cpp file if you prefer)


private:

/* PRIVATE MEMBER DATA */

// Shared_ptr owning *MBI (if allocated internally)
std::shared_ptr<Interface> MBI_shared_ptr;
// Use for the call to MOAB interface, should never be deleted in the DagMC instanced
// MBI is either externally owned or owned by the MBI_shared_ptr
Interface* MBI;
bool moab_instance_created;

GeomTopoTool* GTT;
GeomQueryTool* GQT;
std::shared_ptr<GeomTopoTool> GTT;
std::unique_ptr<GeomQueryTool> GQT;

public:
Tag nameTag, facetingTolTag;
Expand Down
8 changes: 3 additions & 5 deletions src/dagmc/tests/dagmc_pointinvol_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,23 @@ using namespace moab;

using moab::DagMC;

moab::DagMC* DAG;
std::shared_ptr<moab::DagMC> DAG;

static const char input_file[] = "test_geom.h5m";

class DagmcPointInVolTest : public ::testing::Test {
protected:
virtual void SetUp() {
// Create new DAGMC instance
DAG = new DagMC();
DAG = std::make_shared<moab::DagMC>();
// Load mesh from file
rloadval = DAG->load_file(input_file);
assert(rloadval == moab::MB_SUCCESS);
// Create the OBB
rval = DAG->init_OBBTree();
assert(rval == moab::MB_SUCCESS);
}
virtual void TearDown() {
delete DAG;
}
virtual void TearDown() {}
protected:
moab::ErrorCode rloadval;
moab::ErrorCode rval;
Expand Down
8 changes: 3 additions & 5 deletions src/dagmc/tests/dagmc_rayfire_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using namespace moab;

using moab::DagMC;

moab::DagMC* DAG;
std::shared_ptr<moab::DagMC> DAG;

static const char input_file[] = "test_geom.h5m";
double eps = 1.0e-6;
Expand All @@ -20,17 +20,15 @@ class DagmcRayFireTest : public ::testing::Test {
protected:
virtual void SetUp() {
// Create new DAGMC instance
DAG = new DagMC();
DAG = std::make_shared<moab::DagMC>();
// Load mesh from file
rloadval = DAG->load_file(input_file);
assert(rloadval == moab::MB_SUCCESS);
// Create the OBB
rval = DAG->init_OBBTree();
assert(rval == moab::MB_SUCCESS);
}
virtual void TearDown() {
delete DAG;
}
virtual void TearDown() {}
protected:
moab::ErrorCode rloadval;
moab::ErrorCode rval;
Expand Down
54 changes: 15 additions & 39 deletions src/dagmc/tests/dagmc_simple_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,106 +29,90 @@ TEST_F(DagmcSimpleTest, dagmc_load_file) {
TEST_F(DagmcSimpleTest, dagmc_load_file_dagmc) {
/* 1 - Test with external moab, load file in DAGMC*/
// make new moab core
Core* mbi = new moab::Core();
std::shared_ptr<Interface> mbi = std::make_shared<Core>();
// make new dagmc into that moab
DagMC* dagmc = new moab::DagMC(mbi);
std::shared_ptr<DagMC> dagmc = std::make_shared<DagMC>(mbi);

ErrorCode rval;

// load a file
rval = dagmc->load_file(input_file);
EXPECT_EQ(rval, MB_SUCCESS);

// delete dagmc
delete dagmc;
delete mbi;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

}

TEST_F(DagmcSimpleTest, dagmc_load_file_dagmc_via_moab) {
/* 2 - Test with external moab, load file in MOAB*/
// load the file into moab rather than dagmc
ErrorCode rval;

moab::Core* mbi = new moab::Core();
std::shared_ptr<Interface> mbi = std::make_shared<Core>();
rval = mbi->load_file(input_file);
EXPECT_EQ(rval, MB_SUCCESS);
moab::DagMC* dagmc = new moab::DagMC(mbi);
std::shared_ptr<DagMC> dagmc = std::make_shared<DagMC>(mbi);
rval = dagmc->load_existing_contents();
EXPECT_EQ(rval, MB_SUCCESS);

// delete dagmc;
delete dagmc;
delete mbi;
}

TEST_F(DagmcSimpleTest, dagmc_load_file_dagmc_internal) {
/* 3 - Test with internal moab, load file in DAG*/
// make new dagmc into that moab
ErrorCode rval;

moab::DagMC* dagmc = new moab::DagMC();
std::shared_ptr<DagMC> dagmc = std::make_shared<DagMC>();
// load a file
rval = dagmc->load_file(input_file);
EXPECT_EQ(rval, MB_SUCCESS);
delete dagmc;
}

TEST_F(DagmcSimpleTest, dagmc_load_file_dagmc_build_obb) {
/* 1 - Test with external moab, load file in DAGMC*/
// make new moab core
ErrorCode rval;

moab::Core* mbi = new moab::Core();
std::shared_ptr<Interface> mbi = std::make_shared<Core>();
// make new dagmc into that moab
DagMC* dagmc = new moab::DagMC(mbi);
std::shared_ptr<DagMC> dagmc = std::make_shared<DagMC>(mbi);

// load a file
rval = dagmc->load_file(input_file);
EXPECT_EQ(rval, MB_SUCCESS);
rval = dagmc->init_OBBTree();
EXPECT_EQ(rval, MB_SUCCESS);
// delete dagmc
delete dagmc;
delete mbi;
}

TEST_F(DagmcSimpleTest, dagmc_load_file_dagmc_via_moab_build_obb) {
/* 2 - Test with external moab, load file in MOAB*/
// load the file into moab rather than dagmc
ErrorCode rval;

moab::Core* mbi = new moab::Core();
std::shared_ptr<Interface> mbi = std::make_shared<Core>();
rval = mbi->load_file(input_file);
EXPECT_EQ(rval, MB_SUCCESS);
moab::DagMC* dagmc = new moab::DagMC(mbi);
std::shared_ptr<DagMC> dagmc = std::make_shared<DagMC>(mbi);
rval = dagmc->load_existing_contents();
EXPECT_EQ(rval, MB_SUCCESS);
rval = dagmc->init_OBBTree();
EXPECT_EQ(rval, MB_SUCCESS);

// delete dagmc;
delete dagmc;
delete mbi;
}

TEST_F(DagmcSimpleTest, dagmc_load_file_dagmc_internal_build_obb) {
/* 3 - Test with internal moab, load file in DAG*/
// make new dagmc into that moab
ErrorCode rval;

moab::DagMC* dagmc = new moab::DagMC();
std::shared_ptr<DagMC> dagmc = std::make_shared<DagMC>();
// load a file
rval = dagmc->load_file(input_file);
EXPECT_EQ(rval, MB_SUCCESS);
rval = dagmc->init_OBBTree();
EXPECT_EQ(rval, MB_SUCCESS);
delete dagmc;
}

TEST_F(DagmcSimpleTest, dagmc_test_obb_retreval) {
// make new dagmc
std::cout << "test_obb_retreval" << std::endl;

DagMC* dagmc = new moab::DagMC();
std::shared_ptr<DagMC> dagmc = std::make_shared<DagMC>();

ErrorCode rval;
// load a file
Expand All @@ -140,18 +124,14 @@ TEST_F(DagmcSimpleTest, dagmc_test_obb_retreval) {
// write the file
rval = dagmc->write_mesh("fcad", 4);

// now remove the dagmc instance a
delete dagmc;

dagmc = new moab::DagMC();
dagmc.reset(new DagMC());
rval = dagmc->load_file("fcad");
EXPECT_EQ(rval, MB_SUCCESS);
rval = dagmc->init_OBBTree();
EXPECT_EQ(rval, MB_SUCCESS);

// delete the fcad file
remove("fcad");
delete dagmc;
}

TEST_F(DagmcSimpleTest, dagmc_build_obb) {
Expand Down Expand Up @@ -180,7 +160,7 @@ TEST_F(DagmcSimpleTest, dagmc_test_obb_retreval_rayfire) {
// make new dagmc
std::cout << "test_obb_retreval and ray_fire" << std::endl;

DagMC* dagmc = new moab::DagMC();
std::shared_ptr<DagMC> dagmc = std::make_shared<DagMC>();

ErrorCode rval;
// load a file
Expand All @@ -192,11 +172,8 @@ TEST_F(DagmcSimpleTest, dagmc_test_obb_retreval_rayfire) {
// write the file
rval = dagmc->write_mesh("fcad", 4);

// now remove the dagmc instance
delete dagmc;

// now create new DAGMC
dagmc = new moab::DagMC();
dagmc.reset(new DagMC());
rval = dagmc->load_file("fcad");
EXPECT_EQ(rval, MB_SUCCESS);
rval = dagmc->init_OBBTree();
Expand All @@ -220,7 +197,6 @@ TEST_F(DagmcSimpleTest, dagmc_test_obb_retreval_rayfire) {
rval = DAG->ray_fire(vol_h, xyz, dir, next_surf, next_surf_dist);
EXPECT_EQ(rval, MB_SUCCESS);
EXPECT_NEAR(expect_next_surf_dist, next_surf_dist, eps);
delete dagmc;
}

TEST_F(DagmcSimpleTest, dagmc_rayfire) {
Expand Down
Loading