Skip to content

Commit

Permalink
Merge pull request #671 from bam241/shared_ptr
Browse files Browse the repository at this point in the history
Change pointer to shared_ptr
  • Loading branch information
pshriwise authored Apr 30, 2020
2 parents 19d5680 + 3088944 commit 69e699b
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 89 deletions.
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;
}

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;
}

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

0 comments on commit 69e699b

Please sign in to comment.