diff --git a/Framework/API/inc/MantidAPI/LogManager.h b/Framework/API/inc/MantidAPI/LogManager.h index 24ba7027c9415e4f32be62b18ead4c1104f10f16..40dafc25508edfa303a227fd3af62974ce278288 100644 --- a/Framework/API/inc/MantidAPI/LogManager.h +++ b/Framework/API/inc/MantidAPI/LogManager.h @@ -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; diff --git a/Framework/API/src/LogManager.cpp b/Framework/API/src/LogManager.cpp index 8519aeffacf47d55cf57f876eaa17d39ac8fac2b..d1d63aca379c7ad982f6fd82047f4410c79f2c58 100644 --- a/Framework/API/src/LogManager.cpp +++ b/Framework/API/src/LogManager.cpp @@ -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 diff --git a/Framework/API/test/LogManagerTest.h b/Framework/API/test/LogManagerTest.h index bd25357b92c3bb51b46810808d373351a6b9a59e..ec4b9deeb8c0ee8141a003f8057ec030cdb6ea7b 100644 --- a/Framework/API/test/LogManagerTest.h +++ b/Framework/API/test/LogManagerTest.h @@ -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)); + } }; //---------------------------------------------------------------------------------------