Commit 16e0882e authored by Sam Jenkins's avatar Sam Jenkins
Browse files

Re #25465 Removed raw owning pointers from api

Remove raw owning pointers from API tests
parent e68a5999
......@@ -412,7 +412,8 @@ public:
int axes() const;
virtual Axis *getAxis(const std::size_t &axisIndex) const;
void replaceAxis(const std::size_t &axisIndex, Axis *const newAxis);
void replaceAxis(const std::size_t &axisIndex, std::unique_ptr<Axis> newAxis);
void pythonReplaceAxis(const std::size_t &axisIndex, Axis *newAxis);
/// Will return the number of Axis currently stored in the workspace it is not
/// always safe to assume it is just 2
......@@ -571,7 +572,7 @@ protected:
void updateCachedDetectorGrouping(const size_t index) const override;
/// A vector of pointers to the axes for this workspace
std::vector<Axis *> m_axes;
std::vector<std::unique_ptr<Axis>> m_axes;
private:
std::size_t binIndexOfValue(Mantid::HistogramData::HistogramX const &xValues,
......
......@@ -114,7 +114,7 @@ MatrixWorkspace::MatrixWorkspace(const MatrixWorkspace &other)
m_indexInfo = std::make_unique<Indexing::IndexInfo>(other.indexInfo());
m_axes.resize(other.m_axes.size());
for (size_t i = 0; i < m_axes.size(); ++i)
m_axes[i] = other.m_axes[i]->clone(this);
m_axes[i] = std::unique_ptr<Axis>(other.m_axes[i]->clone(this));
m_isCommonBinsFlagValid.store(other.m_isCommonBinsFlagValid.load());
// TODO: Do we need to init m_monitorWorkspace?
}
......@@ -122,11 +122,7 @@ MatrixWorkspace::MatrixWorkspace(const MatrixWorkspace &other)
/// Destructor
// RJT, 3/10/07: The Analysis Data Service needs to be able to delete
// workspaces, so I moved this from protected to public.
MatrixWorkspace::~MatrixWorkspace() {
for (auto &axis : m_axes) {
delete axis;
}
}
MatrixWorkspace::~MatrixWorkspace() {}
/** Returns a const reference to the IndexInfo object of the workspace.
*
......@@ -406,7 +402,7 @@ void MatrixWorkspace::rebuildSpectraMapping(const bool includeMonitors) {
* VALUE is the Workspace Index
*/
spec2index_map MatrixWorkspace::getSpectrumToWorkspaceIndexMap() const {
SpectraAxis *ax = dynamic_cast<SpectraAxis *>(this->m_axes[1]);
SpectraAxis *ax = dynamic_cast<SpectraAxis *>(this->m_axes[1].get());
if (!ax)
throw std::runtime_error("MatrixWorkspace::getSpectrumToWorkspaceIndexMap: "
"axis[1] is not a SpectraAxis, so I cannot "
......@@ -430,7 +426,7 @@ spec2index_map MatrixWorkspace::getSpectrumToWorkspaceIndexMap() const {
*/
std::vector<size_t>
MatrixWorkspace::getSpectrumToWorkspaceIndexVector(specnum_t &offset) const {
SpectraAxis *ax = dynamic_cast<SpectraAxis *>(this->m_axes[1]);
SpectraAxis *ax = dynamic_cast<SpectraAxis *>(this->m_axes[1].get());
if (!ax)
throw std::runtime_error("MatrixWorkspace::getSpectrumToWorkspaceIndexMap: "
"axis[1] is not a SpectraAxis, so I cannot "
......@@ -886,7 +882,7 @@ double MatrixWorkspace::detectorTwoTheta(const Geometry::IDetector &det) const {
/// @return The number of axes which this workspace has
int MatrixWorkspace::axes() const { return static_cast<int>(m_axes.size()); }
/** Get a pointer to a workspace axis
/** Get a non owning pointer to a workspace axis
* @param axisIndex :: The index of the axis required
* @throw IndexError If the argument given is outside the range of axes held by
* this workspace
......@@ -899,7 +895,7 @@ Axis *MatrixWorkspace::getAxis(const std::size_t &axisIndex) const {
"Argument to getAxis is invalid for this workspace");
}
return m_axes[axisIndex];
return m_axes[axisIndex].get();
}
/** Replaces one of the workspace's axes with the new one provided.
......@@ -912,7 +908,7 @@ Axis *MatrixWorkspace::getAxis(const std::size_t &axisIndex) const {
* (within one of the old one)
*/
void MatrixWorkspace::replaceAxis(const std::size_t &axisIndex,
Axis *const newAxis) {
std::unique_ptr<Axis> newAxis) {
// First check that axisIndex is in range
if (axisIndex >= m_axes.size()) {
throw Kernel::Exception::IndexError(
......@@ -920,7 +916,6 @@ void MatrixWorkspace::replaceAxis(const std::size_t &axisIndex,
"Value of axisIndex is invalid for this workspace");
}
// If we're OK, then delete the old axis and set the pointer to the new one
m_axes[axisIndex] = std::move(newAxis);
}
......
......@@ -134,20 +134,20 @@ void WorkspaceFactoryImpl::initializeFromParent(
continue;
}
const bool isBinEdge =
dynamic_cast<const BinEdgeAxis *const>(parent.m_axes[i]) != nullptr;
dynamic_cast<const BinEdgeAxis *const>(parent.m_axes[i].get()) != nullptr;
const size_t newAxisLength =
child.m_axes[i]->length() + (isBinEdge ? 1 : 0);
const size_t oldAxisLength = parent.m_axes[i]->length();
// Need to delete the existing axis created in init above
delete child.m_axes[i];
child.m_axes[i] = nullptr;
if (newAxisLength == oldAxisLength) {
// Now set to a copy of the parent workspace's axis
child.m_axes[i] = parent.m_axes[i]->clone(&child);
child.m_axes[i] = std::unique_ptr<Axis>(parent.m_axes[i]->clone(&child));
} else {
// Call the 'different length' clone variant
child.m_axes[i] = parent.m_axes[i]->clone(newAxisLength, &child);
child.m_axes[i] = std::unique_ptr<Axis>(parent.m_axes[i]->clone(newAxisLength, &child));
}
}
}
......
......@@ -689,7 +689,7 @@ public:
// this occurs in ConvertToMD, copy methadata
ei.mutableRun().addProperty("W_MATRIX", wTrVector, true);
TS_ASSERT_THROWS_NOTHING(ei.saveExperimentInfoNexus(.get()));
TS_ASSERT_THROWS_NOTHING(ei.saveExperimentInfoNexus(th.file.get()));
th.reopenFile();
......
......@@ -28,7 +28,7 @@ public:
boost::make_shared<WorkspaceTester>();
// Matrix with 4 spectra, 5 bins each
ws->initialize(4, 6, 5);
NumericAxis *ax1 = new NumericAxis(4);
auto ax1 = std::make_unique<NumericAxis>(4);
for (size_t wi = 0; wi < 4; wi++) {
ax1->setValue(wi, static_cast<double>(wi) * 2.0);
for (size_t x = 0; x < 6; x++) {
......@@ -51,7 +51,7 @@ public:
ws->getSpectrum(i).addDetectorID(static_cast<detid_t>(i));
}
ws->setInstrument(inst);
ws->replaceAxis(1, ax1);
ws->replaceAxis(1, std::move(ax1));
return ws;
}
......
......@@ -554,9 +554,10 @@ public:
}
void testReplaceAxis() {
Axis *ax = new SpectraAxis(ws.get());
TS_ASSERT_THROWS(ws->replaceAxis(2, ax), const Exception::IndexError &);
TS_ASSERT_THROWS_NOTHING(ws->replaceAxis(0, ax));
auto ax = std::make_unique<SpectraAxis>(ws.get());
auto ax1 = std::make_unique<SpectraAxis>(ws.get());
TS_ASSERT_THROWS(ws->replaceAxis(2, std::move(ax)), const Exception::IndexError &);
TS_ASSERT_THROWS_NOTHING(ws->replaceAxis(0, std::move(ax1)));
TS_ASSERT(ws->getAxis(0)->isSpectra());
}
......@@ -828,9 +829,8 @@ public:
TS_ASSERT_EQUALS(map.size(), 2);
// Check it throws for non-spectra axis
ws.replaceAxis(1, new NumericAxis(1));
TS_ASSERT_THROWS(ws.getSpectrumToWorkspaceIndexMap(),
const std::runtime_error &);
ws.replaceAxis(1, std::make_unique<NumericAxis>(1));
TS_ASSERT_THROWS(ws.getSpectrumToWorkspaceIndexMap(), const std::runtime_error &);
}
void test_getDetectorIDToWorkspaceIndexMap() {
......@@ -959,7 +959,7 @@ public:
const int nBins = 2;
const int nYValues = 1;
ws.initialize(nVertical, nBins, nYValues);
NumericAxis *verticalAxis = new NumericAxis(nVertical);
auto verticalAxis = std::make_unique<NumericAxis>(nVertical);
for (int i = 0; i < nVertical; ++i) {
for (int j = 0; j < nBins; ++j) {
if (j < nYValues) {
......@@ -970,7 +970,7 @@ public:
}
verticalAxis->setValue(i, double(i)); // Vertical axis increments by 1.
}
ws.replaceAxis(1, verticalAxis);
ws.replaceAxis(1, std::move(verticalAxis));
// Signal is always 1 and volume of each box is 1. Therefore normalized
// signal values by volume should always be 1.
......
......@@ -26,8 +26,8 @@ public:
void test_success() {
auto ws = boost::make_shared<WorkspaceTester>();
ws->initialize(2, 11, 10);
auto newAxis = new NumericAxis(2);
ws->replaceAxis(1, newAxis);
auto newAxis = std::make_unique<NumericAxis>(2);
ws->replaceAxis(1, std::move(newAxis));
NumericAxisValidator validator;
TS_ASSERT_EQUALS(validator.isValid(ws), "");
}
......
......@@ -541,7 +541,7 @@ public:
run1.storeHistogramBinBoundaries(m_test_energy_bins);
run1.saveNexus(th.file, "logs");
run1.saveNexus(th.file.get(), "logs");
th.file->openGroup("logs", "NXgroup");
th.file->makeGroup("junk_to_ignore", "NXmaterial");
th.file->makeGroup("more_junk_to_ignore", "NXsample");
......@@ -549,7 +549,7 @@ public:
// ---- Now re-load the same and compare ------
th.reopenFile();
Run run2;
run2.loadNexus(th.file, "logs");
run2.loadNexus(th.file.get(), "logs");
TS_ASSERT(run2.hasProperty("double_series"));
TS_ASSERT(run2.hasProperty("int_val"));
TS_ASSERT(run2.hasProperty("string_val"));
......@@ -566,7 +566,7 @@ public:
// files)
Run run3;
th.file->openGroup("logs", "NXgroup");
run3.loadNexus(th.file, "");
run3.loadNexus(th.file.get(), "");
TS_ASSERT(run3.hasProperty("double_series"));
TS_ASSERT(run3.hasProperty("int_val"));
TS_ASSERT(run3.hasProperty("string_val"));
......@@ -605,7 +605,7 @@ public:
th.reopenFile();
th.file->openGroup("sample", "NXsample");
Run run3;
run3.loadNexus(th.file, "");
run3.loadNexus(th.file.get(), "");
TS_ASSERT_DELTA(run3.getProtonCharge(), 1.234, 1e-5);
}
......
......@@ -330,11 +330,11 @@ public:
TS_ASSERT(
dynamic_cast<const CSGObject &>(sample.getShape()).getShapeXML() != "");
sample.saveNexus(th.file, "sample");
sample.saveNexus(th.file.get(), "sample");
th.reopenFile();
Sample loaded;
loaded.loadNexus(th.file, "sample");
loaded.loadNexus(th.file.get(), "sample");
TS_ASSERT_EQUALS(loaded.size(), 2);
TS_ASSERT_EQUALS(loaded.getName(), sample.getName());
......@@ -359,11 +359,11 @@ public:
Sample sample;
sample.saveNexus(th.file, "sample");
sample.saveNexus(th.file.get(), "sample");
th.reopenFile();
Sample loaded;
loaded.loadNexus(th.file, "sample");
loaded.loadNexus(th.file.get(), "sample");
TS_ASSERT(loaded.getName().empty());
}
......
......@@ -26,8 +26,8 @@ public:
void test_fail() {
auto ws = boost::make_shared<WorkspaceTester>();
ws->initialize(2, 11, 10);
auto newAxis = new NumericAxis(2);
ws->replaceAxis(1, newAxis);
auto newAxis = std::make_unique<NumericAxis>(2);
ws->replaceAxis(1, std::move(newAxis));
SpectraAxisValidator validator;
TS_ASSERT_EQUALS(
validator.isValid(ws),
......
......@@ -384,7 +384,7 @@ public:
WorkspaceHistory history;
// will throw nothing as it will return with only a warning, no exception
TS_ASSERT_THROWS_NOTHING(history.loadNexus(testfile.file));
TS_ASSERT_THROWS_NOTHING(history.loadNexus(testfile.file.get()));
const auto &histories = history.getAlgorithmHistories();
TS_ASSERT_EQUALS(0, histories.size());
......@@ -400,7 +400,7 @@ public:
NexusTestHelper testfile(true);
failiure_testfile_setup(2, testfile);
WorkspaceHistory history;
TS_ASSERT_THROWS_NOTHING(history.loadNexus(testfile.file));
TS_ASSERT_THROWS_NOTHING(history.loadNexus(testfile.file.get()));
const auto &histories = history.getAlgorithmHistories();
// three will still exist as it doesn't really care about the author
TS_ASSERT_EQUALS(3, histories.size());
......@@ -411,7 +411,7 @@ public:
NexusTestHelper testfile(true);
failiure_testfile_setup(3, testfile);
WorkspaceHistory history;
TS_ASSERT_THROWS_NOTHING(history.loadNexus(testfile.file));
TS_ASSERT_THROWS_NOTHING(history.loadNexus(testfile.file.get()));
const auto &histories = history.getAlgorithmHistories();
// three will still exist as it doesn't really care about the author
......@@ -425,7 +425,7 @@ public:
WorkspaceHistory history;
// this WILL throw as it looks for a data field using
// ::NeXus::File::readData() and it won't be found
TS_ASSERT_THROWS_ANYTHING(history.loadNexus(testfile.file));
TS_ASSERT_THROWS_ANYTHING(history.loadNexus(testfile.file.get()));
const auto &histories = history.getAlgorithmHistories();
// only one will exist as it will throw on the second (wihtout the data) and
// skip the rest
......@@ -438,7 +438,7 @@ public:
failiure_testfile_setup(5, testfile);
WorkspaceHistory history;
// won't throw as it'll simply ignore the bad data
TS_ASSERT_THROWS_NOTHING(history.loadNexus(testfile.file));
TS_ASSERT_THROWS_NOTHING(history.loadNexus(testfile.file.get()));
const auto &histories = history.getAlgorithmHistories();
// only two will exist as it will ignore the second (with only the single
......@@ -453,7 +453,7 @@ public:
WorkspaceHistory history;
// nothign will throw but nothing will be loaded either as the data is
// invalid
TS_ASSERT_THROWS_NOTHING(history.loadNexus(testfile.file));
TS_ASSERT_THROWS_NOTHING(history.loadNexus(testfile.file.get()));
const auto &histories = history.getAlgorithmHistories();
// size should be zero as nothing went in the file
TS_ASSERT_EQUALS(0, histories.size());
......@@ -466,7 +466,7 @@ public:
WorkspaceHistory history;
// this will throw on the second due to the unformatted data - the function
// expects well formatted data
TS_ASSERT_THROWS_ANYTHING(history.loadNexus(testfile.file));
TS_ASSERT_THROWS_ANYTHING(history.loadNexus(testfile.file.get()));
const auto &histories = history.getAlgorithmHistories();
// only one will exist as it will throw on the second (with the bad data)
// and skip the rest
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment