Skip to content

Commit

Permalink
refs #3925, refs #3763 initial attempt to implement signaling validators
Browse files Browse the repository at this point in the history
  • Loading branch information
abuts committed Nov 3, 2011
1 parent a88f646 commit d07ca53
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Code/Mantid/Framework/Kernel/CMakeLists.txt
Expand Up @@ -183,6 +183,7 @@ set ( INC_FILES
inc/MantidKernel/VectorHelper.h
inc/MantidKernel/VisibleWhenProperty.h
inc/MantidKernel/cow_ptr.h
inc/MantidKernel/ValidatorSignalChange.h
)

set ( TEST_FILES
Expand Down Expand Up @@ -257,6 +258,7 @@ set ( TEST_FILES
test/VMDTest.h
test/VectorHelperTest.h
test/VisibleWhenPropertyTest.h
test/ValidatorSignalChangeTest.h
)

if(UNITY_BUILD)
Expand Down
@@ -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 &copy; 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 Code/Mantid/Framework/Kernel/test/ValidatorSignalChangeTest.h
@@ -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_ */

0 comments on commit d07ca53

Please sign in to comment.