CFitFactory

Name

CFitFactory, CFitCreator, CGaussianFitCreator, CLinearFitCreator -- Creating fit objects by name

Synopsis


#include <CFitFactory.h>

class CFitFactory 
{
  // Public data types:
public:
  typedef std::map<std::string, CFitCreator*> FitCreatorMap;
  typedef FitCreatorMap::iterator  FitCreatorIterator;

  typedef std::map<std::string, CFit*>        FitMap;
  typedef FitMap::iterator          FitIterator;

public:


  static void AddFitType (const std::string& rType, CFitCreator* pCreator)   ; 
  static CFit* Create (std::string sFitType, std::string sFitName)   ; 
  static bool Delete (std::string sName)   ; 
  static bool Perform (std::string sName)   ; 
  static bool AddPoints (std::string sName,
			 std::vector<FPoint> vPoints)   ; 
  static double Evaluate (std::string sName, double x)   ; 
  static FitCreatorIterator beginCreators();
  static FitCreatorIterator endCreators();
  static int    sizeCreators();
  static FitCreatorIterator FindFitCreator (std::string sType)   ; 
  static int size ()   ; 
  static FitIterator begin ()   ; 
  static FitIterator end ()   ; 
  static FitIterator FindFit (std::string sName) ; 


};

        

DESCRIPTION

The CFitFactory is both a factory for fits and a registry/dictionary of the fits it creates. Note that this is an extensible factory. Extensible factories are not standalone entities. They rely on objects called Creators to recognize specific type specifiers and to create the specific type on behalf of the factory.

Associated with the fit factory is a class hierarhcy of CFitCreators. This hierarchy exactly mirrors the CFit class hierarchy. Concrete classes of the CFitCreator are instantiated and registered with the factory to create the corresponding fits.

The remainder of this man page will describe the data types introduced by this class, the methods this class exports and the fit creator hierarchy and pre-defined concrete creator classes.

Note that all methods of this class are static. It's therefore not necessary to create an instance of this class. Furthermore, this implies that the factory containers are application wide.

Data types

The class defines several nested data types. These define a keyed container and iterator for the creators, and a keyed container and iterator for the fits themselves. Note that iterators 'point' to an std::pair<std::string, T*> where T represents the type of itemin the container (e.g. a CFitCreator or CFit).

typedef std::map<std::string, CFitCreator*> FitCreatorMap;

CFitFactory::FitCreatorMap is the keyed container that holds fit creators. Fit creators, recall are objects that will create a fit that corresponds to the key at which they live in the container.

typedef FitCreatorMap::iterator FitCreatorIterator;

Iterator into the FitCreator container. See DESCRIPTION for information about this.

typedef std::map<std::string, CFit*> FitMap

CFitFactory::FitMap defines the container for the fits themselves. This is a container that is indexed by a string that names the fit. This name need not be the same as the name of the fit itself. See, e.g. Create.

typedef FitMap::iterator FitIterator;

Iterator into the fit container

METHODS

All methods are class level (static) methods. They do not require an object to invoke e.g. FitFactory::sizeCreators() returns the number of fit creators registered.

static void AddFitType (const std::string& rType, CFitCreator* pCreator);

Registers a new fit type with the factory. rType will be the name of the fit type. This value, when passed to Create will cause the creator passed in as pCreator to be asked to produce a fit object.

If a fit of the same type as rType has already been registered, an exception is thrown.

static CFit* Create (std::string sFitType, std::string sFitName);

Asks the factory to crate a new fit of the type sFitType and to give it the name sFitName. The resulting fit is returned. The resulting fit is also registered with the factory.

If a fit of the name sFitName already exists, an exception is thrown. If not creator matches the fit type; sFitType, a null pointer is returned.

The fit is dynamically created by new, however you should not directly delete it. See the Delete method below.

static bool Delete (std::string sName);

Deletes the fit named sName from the fit dictionary. If the fit existed (and had to be deleted) true is returned. If the fit did not exist false is returned.

static bool Perform(std::string sName);

Requests that the fit named sName be performed with the current set of points, This locates the fit in the fit dictionary and just delegates to that fit's Perform method.

If the fit exists, and canbe performed successfully, true is returned. If the fit does not exist or exists but fails, false is returned instead.

static bool AddPoints(std::string sName, std::vector<FPoint> vPoints);

Adds a vector of points to the fit. See Point.h for the definition of a FPoint object. This call puts the underlying fit in the CFit::Accepting state.

If the fit named by sName was found the the points in vPoints are added via successive callse to the underlying fit's AddPoint method and true is returned. If the fit is not found; false is returned.

static double Evaluate(std::string sName, double x);

Evaluates the fit sName at the X coordinate x. The value of the fit function at that point is returned. Note that the fit must be in the CFit::Performed state.

An exception is thrown if the fit does not exist. If the fit is not in the CFit::Performed state, an exception is thrown.

static FitCreatorIterator beginCreators(); , static FitCreatorIterator endCreators();

These methods support iteration of the container of fit creators. They both return pointer like objects that point to an std::pair<std::string, CFitCreator*>.

beginCreators returns an iterator to the "first" item of the container. Successive increments will step the iterator through all items until the increment results in a iterator that is equal to that returned by endCreators which points just past the end of the container.

static int sizeCreators();

Returns the number of fit creators that have been registered with the factory.

static FitCreatorIterator FindFitCreator (std::string sType);

Returns an iterator to the container item that has the fit for the fit type sType. If there is no matching fit creator, an iterator equal to that returned by endCreators is returned.

static FitIterator begin (); , static FitIterator end ();

Supports iteration over the container that is the dictionary of fits. These methods return iterators that are pointer like objects. Each iterator points to an object of the type: std::pair<std::string, CFit*>.

begin points to the first object in the container. Incrementing it will step sequentially through the items in the container. When all items have been visited, the increment will result in an iterator equal to that returned by end.

static int size();

Returns the number of fits in the fit dictionary.

static FitIterator FindFit (std::string sName);

Returns an iterator that points to the object in the fit dictionary that matches the name sName. If there is no matching fit, an object equal to that returned by end is returned.

Fit Creators

This section describes the abstract base class the defines the fit creator interface. SpecTcl defines two concrete fit creator classes. The first of these; CGaussianFitCreator creates CGaussianFit objects. The second; CLinearFitCreator creates CLinearFit objects


#include <CFitCreator.h>
#include <CGaussianFitCreator.h>
#include <CLinearFitCreator.h>

class CFitCreator      
{

public:
  CFitCreator ();
  
  virtual   CFit* operator() ()   = 0; 
  virtual   std::string DescribeFit()  = 0;
};
        

The methods that must be implemented by concrete classes are:

virtual = 0 CFit* operator() ();

Must create the fit of the appropriate type and return it. The fit should be dynamically created. It will be the client's responsibility to destroy it.

virtual = 0 std::string DescribeFit();

Returns some brief text to describe the type of fit object produced by the creator. SpecTcl's fit command iterates through the fit creators and uses these texts when constructing the command help.