Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

Object::Factory Class Reference

An abstract class for creating objects. More...

#include <Object.h>

Inheritance diagram for Object::Factory:

Circle::Factory CircleTool::Factory ColorTool::Factory CutTool::Factory Drawing::Factory ExitTool::Factory Group::Factory MoveTool::Factory MutateTool::Factory Rectangle::Factory RectangleTool::Factory SelectTool::Factory ToBackTool::Factory ToFrontTool::Factory List of all members.

Static Public Methods

Objectcreate (std::string class_name)
 Create an object for the given class.

bool is_recognized_class (std::string class_name)
 Return true if a factory is ready to create objects of this class.


Public Attributes

const std::string production_type
 The class name.


Protected Methods

 Factory (std::string production_type)
 Create a factory and register it as handling a class name.

virtual ~Factory ()
 This method unregisters the class.

virtual Objectcreate ()=0
 Create an object.


Private Types

typedef std::vector< Factory * > Factories
typedef Factories::iterator iterator

Static Private Methods

Factoriesget_factories ()
 Return all the created factory objects.

Factory * find_factory (std::string class_name)
 Returned a pointer to the Factory assigned to create Entities of type <class_name>.


Detailed Description

An abstract class for creating objects.

Each factory is in charge with making a certain class of objects. It is registered with the class name it supports, and called as needed when reading in a drawing file. Factory is subclassed as an inner class in most Tool subclasses and DrawElement subclasses. It's used by XMLDrawFile to create objects from the names it reads from the draw file.


Member Typedef Documentation

typedef std::vector<Factory *> Object::Factory::Factories [private]
 

typedef Factories::iterator Object::Factory::iterator [private]
 


Constructor & Destructor Documentation

Object::Factory::Factory std::string    production_type [protected]
 

Create a factory and register it as handling a class name.

Parameters:
production_type  the class name handled by this factory.

00103   : production_type(p_type)
00104 {
00105   // quality control:
00106   if (production_type.empty())
00107     throw invalid_argument("Object::Factory: received an empty type name");
00108 
00109   // make sure that no other Factory produces the same "type" of Object.
00110   if (is_recognized_class(production_type))
00111     throw runtime_error("Factory for " + production_type + " already exists.");
00112 
00113   get_factories().push_back(this);
00114   // add this Factory to the set of known Factories.
00115 }

Object::Factory::~Factory   [protected, virtual]
 

This method unregisters the class.

This method currently does nothing.

00123 {
00124   // get_factories().remove(this);
00125 }


Member Function Documentation

virtual Object* Object::Factory::create   [protected, pure virtual]
 

Create an object.

Implemented in Circle::Factory, CircleTool::Factory, ColorTool::Factory, CutTool::Factory, Drawing::Factory, ExitTool::Factory, Group::Factory, MoveTool::Factory, MutateTool::Factory, Rectangle::Factory, RectangleTool::Factory, SelectTool::Factory, ToBackTool::Factory, and ToFrontTool::Factory.

Object * Object::Factory::create std::string    class_name [static]
 

Create an object for the given class.

Exceptions:
unknown_class  if class_name is not recognized.

00134 {
00135   Factory* f = find_factory(cl_name);
00136 
00137   // if the class has no Factory, throw an exception.
00138   if (!f)
00139     {
00140       throw unknown_class(cl_name);
00141     }
00142 
00143   return f -> create();
00144 }

Object::Factory * Object::Factory::find_factory std::string    class_name [static, private]
 

Returned a pointer to the Factory assigned to create Entities of type <class_name>.

If no Factory was found, returned NULL. This is accomplished by a simple linear search of the known factories.

00083 {
00084   Factories& factories = get_factories();
00085   iterator begin = factories.begin(), end = factories.end();
00086   
00087   // search the Factories from begin to end.
00088   for (iterator i = begin; i != end; ++i)
00089     if (class_name == (*i) -> production_type)
00090       return (*i);
00091 
00092   return NULL;
00093   // since none was found in the loop, return NULL.
00094 }

Object::Factory::Factories & Object::Factory::get_factories   [static, private]
 

Return all the created factory objects.

This is stored as a static vector declade inside the function that is returned to the caller.

00071 {
00072   static Factories known_factories;
00073   return known_factories;
00074 }

bool Object::Factory::is_recognized_class std::string    class_name [static]
 

Return true if a factory is ready to create objects of this class.

Uses find_factory to see if the Factory exists.

00152 {
00153   return bool(find_factory(class_name));
00154 }


Member Data Documentation

const std::string Object::Factory::production_type
 

The class name.


The documentation for this class was generated from the following files:
Generated on Fri Nov 8 10:52:31 2002 for Draw by doxygen1.2.17