Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
3 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
Code/Mantid/Framework/Kernel/inc/MantidKernel/ValidatorSignalChange.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#ifndef MANTID_KERNEL_VALIDATOR_SIGNAL_CHANGE_H_ | ||
#define MANTID_KERNEL_VALIDATOR_SIGNAL_CHANGE_H_ | ||
|
||
//---------------------------------------------------------------------- | ||
// Includes | ||
//---------------------------------------------------------------------- | ||
#include "MantidKernel/DllConfig.h" | ||
#include "MantidKernel/IValidator.h" | ||
#include "MantidKernel/Property.h" | ||
#include "MantidKernel/Logger.h" | ||
#include "boost/Signal.hpp" | ||
//#include <string> | ||
|
||
namespace Mantid | ||
{ | ||
namespace Kernel | ||
{ | ||
|
||
/** ValidatorSignalChange is a validator, which connects to a property and signals to subscribers that this property has changed | ||
@author Alex Buts | ||
@date 03/11/2011 | ||
Copyright © 2007-9 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 <http://www.gnu.org/licenses/>. | ||
File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>. | ||
Code Documentation is available at: <http://doxygen.mantidproject.org> | ||
*/ | ||
template <typename TYPE> | ||
class DLLExport ValidatorSignalChange : public IValidator<TYPE>, public boost::signal<void (const Property *)> | ||
{ | ||
public: | ||
/// Constructor | ||
ValidatorSignalChange(const Property *pProp): | ||
pPropObserved(pProp) | ||
{} | ||
|
||
///virtual Destructor | ||
virtual ~ValidatorSignalChange() {} | ||
|
||
//------------------------------------------------------------------------------------------------------------ | ||
/** Calls the validator | ||
* Always valid | ||
*/ | ||
std::string isValid(const TYPE &value) const{ | ||
this->operator()(pPropObserved); | ||
return ""; | ||
} | ||
|
||
//------------------------------------------------------------------------------------------------------------ | ||
/** Does not verify allowed values | ||
* @return The set of allowed values that this validator may have or an empty set | ||
*/ | ||
std::set<std::string> allowedValues() const { return std::set<std::string>(); } | ||
// | ||
/** Make a copy of the present type of validator */ | ||
IValidator<TYPE>* clone(){return static_cast<IValidator<TYPE>* >(NULL);} | ||
// | ||
protected: | ||
/// always valid | ||
std::string checkValidity(const TYPE &) const{return std::string("");} | ||
private: | ||
const Property *pPropObserved; | ||
// boost::signal<void (Property *)> *pSignal; | ||
|
||
}; | ||
} // namespace Kernel | ||
} // namespace Mantid | ||
|
||
#endif /*MANTID_KERNEL_IVALIDATOR_H_*/ |
47 changes: 47 additions & 0 deletions
47
Code/Mantid/Framework/Kernel/test/ValidatorSignalChangeTest.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#ifndef MANTID_KERNEL_SIGNALCHANGETEST_H_ | ||
#define MANTID_KERNEL_SIGNALCHANGETEST_H_ | ||
|
||
#include <cxxtest/TestSuite.h> | ||
|
||
#include "MantidKernel/ValidatorSignalChange.h" | ||
#include "MantidKernel/PropertyWithValue.h" | ||
|
||
using namespace Mantid; | ||
using namespace Mantid::Kernel; | ||
|
||
void actOnPropertyChange(const Property *pProp) | ||
{ | ||
int middle_val(1); | ||
int new_val = boost::lexical_cast<int>(pProp->value()); | ||
if (new_val>middle_val){ | ||
throw(" new prop is bigger"); | ||
}else{ | ||
throw(std::string(" new prop is smaller")); | ||
} | ||
|
||
}; | ||
|
||
class SignalChangeTest : public CxxTest::TestSuite | ||
{ | ||
public: | ||
|
||
void test_SendSignal() | ||
{ | ||
|
||
PropertyWithValue<int> *ipProp = new PropertyWithValue<int>("intProp", 1); | ||
ValidatorSignalChange<int> * pValC = new ValidatorSignalChange<int>(ipProp); | ||
pValC->connect(&actOnPropertyChange); | ||
|
||
ipProp->setValue("2"); | ||
// value provided to validator function is irrelevant here, as check occurs on ipProp and the number is just to keep the function signaturel | ||
TSM_ASSERT_THROWS("should throw a pointer to ""new prop is bigger""",pValC->isValid(2),const char *); | ||
// | ||
ipProp->setValue("0"); | ||
// value provided to validator function is irrelevant here, as check occurs on ipProp and the number is just to keep the function signaturel | ||
TSM_ASSERT_THROWS("should throw a pointer to ""new prop is smaller""",pValC->isValid(2),std::string); | ||
// | ||
delete ipProp; | ||
delete pValC; | ||
} | ||
}; | ||
#endif /* MANTID_KERNEL_SIGNALCHANGETEST_H_ */ |