EMAN2
Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes
EMAN::Factory< T > Class Template Reference

Factory is used to store objects to create new instances. More...

#include <emobject.h>

Collaboration diagram for EMAN::Factory< T >:
Collaboration graph
[legend]

List of all members.

Public Types

typedef T *(* InstanceType )()

Public Member Functions

template<>
Symmetry3Dget (const string &instancename)

Static Public Member Functions

template<class ClassType >
static void add ()
static T * get (const string &instance_name)
static T * get (const string &instance_name, const Dict &params)
static vector< string > get_list ()

Private Member Functions

 Factory ()
 Factory (const Factory< T > &)
 ~Factory ()
template<class ClassType >
void force_add ()
template<>
 Factory ()
template<>
 Factory ()
template<>
 Factory ()
template<>
 Factory ()
template<>
 Factory ()
template<>
 Factory ()
template<>
 Factory ()
template<>
 Factory ()
template<>
 Factory ()
template<>
 Factory ()
template<>
 Factory ()

Static Private Member Functions

static void init ()

Private Attributes

map< string, InstanceTypemy_dict

Static Private Attributes

static Factory< T > * my_instance = 0

Detailed Description

template<class T>
class EMAN::Factory< T >

Factory is used to store objects to create new instances.

It is a singleton template. Typical usages are as follows:

1. How to define a new factory (e.g. Processor Factory):

template<> Factory<Processor>::Factory() { force_add(&AbsoluateValueProcessor::NEW); force_add(&BooleanProcessor::NEW); }

2. How to use a Factory (e.g. Processor Factory):

Processor *f1 = Factory<Processor>::get("math.absvalue"); Processor *f2 = Factory<Processor>::get("filter.lowpass.gauss", Dict("cufoff_freq", EMObject(12));

Author:
Liwei Peng

Definition at line 728 of file emobject.h.


Member Typedef Documentation

template<class T>
typedef T*(* EMAN::Factory< T >::InstanceType)()

Definition at line 731 of file emobject.h.


Constructor & Destructor Documentation

template<class T>
EMAN::Factory< T >::Factory ( ) [private]
template<class T>
EMAN::Factory< T >::Factory ( const Factory< T > &  ) [private]
template<class T>
EMAN::Factory< T >::~Factory ( ) [private]
template<>
EMAN::Factory< Aligner >::Factory ( ) [private]
template<>
EMAN::Factory< Analyzer >::Factory ( ) [private]

Definition at line 59 of file analyzer.cpp.

        {
                force_add<PCAsmall>();
                force_add<PCAlarge>();
                force_add<varimax>();
                force_add<InertiaMatrixAnalyzer>();
                force_add<ShapeAnalyzer>();
                force_add<KMeansAnalyzer>();
                force_add<SVDAnalyzer>();
        }
template<>
EMAN::Factory< Analyzer >::Factory ( ) [private]
template<>
EMAN::Factory< Averager >::Factory ( ) [private]
template<>
EMAN::Factory< Cmp >::Factory ( ) [private]
template<>
EMAN::Factory< Processor >::Factory ( ) [private]
template<>
EMAN::Factory< Projector >::Factory ( ) [private]
template<>
EMAN::Factory< Reconstructor >::Factory ( ) [private]
template<>
EMAN::Factory< FourierPixelInserter3D >::Factory ( ) [private]
template<>
EMAN::Factory< Symmetry3D >::Factory ( ) [private]
template<>
EMAN::Factory< OrientationGenerator >::Factory ( ) [private]

Member Function Documentation

template<class T >
template<class ClassType >
void EMAN::Factory< T >::add ( ) [static]

Definition at line 769 of file emobject.h.

        {
                init();

                string name = ClassType::NAME;
                typename map < string, InstanceType >::iterator fi =
                        my_instance->my_dict.find(name);

                if (fi == my_instance->my_dict.end()) {
                        my_instance->my_dict[name] = &ClassType::NEW;
                }
        }
template<class T >
template<class ClassType >
void EMAN::Factory< T >::force_add ( ) [private]

Definition at line 760 of file emobject.h.

        {
                string name = ClassType::NAME;
                my_dict[name] = &ClassType::NEW;
        }
template<class T >
T * EMAN::Factory< T >::get ( const string &  instance_name,
const Dict params 
) [static]

Definition at line 802 of file emobject.h.

References InvalidParameterException, and NotExistingObjectException.

        {
                init();

                typename map < string, InstanceType >::iterator fi =
                        my_instance->my_dict.find(instancename);

                string lower = instancename;
                if (fi == my_instance->my_dict.end()) {
                        for (unsigned int i=0; i<lower.length(); i++) lower[i]=tolower(lower[i]);
                        fi = my_instance->my_dict.find(lower);
                }

                if (fi != my_instance->my_dict.end()) {
                        T *i = my_instance->my_dict[lower] ();

                        const vector<string> para_keys = params.keys();
//                      std::cout << "the number of keys is " << para_keys.size() << std::endl; // PRB May 19th
                        const vector<string> valid_keys = i->get_param_types().keys();
                        typename vector<string>::const_iterator it;
                        for(it=para_keys.begin(); it!=para_keys.end(); ++it) {
//                              std::cout << "the iterator  is " << *it << std::endl; // PRB May 19th
                                if( find(valid_keys.begin(), valid_keys.end(), *it) == valid_keys.end() ) {
                                        throw InvalidParameterException(*it);
                                }
                        }

                        i->set_params(params);
                        return i;
                }


                throw NotExistingObjectException(instancename, "No such an instance existing");
        }
template<class T >
T * EMAN::Factory< T >::get ( const string &  instance_name) [static]

Definition at line 782 of file emobject.h.

References NotExistingObjectException.

Referenced by ali3d_d(), EMAN::dump_factory_list(), and recons3d_4nn().

        {
                init();
                typename map < string, InstanceType >::iterator fi =
                        my_instance->my_dict.find(instancename);
                if (fi != my_instance->my_dict.end()) {
                        return my_instance->my_dict[instancename] ();
                }

                string lower = instancename;
                for (unsigned int i=0; i<lower.length(); i++) lower[i]=tolower(lower[i]);

                fi = my_instance->my_dict.find(lower);
                if (fi != my_instance->my_dict.end()) {
                        return my_instance->my_dict[lower] ();
                }

                throw NotExistingObjectException(instancename, "The named object doesn't exist");
        }
template<>
Symmetry3D * EMAN::Factory< Symmetry3D >::get ( const string &  instancename)
template<class T >
vector< string > EMAN::Factory< T >::get_list ( ) [static]

Definition at line 838 of file emobject.h.

                                                                       {
                init();
                vector < string > result;
                typename map < string, InstanceType >::const_iterator p;
                for (p = my_instance->my_dict.begin(); p != my_instance->my_dict.end(); p++) {
                        result.push_back(p->first);
                }

                return result;
        }
template<class T >
void EMAN::Factory< T >::init ( ) [static, private]

Definition at line 751 of file emobject.h.

        {
                if (!my_instance) {
                        my_instance = new Factory < T > ();
                }
        }

Member Data Documentation

template<class T>
map< string, InstanceType > EMAN::Factory< T >::my_dict [private]

Definition at line 746 of file emobject.h.

template<class T>
Factory< T > * EMAN::Factory< T >::my_instance = 0 [static, private]

Definition at line 745 of file emobject.h.


The documentation for this class was generated from the following file: