Classes | Functions
minxlib's Object Factory Framework

Classes

class  minxlib::factory_map< K, F, D >
 A registry mapping keys to factory functions. More...
 
class  minxlib::factory< T, K, F, M >
 Object factory for subclasses of type T. More...
 

Functions

template<typename B , typename D >
B * minxlib::create_object ()
 Create object of type D using new operator and upcast to B. More...
 
template<typename B , typename D , typename A1 >
B * minxlib::create_object (A1 a1)
 Create object of type D using new operator and upcast to B. More...
 
template<typename B , typename D , typename A1 , typename A2 >
B * minxlib::create_object (A1 a1, A2 a2)
 Create object of type D using new operator and upcast to B. More...
 

Detailed Description

This file defines classes that provide a generic object factory framework for use in other parts of minxlib.

Note
This framework is designed specifically for use in minxlib. It is not some bulletproof, industrial strength concoction suitable for wide reuse.

Here is a sample program illustrating how this framework is meant to be used:

#include "factory.hh"
#include <boost/shared_ptr.hpp>
#include <iostream>
#include <sstream>
#include <string>
namespace minxlib {
class base {
std::string m_name ;
protected:
base(const std::string& s) {
std::ostringstream str ;
str << s << '_' << reinterpret_cast<unsigned long>(this) ;
m_name = str.str() ;
}
// Base class should specify these types to ease registration
// with factory.
typedef base* (*create_func)() ;
typedef factory_map<int, create_func> registry ;
public:
// Usually, base class will provide a factory method for
// instantiating subclasses in the hierarchy.
static boost::shared_ptr<base> create(int t) {
typedef factory<base, int,
create_func, registry> object_factory ;
try {
boost::shared_ptr<base> p(object_factory::create(t)) ;
return p ;
}
catch (object_factory::unknown_type&) {
boost::shared_ptr<base> p(new base("base")) ;
return p ;
}
}
const std::string& name() const {return m_name ;}
virtual ~base(){}
} ;
std::ostream& operator<<(std::ostream& os, const base& b)
{
return os << b.name() ;
}
class derived_one: public base {
derived_one(): base("derived_one"){}
// Subclasses should have these to make factory work
static bool registered ;
friend base* create_object<base, derived_one>() ;
} ;
bool derived_one::registered =
base::registry::add(1, create_object<base, derived_one>) ;
class derived_two: public base {
derived_two(): base("derived_two"){}
// Subclasses should have these to make factory work
static bool registered ;
friend base* create_object<base, derived_two>() ;
} ;
bool derived_two::registered =
base::registry::add(2, create_object<base, derived_two>) ;
} // namespace minxlib
int main()
{
const int n = 10 ;
int id[n] = {0, 1, 2, 2, 1, 3, 1, 2, 1, -1} ;
for (int i = 0; i < n; ++i) {
boost::shared_ptr<minxlib::base>
p(minxlib::base::create(id[i])) ;
std::cout << "object " << (i+1) << ": " << id[i]
<< ' ' << *p << '\n' ;
}
return 0 ;
}

The above code shows classes that are created using their default constructors. The factory::create() methods and create_object() functions have overloads that support up to two arguments.

Moreover, the above sample program does not illustrate the factory_map's support for storing arbitrary, client-supplied data for each class. See the implementations of minxlib::event (in event.hh and event.cc) and minxlib::protocol_error (exception.{hh,cc}) for the low-down on that feature.

Function Documentation

template<typename B , typename D >
B* minxlib::create_object ( )

Create object of type D using new operator and upcast to B.

Returns
Dynamically allocated derived class instance.

When using this object factory framework, more often than not, client classes will need a factory function to create a concrete type. Usually, these factory functions all follow the same pattern. Thus, instead of each client supplying its own factory function, we define this template function that different class hierarchies can use. The sample code at the beginning of this file illustrates how these factory functions can be used.

template<typename B , typename D , typename A1 >
B* minxlib::create_object ( A1  a1)

Create object of type D using new operator and upcast to B.

Parameters
a1First constructor argument.
Returns
Dynamically allocated derived class instance.

When using this object factory framework, more often than not, client classes will need a factory function to create a concrete type. Usually, these factory functions all follow the same pattern. Thus, instead of each client supplying its own factory function, we define this template function that different class hierarchies can use. The sample code at the beginning of this file illustrates how these factory functions can be used.

template<typename B , typename D , typename A1 , typename A2 >
B* minxlib::create_object ( A1  a1,
A2  a2 
)

Create object of type D using new operator and upcast to B.

Parameters
a1First constructor argument.
a2Second constructor argument.
Returns
Dynamically allocated derived class instance.

When using this object factory framework, more often than not, client classes will need a factory function to create a concrete type. Usually, these factory functions all follow the same pattern. Thus, instead of each client supplying its own factory function, we define this template function that different class hierarchies can use. The sample code at the beginning of this file illustrates how these factory functions can be used.