diff --git a/Code/Mantid/Framework/API/CMakeLists.txt b/Code/Mantid/Framework/API/CMakeLists.txt index e178843e809b..f8356b6b2c05 100644 --- a/Code/Mantid/Framework/API/CMakeLists.txt +++ b/Code/Mantid/Framework/API/CMakeLists.txt @@ -106,6 +106,7 @@ set ( SRC_FILES src/WorkspaceFactory.cpp src/WorkspaceGroup.cpp src/WorkspaceHistory.cpp + src/WorkspaceListProperty.cpp src/WorkspaceOpOverloads.cpp src/WorkspaceProperty.cpp ) @@ -259,6 +260,7 @@ set ( INC_FILES inc/MantidAPI/WorkspaceHistory.h inc/MantidAPI/WorkspaceIterator.h inc/MantidAPI/WorkspaceIteratorCode.h + inc/MantidAPI/WorkspaceListProperty.h inc/MantidAPI/WorkspaceOpOverloads.h inc/MantidAPI/WorkspaceProperty.h inc/MantidAPI/WorkspaceValidators.h @@ -288,9 +290,9 @@ set ( TEST_FILES FilePropertyTest.h FrameworkManagerTest.h FuncMinimizerFactoryTest.h - FunctionAttributeTest.h - FunctionDomainTest.h - FunctionFactoryTest.h + FunctionAttributeTest.h + FunctionDomainTest.h + FunctionFactoryTest.h FunctionPropertyTest.h FunctionTest.h FunctionValuesTest.h @@ -305,7 +307,7 @@ set ( TEST_FILES IncreasingAxisValidatorTest.h InstrumentDataServiceTest.h LiveListenerFactoryTest.h - LogManagerTest.h + LogManagerTest.h MDGeometryTest.h MatrixWorkspaceMDIteratorTest.h MemoryManagerTest.h @@ -331,6 +333,7 @@ set ( TEST_FILES WorkspaceFactoryTest.h WorkspaceGroupTest.h WorkspaceHistoryTest.h + WorkspaceListPropertyTest.h WorkspaceOpOverloadsTest.h WorkspacePropertyTest.h ) diff --git a/Code/Mantid/Framework/API/inc/MantidAPI/WorkspaceListProperty.h b/Code/Mantid/Framework/API/inc/MantidAPI/WorkspaceListProperty.h new file mode 100644 index 000000000000..53dc49103aa8 --- /dev/null +++ b/Code/Mantid/Framework/API/inc/MantidAPI/WorkspaceListProperty.h @@ -0,0 +1,129 @@ +#ifndef MANTID_API_WORKSPACELISTPROPERTY_H_ +#define MANTID_API_WORKSPACELISTPROPERTY_H_ + +#include +#include +#include +#include "MantidKernel/System.h" +#include "MantidAPI/WorkspaceProperty.h" + + +namespace Mantid +{ +namespace API +{ + + /** WorkspaceListProperty : Property with value that constrains the contents to be a list of workspaces of a single type. + + Copyright © 2013 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory + + This file is part of Mantid. + + Mantid is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + Mantid is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + File change history is stored at: + Code Documentation is available at: + */ + template + class DLLExport WorkspaceListProperty : public Mantid::Kernel::PropertyWithValue > > + { + public: + + /// Typedef the value type of this property with value. + typedef std::vector > WorkspacePropertyListType; + + /** + * Helper method to split a comma separated string into a vector of strings. + */ + std::vector namesToVector(std::string names) + { + names.erase(std::remove_if(names.begin(), names.end(), (int(*)(int))std::isspace), names.end()); + std::vector splitNames; + boost::split(splitNames, names, boost::is_any_of(",")); + return splitNames; + } + + /** Constructor. + * Sets the property and workspace names but initialises the workspace pointers to null. + * @param name :: The name to assign to the property + * @param wsNames :: The names of the workspaces + * @param direction :: Whether this is a Direction::Input, Direction::Output or Direction::InOut (Input & Output) workspace + * @param validator :: The (optional) validator to use for this property + * @throw std::out_of_range if the direction argument is not a member of the Direction enum (i.e. 0-2) + */ + explicit WorkspaceListProperty( const std::string &name, const std::string &wsNames, const unsigned int direction, + Mantid::Kernel::IValidator_sptr validator = Mantid::Kernel::IValidator_sptr(new Kernel::NullValidator)) : + Mantid::Kernel::PropertyWithValue( name, WorkspacePropertyListType(0), validator, direction ), + m_optional(PropertyMode::Mandatory), m_workspaceNames(0) + // ,m_workspaceName( wsName ), m_initialWSName( wsName ), m_optional(PropertyMode::Mandatory), m_locking(LockMode::Lock) + { + m_workspaceNames = namesToVector(wsNames); + } + + /** Constructor. + * Sets the property and workspace names but initialises the workspace pointers to null. + * @param name :: The name to assign to the property + * @param wsNames :: The name of the workspace + * @param direction :: Whether this is a Direction::Input, Direction::Output or Direction::InOut (Input & Output) workspace + * @param optional :: Flag to determine whether this property is optional or not. + * @param validator :: The (optional) validator to use for this property + * @throw std::out_of_range if the direction argument is not a member of the Direction enum (i.e. 0-2) + */ + explicit WorkspaceListProperty( const std::string &name, const std::string &wsNames, const unsigned int direction, const PropertyMode::Type optional, + Mantid::Kernel::IValidator_sptr validator = Mantid::Kernel::IValidator_sptr(new Kernel::NullValidator)) : + Mantid::Kernel::PropertyWithValue( name, WorkspacePropertyListType(0), validator, direction ), + m_optional(optional), m_workspaceNames(0) + { + m_workspaceNames = namesToVector(wsNames); + } + + + /// Copy constructor, the default name stored in the new object is the same as the default name from the original object + WorkspaceListProperty( const WorkspaceListProperty& right ) : + Kernel::PropertyWithValue< WorkspacePropertyListType >( right ), + m_optional(right.m_optional) + { + } + + /// Is the input workspace property optional? + virtual bool isOptional() const + { + return m_optional == PropertyMode::Optional; + } + + /** + * Getter for the workspace names. + */ + std::vector getWorkspaceNames() const + { + return m_workspaceNames; + } + + virtual ~WorkspaceListProperty() + { + } + + private: + /// Flag indicating whether the type is optional or not. + PropertyMode::Type m_optional; + /// Keys to the workspaces in the ADS + std::vector m_workspaceNames; + + }; + + +} // namespace API +} // namespace Mantid + +#endif /* MANTID_API_WORKSPACELISTPROPERTY_H_ */ diff --git a/Code/Mantid/Framework/API/src/WorkspaceListProperty.cpp b/Code/Mantid/Framework/API/src/WorkspaceListProperty.cpp new file mode 100644 index 000000000000..9f76b8daa765 --- /dev/null +++ b/Code/Mantid/Framework/API/src/WorkspaceListProperty.cpp @@ -0,0 +1,23 @@ +#include "MantidAPI/Workspace.h" +#include "MantidAPI/WorkspaceListProperty.h" +#include "MantidAPI/IMDWorkspace.h" +#include "MantidAPI/IEventWorkspace.h" +#include "MantidAPI/IMDEventWorkspace.h" +#include "MantidAPI/ITableWorkspace.h" +#include "MantidAPI/ISplittersWorkspace.h" + +namespace Mantid +{ +namespace API +{ +///@cond TEMPLATE +template MANTID_API_DLL class Mantid::API::WorkspaceListProperty; +template MANTID_API_DLL class Mantid::API::WorkspaceListProperty; +template MANTID_API_DLL class Mantid::API::WorkspaceListProperty; +template MANTID_API_DLL class Mantid::API::WorkspaceListProperty; +template MANTID_API_DLL class Mantid::API::WorkspaceListProperty; +template MANTID_API_DLL class Mantid::API::WorkspaceListProperty; +template MANTID_API_DLL class Mantid::API::WorkspaceListProperty; +///@endcond TEMPLATE +} // namespace API +} // namespace Mantid diff --git a/Code/Mantid/Framework/API/test/WorkspaceListPropertyTest.h b/Code/Mantid/Framework/API/test/WorkspaceListPropertyTest.h new file mode 100644 index 000000000000..e91de3057e70 --- /dev/null +++ b/Code/Mantid/Framework/API/test/WorkspaceListPropertyTest.h @@ -0,0 +1,51 @@ +#ifndef MANTID_API_WORKSPACELISTPROPERTYTEST_H_ +#define MANTID_API_WORKSPACELISTPROPERTYTEST_H_ + +#include + +#include "MantidAPI/WorkspaceListProperty.h" +#include "MantidAPI/Workspace.h" +#include "MantidAPI/MatrixWorkspace.h" +#include "MantidAPI/IMDHistoWorkspace.h" +#include "MantidAPI/IMDEventWorkspace.h" +#include "MantidAPI/ITableWorkspace.h" +#include "MantidAPI/IEventWorkspace.h" +#include "MantidAPI/ISplittersWorkspace.h" + +using namespace Mantid::API; +using namespace Mantid::Kernel; + +class WorkspaceListPropertyTest : public CxxTest::TestSuite +{ +public: + // This pair of boilerplate methods prevent the suite being created statically + // This means the constructor isn't called when running other tests + static WorkspaceListPropertyTest *createSuite() { return new WorkspaceListPropertyTest(); } + static void destroySuite( WorkspaceListPropertyTest *suite ) { delete suite; } + + + void testConstructionMinimalistic() + { + TS_ASSERT_THROWS_NOTHING(WorkspaceListProperty("MyWorkspaceProperty", "MyWorkspace", Direction::Input)); + TS_ASSERT_THROWS_NOTHING(WorkspaceListProperty("MyWorkspaceProperty", "MyWorkspace", Direction::Input)); + TS_ASSERT_THROWS_NOTHING(WorkspaceListProperty("MyWorkspaceProperty", "MyWorkspace", Direction::Input)); + TS_ASSERT_THROWS_NOTHING(WorkspaceListProperty("MyWorkspaceProperty", "MyWorkspace", Direction::Input)); + TS_ASSERT_THROWS_NOTHING(WorkspaceListProperty("MyWorkspaceProperty", "MyWorkspace", Direction::Input)); + TS_ASSERT_THROWS_NOTHING(WorkspaceListProperty("MyWorkspaceProperty", "MyWorkspace", Direction::Input)); + } + + void testConstruction() + { + WorkspaceListProperty workspaceListProperty("MyWorkspaceProperty", "MyWorkspace1, MyWorkspace2", Direction::Input, PropertyMode::Mandatory); + auto workspaceNames = workspaceListProperty.getWorkspaceNames(); + TS_ASSERT_EQUALS(2, workspaceNames.size()); + TS_ASSERT_EQUALS(workspaceNames.front(), "MyWorkspace1"); + TS_ASSERT_EQUALS(workspaceNames.back(), "MyWorkspace2"); + TS_ASSERT_EQUALS(workspaceListProperty.isOptional(), false) + } + + +}; + + +#endif /* MANTID_API_WORKSPACELISTPROPERTYTEST_H_ */ diff --git a/Code/Mantid/Framework/Kernel/inc/MantidKernel/PropertyWithValue.h b/Code/Mantid/Framework/Kernel/inc/MantidKernel/PropertyWithValue.h index 61f6d2b04c91..0d75d4529053 100644 --- a/Code/Mantid/Framework/Kernel/inc/MantidKernel/PropertyWithValue.h +++ b/Code/Mantid/Framework/Kernel/inc/MantidKernel/PropertyWithValue.h @@ -174,6 +174,12 @@ void toValue(const std::string& strvalue, std::vector& value) } } +template +void toValue(const std::string& strvalue, std::vector >&) +{ + throw boost::bad_lexical_cast(); +} + template void toValue(const std::string& strvalue, std::vector >& value, const std::string & outerDelimiter = ",", const std::string & innerDelimiter = "+")