Skip to content

Commit

Permalink
Re #2702. Added FunctionProperty.
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed Feb 9, 2012
1 parent 948341d commit e6adb86
Show file tree
Hide file tree
Showing 10 changed files with 407 additions and 313 deletions.
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/API/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ set ( SRC_FILES
src/FrameworkManager.cpp
src/FunctionDomain.cpp
src/FunctionFactory.cpp
src/FunctionProperty.cpp
src/IDataFileChecker.cpp
src/IEventList.cpp
src/IEventWorkspace.cpp
Expand Down Expand Up @@ -127,6 +128,7 @@ set ( INC_FILES
inc/MantidAPI/FrameworkManager.h
inc/MantidAPI/FunctionDomain.h
inc/MantidAPI/FunctionFactory.h
inc/MantidAPI/FunctionProperty.h
inc/MantidAPI/IAlgorithm.h
inc/MantidAPI/IArchiveSearch.h
inc/MantidAPI/IBackgroundFunction.h
Expand Down Expand Up @@ -223,6 +225,7 @@ set ( TEST_FILES
test/FilePropertyTest.h
test/FrameworkManagerTest.h
test/FunctionTest.h
test/FunctionPropertyTest.h
test/IEventListTest.h
test/IFunctionMDTest.h
test/IMDWorkspaceTest.h
Expand Down
103 changes: 103 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/FunctionProperty.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#ifndef MANTID_API_FUNCTIONPROPERTY_H_
#define MANTID_API_FUNCTIONPROPERTY_H_

//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/DllConfig.h"
#include "MantidAPI/IFitFunction.h"
#include "MantidKernel/PropertyWithValue.h"
#include "MantidKernel/Logger.h"
#include "MantidKernel/Exception.h"

#include <boost/shared_ptr.hpp>

#include <iostream>
#include <string>

namespace Mantid
{
namespace API
{
/** A property class for functions. Holds a shared pointer to a function. The string representation
is the creation string accepted by the FunctionFactory.
@author Roman Tolchenov, Tessella plc
@date 08/02/2012
Copyright &copy; 2007-2010 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>
*/
class MANTID_API_DLL FunctionProperty : public Kernel::PropertyWithValue< boost::shared_ptr<IFitFunction> >
{
public:
/// Constructor.
explicit FunctionProperty( const std::string &name );

/// Copy constructor
FunctionProperty( const FunctionProperty& right );

/// Copy assignment operator. Only copies the value (i.e. the pointer to the workspace)
FunctionProperty& operator=( const FunctionProperty& right );

/// Bring in the PropertyWithValue assignment operator explicitly (avoids VSC++ warning)
virtual boost::shared_ptr<IFitFunction>& operator=( const boost::shared_ptr<IFitFunction>& value );

///Add the value of another property
virtual FunctionProperty& operator+=( Kernel::Property const * );

/// 'Virtual copy constructor'
Kernel::Property* clone() { return new FunctionProperty(*this); }

/// Virtual destructor
virtual ~FunctionProperty();

/// Get the name of the workspace
virtual std::string value() const;

/// Get the value the property was initialised with -its default value
virtual std::string getDefault() const;

/// Set the value of the property
virtual std::string setValue( const std::string& value );

/// Checks whether the entered function is valid.
std::string isValid() const;

/// Is default
bool isDefault() const;

/// Create a history record
virtual const Kernel::PropertyHistory createHistory() const;

private:

/// The name of the workspace (as used by the AnalysisDataService)
std::string m_definition;

/// for access to logging streams
static Kernel::Logger& g_log;
};


} // namespace API
} // namespace Mantid

#endif /*MANTID_API_FUNCTIONPROPERTY_H_*/
2 changes: 2 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/IFitFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ class MANTID_API_DLL IFitFunction: public virtual IFunction
/// Overload operator <<
MANTID_API_DLL std::ostream& operator<<(std::ostream& ostr,const IFitFunction& f);

typedef boost::shared_ptr<IFitFunction> IFitFunction_sptr;

/**
* Classes inherited from FunctionHandler will handle the function.
* The intended purpose is to help with displaying nested composite
Expand Down
121 changes: 121 additions & 0 deletions Code/Mantid/Framework/API/src/FunctionProperty.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#include "MantidAPI/FunctionProperty.h"
#include "MantidAPI/FunctionFactory.h"

namespace Mantid
{
namespace API
{
Kernel::Logger& FunctionProperty::g_log = Kernel::Logger::get("FunctionProperty");

/** Constructor.
* Sets the property names but initialises the function pointer to null.
* @param name :: The name to assign to the property
*/
FunctionProperty::FunctionProperty( const std::string &name ) :
Kernel::PropertyWithValue <boost::shared_ptr<IFitFunction> >(
name,
boost::shared_ptr<IFitFunction>( ),
new Kernel::NullValidator< boost::shared_ptr<IFitFunction> >,
Kernel::Direction::InOut
)
{
}

/// Copy constructor
FunctionProperty::FunctionProperty( const FunctionProperty& right ) :
Kernel::PropertyWithValue< boost::shared_ptr<IFitFunction> >( right )
{
}

/// Copy assignment operator. Copies the pointer to the function.
FunctionProperty& FunctionProperty::operator=( const FunctionProperty& right )
{
if ( &right == this ) return *this;
Kernel::PropertyWithValue< boost::shared_ptr<IFitFunction> >::operator=( right );
return *this;
}

/** Bring in the PropertyWithValue assignment operator explicitly (avoids VSC++ warning)
* @param value :: The value to set to
* @return assigned PropertyWithValue
*/
boost::shared_ptr<IFitFunction>& FunctionProperty::operator=( const boost::shared_ptr<IFitFunction>& value )
{
return Kernel::PropertyWithValue< boost::shared_ptr<IFitFunction> >::operator=( value );
}

//--------------------------------------------------------------------------------------
///Add the value of another property
FunctionProperty& FunctionProperty::operator+=( Kernel::Property const * )
{
throw Kernel::Exception::NotImplementedError("+= operator is not implemented for FunctionProperty.");
return *this;
}

/// Virtual destructor
FunctionProperty::~FunctionProperty()
{
}

/** Get the function definition
* @return The function definition
*/
std::string FunctionProperty::value() const
{
return m_value->asString();
}

/** Get the value the property was initialised with -its default value
* @return The default value
*/
std::string FunctionProperty::getDefault() const
{
return "";
}

/** Set the function definition.
* Also tries to create the function with FunctionFactory.
* @param value :: The function definition string.
* @return Error message from FunctionFactory or "" on success.
*/
std::string FunctionProperty::setValue( const std::string& value )
{
std::string error = "";
try
{
m_value = boost::shared_ptr<IFitFunction>(FunctionFactory::Instance().createInitialized(value));
m_definition = value;
}
catch(std::exception& e)
{
error = e.what();
}
return error;
}

/** Checks whether the entered function is valid.
* To be valid it has to be
* @returns A user level description of the problem or "" if it is valid.
*/
std::string FunctionProperty::isValid() const
{
return isDefault() ? "Funcion is empty." : "";
}

/** Indicates if the object is still pointing to the same workspace, using the workspace name
* @return true if the value is the same as the initial value or false otherwise
*/
bool FunctionProperty::isDefault() const
{
return m_value == boost::shared_ptr<IFitFunction>();
}

/// Create a history record
/// @return A populated PropertyHistory for this class
const Kernel::PropertyHistory FunctionProperty::createHistory() const
{
return Kernel::PropertyHistory(this->name(),this->value(),this->type(),this->isDefault(),Kernel::PropertyWithValue<boost::shared_ptr<IFitFunction> >::direction());
}

} // namespace API
} // namespace Mantid

0 comments on commit e6adb86

Please sign in to comment.