From 0462a276fec2aeeca1c45a6e6819a7816a6087e3 Mon Sep 17 00:00:00 2001 From: Ian Bush <bush@ill.fr> Date: Fri, 12 Aug 2016 15:20:51 +0200 Subject: [PATCH] Refs #16928 Added method for getting a single integer value --- Framework/API/inc/MantidAPI/LogManager.h | 2 ++ Framework/API/src/LogManager.cpp | 33 +++++++++++++++---- Framework/API/test/LogManagerTest.h | 40 ++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/Framework/API/inc/MantidAPI/LogManager.h b/Framework/API/inc/MantidAPI/LogManager.h index 24ba7027c94..40dafc25508 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 8519aeffacf..d1d63aca379 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 bd25357b92c..ec4b9deeb8c 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)); + } }; //--------------------------------------------------------------------------------------- -- GitLab