M
ma740988
Consider:
// factory.h
#ifndef TEST_FACTORY_H
#define TEST_FACTORY_H
# include <map>
# include <string>
using namespace std; // remove this ..
struct msg_data_stream {
std::string id;
};
class testFactory;
typedef testFactory* (*CreateInstanceFunc)();
class testFactory {
static map<string, CreateInstanceFunc> factoryMap;
public:
static testFactory* instance ( const msg_data_stream&
data_stream );
static bool register_class( const string& class_name,
CreateInstanceFunc createFunction);
};
#endif
// factory.cpp
# include "factory.h"
map<string, CreateInstanceFunc> testFactory::factoryMap;
testFactory*
testFactory::instance ( const msg_data_stream&
data_stream )
{
map<string, CreateInstanceFunc>::iterator it;
if (( it = factoryMap.find (data_stream.id)) ==
factoryMap.end())
return 0;
return (it->second)();
}
bool
testFactory::register_class( const string& class_name,
CreateInstanceFunc createFunction)
{
map<string, CreateInstanceFunc>::iterator it;
if (( it = factoryMap.find (class_name)) != factoryMap.end())
return false;
factoryMap[class_name] = createFunction;
return true;
}
// main.cpp
# include <iostream>
# include "factory.h"
struct test_class : public testFactory
{
int idx;
static testFactory* createTestClass() {
return new test_class; }
test_class () : idx(99)
{
// registration probably better off within int main
testFactory::register_class( "test_class",
test_class::createTestClass);
}
};
void computeLikeCrazy()
{
test_class *p_test = (test_class *)testFactory::instance(stream);
if ( p_test )
{
// do something
delete p_test;
}
}
Referencing the function 'computeLikeCrazy'. With each call to the
instance function. A pointer to a new object is returned. The pointer
is then deleted. I'd like to rid the allocation/deletion of the
pointer. How would i achieve this?
// factory.h
#ifndef TEST_FACTORY_H
#define TEST_FACTORY_H
# include <map>
# include <string>
using namespace std; // remove this ..
struct msg_data_stream {
std::string id;
};
class testFactory;
typedef testFactory* (*CreateInstanceFunc)();
class testFactory {
static map<string, CreateInstanceFunc> factoryMap;
public:
static testFactory* instance ( const msg_data_stream&
data_stream );
static bool register_class( const string& class_name,
CreateInstanceFunc createFunction);
};
#endif
// factory.cpp
# include "factory.h"
map<string, CreateInstanceFunc> testFactory::factoryMap;
testFactory*
testFactory::instance ( const msg_data_stream&
data_stream )
{
map<string, CreateInstanceFunc>::iterator it;
if (( it = factoryMap.find (data_stream.id)) ==
factoryMap.end())
return 0;
return (it->second)();
}
bool
testFactory::register_class( const string& class_name,
CreateInstanceFunc createFunction)
{
map<string, CreateInstanceFunc>::iterator it;
if (( it = factoryMap.find (class_name)) != factoryMap.end())
return false;
factoryMap[class_name] = createFunction;
return true;
}
// main.cpp
# include <iostream>
# include "factory.h"
struct test_class : public testFactory
{
int idx;
static testFactory* createTestClass() {
return new test_class; }
test_class () : idx(99)
{
// registration probably better off within int main
testFactory::register_class( "test_class",
test_class::createTestClass);
}
};
void computeLikeCrazy()
{
test_class *p_test = (test_class *)testFactory::instance(stream);
if ( p_test )
{
// do something
delete p_test;
}
}
Referencing the function 'computeLikeCrazy'. With each call to the
instance function. A pointer to a new object is returned. The pointer
is then deleted. I'd like to rid the allocation/deletion of the
pointer. How would i achieve this?