Skip to content

Commit

Permalink
Re #4158. Changed FunctionFactory to return shared pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed Apr 2, 2012
1 parent c7ce196 commit 1b02431
Show file tree
Hide file tree
Showing 21 changed files with 642 additions and 741 deletions.
1 change: 1 addition & 0 deletions Code/Mantid/Framework/API/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ set ( TEST_FILES
test/FunctionDomainTest.h
test/FunctionFactoryTest.h
test/FunctionTest.h
test/FunctionPropertyTest.h
test/FunctionValuesTest.h
test/IEventListTest.h
test/IFunction1DTest.h
Expand Down
21 changes: 13 additions & 8 deletions Code/Mantid/Framework/API/inc/MantidAPI/CompositeFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ class MANTID_API_DLL CompositeFunction : public virtual IFunction
/// Return parameter index from a parameter reference.
size_t getParameterIndex(const ParameterReference& ref)const;
/// Get the containing function
IFunction* getContainingFunction(const ParameterReference& ref)const;
IFunction_sptr getContainingFunction(const ParameterReference& ref)const;
/// Get the containing function
IFunction* getContainingFunction(const IFunction* fun);
//IFunction_sptr getContainingFunction(IFunction_sptr fun);

/// Apply the ties
void applyTies();
Expand All @@ -134,17 +134,17 @@ class MANTID_API_DLL CompositeFunction : public virtual IFunction
/* CompositeFunction own methods */

/// Add a function at the back of the internal function list
virtual size_t addFunction(IFunction* f);
virtual size_t addFunction(IFunction_sptr f);
/// Returns the pointer to i-th function
IFunction* getFunction(std::size_t i)const;
IFunction_sptr getFunction(std::size_t i)const;
/// Number of functions
std::size_t nFunctions()const{return m_functions.size();}
/// Remove a function
void removeFunction(size_t i, bool del=true);
void removeFunction(size_t i);
/// Replace a function
void replaceFunction(size_t i,IFunction* f);
void replaceFunction(size_t i,IFunction_sptr f);
/// Replace a function
void replaceFunctionPtr(const IFunction* f_old,IFunction* f_new);
void replaceFunctionPtr(const IFunction_sptr f_old,IFunction_sptr f_new);
/// Get the function index
std::size_t functionIndex(std::size_t i)const;
/// Get the function index
Expand Down Expand Up @@ -173,7 +173,7 @@ class MANTID_API_DLL CompositeFunction : public virtual IFunction
static void parseName(const std::string& varName,size_t& index, std::string& name);

/// Pointers to the included funtions
std::vector<IFunction*> m_functions;
std::vector<IFunction_sptr> m_functions;
/// Individual function parameter offsets (function index in m_functions)
/// e.g. m_functions[i]->activeParameter(m_activeOffsets[i]+1) gives second active parameter of i-th function
std::vector<size_t> m_activeOffsets;
Expand All @@ -193,6 +193,11 @@ class MANTID_API_DLL CompositeFunction : public virtual IFunction

};

///shared pointer to the composite function base class
typedef boost::shared_ptr<CompositeFunction> CompositeFunction_sptr;
///shared pointer to the composite function base class (const version)
typedef boost::shared_ptr<const CompositeFunction> CompositeFunction_const_sptr;

/** A Jacobian for individual functions
*/
class PartialJacobian: public Jacobian
Expand Down
22 changes: 12 additions & 10 deletions Code/Mantid/Framework/API/inc/MantidAPI/FunctionFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "MantidKernel/DynamicFactory.h"
#include "MantidKernel/SingletonHolder.h"

#include <boost/shared_ptr.hpp>

