Factory patterns in library code
Hello.
I encountered a problem when I wrote a library with a factory-pattern
(with Visual C++ 2008 in Windows).
Sample code here (Two projects):
-------------------------------------------------------------
Project1 (factory_test.exe)
-------------------------------------------------------------
==== main.cpp ===============================================
#include <iostream>
#include <factory_lib.hpp>
int main()
{
dummy_1(); // If comment out this line, "item_1" do not
registered.
//dummy_2();
std::cout << "count = " << item_factory::get_instance().count() <<
std::endl;
if (item_base *p = item_factory::get_instance().get(1)) {
std::cout << p->test() << std::endl;
}
else {
std::cout << "(NULL)" << std::endl;
}
return 0;
}
-------------------------------------------------------------
Project2 (factory_lib.lib)
-------------------------------------------------------------
=== factory_lib.hpp =========================================
#ifndef __FACTORY_LIB_HPP__
#define __FACTORY_LIB_HPP__
#include <map>
struct item_base
{
virtual ~item_base() {}
virtual const char *test() const { return "item_base"; }
};
class item_factory
{
std::map<int, item_base*> m;
public:
static item_factory& get_instance()
{
static item_factory instance;
return instance;
}
// Count registered items.
size_t count()
{
return m.size();
}
// Get a registered item specified by ID.
item_base *get(int id)
{
std::map<int, item_base*>::iterator ite = m.find(id);
if (ite != m.end()) {
return ite->second;
}
else {
return 0;
}
}
// Register a item with ID.
void set(int id, item_base* item)
{
m[id] = item;
}
};
struct item_registrar
{
item_registrar(int item_id, item_base* item)
{
item_factory::get_instance().set(item_id, item);
}
};
void dummy_1();
void dummy_2();
#endif
=== item_1.cpp ============================================
#include "factory_lib.hpp"
void dummy_1() {}
struct item_1 : public item_base
{
virtual const char *test() const { return "item_1"; }
};
static item_registrar reg(1, new item_1);
=== item_2.cpp ============================================
#include "factory_lib.hpp"
void dummy_2() {}
struct item_2 : public item_base
{
virtual const char *test() const { return "item_2"; }
};
static item_registrar reg(2, new item_2);
There are two prejects. "factory_test.exe" is main executable project,
and "factory_lib.lib" is a static-linked library project.
The item_factory class has a map container hold pairs of item-ID and a
pointer to item derived from item_base class.
Two objects of item_1 and item_2 should be automatically registered
into item_factory in constructor of item_registrar class when this
application was initialized.
But if I removed dummy_1() and dummy_2() functions, no item
registered.
I know why. (Because there are no reference from main.cpp to
item_1.cpp and item_2.cpp)
Are there any recommended solution in a platform-independent way or
any library ?
I think this is maybe a common implementation problem of factory
patterns in library code.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]