diff --git a/Framework/API/src/Algorithm.cpp b/Framework/API/src/Algorithm.cpp index f3faab12974680bde0ef301eba503b26e280ae06..7b678fb3edbf4e739a9d704b329c4b2fbee6671d 100644 --- a/Framework/API/src/Algorithm.cpp +++ b/Framework/API/src/Algorithm.cpp @@ -1600,8 +1600,8 @@ IPropertyManager::getValue<API::IAlgorithm_sptr>( if (prop) { return *prop; } else { - std::string message = - "Attempt to assign property " + name + " to incorrect type"; + std::string message = "Attempt to assign property " + name + + " to incorrect type. Expected shared_ptr<IAlgorithm>"; throw std::runtime_error(message); } } @@ -1623,7 +1623,8 @@ IPropertyManager::getValue<API::IAlgorithm_const_sptr>( return prop->operator()(); } else { std::string message = - "Attempt to assign property " + name + " to incorrect type"; + "Attempt to assign property " + name + + " to incorrect type. Expected const shared_ptr<IAlgorithm>"; throw std::runtime_error(message); } } diff --git a/Framework/API/src/ExperimentInfo.cpp b/Framework/API/src/ExperimentInfo.cpp index 3ddd52bfdf0bbefe09fb0864bbf10fd4f1ba0e93..62704d501404e41a88fc459c8a044e3c204e45ba 100644 --- a/Framework/API/src/ExperimentInfo.cpp +++ b/Framework/API/src/ExperimentInfo.cpp @@ -1245,8 +1245,9 @@ IPropertyManager::getValue<Mantid::API::ExperimentInfo_sptr>( if (prop) { return *prop; } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected ExperimentInfo."; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected shared_ptr<ExperimentInfo>."; throw std::runtime_error(message); } } @@ -1261,8 +1262,9 @@ IPropertyManager::getValue<Mantid::API::ExperimentInfo_const_sptr>( if (prop) { return prop->operator()(); } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected const ExperimentInfo."; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected const shared_ptr<ExperimentInfo>."; throw std::runtime_error(message); } } diff --git a/Framework/API/src/IEventWorkspace.cpp b/Framework/API/src/IEventWorkspace.cpp index 7eec83f3099d564869d2e334191978b8f67d9050..e802d949a231d2d1952157d9aafd9f2a37e37991 100644 --- a/Framework/API/src/IEventWorkspace.cpp +++ b/Framework/API/src/IEventWorkspace.cpp @@ -47,8 +47,9 @@ IPropertyManager::getValue<Mantid::API::IEventWorkspace_sptr>( if (prop) { return *prop; } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected IEventWorkspace."; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected shared_ptr<IEventWorkspace>."; throw std::runtime_error(message); } } @@ -63,8 +64,9 @@ IPropertyManager::getValue<Mantid::API::IEventWorkspace_const_sptr>( if (prop) { return prop->operator()(); } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected const IEventWorkspace."; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected const shared_ptr<IEventWorkspace>."; throw std::runtime_error(message); } } diff --git a/Framework/API/src/IFunction.cpp b/Framework/API/src/IFunction.cpp index 379ee94adba1b572e0e2f4da29316a1854c2431b..91f4f47621d840abf4d5c055e9537c51441e385d 100644 --- a/Framework/API/src/IFunction.cpp +++ b/Framework/API/src/IFunction.cpp @@ -1118,7 +1118,25 @@ IPropertyManager::getValue<boost::shared_ptr<Mantid::API::IFunction>>( return *prop; } else { std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected IFitFunction."; + " to incorrect type. Expected shared_ptr<IFunction>."; + throw std::runtime_error(message); + } +} + +template <> +MANTID_API_DLL boost::shared_ptr<const Mantid::API::IFunction> +IPropertyManager::getValue<boost::shared_ptr<const Mantid::API::IFunction>>( + const std::string &name) const { + PropertyWithValue<boost::shared_ptr<Mantid::API::IFunction>> *prop = + dynamic_cast< + PropertyWithValue<boost::shared_ptr<Mantid::API::IFunction>> *>( + getPointerToProperty(name)); + if (prop) { + return prop->operator()(); + } else { + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected const shared_ptr<IFunction>."; throw std::runtime_error(message); } } diff --git a/Framework/API/src/IMDEventWorkspace.cpp b/Framework/API/src/IMDEventWorkspace.cpp index 75ec797f8b7b8382d48a00b37d9dc3018779bca0..7828791412691129d27513e4d64449ed691d8e8d 100644 --- a/Framework/API/src/IMDEventWorkspace.cpp +++ b/Framework/API/src/IMDEventWorkspace.cpp @@ -85,8 +85,9 @@ IPropertyManager::getValue<Mantid::API::IMDEventWorkspace_sptr>( if (prop) { return *prop; } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected IMDEventWorkspace."; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected shared_ptr<IMDEventWorkspace>."; throw std::runtime_error(message); } } @@ -104,10 +105,20 @@ IPropertyManager::getValue<Mantid::API::IMDEventWorkspace_const_sptr>( if (prop) { return prop->operator()(); } else { - std::string message = - "Attempt to assign property " + name + - " to incorrect type. Expected const IMDEventWorkspace."; - throw std::runtime_error(message); + // Every other class with this behaviour allows you to get a shared_ptr<T> + // property as a shared_ptr<const T>. This class should be consistent, so + // try that: + PropertyWithValue<Mantid::API::IMDEventWorkspace_sptr> *nonConstProp = + dynamic_cast<PropertyWithValue<Mantid::API::IMDEventWorkspace_sptr> *>( + getPointerToProperty(name)); + if (nonConstProp) { + return nonConstProp->operator()(); + } else { + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected const shared_ptr<IMDEventWorkspace>."; + throw std::runtime_error(message); + } } } diff --git a/Framework/API/src/IMDHistoWorkspace.cpp b/Framework/API/src/IMDHistoWorkspace.cpp index a8cac829bec8b3d63e9d8469c03d9fbad71d7f80..3f4ce43023ef20737cbe016616156b1beec455d3 100644 --- a/Framework/API/src/IMDHistoWorkspace.cpp +++ b/Framework/API/src/IMDHistoWorkspace.cpp @@ -51,8 +51,9 @@ IPropertyManager::getValue<Mantid::API::IMDHistoWorkspace_sptr>( if (prop) { return *prop; } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected IMDHistoWorkspace."; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected shared_ptr<IMDHistoWorkspace>."; throw std::runtime_error(message); } } @@ -71,7 +72,7 @@ IPropertyManager::getValue<Mantid::API::IMDHistoWorkspace_const_sptr>( } else { std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected const IMDHistoWorkspace."; + " to incorrect type. Expected const shared_ptr<IMDHistoWorkspace>."; throw std::runtime_error(message); } } diff --git a/Framework/API/src/IMDWorkspace.cpp b/Framework/API/src/IMDWorkspace.cpp index 20766404902d1ac05322116c7732dc4b442f8db6..ecc894f053f0067f76d887285636010129744e48 100644 --- a/Framework/API/src/IMDWorkspace.cpp +++ b/Framework/API/src/IMDWorkspace.cpp @@ -155,8 +155,9 @@ IPropertyManager::getValue<Mantid::API::IMDWorkspace_sptr>( if (prop) { return *prop; } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected IMDWorkspace."; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected shared_ptr<IMDWorkspace>."; throw std::runtime_error(message); } } @@ -173,8 +174,9 @@ IPropertyManager::getValue<Mantid::API::IMDWorkspace_const_sptr>( if (prop) { return prop->operator()(); } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected const IMDWorkspace."; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected const shared_ptr<IMDWorkspace>."; throw std::runtime_error(message); } } diff --git a/Framework/API/src/IPeaksWorkspace.cpp b/Framework/API/src/IPeaksWorkspace.cpp index bcbccf7ae2c54eefdba8a741aaaaba3bc792c6ee..813a7e6bcf9b0afde6a0512f2b50bd9813b2c454 100644 --- a/Framework/API/src/IPeaksWorkspace.cpp +++ b/Framework/API/src/IPeaksWorkspace.cpp @@ -37,8 +37,9 @@ IPropertyManager::getValue<Mantid::API::IPeaksWorkspace_sptr>( if (prop) { return *prop; } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected PeaksWorkspace."; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected shared_ptr<PeaksWorkspace>."; throw std::runtime_error(message); } } @@ -53,8 +54,9 @@ IPropertyManager::getValue<Mantid::API::IPeaksWorkspace_const_sptr>( if (prop) { return prop->operator()(); } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected const PeaksWorkspace."; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected const shared_ptr<PeaksWorkspace>."; throw std::runtime_error(message); } } diff --git a/Framework/API/src/ITableWorkspace.cpp b/Framework/API/src/ITableWorkspace.cpp index 6ad853d297a24c9eb9d29a3fbf7e7545b39f19e5..3a1e649c462df977cafcd8e57ac218d3a9404291 100644 --- a/Framework/API/src/ITableWorkspace.cpp +++ b/Framework/API/src/ITableWorkspace.cpp @@ -107,8 +107,9 @@ IPropertyManager::getValue<API::ITableWorkspace_sptr>( if (prop) { return *prop; } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected ITableWorkspace"; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected shared_ptr<ITableWorkspace>"; throw std::runtime_error(message); } } @@ -123,8 +124,9 @@ IPropertyManager::getValue<API::ITableWorkspace_const_sptr>( if (prop) { return prop->operator()(); } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected const ITableWorkspace"; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected const shared_ptr<ITableWorkspace>"; throw std::runtime_error(message); } } diff --git a/Framework/API/src/MatrixWorkspace.cpp b/Framework/API/src/MatrixWorkspace.cpp index 0d523b4a07c898f67bd9781d66df6e293fb683fe..10ce0e137d658b2acf76564a1de0e6c33e11da72 100644 --- a/Framework/API/src/MatrixWorkspace.cpp +++ b/Framework/API/src/MatrixWorkspace.cpp @@ -2049,8 +2049,9 @@ IPropertyManager::getValue<Mantid::API::MatrixWorkspace_sptr>( if (prop) { return *prop; } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected MatrixWorkspace."; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected shared_ptr<MatrixWorkspace>."; throw std::runtime_error(message); } } @@ -2065,8 +2066,9 @@ IPropertyManager::getValue<Mantid::API::MatrixWorkspace_const_sptr>( if (prop) { return prop->operator()(); } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected const MatrixWorkspace."; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected const shared_ptr<MatrixWorkspace>."; throw std::runtime_error(message); } } diff --git a/Framework/API/src/Workspace.cpp b/Framework/API/src/Workspace.cpp index 2b6dcfbf166f9272b176a9cd561cf9d497d27020..da102183943c340fb946557141623d42da607f23 100644 --- a/Framework/API/src/Workspace.cpp +++ b/Framework/API/src/Workspace.cpp @@ -99,7 +99,7 @@ IPropertyManager::getValue<Mantid::API::Workspace_sptr>( return *prop; } else { std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected Workspace."; + " to incorrect type. Expected shared_ptr<Workspace>."; throw std::runtime_error(message); } } @@ -114,8 +114,9 @@ IPropertyManager::getValue<Mantid::API::Workspace_const_sptr>( if (prop) { return prop->operator()(); } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected const Workspace."; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected const shared_ptr<Workspace>."; throw std::runtime_error(message); } } diff --git a/Framework/API/src/WorkspaceGroup.cpp b/Framework/API/src/WorkspaceGroup.cpp index d4a27585723d98ef140578fe43acdb7e6a536677..8f8c3d9bab5b6384ed7c61c953e7b66ce9d97c3e 100644 --- a/Framework/API/src/WorkspaceGroup.cpp +++ b/Framework/API/src/WorkspaceGroup.cpp @@ -401,8 +401,9 @@ IPropertyManager::getValue<Mantid::API::WorkspaceGroup_sptr>( if (prop) { return *prop; } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected WorkspaceGroup."; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected shared_ptr<WorkspaceGroup>."; throw std::runtime_error(message); } } @@ -417,8 +418,9 @@ IPropertyManager::getValue<Mantid::API::WorkspaceGroup_const_sptr>( if (prop) { return prop->operator()(); } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected const WorkspaceGroup."; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected const shared_ptr<WorkspaceGroup>."; throw std::runtime_error(message); } } diff --git a/Framework/API/test/AlgorithmTest.h b/Framework/API/test/AlgorithmTest.h index 338c84efe951f12f2d99a5808363795b5afe4e13..cf231805b9f04c07ec1908b740db6bce0786f45d 100644 --- a/Framework/API/test/AlgorithmTest.h +++ b/Framework/API/test/AlgorithmTest.h @@ -14,6 +14,7 @@ #include "MantidKernel/ArrayProperty.h" #include "MantidKernel/RebinParamsValidator.h" #include "FakeAlgorithms.h" +#include "PropertyManagerHelper.h" #include <map> using namespace Mantid::Kernel; @@ -737,6 +738,39 @@ public: TS_ASSERT_EQUALS(ws3->getTitle(), "A3+D3+D3"); } + /** + * Test declaring an algorithm property and retrieving as const + * and non-const + */ + void testGetProperty_const_sptr() { + const std::string algName = "InputAlgorithm"; + IAlgorithm_sptr algInput(new StubbedWorkspaceAlgorithm()); + PropertyManagerHelper manager; + manager.declareProperty(algName, algInput, + Mantid::Kernel::Direction::Input); + + // Check property can be obtained as const or non-const sptr + IAlgorithm_const_sptr algConst; + IAlgorithm_sptr algNonConst; + TS_ASSERT_THROWS_NOTHING( + algConst = manager.getValue<IAlgorithm_const_sptr>(algName)); + TS_ASSERT(algConst != NULL); + TS_ASSERT_THROWS_NOTHING(algNonConst = + manager.getValue<IAlgorithm_sptr>(algName)); + TS_ASSERT(algNonConst != NULL); + TS_ASSERT_EQUALS(algConst, algNonConst); + + // Check TypedValue can be cast to const_sptr or to sptr + PropertyManagerHelper::TypedValue val(manager, algName); + IAlgorithm_const_sptr algCastConst; + IAlgorithm_sptr algCastNonConst; + TS_ASSERT_THROWS_NOTHING(algCastConst = (IAlgorithm_const_sptr)val); + TS_ASSERT(algCastConst != NULL); + TS_ASSERT_THROWS_NOTHING(algCastNonConst = (IAlgorithm_sptr)val); + TS_ASSERT(algCastNonConst != NULL); + TS_ASSERT_EQUALS(algCastConst, algCastNonConst); + } + private: IAlgorithm_sptr runFromString(const std::string &input) { IAlgorithm_sptr testAlg; diff --git a/Framework/API/test/ExperimentInfoTest.h b/Framework/API/test/ExperimentInfoTest.h index 6fe00df70cd04aa29d5d171953dcbb56bf72b86d..c4acb15b37c1f02d0bf9407281fc405ec5383d2e 100644 --- a/Framework/API/test/ExperimentInfoTest.h +++ b/Framework/API/test/ExperimentInfoTest.h @@ -16,6 +16,7 @@ #include "MantidTestHelpers/ComponentCreationHelper.h" #include "MantidTestHelpers/NexusTestHelper.h" +#include "PropertyManagerHelper.h" #include <nexus/NeXusFile.hpp> #include <nexus/NeXusException.hpp> @@ -693,6 +694,38 @@ public: TS_ASSERT_EQUALS(params.size(), 613); // Check size of parameter string } + /** + * Test declaring an ExperimentInfo property and retrieving as const or + * non-const + */ + void testGetProperty_const_sptr() { + const std::string eiName = "InputEi"; + ExperimentInfo_sptr eiInput(new ExperimentInfo()); + PropertyManagerHelper manager; + manager.declareProperty(eiName, eiInput, Mantid::Kernel::Direction::Input); + + // Check property can be obtained as const_sptr or sptr + ExperimentInfo_const_sptr eiConst; + ExperimentInfo_sptr eiNonConst; + TS_ASSERT_THROWS_NOTHING( + eiConst = manager.getValue<ExperimentInfo_const_sptr>(eiName)); + TS_ASSERT(eiConst != NULL); + TS_ASSERT_THROWS_NOTHING(eiNonConst = + manager.getValue<ExperimentInfo_sptr>(eiName)); + TS_ASSERT(eiNonConst != NULL); + TS_ASSERT_EQUALS(eiConst, eiNonConst); + + // Check TypedValue can be cast to const_sptr or to sptr + PropertyManagerHelper::TypedValue val(manager, eiName); + ExperimentInfo_const_sptr eiCastConst; + ExperimentInfo_sptr eiCastNonConst; + TS_ASSERT_THROWS_NOTHING(eiCastConst = (ExperimentInfo_const_sptr)val); + TS_ASSERT(eiCastConst != NULL); + TS_ASSERT_THROWS_NOTHING(eiCastNonConst = (ExperimentInfo_sptr)val); + TS_ASSERT(eiCastNonConst != NULL); + TS_ASSERT_EQUALS(eiCastConst, eiCastNonConst); + } + private: void addInstrumentWithParameter(ExperimentInfo &expt, const std::string &name, const std::string &value) { diff --git a/Framework/API/test/FunctionTest.h b/Framework/API/test/FunctionTest.h index 607c81f1ffb0e76517433e01d70c8f69b1947805..847e79c8b71959dfbf58f6cccd99c067fe6c062c 100644 --- a/Framework/API/test/FunctionTest.h +++ b/Framework/API/test/FunctionTest.h @@ -9,6 +9,7 @@ #include "MantidAPI/FrameworkManager.h" #include "MantidAPI/WorkspaceFactory.h" #include "MantidAPI/ParameterTie.h" +#include "PropertyManagerHelper.h" #include <cxxtest/TestSuite.h> @@ -406,6 +407,38 @@ public: TS_ASSERT(!f.isExplicitlySet(3)); } + /** + * Test declaring a const IFunction property and retrieving as const or + * non-const + */ + void testGetProperty_const_sptr() { + const std::string funcName = "InputFunction"; + IFunction_sptr funcInput(new IFT_Funct()); + PropertyManagerHelper manager; + manager.declareProperty(funcName, funcInput, Kernel::Direction::Input); + + // Check property can be obtained as const_sptr or sptr + IFunction_const_sptr funcConst; + IFunction_sptr funcNonConst; + TS_ASSERT_THROWS_NOTHING( + funcConst = manager.getValue<IFunction_const_sptr>(funcName)); + TS_ASSERT(funcConst != NULL); + TS_ASSERT_THROWS_NOTHING(funcNonConst = + manager.getValue<IFunction_sptr>(funcName)); + TS_ASSERT(funcNonConst != NULL); + TS_ASSERT_EQUALS(funcConst, funcNonConst); + + // Check TypedValue can be cast to const_sptr or to sptr + PropertyManagerHelper::TypedValue val(manager, funcName); + IFunction_const_sptr funcCastConst; + IFunction_sptr funcCastNonConst; + TS_ASSERT_THROWS_NOTHING(funcCastConst = (IFunction_const_sptr)val); + TS_ASSERT(funcCastConst != NULL); + TS_ASSERT_THROWS_NOTHING(funcCastNonConst = (IFunction_sptr)val); + TS_ASSERT(funcCastNonConst != NULL); + TS_ASSERT_EQUALS(funcCastConst, funcCastNonConst); + } + // void xtest_setWorkspace_works() //{ // MatrixWorkspace_sptr ws(new MocMatrixWorkspace(10,11,10)); diff --git a/Framework/API/test/IMDWorkspaceTest.h b/Framework/API/test/IMDWorkspaceTest.h index 02a4369038b147c9ae51ff809b3ff3dba9d104b0..cf412a40b3baad77c62f6c8a62decefb5a7bcb1f 100644 --- a/Framework/API/test/IMDWorkspaceTest.h +++ b/Framework/API/test/IMDWorkspaceTest.h @@ -12,6 +12,7 @@ #include "MantidGeometry/MDGeometry/IMDDimension.h" #include "MantidAPI/MatrixWSIndexCalculator.h" #include "MantidTestHelpers/FakeObjects.h" +#include "PropertyManagerHelper.h" using std::size_t; using namespace Mantid::Kernel; @@ -135,6 +136,37 @@ public: TSM_ASSERT_EQUALS("bin index has not been calculated correctly.", 2, binIndexB); } + + /** + * Test declaring an input workspace and retrieving as const_sptr or sptr + */ + void testGetProperty_const_sptr() { + const std::string wsName = "InputWorkspace"; + IMDWorkspace_sptr wsInput(new WorkspaceTester()); + PropertyManagerHelper manager; + manager.declareProperty(wsName, wsInput, Direction::Input); + + // Check property can be obtained as const_sptr or sptr + IMDWorkspace_const_sptr wsConst; + IMDWorkspace_sptr wsNonConst; + TS_ASSERT_THROWS_NOTHING( + wsConst = manager.getValue<IMDWorkspace_const_sptr>(wsName)); + TS_ASSERT(wsConst != NULL); + TS_ASSERT_THROWS_NOTHING(wsNonConst = + manager.getValue<IMDWorkspace_sptr>(wsName)); + TS_ASSERT(wsNonConst != NULL); + TS_ASSERT_EQUALS(wsConst, wsNonConst); + + // Check TypedValue can be cast to const_sptr or to sptr + PropertyManagerHelper::TypedValue val(manager, wsName); + IMDWorkspace_const_sptr wsCastConst; + IMDWorkspace_sptr wsCastNonConst; + TS_ASSERT_THROWS_NOTHING(wsCastConst = (IMDWorkspace_const_sptr)val); + TS_ASSERT(wsCastConst != NULL); + TS_ASSERT_THROWS_NOTHING(wsCastNonConst = (IMDWorkspace_sptr)val); + TS_ASSERT(wsCastNonConst != NULL); + TS_ASSERT_EQUALS(wsCastConst, wsCastNonConst); + } }; #endif /*IMD_MATRIX_WORKSPACETEST_H_*/ diff --git a/Framework/API/test/MatrixWorkspaceTest.h b/Framework/API/test/MatrixWorkspaceTest.h index 6878f6d2479b67eb1c705ea5c90e9520f9908048..170604cbe854054dd84bad96babfbfa6ac3a9f59 100644 --- a/Framework/API/test/MatrixWorkspaceTest.h +++ b/Framework/API/test/MatrixWorkspaceTest.h @@ -14,6 +14,7 @@ #include "MantidTestHelpers/FakeGmockObjects.h" #include "MantidTestHelpers/FakeObjects.h" #include "MantidTestHelpers/NexusTestHelper.h" +#include "PropertyManagerHelper.h" #include <cxxtest/TestSuite.h> #include <boost/make_shared.hpp> @@ -1257,6 +1258,37 @@ public: TS_ASSERT_EQUALS(ws.readE(7)[0], 6); } + /** + * Test declaring an input workspace and retrieving as const_sptr or sptr + */ + void testGetProperty_const_sptr() { + const std::string wsName = "InputWorkspace"; + MatrixWorkspace_sptr wsInput(new WorkspaceTester()); + PropertyManagerHelper manager; + manager.declareProperty(wsName, wsInput, Direction::Input); + + // Check property can be obtained as const_sptr or sptr + MatrixWorkspace_const_sptr wsConst; + MatrixWorkspace_sptr wsNonConst; + TS_ASSERT_THROWS_NOTHING( + wsConst = manager.getValue<MatrixWorkspace_const_sptr>(wsName)); + TS_ASSERT(wsConst != NULL); + TS_ASSERT_THROWS_NOTHING( + wsNonConst = manager.getValue<MatrixWorkspace_sptr>(wsName)); + TS_ASSERT(wsNonConst != NULL); + TS_ASSERT_EQUALS(wsConst, wsNonConst); + + // Check TypedValue can be cast to const_sptr or to sptr + PropertyManagerHelper::TypedValue val(manager, wsName); + MatrixWorkspace_const_sptr wsCastConst; + MatrixWorkspace_sptr wsCastNonConst; + TS_ASSERT_THROWS_NOTHING(wsCastConst = (MatrixWorkspace_const_sptr)val); + TS_ASSERT(wsCastConst != NULL); + TS_ASSERT_THROWS_NOTHING(wsCastNonConst = (MatrixWorkspace_sptr)val); + TS_ASSERT(wsCastNonConst != NULL); + TS_ASSERT_EQUALS(wsCastConst, wsCastNonConst); + } + private: Mantid::API::MantidImage_sptr createImage(size_t width, size_t height) { auto image = new Mantid::API::MantidImage(height); diff --git a/Framework/API/test/PropertyManagerHelper.h b/Framework/API/test/PropertyManagerHelper.h new file mode 100644 index 0000000000000000000000000000000000000000..c626338a48250b65bb74e4fff8cc3b1675c09add --- /dev/null +++ b/Framework/API/test/PropertyManagerHelper.h @@ -0,0 +1,19 @@ +#ifndef APITEST_PROPERTYMANAGERHELPER_H_ +#define APITEST_PROPERTYMANAGERHELPER_H_ + +#include "MantidKernel/PropertyManager.h" + +/** +* Helper class to use IPropertyManager methods in the DataObjects tests +*/ +class PropertyManagerHelper : public Mantid::Kernel::PropertyManager { +public: + PropertyManagerHelper() : PropertyManager() {} + + using PropertyManager::declareProperty; + using PropertyManager::setProperty; + using IPropertyManager::TypedValue; + using IPropertyManager::getValue; +}; + +#endif diff --git a/Framework/API/test/WorkspaceGroupTest.h b/Framework/API/test/WorkspaceGroupTest.h index f14a8d18889924d490797a14eac2981f925c289d..91fcf1a0603d717e8f9a62004c61e7ed457d53d3 100644 --- a/Framework/API/test/WorkspaceGroupTest.h +++ b/Framework/API/test/WorkspaceGroupTest.h @@ -7,6 +7,7 @@ #include "MantidAPI/WorkspaceGroup.h" #include "MantidAPI/AnalysisDataService.h" #include "MantidTestHelpers/FakeObjects.h" +#include "PropertyManagerHelper.h" #include <boost/shared_ptr.hpp> #include <cxxtest/TestSuite.h> @@ -323,6 +324,69 @@ public: TS_ASSERT_THROWS(group->isInGroup(*b), std::runtime_error); group1->removeAll(); } + + /** + * Test declaring an input workspace group and retrieving as const_sptr or sptr + */ + void testGetProperty_const_sptr() { + const std::string wsName = "InputWorkspace"; + WorkspaceGroup_sptr wsInput(new WorkspaceGroup()); + PropertyManagerHelper manager; + manager.declareProperty(wsName, wsInput, Direction::Input); + + // Check property can be obtained as const_sptr or sptr + WorkspaceGroup_const_sptr wsConst; + WorkspaceGroup_sptr wsNonConst; + TS_ASSERT_THROWS_NOTHING( + wsConst = manager.getValue<WorkspaceGroup_const_sptr>(wsName)); + TS_ASSERT(wsConst != NULL); + TS_ASSERT_THROWS_NOTHING(wsNonConst = + manager.getValue<WorkspaceGroup_sptr>(wsName)); + TS_ASSERT(wsNonConst != NULL); + TS_ASSERT_EQUALS(wsConst, wsNonConst); + + // Check TypedValue can be cast to const_sptr or to sptr + PropertyManagerHelper::TypedValue val(manager, wsName); + WorkspaceGroup_const_sptr wsCastConst; + WorkspaceGroup_sptr wsCastNonConst; + TS_ASSERT_THROWS_NOTHING(wsCastConst = (WorkspaceGroup_const_sptr)val); + TS_ASSERT(wsCastConst != NULL); + TS_ASSERT_THROWS_NOTHING(wsCastNonConst = (WorkspaceGroup_sptr)val); + TS_ASSERT(wsCastNonConst != NULL); + TS_ASSERT_EQUALS(wsCastConst, wsCastNonConst); + } + + /** + * Test declaring an input workspace and retrieving as const_sptr or sptr + * (here Workspace rather than WorkspaceGroup) + */ + void testGetProperty_Workspace_const_sptr() { + const std::string wsName = "InputWorkspace"; + Workspace_sptr wsInput(new WorkspaceTester()); + PropertyManagerHelper manager; + manager.declareProperty(wsName, wsInput, Direction::Input); + + // Check property can be obtained as const_sptr or sptr + Workspace_const_sptr wsConst; + Workspace_sptr wsNonConst; + TS_ASSERT_THROWS_NOTHING( + wsConst = manager.getValue<Workspace_const_sptr>(wsName)); + TS_ASSERT(wsConst != NULL); + TS_ASSERT_THROWS_NOTHING(wsNonConst = + manager.getValue<Workspace_sptr>(wsName)); + TS_ASSERT(wsNonConst != NULL); + TS_ASSERT_EQUALS(wsConst, wsNonConst); + + // Check TypedValue can be cast to const_sptr or to sptr + PropertyManagerHelper::TypedValue val(manager, wsName); + Workspace_const_sptr wsCastConst; + Workspace_sptr wsCastNonConst; + TS_ASSERT_THROWS_NOTHING(wsCastConst = (Workspace_const_sptr)val); + TS_ASSERT(wsCastConst != NULL); + TS_ASSERT_THROWS_NOTHING(wsCastNonConst = (Workspace_sptr)val); + TS_ASSERT(wsCastNonConst != NULL); + TS_ASSERT_EQUALS(wsCastConst, wsCastNonConst); + } }; #endif /* MANTID_API_WORKSPACEGROUPTEST_H_ */ diff --git a/Framework/DataObjects/src/EventWorkspace.cpp b/Framework/DataObjects/src/EventWorkspace.cpp index ccefaf41c27b36585b8bc77cbd03806dc03078c9..7130d5031920d8c0dd4568590cfee5ad5a68bbfe 100644 --- a/Framework/DataObjects/src/EventWorkspace.cpp +++ b/Framework/DataObjects/src/EventWorkspace.cpp @@ -986,8 +986,9 @@ IPropertyManager::getValue<Mantid::DataObjects::EventWorkspace_sptr>( if (prop) { return *prop; } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected EventWorkspace."; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected shared_ptr<EventWorkspace>."; throw std::runtime_error(message); } } @@ -1003,8 +1004,9 @@ IPropertyManager::getValue<Mantid::DataObjects::EventWorkspace_const_sptr>( if (prop) { return prop->operator()(); } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected const EventWorkspace."; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected const shared_ptr<EventWorkspace>."; throw std::runtime_error(message); } } diff --git a/Framework/DataObjects/src/GroupingWorkspace.cpp b/Framework/DataObjects/src/GroupingWorkspace.cpp index 6ca3f0867f20384dc1dcc1b56f64af71ebde0294..6ddd541c0a3f8878506ac2d3fba327eb92915635 100644 --- a/Framework/DataObjects/src/GroupingWorkspace.cpp +++ b/Framework/DataObjects/src/GroupingWorkspace.cpp @@ -117,8 +117,9 @@ IPropertyManager::getValue<Mantid::DataObjects::GroupingWorkspace_sptr>( if (prop) { return *prop; } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected GroupingWorkspace."; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected shared_ptr<GroupingWorkspace>."; throw std::runtime_error(message); } } @@ -136,7 +137,7 @@ IPropertyManager::getValue<Mantid::DataObjects::GroupingWorkspace_const_sptr>( } else { std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected const GroupingWorkspace."; + " to incorrect type. Expected const shared_ptr<GroupingWorkspace>."; throw std::runtime_error(message); } } diff --git a/Framework/DataObjects/src/MaskWorkspace.cpp b/Framework/DataObjects/src/MaskWorkspace.cpp index 8d638313de96d2c50267a58152e5c054ba4863bb..edddf2db92edd4ae382925d287d1baa7f2e17220 100644 --- a/Framework/DataObjects/src/MaskWorkspace.cpp +++ b/Framework/DataObjects/src/MaskWorkspace.cpp @@ -311,8 +311,9 @@ IPropertyManager::getValue<Mantid::DataObjects::MaskWorkspace_sptr>( if (prop) { return *prop; } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected MaskWorkspace."; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected shared_ptr<MaskWorkspace>."; throw std::runtime_error(message); } } @@ -328,8 +329,9 @@ IPropertyManager::getValue<Mantid::DataObjects::MaskWorkspace_const_sptr>( if (prop) { return prop->operator()(); } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected const MaskWorkspace."; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected const shared_ptr<MaskWorkspace>."; throw std::runtime_error(message); } } diff --git a/Framework/DataObjects/src/OffsetsWorkspace.cpp b/Framework/DataObjects/src/OffsetsWorkspace.cpp index 8f42385743caf4450fb2ef8d7e61cc4c449810d2..2ed1bbb6df41ca2a463fd32fc3f772f6b7fe327b 100644 --- a/Framework/DataObjects/src/OffsetsWorkspace.cpp +++ b/Framework/DataObjects/src/OffsetsWorkspace.cpp @@ -48,8 +48,9 @@ IPropertyManager::getValue<Mantid::DataObjects::OffsetsWorkspace_sptr>( if (prop) { return *prop; } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected OffsetsWorkspace."; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected shared_ptr<OffsetsWorkspace>."; throw std::runtime_error(message); } } @@ -67,7 +68,7 @@ IPropertyManager::getValue<Mantid::DataObjects::OffsetsWorkspace_const_sptr>( } else { std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected const OffsetsWorkspace."; + " to incorrect type. Expected const shared_ptr<OffsetsWorkspace>."; throw std::runtime_error(message); } } diff --git a/Framework/DataObjects/src/PeaksWorkspace.cpp b/Framework/DataObjects/src/PeaksWorkspace.cpp index 083c66123c4b9222af4c529934c007b27cdb7c9f..f75ca8b4064828ab7486d3b5ac1b37dd69d7a564 100644 --- a/Framework/DataObjects/src/PeaksWorkspace.cpp +++ b/Framework/DataObjects/src/PeaksWorkspace.cpp @@ -856,8 +856,9 @@ IPropertyManager::getValue<Mantid::DataObjects::PeaksWorkspace_sptr>( if (prop) { return *prop; } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected PeaksWorkspace."; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected shared_ptr<PeaksWorkspace>."; throw std::runtime_error(message); } } @@ -873,8 +874,9 @@ IPropertyManager::getValue<Mantid::DataObjects::PeaksWorkspace_const_sptr>( if (prop) { return prop->operator()(); } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected const PeaksWorkspace."; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected const shared_ptr<PeaksWorkspace>."; throw std::runtime_error(message); } } diff --git a/Framework/DataObjects/src/SpecialWorkspace2D.cpp b/Framework/DataObjects/src/SpecialWorkspace2D.cpp index 23cf4711a5cfceac7407ba76d4dfe9307a1087d9..ba520975e3e4e88160a8a83d42598b8da55f8cd8 100644 --- a/Framework/DataObjects/src/SpecialWorkspace2D.cpp +++ b/Framework/DataObjects/src/SpecialWorkspace2D.cpp @@ -451,8 +451,9 @@ IPropertyManager::getValue<Mantid::DataObjects::SpecialWorkspace2D_sptr>( if (prop) { return *prop; } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected SpecialWorkspace2D."; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected shared_ptr<SpecialWorkspace2D>."; throw std::runtime_error(message); } } @@ -470,7 +471,7 @@ IPropertyManager::getValue<Mantid::DataObjects::SpecialWorkspace2D_const_sptr>( } else { std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected const SpecialWorkspace2D."; + " to incorrect type. Expected const shared_ptr<SpecialWorkspace2D>."; throw std::runtime_error(message); } } diff --git a/Framework/DataObjects/src/SplittersWorkspace.cpp b/Framework/DataObjects/src/SplittersWorkspace.cpp index 70f5e91b07e0399ae9d15ced06f32af79608cfb5..41f4b1aa65f20f00ca4760549175bec4a5cdb1aa 100644 --- a/Framework/DataObjects/src/SplittersWorkspace.cpp +++ b/Framework/DataObjects/src/SplittersWorkspace.cpp @@ -91,8 +91,9 @@ IPropertyManager::getValue<Mantid::DataObjects::SplittersWorkspace_sptr>( if (prop) { return *prop; } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected SplittersWorkspace."; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected shared_ptr<SplittersWorkspace>."; throw std::runtime_error(message); } } @@ -110,7 +111,7 @@ IPropertyManager::getValue<Mantid::DataObjects::SplittersWorkspace_const_sptr>( } else { std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected const SplittersWorkspace."; + " to incorrect type. Expected const shared_ptr<SplittersWorkspace>."; throw std::runtime_error(message); } } diff --git a/Framework/DataObjects/src/TableWorkspace.cpp b/Framework/DataObjects/src/TableWorkspace.cpp index c352028bfd15d5f210bd0409cb2cb8dc000b6d85..e650c051c49aa5f2608b21fc64b82c4b56204608 100644 --- a/Framework/DataObjects/src/TableWorkspace.cpp +++ b/Framework/DataObjects/src/TableWorkspace.cpp @@ -303,8 +303,26 @@ IPropertyManager::getValue<DataObjects::TableWorkspace_sptr>( if (prop) { return *prop; } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected TableWorkspace."; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected shared_ptr<TableWorkspace>."; + throw std::runtime_error(message); + } +} + +template <> +DLLExport DataObjects::TableWorkspace_const_sptr +IPropertyManager::getValue<DataObjects::TableWorkspace_const_sptr>( + const std::string &name) const { + PropertyWithValue<DataObjects::TableWorkspace_sptr> *prop = + dynamic_cast<PropertyWithValue<DataObjects::TableWorkspace_sptr> *>( + getPointerToProperty(name)); + if (prop) { + return prop->operator()(); + } else { + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected const shared_ptr<TableWorkspace>."; throw std::runtime_error(message); } } diff --git a/Framework/DataObjects/src/Workspace2D.cpp b/Framework/DataObjects/src/Workspace2D.cpp index 79325874a67b442b60c381fb43c6818670db379e..8c91015de347de4c57c0431b386f6227f6391a2b 100644 --- a/Framework/DataObjects/src/Workspace2D.cpp +++ b/Framework/DataObjects/src/Workspace2D.cpp @@ -325,8 +325,9 @@ IPropertyManager::getValue<Mantid::DataObjects::Workspace2D_sptr>( if (prop) { return *prop; } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected Workspace2D."; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected shared_ptr<Workspace2D>."; throw std::runtime_error(message); } } @@ -341,8 +342,9 @@ IPropertyManager::getValue<Mantid::DataObjects::Workspace2D_const_sptr>( if (prop) { return prop->operator()(); } else { - std::string message = "Attempt to assign property " + name + - " to incorrect type. Expected const Workspace2D."; + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected const shared_ptr<Workspace2D>."; throw std::runtime_error(message); } } diff --git a/Framework/DataObjects/src/WorkspaceSingleValue.cpp b/Framework/DataObjects/src/WorkspaceSingleValue.cpp index 10c05ee50335cde3e374f3fa4785dd4143e7caa5..9d00e0307463caf39ca88599bdba7055890cafb8 100644 --- a/Framework/DataObjects/src/WorkspaceSingleValue.cpp +++ b/Framework/DataObjects/src/WorkspaceSingleValue.cpp @@ -94,8 +94,29 @@ IPropertyManager::getValue<Mantid::DataObjects::WorkspaceSingleValue_sptr>( if (prop) { return *prop; } else { - throw std::runtime_error("Attempt to assign property of incorrect type. " - "Expected WorkspaceSingleValue."); + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected shared_ptr<WorkspaceSingleValue>."; + throw std::runtime_error(message); + } +} + +template <> +DLLExport Mantid::DataObjects::WorkspaceSingleValue_const_sptr +IPropertyManager::getValue< + Mantid::DataObjects::WorkspaceSingleValue_const_sptr>( + const std::string &name) const { + PropertyWithValue<Mantid::DataObjects::WorkspaceSingleValue_sptr> *prop = + dynamic_cast< + PropertyWithValue<Mantid::DataObjects::WorkspaceSingleValue_sptr> *>( + getPointerToProperty(name)); + if (prop) { + return prop->operator()(); + } else { + std::string message = + "Attempt to assign property " + name + + " to incorrect type. Expected const shared_ptr<WorkspaceSingleValue>."; + throw std::runtime_error(message); } } diff --git a/Framework/DataObjects/test/EventWorkspaceTest.h b/Framework/DataObjects/test/EventWorkspaceTest.h index a5ab9dcc5deffc6ede0b848aac1a39bdfedb0a40..60ba9b4ba133fd7fc591acbcd909041283efa037 100644 --- a/Framework/DataObjects/test/EventWorkspaceTest.h +++ b/Framework/DataObjects/test/EventWorkspaceTest.h @@ -12,12 +12,15 @@ #include <boost/date_time/gregorian/gregorian.hpp> #include <boost/date_time/posix_time/posix_time.hpp> +#include <string> + #include "MantidDataObjects/EventList.h" #include "MantidDataObjects/EventWorkspace.h" #include "MantidTestHelpers/WorkspaceCreationHelper.h" #include "MantidTestHelpers/ComponentCreationHelper.h" #include "MantidKernel/Memory.h" #include "MantidKernel/Timer.h" +#include "PropertyManagerHelper.h" using namespace Mantid; using namespace Mantid::DataObjects; @@ -822,6 +825,68 @@ public: TS_ASSERT_DELTA(wksp->getEventXMin(), 500, .01); TS_ASSERT_DELTA(wksp->getEventXMax(), 1023500, .01); } + + /** + * Test declaring an input EventWorkspace and retrieving as const_sptr or sptr + */ + void testGetProperty_const_sptr() { + const std::string wsName = "InputWorkspace"; + EventWorkspace_sptr wsInput(new EventWorkspace()); + PropertyManagerHelper manager; + manager.declareProperty(wsName, wsInput, Direction::Input); + + // Check property can be obtained as const_sptr or sptr + EventWorkspace_const_sptr wsConst; + EventWorkspace_sptr wsNonConst; + TS_ASSERT_THROWS_NOTHING( + wsConst = manager.getValue<EventWorkspace_const_sptr>(wsName)); + TS_ASSERT(wsConst != NULL); + TS_ASSERT_THROWS_NOTHING(wsNonConst = + manager.getValue<EventWorkspace_sptr>(wsName)); + TS_ASSERT(wsNonConst != NULL); + TS_ASSERT_EQUALS(wsConst, wsNonConst); + + // Check TypedValue can be cast to const_sptr or to sptr + PropertyManagerHelper::TypedValue val(manager, wsName); + EventWorkspace_const_sptr wsCastConst; + EventWorkspace_sptr wsCastNonConst; + TS_ASSERT_THROWS_NOTHING(wsCastConst = (EventWorkspace_const_sptr)val); + TS_ASSERT(wsCastConst != NULL); + TS_ASSERT_THROWS_NOTHING(wsCastNonConst = (EventWorkspace_sptr)val); + TS_ASSERT(wsCastNonConst != NULL); + TS_ASSERT_EQUALS(wsCastConst, wsCastNonConst); + } + + /** + * Test declaring an input IEventWorkspace and retrieving as const_sptr or sptr + */ + void testGetProperty_IEventWS_const_sptr() { + const std::string wsName = "InputWorkspace"; + IEventWorkspace_sptr wsInput(new EventWorkspace()); + PropertyManagerHelper manager; + manager.declareProperty(wsName, wsInput, Direction::Input); + + // Check property can be obtained as const_sptr or sptr + IEventWorkspace_const_sptr wsConst; + IEventWorkspace_sptr wsNonConst; + TS_ASSERT_THROWS_NOTHING( + wsConst = manager.getValue<IEventWorkspace_const_sptr>(wsName)); + TS_ASSERT(wsConst != NULL); + TS_ASSERT_THROWS_NOTHING( + wsNonConst = manager.getValue<IEventWorkspace_sptr>(wsName)); + TS_ASSERT(wsNonConst != NULL); + TS_ASSERT_EQUALS(wsConst, wsNonConst); + + // Check TypedValue can be cast to const_sptr or to sptr + PropertyManagerHelper::TypedValue val(manager, wsName); + IEventWorkspace_const_sptr wsCastConst; + IEventWorkspace_sptr wsCastNonConst; + TS_ASSERT_THROWS_NOTHING(wsCastConst = (IEventWorkspace_const_sptr)val); + TS_ASSERT(wsCastConst != NULL); + TS_ASSERT_THROWS_NOTHING(wsCastNonConst = (IEventWorkspace_sptr)val); + TS_ASSERT(wsCastNonConst != NULL); + TS_ASSERT_EQUALS(wsCastConst, wsCastNonConst); + } }; #endif /* EVENTWORKSPACETEST_H_ */ diff --git a/Framework/DataObjects/test/GroupingWorkspaceTest.h b/Framework/DataObjects/test/GroupingWorkspaceTest.h index ee0db8b637f01c9044689190e5a87fca61b795bc..f9227b6a36f6f1023336996a5db592d1470a389f 100644 --- a/Framework/DataObjects/test/GroupingWorkspaceTest.h +++ b/Framework/DataObjects/test/GroupingWorkspaceTest.h @@ -7,6 +7,7 @@ #include "MantidTestHelpers/ComponentCreationHelper.h" #include "MantidTestHelpers/WorkspaceCreationHelper.h" #include <cxxtest/TestSuite.h> +#include "PropertyManagerHelper.h" using namespace Mantid; using namespace Mantid::API; @@ -92,6 +93,38 @@ public: TS_ASSERT_EQUALS(map[10], 2); TS_ASSERT_EQUALS(map[45], 5); } + + /** + * Test declaring an input workspace property and retrieving as const_sptr or + * sptr + */ + void testGetProperty_const_sptr() { + const std::string wsName = "InputWorkspace"; + GroupingWorkspace_sptr wsInput(new GroupingWorkspace()); + PropertyManagerHelper manager; + manager.declareProperty(wsName, wsInput, Mantid::Kernel::Direction::Input); + + // Check property can be obtained as const_sptr or sptr + GroupingWorkspace_const_sptr wsConst; + GroupingWorkspace_sptr wsNonConst; + TS_ASSERT_THROWS_NOTHING( + wsConst = manager.getValue<GroupingWorkspace_const_sptr>(wsName)); + TS_ASSERT(wsConst != NULL); + TS_ASSERT_THROWS_NOTHING( + wsNonConst = manager.getValue<GroupingWorkspace_sptr>(wsName)); + TS_ASSERT(wsNonConst != NULL); + TS_ASSERT_EQUALS(wsConst, wsNonConst); + + // Check TypedValue can be cast to const_sptr or to sptr + PropertyManagerHelper::TypedValue val(manager, wsName); + GroupingWorkspace_const_sptr wsCastConst; + GroupingWorkspace_sptr wsCastNonConst; + TS_ASSERT_THROWS_NOTHING(wsCastConst = (GroupingWorkspace_const_sptr)val); + TS_ASSERT(wsCastConst != NULL); + TS_ASSERT_THROWS_NOTHING(wsCastNonConst = (GroupingWorkspace_sptr)val); + TS_ASSERT(wsCastNonConst != NULL); + TS_ASSERT_EQUALS(wsCastConst, wsCastNonConst); + } }; #endif /* MANTID_DATAOBJECTS_GROUPINGWORKSPACETEST_H_ */ diff --git a/Framework/DataObjects/test/MDEventWorkspaceTest.h b/Framework/DataObjects/test/MDEventWorkspaceTest.h index bf8d4863f790e03801d5d4d94b3d6c9f83cf5177..c1eb4db378a7b258ec780b1eb12ecf9f96961a5c 100644 --- a/Framework/DataObjects/test/MDEventWorkspaceTest.h +++ b/Framework/DataObjects/test/MDEventWorkspaceTest.h @@ -17,6 +17,7 @@ #include "MantidDataObjects/MDGridBox.h" #include "MantidDataObjects/MDLeanEvent.h" #include "MantidTestHelpers/MDEventsTestHelper.h" +#include "PropertyManagerHelper.h" #include <boost/random/linear_congruential.hpp> #include <boost/random/mersenne_twister.hpp> #include <boost/random/uniform_int.hpp> @@ -579,6 +580,38 @@ public: "Should be set to number of events normalizationnormalization", ew->displayNormalizationHisto(), histoSetting); } + + /** + * Test declaring an input IMDEventWorkspace and retrieving as const_sptr or + * sptr + */ + void testGetProperty_const_sptr() { + const std::string wsName = "InputWorkspace"; + IMDEventWorkspace_sptr wsInput(new MDEventWorkspace<MDLeanEvent<3>, 3>()); + PropertyManagerHelper manager; + manager.declareProperty(wsName, wsInput, Direction::Input); + + // Check property can be obtained as const_sptr or sptr + IMDEventWorkspace_const_sptr wsConst; + IMDEventWorkspace_sptr wsNonConst; + TS_ASSERT_THROWS_NOTHING( + wsConst = manager.getValue<IMDEventWorkspace_const_sptr>(wsName)); + TS_ASSERT(wsConst != NULL); + TS_ASSERT_THROWS_NOTHING( + wsNonConst = manager.getValue<IMDEventWorkspace_sptr>(wsName)); + TS_ASSERT(wsNonConst != NULL); + TS_ASSERT_EQUALS(wsConst, wsNonConst); + + // Check TypedValue can be cast to const_sptr or to sptr + PropertyManagerHelper::TypedValue val(manager, wsName); + IMDEventWorkspace_const_sptr wsCastConst; + IMDEventWorkspace_sptr wsCastNonConst; + TS_ASSERT_THROWS_NOTHING(wsCastConst = (IMDEventWorkspace_const_sptr)val); + TS_ASSERT(wsCastConst != NULL); + TS_ASSERT_THROWS_NOTHING(wsCastNonConst = (IMDEventWorkspace_sptr)val); + TS_ASSERT(wsCastNonConst != NULL); + TS_ASSERT_EQUALS(wsCastConst, wsCastNonConst); + } }; class MDEventWorkspaceTestPerformance : public CxxTest::TestSuite { diff --git a/Framework/DataObjects/test/MDHistoWorkspaceTest.h b/Framework/DataObjects/test/MDHistoWorkspaceTest.h index 5d3b2fffe5ecd138d8a9722a2c974c3d6af279f3..eb58893d5894dc913ed6961f1137463e4216b1b2 100644 --- a/Framework/DataObjects/test/MDHistoWorkspaceTest.h +++ b/Framework/DataObjects/test/MDHistoWorkspaceTest.h @@ -19,6 +19,7 @@ #include <cxxtest/TestSuite.h> #include "MantidAPI/ExperimentInfo.h" #include "MantidKernel/Strings.h" +#include "PropertyManagerHelper.h" using namespace Mantid::DataObjects; using namespace Mantid::DataObjects; @@ -1066,6 +1067,42 @@ public: auto clone = ws3.clone(); TS_ASSERT_EQUALS(targetDisplayNormalization, clone->displayNormalization()); } + + /** + * Test declaring an input IMDHistoWorkspace and retrieving as const_sptr or + * sptr + */ + void testGetProperty_const_sptr() { + const std::string wsName = "InputWorkspace"; + Mantid::Geometry::GeneralFrame frame("m", "m"); + MDHistoDimension_sptr dimX( + new MDHistoDimension("X", "x", frame, -10, 10, 5)); + IMDHistoWorkspace_sptr wsInput(new MDHistoWorkspace( + dimX, dimX, dimX, dimX, Mantid::API::VolumeNormalization)); + PropertyManagerHelper manager; + manager.declareProperty(wsName, wsInput, Direction::Input); + + // Check property can be obtained as const_sptr or sptr + IMDHistoWorkspace_const_sptr wsConst; + IMDHistoWorkspace_sptr wsNonConst; + TS_ASSERT_THROWS_NOTHING( + wsConst = manager.getValue<IMDHistoWorkspace_const_sptr>(wsName)); + TS_ASSERT(wsConst != NULL); + TS_ASSERT_THROWS_NOTHING( + wsNonConst = manager.getValue<IMDHistoWorkspace_sptr>(wsName)); + TS_ASSERT(wsNonConst != NULL); + TS_ASSERT_EQUALS(wsConst, wsNonConst); + + // Check TypedValue can be cast to const_sptr or to sptr + PropertyManagerHelper::TypedValue val(manager, wsName); + IMDHistoWorkspace_const_sptr wsCastConst; + IMDHistoWorkspace_sptr wsCastNonConst; + TS_ASSERT_THROWS_NOTHING(wsCastConst = (IMDHistoWorkspace_const_sptr)val); + TS_ASSERT(wsCastConst != NULL); + TS_ASSERT_THROWS_NOTHING(wsCastNonConst = (IMDHistoWorkspace_sptr)val); + TS_ASSERT(wsCastNonConst != NULL); + TS_ASSERT_EQUALS(wsCastConst, wsCastNonConst); + } }; #endif /* MANTID_DATAOBJECTS_MDHISTOWORKSPACETEST_H_ */ diff --git a/Framework/DataObjects/test/MaskWorkspaceTest.h b/Framework/DataObjects/test/MaskWorkspaceTest.h index 693e93fcc84d0fa500fccb411647af3f2fd48f11..f32094b5c2b86c788c5b69c6f1480f1f26bee012 100644 --- a/Framework/DataObjects/test/MaskWorkspaceTest.h +++ b/Framework/DataObjects/test/MaskWorkspaceTest.h @@ -6,6 +6,10 @@ #include "MantidDataObjects/MaskWorkspace.h" #include "MantidGeometry/Instrument.h" #include "MantidTestHelpers/ComponentCreationHelper.h" +#include "PropertyManagerHelper.h" + +using Mantid::DataObjects::MaskWorkspace_const_sptr; +using Mantid::DataObjects::MaskWorkspace_sptr; class MaskWorkspaceTest : public CxxTest::TestSuite { public: @@ -109,6 +113,38 @@ public: detIds.insert(2); TS_ASSERT_EQUALS(maskWS.isMasked(detIds), true); } + + /** + * Test declaring an input MaskWorkspace and retrieving it as const_sptr or + * sptr + */ + void testGetProperty_const_sptr() { + const std::string wsName = "InputWorkspace"; + MaskWorkspace_sptr wsInput(new Mantid::DataObjects::MaskWorkspace()); + PropertyManagerHelper manager; + manager.declareProperty(wsName, wsInput, Mantid::Kernel::Direction::Input); + + // Check property can be obtained as const_sptr or sptr + MaskWorkspace_const_sptr wsConst; + MaskWorkspace_sptr wsNonConst; + TS_ASSERT_THROWS_NOTHING( + wsConst = manager.getValue<MaskWorkspace_const_sptr>(wsName)); + TS_ASSERT(wsConst != NULL); + TS_ASSERT_THROWS_NOTHING(wsNonConst = + manager.getValue<MaskWorkspace_sptr>(wsName)); + TS_ASSERT(wsNonConst != NULL); + TS_ASSERT_EQUALS(wsConst, wsNonConst); + + // Check TypedValue can be cast to const_sptr or to sptr + PropertyManagerHelper::TypedValue val(manager, wsName); + MaskWorkspace_const_sptr wsCastConst; + MaskWorkspace_sptr wsCastNonConst; + TS_ASSERT_THROWS_NOTHING(wsCastConst = (MaskWorkspace_const_sptr)val); + TS_ASSERT(wsCastConst != NULL); + TS_ASSERT_THROWS_NOTHING(wsCastNonConst = (MaskWorkspace_sptr)val); + TS_ASSERT(wsCastNonConst != NULL); + TS_ASSERT_EQUALS(wsCastConst, wsCastNonConst); + } }; #endif // MANTID_DATAOBJECTS_MASKWORKSPACETEST_H diff --git a/Framework/DataObjects/test/OffsetsWorkspaceTest.h b/Framework/DataObjects/test/OffsetsWorkspaceTest.h index 08f5bff01aa0ce8691a045bfd08e5373725ad446..c910bbb631d9c58555d04060070ee9ee96549e0f 100644 --- a/Framework/DataObjects/test/OffsetsWorkspaceTest.h +++ b/Framework/DataObjects/test/OffsetsWorkspaceTest.h @@ -7,6 +7,7 @@ #include "MantidDataObjects/OffsetsWorkspace.h" #include "MantidTestHelpers/ComponentCreationHelper.h" +#include "PropertyManagerHelper.h" using namespace Mantid::DataObjects; using namespace Mantid::API; @@ -21,6 +22,38 @@ public: OffsetsWorkspace ws(inst); TS_ASSERT_THROWS_NOTHING(ws.clone()); } + + /** + * Test declaring an input OffsetsWorkspace and retrieving it as const_sptr or + * sptr + */ + void testGetProperty_const_sptr() { + const std::string wsName = "InputWorkspace"; + OffsetsWorkspace_sptr wsInput(new OffsetsWorkspace()); + PropertyManagerHelper manager; + manager.declareProperty(wsName, wsInput, Mantid::Kernel::Direction::Input); + + // Check property can be obtained as const_sptr or sptr + OffsetsWorkspace_const_sptr wsConst; + OffsetsWorkspace_sptr wsNonConst; + TS_ASSERT_THROWS_NOTHING( + wsConst = manager.getValue<OffsetsWorkspace_const_sptr>(wsName)); + TS_ASSERT(wsConst != NULL); + TS_ASSERT_THROWS_NOTHING( + wsNonConst = manager.getValue<OffsetsWorkspace_sptr>(wsName)); + TS_ASSERT(wsNonConst != NULL); + TS_ASSERT_EQUALS(wsConst, wsNonConst); + + // Check TypedValue can be cast to const_sptr or to sptr + PropertyManagerHelper::TypedValue val(manager, wsName); + OffsetsWorkspace_const_sptr wsCastConst; + OffsetsWorkspace_sptr wsCastNonConst; + TS_ASSERT_THROWS_NOTHING(wsCastConst = (OffsetsWorkspace_const_sptr)val); + TS_ASSERT(wsCastConst != NULL); + TS_ASSERT_THROWS_NOTHING(wsCastNonConst = (OffsetsWorkspace_sptr)val); + TS_ASSERT(wsCastNonConst != NULL); + TS_ASSERT_EQUALS(wsCastConst, wsCastNonConst); + } }; #endif /* MANTID_DATAOBJECTS_OFFSETSWORKSPACETEST_H_ */ diff --git a/Framework/DataObjects/test/PeaksWorkspaceTest.h b/Framework/DataObjects/test/PeaksWorkspaceTest.h index 3ffb18821ed63a22a906b908c50c422ce1bf54ac..9c523ee558985ba11866c17a52d8d2002805812b 100644 --- a/Framework/DataObjects/test/PeaksWorkspaceTest.h +++ b/Framework/DataObjects/test/PeaksWorkspaceTest.h @@ -18,6 +18,7 @@ #include "MantidTestHelpers/ComponentCreationHelper.h" #include "MantidAPI/AlgorithmManager.h" #include "MantidAPI/LogManager.h" +#include "PropertyManagerHelper.h" #include <Poco/File.h> @@ -418,6 +419,70 @@ public: delete peak; } + /** + * Test declaring an input PeaksWorkspace and retrieving it as const_sptr or + * sptr + */ + void testGetProperty_const_sptr() { + const std::string wsName = "InputWorkspace"; + PeaksWorkspace_sptr wsInput(new PeaksWorkspace()); + PropertyManagerHelper manager; + manager.declareProperty(wsName, wsInput, Mantid::Kernel::Direction::Input); + + // Check property can be obtained as const_sptr or sptr + PeaksWorkspace_const_sptr wsConst; + PeaksWorkspace_sptr wsNonConst; + TS_ASSERT_THROWS_NOTHING( + wsConst = manager.getValue<PeaksWorkspace_const_sptr>(wsName)); + TS_ASSERT(wsConst != NULL); + TS_ASSERT_THROWS_NOTHING(wsNonConst = + manager.getValue<PeaksWorkspace_sptr>(wsName)); + TS_ASSERT(wsNonConst != NULL); + TS_ASSERT_EQUALS(wsConst, wsNonConst); + + // Check TypedValue can be cast to const_sptr or to sptr + PropertyManagerHelper::TypedValue val(manager, wsName); + PeaksWorkspace_const_sptr wsCastConst; + PeaksWorkspace_sptr wsCastNonConst; + TS_ASSERT_THROWS_NOTHING(wsCastConst = (PeaksWorkspace_const_sptr)val); + TS_ASSERT(wsCastConst != NULL); + TS_ASSERT_THROWS_NOTHING(wsCastNonConst = (PeaksWorkspace_sptr)val); + TS_ASSERT(wsCastNonConst != NULL); + TS_ASSERT_EQUALS(wsCastConst, wsCastNonConst); + } + + /** + * Test declaring an input IPeaksWorkspace and retrieving it as const_sptr or + * sptr + */ + void testGetProperty_IPeaksWS_const_sptr() { + const std::string wsName = "InputWorkspace"; + IPeaksWorkspace_sptr wsInput(new PeaksWorkspace()); + PropertyManagerHelper manager; + manager.declareProperty(wsName, wsInput, Mantid::Kernel::Direction::Input); + + // Check property can be obtained as const_sptr or sptr + IPeaksWorkspace_const_sptr wsConst; + IPeaksWorkspace_sptr wsNonConst; + TS_ASSERT_THROWS_NOTHING( + wsConst = manager.getValue<IPeaksWorkspace_const_sptr>(wsName)); + TS_ASSERT(wsConst != NULL); + TS_ASSERT_THROWS_NOTHING( + wsNonConst = manager.getValue<IPeaksWorkspace_sptr>(wsName)); + TS_ASSERT(wsNonConst != NULL); + TS_ASSERT_EQUALS(wsConst, wsNonConst); + + // Check TypedValue can be cast to const_sptr or to sptr + PropertyManagerHelper::TypedValue val(manager, wsName); + IPeaksWorkspace_const_sptr wsCastConst; + IPeaksWorkspace_sptr wsCastNonConst; + TS_ASSERT_THROWS_NOTHING(wsCastConst = (IPeaksWorkspace_const_sptr)val); + TS_ASSERT(wsCastConst != NULL); + TS_ASSERT_THROWS_NOTHING(wsCastNonConst = (IPeaksWorkspace_sptr)val); + TS_ASSERT(wsCastNonConst != NULL); + TS_ASSERT_EQUALS(wsCastConst, wsCastNonConst); + } + private: PeaksWorkspace_sptr createSaveTestPeaksWorkspace() { // Create peak workspace diff --git a/Framework/DataObjects/test/PropertyManagerHelper.h b/Framework/DataObjects/test/PropertyManagerHelper.h new file mode 100644 index 0000000000000000000000000000000000000000..29473cf08863c7fca93dd5ad61ce1ca43fabee23 --- /dev/null +++ b/Framework/DataObjects/test/PropertyManagerHelper.h @@ -0,0 +1,19 @@ +#ifndef DATAOBJECTSTEST_PROPERTYMANAGERHELPER_H_ +#define DATAOBJECTSTEST_PROPERTYMANAGERHELPER_H_ + +#include "MantidKernel/PropertyManager.h" + +/** +* Helper class to use IPropertyManager methods in the DataObjects tests +*/ +class PropertyManagerHelper : public Mantid::Kernel::PropertyManager { +public: + PropertyManagerHelper() : PropertyManager() {} + + using PropertyManager::declareProperty; + using PropertyManager::setProperty; + using IPropertyManager::TypedValue; + using IPropertyManager::getValue; +}; + +#endif diff --git a/Framework/DataObjects/test/SpecialWorkspace2DTest.h b/Framework/DataObjects/test/SpecialWorkspace2DTest.h index 9598500b31560a7431da88ae9ef797a53952a208..a9ee4305c129b0f02700adec3bf3c4baafeb3081 100644 --- a/Framework/DataObjects/test/SpecialWorkspace2DTest.h +++ b/Framework/DataObjects/test/SpecialWorkspace2DTest.h @@ -7,6 +7,7 @@ #include "MantidTestHelpers/ComponentCreationHelper.h" #include "MantidTestHelpers/WorkspaceCreationHelper.h" #include "MantidAPI/WorkspaceProperty.h" +#include "PropertyManagerHelper.h" #include <cxxtest/TestSuite.h> using namespace Mantid::DataObjects; @@ -214,6 +215,38 @@ public: TS_ASSERT_EQUALS("SpecialWorkspace2D", Mantid::Kernel::getUnmangledTypeName( *property.type_info())); } + + /** + * Test declaring an input SpecialWorkspace2D and retrieving it as const_sptr + * or sptr + */ + void testGetProperty_const_sptr() { + const std::string wsName = "InputWorkspace"; + SpecialWorkspace2D_sptr wsInput(new SpecialWorkspace2D()); + PropertyManagerHelper manager; + manager.declareProperty(wsName, wsInput, Mantid::Kernel::Direction::Input); + + // Check property can be obtained as const_sptr or sptr + SpecialWorkspace2D_const_sptr wsConst; + SpecialWorkspace2D_sptr wsNonConst; + TS_ASSERT_THROWS_NOTHING( + wsConst = manager.getValue<SpecialWorkspace2D_const_sptr>(wsName)); + TS_ASSERT(wsConst != NULL); + TS_ASSERT_THROWS_NOTHING( + wsNonConst = manager.getValue<SpecialWorkspace2D_sptr>(wsName)); + TS_ASSERT(wsNonConst != NULL); + TS_ASSERT_EQUALS(wsConst, wsNonConst); + + // Check TypedValue can be cast to const_sptr or to sptr + PropertyManagerHelper::TypedValue val(manager, wsName); + SpecialWorkspace2D_const_sptr wsCastConst; + SpecialWorkspace2D_sptr wsCastNonConst; + TS_ASSERT_THROWS_NOTHING(wsCastConst = (SpecialWorkspace2D_const_sptr)val); + TS_ASSERT(wsCastConst != NULL); + TS_ASSERT_THROWS_NOTHING(wsCastNonConst = (SpecialWorkspace2D_sptr)val); + TS_ASSERT(wsCastNonConst != NULL); + TS_ASSERT_EQUALS(wsCastConst, wsCastNonConst); + } }; #endif /* MANTID_DATAOBJECTS_SPECIALWORKSPACE2DTEST_H_ */ diff --git a/Framework/DataObjects/test/SplittersWorkspaceTest.h b/Framework/DataObjects/test/SplittersWorkspaceTest.h index 45f52e9334e7cd73ad1f9368fda2e53b376731df..8aea54f47aabc9e71ebe3f960e8f6a6d802f18c7 100644 --- a/Framework/DataObjects/test/SplittersWorkspaceTest.h +++ b/Framework/DataObjects/test/SplittersWorkspaceTest.h @@ -5,7 +5,7 @@ #include "MantidKernel/Timer.h" #include "MantidKernel/System.h" #include "MantidAPI/WorkspaceProperty.h" - +#include "PropertyManagerHelper.h" #include "MantidDataObjects/SplittersWorkspace.h" using namespace Mantid; @@ -93,6 +93,38 @@ public: TS_ASSERT_EQUALS("SplittersWorkspace", Mantid::Kernel::getUnmangledTypeName( *property.type_info())); } + + /** + * Test declaring an input SplittersWorkspace and retrieving it as const_sptr + * or sptr + */ + void testGetProperty_const_sptr() { + const std::string wsName = "InputWorkspace"; + SplittersWorkspace_sptr wsInput(new SplittersWorkspace()); + PropertyManagerHelper manager; + manager.declareProperty(wsName, wsInput, Mantid::Kernel::Direction::Input); + + // Check property can be obtained as const_sptr or sptr + SplittersWorkspace_const_sptr wsConst; + SplittersWorkspace_sptr wsNonConst; + TS_ASSERT_THROWS_NOTHING( + wsConst = manager.getValue<SplittersWorkspace_const_sptr>(wsName)); + TS_ASSERT(wsConst != NULL); + TS_ASSERT_THROWS_NOTHING( + wsNonConst = manager.getValue<SplittersWorkspace_sptr>(wsName)); + TS_ASSERT(wsNonConst != NULL); + TS_ASSERT_EQUALS(wsConst, wsNonConst); + + // Check TypedValue can be cast to const_sptr or to sptr + PropertyManagerHelper::TypedValue val(manager, wsName); + SplittersWorkspace_const_sptr wsCastConst; + SplittersWorkspace_sptr wsCastNonConst; + TS_ASSERT_THROWS_NOTHING(wsCastConst = (SplittersWorkspace_const_sptr)val); + TS_ASSERT(wsCastConst != NULL); + TS_ASSERT_THROWS_NOTHING(wsCastNonConst = (SplittersWorkspace_sptr)val); + TS_ASSERT(wsCastNonConst != NULL); + TS_ASSERT_EQUALS(wsCastConst, wsCastNonConst); + } }; #endif /* MANTID_DATAOBJECTS_SPLITTERSWORKSPACETEST_H_ */ diff --git a/Framework/DataObjects/test/TableWorkspaceTest.h b/Framework/DataObjects/test/TableWorkspaceTest.h index f2cb5a7979223a32837d21d7ab18bf2d6fd8fa45..2e9318637562259388f7fc8845de56ee007373d4 100644 --- a/Framework/DataObjects/test/TableWorkspaceTest.h +++ b/Framework/DataObjects/test/TableWorkspaceTest.h @@ -11,6 +11,7 @@ #include "MantidAPI/TableRow.h" #include "MantidAPI/ColumnFactory.h" #include "MantidAPI/WorkspaceProperty.h" +#include "PropertyManagerHelper.h" #include <limits> @@ -646,6 +647,70 @@ public: TS_ASSERT_EQUALS(data2[0], "hello"); TS_ASSERT_EQUALS(data3[0], 5); } + + /** + * Test declaring an input TableWorkspace and retrieving it as const_sptr + * or sptr + */ + void testGetProperty_const_sptr() { + const std::string wsName = "InputWorkspace"; + TableWorkspace_sptr wsInput(new TableWorkspace()); + PropertyManagerHelper manager; + manager.declareProperty(wsName, wsInput, Mantid::Kernel::Direction::Input); + + // Check property can be obtained as const_sptr or sptr + TableWorkspace_const_sptr wsConst; + TableWorkspace_sptr wsNonConst; + TS_ASSERT_THROWS_NOTHING( + wsConst = manager.getValue<TableWorkspace_const_sptr>(wsName)); + TS_ASSERT(wsConst != NULL); + TS_ASSERT_THROWS_NOTHING(wsNonConst = + manager.getValue<TableWorkspace_sptr>(wsName)); + TS_ASSERT(wsNonConst != NULL); + TS_ASSERT_EQUALS(wsConst, wsNonConst); + + // Check TypedValue can be cast to const_sptr or to sptr + PropertyManagerHelper::TypedValue val(manager, wsName); + TableWorkspace_const_sptr wsCastConst; + TableWorkspace_sptr wsCastNonConst; + TS_ASSERT_THROWS_NOTHING(wsCastConst = (TableWorkspace_const_sptr)val); + TS_ASSERT(wsCastConst != NULL); + TS_ASSERT_THROWS_NOTHING(wsCastNonConst = (TableWorkspace_sptr)val); + TS_ASSERT(wsCastNonConst != NULL); + TS_ASSERT_EQUALS(wsCastConst, wsCastNonConst); + } + + /** + * Test declaring an input ITableWorkspace and retrieving it as const_sptr + * or sptr + */ + void testGetProperty_ITableWS_const_sptr() { + const std::string wsName = "InputWorkspace"; + ITableWorkspace_sptr wsInput(new TableWorkspace()); + PropertyManagerHelper manager; + manager.declareProperty(wsName, wsInput, Mantid::Kernel::Direction::Input); + + // Check property can be obtained as const_sptr or sptr + ITableWorkspace_const_sptr wsConst; + ITableWorkspace_sptr wsNonConst; + TS_ASSERT_THROWS_NOTHING( + wsConst = manager.getValue<ITableWorkspace_const_sptr>(wsName)); + TS_ASSERT(wsConst != NULL); + TS_ASSERT_THROWS_NOTHING( + wsNonConst = manager.getValue<ITableWorkspace_sptr>(wsName)); + TS_ASSERT(wsNonConst != NULL); + TS_ASSERT_EQUALS(wsConst, wsNonConst); + + // Check TypedValue can be cast to const_sptr or to sptr + PropertyManagerHelper::TypedValue val(manager, wsName); + ITableWorkspace_const_sptr wsCastConst; + ITableWorkspace_sptr wsCastNonConst; + TS_ASSERT_THROWS_NOTHING(wsCastConst = (ITableWorkspace_const_sptr)val); + TS_ASSERT(wsCastConst != NULL); + TS_ASSERT_THROWS_NOTHING(wsCastNonConst = (ITableWorkspace_sptr)val); + TS_ASSERT(wsCastNonConst != NULL); + TS_ASSERT_EQUALS(wsCastConst, wsCastNonConst); + } }; #endif /*TESTTABLEWORKSPACE_*/ diff --git a/Framework/DataObjects/test/Workspace2DTest.h b/Framework/DataObjects/test/Workspace2DTest.h index a0561b75fee9a953d52b9bb1b8a7e37766c36267..a4aed545520a0daa281988ae5ec88115b4b3ff77 100644 --- a/Framework/DataObjects/test/Workspace2DTest.h +++ b/Framework/DataObjects/test/Workspace2DTest.h @@ -9,6 +9,7 @@ #include "MantidAPI/ISpectrum.h" #include "MantidAPI/SpectraAxis.h" #include "MantidKernel/CPUTimer.h" +#include "PropertyManagerHelper.h" using namespace std; using namespace Mantid; @@ -233,6 +234,39 @@ public: TS_ASSERT(spec); TS_ASSERT_THROWS_ANYTHING(spec = ws->getSpectrum(4)); } + + /** + * Test that a Workspace2D_sptr can be held as a property and + * retrieved as const or non-const sptr, + * and that the cast from TypedValue works properly + */ + void testGetProperty_const_sptr() { + const std::string wsName = "InputWorkspace"; + Workspace2D_sptr wsInput(new Workspace2D()); + PropertyManagerHelper manager; + manager.declareProperty(wsName, wsInput, Mantid::Kernel::Direction::Input); + + // Check property can be obtained as const_sptr or sptr + Workspace2D_const_sptr wsConst; + Workspace2D_sptr wsNonConst; + TS_ASSERT_THROWS_NOTHING( + wsConst = manager.getValue<Workspace2D_const_sptr>(wsName)); + TS_ASSERT(wsConst != NULL); + TS_ASSERT_THROWS_NOTHING(wsNonConst = + manager.getValue<Workspace2D_sptr>(wsName)); + TS_ASSERT(wsNonConst != NULL); + TS_ASSERT_EQUALS(wsConst, wsNonConst); + + // Check TypedValue can be cast to const_sptr or to sptr + PropertyManagerHelper::TypedValue val(manager, wsName); + Workspace2D_const_sptr wsCastConst; + Workspace2D_sptr wsCastNonConst; + TS_ASSERT_THROWS_NOTHING(wsCastConst = (Workspace2D_const_sptr)val); + TS_ASSERT(wsCastConst != NULL); + TS_ASSERT_THROWS_NOTHING(wsCastNonConst = (Workspace2D_sptr)val); + TS_ASSERT(wsCastNonConst != NULL); + TS_ASSERT_EQUALS(wsCastConst, wsCastNonConst); + } }; class Workspace2DTestPerformance : public CxxTest::TestSuite { @@ -291,4 +325,5 @@ public: << std::endl; } }; + #endif diff --git a/Framework/DataObjects/test/WorkspaceSingleValueTest.h b/Framework/DataObjects/test/WorkspaceSingleValueTest.h index 89ff2e6f2ee9a6f7e9bc4760aa8d679caf7c5cfd..facddcc1d3fab004581f258269ebbcd8c2f704e9 100644 --- a/Framework/DataObjects/test/WorkspaceSingleValueTest.h +++ b/Framework/DataObjects/test/WorkspaceSingleValueTest.h @@ -7,8 +7,11 @@ #include <cxxtest/TestSuite.h> #include "MantidDataObjects/WorkspaceSingleValue.h" +#include "PropertyManagerHelper.h" using Mantid::DataObjects::WorkspaceSingleValue; +using Mantid::DataObjects::WorkspaceSingleValue_const_sptr; +using Mantid::DataObjects::WorkspaceSingleValue_sptr; class WorkspaceSingleValueTest : public CxxTest::TestSuite { @@ -59,5 +62,38 @@ public: WorkspaceSingleValue ws; TS_ASSERT_EQUALS(0, ws.getNumDims()); } + + /** + * Test declaring an input WorkspaceSingleValue and retrieving it as const_sptr + * or sptr + */ + void testGetProperty_const_sptr() { + const std::string wsName = "InputWorkspace"; + WorkspaceSingleValue_sptr wsInput(new WorkspaceSingleValue()); + PropertyManagerHelper manager; + manager.declareProperty(wsName, wsInput, Mantid::Kernel::Direction::Input); + + // Check property can be obtained as const_sptr or sptr + WorkspaceSingleValue_const_sptr wsConst; + WorkspaceSingleValue_sptr wsNonConst; + TS_ASSERT_THROWS_NOTHING( + wsConst = manager.getValue<WorkspaceSingleValue_const_sptr>(wsName)); + TS_ASSERT(wsConst != NULL); + TS_ASSERT_THROWS_NOTHING( + wsNonConst = manager.getValue<WorkspaceSingleValue_sptr>(wsName)); + TS_ASSERT(wsNonConst != NULL); + TS_ASSERT_EQUALS(wsConst, wsNonConst); + + // Check TypedValue can be cast to const_sptr or to sptr + PropertyManagerHelper::TypedValue val(manager, wsName); + WorkspaceSingleValue_const_sptr wsCastConst; + WorkspaceSingleValue_sptr wsCastNonConst; + TS_ASSERT_THROWS_NOTHING(wsCastConst = + (WorkspaceSingleValue_const_sptr)val); + TS_ASSERT(wsCastConst != NULL); + TS_ASSERT_THROWS_NOTHING(wsCastNonConst = (WorkspaceSingleValue_sptr)val); + TS_ASSERT(wsCastNonConst != NULL); + TS_ASSERT_EQUALS(wsCastConst, wsCastNonConst); + } }; #endif /*TESTWORKSPACESINGLEVALUE_*/ diff --git a/Framework/Kernel/inc/MantidKernel/IPropertyManager.h b/Framework/Kernel/inc/MantidKernel/IPropertyManager.h index b606962bec3a857d1f5c869e14c2abb2d135464d..dbfaa5cab13978a8aa59957d67c9345a748b5b2c 100644 --- a/Framework/Kernel/inc/MantidKernel/IPropertyManager.h +++ b/Framework/Kernel/inc/MantidKernel/IPropertyManager.h @@ -403,6 +403,10 @@ protected: template <typename T> operator boost::shared_ptr<T>() { return pm.getValue<boost::shared_ptr<T>>(prop); } + /// explicit specialization for boost::shared_ptr to const T + template <typename T> operator boost::shared_ptr<const T>() { + return pm.getValue<boost::shared_ptr<T>>(prop); + } /// explicit version for Matrices template <typename T> operator Matrix<T>() { return pm.getValue<Matrix<T>>(prop);