Commit 0462a276 authored by Ian Bush's avatar Ian Bush
Browse files

Refs #16928 Added method for getting a single integer value

parent f89b4f1e
......@@ -117,6 +117,8 @@ public:
double getPropertyAsSingleValue(
const std::string &name,
Kernel::Math::StatisticType statistic = Kernel::Math::Mean) const;
/// Returns a property as an integer value
int getPropertyAsIntegerValue(const std::string &name) const;
/// Returns the named property as a pointer
Kernel::Property *getProperty(const std::string &name) const;
......
......@@ -4,14 +4,8 @@
#include "MantidAPI/LogManager.h"
#include "MantidKernel/PropertyNexus.h"
#include "MantidKernel/DateAndTime.h"
#include "MantidKernel/TimeSplitter.h"
#include "MantidKernel/TimeSeriesProperty.h"
#include <nexus/NeXusFile.hpp>
#include <algorithm>
namespace Mantid {
namespace API {
......@@ -370,6 +364,33 @@ double LogManager::getPropertyAsSingleValue(
return singleValue;
}
/**
* Returns a property as a n integer, if the underlying value is an integer.
* Throws otherwise.
* @param name :: The name of the property
* @return A single integer value
* @throws std::invalid_argument if property is not an integer type
*/
int LogManager::getPropertyAsIntegerValue(const std::string &name) const {
int singleValue(0);
double discard(0);
Property *prop = getProperty(name);
if (convertSingleValue<int32_t>(prop, discard) ||
convertSingleValue<int64_t>(prop, discard) ||
convertSingleValue<uint32_t>(prop, discard) ||
convertSingleValue<uint64_t>(prop, discard)) {
singleValue = std::stoi(prop->value());
} else {
throw std::invalid_argument("Run::getPropertyAsIntegerValue - Property \"" +
name +
"\" cannot be converted to an integer value.");
}
return singleValue;
}
/**
* Get a pointer to a property by name
* @param name :: The name of a property, throws an Exception::NotFoundError if
......
......@@ -264,6 +264,36 @@ public:
TS_ASSERT_DELTA(1.0, result, 1e-12);
}
void test_GetPropertyAsIntegerValue_SingleValue_Int32Type() {
doTest_GetPropertyAsIntegerValue<int32_t>(1);
}
void test_GetPropertyAsIntegerValue_SingleValue_Int64Type() {
doTest_GetPropertyAsIntegerValue<int64_t>(1L);
}
void test_GetPropertyAsIntegerValue_SingleValue_Uint32Type() {
doTest_GetPropertyAsIntegerValue<uint32_t>(1U);
}
void test_GetPropertyAsIntegerValue_SingleValue_Uint64Type() {
doTest_GetPropertyAsIntegerValue<uint64_t>(1UL);
}
void test_GetPropertyAsSingleInteger_DoubleType_Throws() {
LogManager runInfo;
const std::string name = "T_prop";
runInfo.addProperty<double>(name, 1.0);
TS_ASSERT_THROWS(runInfo.getPropertyAsIntegerValue(name),
std::invalid_argument);
}
void test_GetPropertyAsSingleInteger_Throws_for_nonexistant_property() {
LogManager runInfo;
TS_ASSERT_THROWS(runInfo.getPropertyAsIntegerValue("T_prop"),
Exception::NotFoundError);
}
void test_GetPropertyAsSingleValue_TimeSeries_DoubleType() {
doTest_GetPropertyAsSingleValue_TimeSeriesType<double>();
}
......@@ -502,6 +532,16 @@ private:
runInfo.getPropertyAsSingleValue(name, Mantid::Kernel::Math::Mean),
expectedValue, 1e-12);
}
template <typename T> void doTest_GetPropertyAsIntegerValue(const T value) {
LogManager runInfo;
const std::string name = "T_prop";
runInfo.addProperty<T>(name, value);
int result(-1);
result = runInfo.getPropertyAsIntegerValue(name);
TS_ASSERT_THROWS_NOTHING(result = runInfo.getPropertyAsIntegerValue(name));
TS_ASSERT_EQUALS(value, static_cast<T>(result));
}
};
//---------------------------------------------------------------------------------------
......
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