namespace Mantid
{

Expand Down Expand Up @@ -67,13 +69,13 @@ namespace API
* @param type :: The function's type
* @return A pointer to the created function
*/
IFunction* createFunction(const std::string& type) const;
boost::shared_ptr<IFunction> createFunction(const std::string& type) const;

///Creates an instance of a function
IFunction* createInitialized(const std::string& input) const;
boost::shared_ptr<IFunction> createInitialized(const std::string& input) const;

///Creates an instance of a function
IFunction* createFitFunction(const std::string& input) const;
boost::shared_ptr<IFunction> createFitFunction(const std::string& input) const;

/// Query available functions based on the template type
template<typename FunctionType>
Expand All @@ -96,22 +98,22 @@ namespace API
using Kernel::DynamicFactory<IFunction>::createUnwrapped;

/// Create a simple function
IFunction* createSimple(const Expression& expr)const;
boost::shared_ptr<IFunction> createSimple(const Expression& expr)const;
/// Create a composite function
CompositeFunction* createComposite(const Expression& expr)const;
boost::shared_ptr<CompositeFunction> createComposite(const Expression& expr)const;
///Creates an instance of a function
IFunction* createFitFunction(const Expression& expr) const;
boost::shared_ptr<IFunction> createFitFunction(const Expression& expr) const;

/// Throw an exception
void inputError(const std::string& str="")const;
/// Add constraints to the created function
void addConstraints(IFunction* fun,const Expression& expr)const;
void addConstraints(boost::shared_ptr<IFunction> fun,const Expression& expr)const;
/// Add a single constraint to the created function
void addConstraint(IFunction* fun,const Expression& expr)const;
void addConstraint(boost::shared_ptr<IFunction> fun,const Expression& expr)const;
/// Add ties to the created function
void addTies(IFunction* fun,const Expression& expr)const;
void addTies(boost::shared_ptr<IFunction> fun,const Expression& expr)const;
/// Add a tie to the created function
void addTie(IFunction* fun,const Expression& expr)const;
void addTie(boost::shared_ptr<IFunction> fun,const Expression& expr)const;

///static reference to the logger class
Kernel::Logger& g_log;
Expand Down
16 changes: 13 additions & 3 deletions Code/Mantid/Framework/API/inc/MantidAPI/FunctionProperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/DllConfig.h"
#include "MantidAPI/IFitFunction.h"
#include "MantidAPI/IFunction.h"
#include "MantidKernel/PropertyWithValue.h"
#include "MantidKernel/Logger.h"
#include "MantidKernel/Exception.h"
Expand Down Expand Up @@ -45,7 +45,7 @@ namespace Mantid
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> >
class MANTID_API_DLL FunctionProperty : public Kernel::PropertyWithValue< boost::shared_ptr<IFunction> >
{
public:
/// Constructor.
Expand All @@ -58,7 +58,7 @@ namespace Mantid
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 );
virtual boost::shared_ptr<IFunction>& operator=( const boost::shared_ptr<IFunction>& value );

///Add the value of another property
virtual FunctionProperty& operator+=( Kernel::Property const * );
Expand All @@ -78,6 +78,16 @@ namespace Mantid
/// Set the value of the property
virtual std::string setValue( const std::string& value );

/**
* Set a property value via a DataItem
* @param data :: A shared pointer to a data item
* @return "" if the assignment was successful or a user level description of the problem
*/
virtual std::string setValue(const boost::shared_ptr<Kernel::DataItem> data)
{
return Kernel::PropertyWithValue< boost::shared_ptr<IFunction> >::setValue(data);
}

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

Expand Down
17 changes: 7 additions & 10 deletions Code/Mantid/Framework/API/inc/MantidAPI/IFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,17 +296,9 @@ class MANTID_API_DLL IFunction

/// Return parameter index from a parameter reference. Usefull for constraints and ties in composite functions
virtual size_t getParameterIndex(const ParameterReference& ref)const = 0;
/// Get a function containing the parameter refered to by the reference. In case of a simple function
/// it will be the same as ParameterReference::getFunction(). In case of a CompositeFunction it returns
/// a top-level function that contains the parameter. The return function itself can be a CompositeFunction
/// @param ref :: The Parameter reference
/// @return A pointer to the containing function
virtual IFunction* getContainingFunction(const ParameterReference& ref)const = 0;
/// The same as the method above but the argument is a function
virtual IFunction* getContainingFunction(const IFunction* fun) = 0;

/// Tie a parameter to other parameters (or a constant)
virtual ParameterTie* tie(const std::string& parName,const std::string& expr);
virtual ParameterTie* tie(const std::string& parName, const std::string& expr);
/// Apply the ties
virtual void applyTies() = 0;
/// Removes the tie off a parameter
Expand Down Expand Up @@ -369,7 +361,7 @@ class MANTID_API_DLL IFunction
void calNumericalDeriv(const FunctionDomain& domain, Jacobian& out){}

/// Create an instance of a tie without actually tying it to anything
virtual ParameterTie* createTie(const std::string& parName);
//virtual ParameterTie* createTie(const std::string& parName);
/// Add a new tie
virtual void addTie(ParameterTie* tie) = 0;

Expand Down Expand Up @@ -405,6 +397,11 @@ class FunctionHandler
IFunction* m_fun;///< pointer to the handled function
};

///shared pointer to the function base class
typedef boost::shared_ptr<IFunction> IFunction_sptr;
///shared pointer to the function base class (const version)
typedef boost::shared_ptr<const IFunction> IFunction_const_sptr;

} // namespace API
} // namespace Mantid

Expand Down
4 changes: 2 additions & 2 deletions Code/Mantid/Framework/API/inc/MantidAPI/ParamFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ class MANTID_API_DLL ParamFunction : public virtual IFunction
/// Return parameter index from a parameter reference. Usefull for constraints and ties in composite functions
virtual size_t getParameterIndex(const ParameterReference& ref)const;
/// Get the containing function
IFunction* getContainingFunction(const ParameterReference& ref)const;
IFunction_sptr getContainingFunction(const ParameterReference& ref)const;
/// Get the containing function
IFunction* getContainingFunction(const IFunction* fun);
IFunction_sptr getContainingFunction(IFunction_sptr fun);

/// Apply the ties
virtual void applyTies();
Expand Down
3 changes: 1 addition & 2 deletions Code/Mantid/Framework/API/inc/MantidAPI/ParameterReference.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,11 @@ class MANTID_API_DLL ParameterReference
public:
ParameterReference();
ParameterReference(IFunction* fun, std::size_t index);
IFunction* getFunction() const;
std::size_t getIndex() const;
void reset(IFunction* fun, std::size_t index);
void setParameter(const double& value);
double getParameter() const;

IFunction* getFunction() const;
private:
IFunction* m_function; ///< pointer to the function
std::size_t m_index; ///< parameter index
Expand Down
4 changes: 2 additions & 2 deletions Code/Mantid/Framework/API/inc/MantidAPI/ParameterTie.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ namespace API
{
public:
/// Constructor
ParameterTie(IFunction* funct,const std::string& parName);
ParameterTie(IFunction* funct,const std::string& parName,const std::string& expr);
/// Destructor
virtual ~ParameterTie();
/// Set the tie expression
virtual void set(const std::string& expr);
/// Evaluate the expression
virtual double eval();
/// Return the string that can be used to recreate this tie
virtual std::string asString(const IFunction* fun = 0)const;
virtual std::string asString(const IFunction* fun = NULL)const;

/// Check if the tie has any references to certain parameters
bool findParametersOf(const IFunction* fun)const;
Expand Down

0 comments on commit 1b02431

Please sign in to comment.