By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,385 Members | 847 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,385 IT Pros & Developers. It's quick & easy.

Problem implementing an object factory

P: n/a
Here is my attempt at implementing a object factory. The purpose of this
is to replace a large switch statement in a factory class with the
functors. I get an error at line 88, marked, "expected primary-expression
before ')' token". I am using Modern C++ Design chapter 8 as a guide.

Stephen

---------------------

#include <map>
#include <iostream>

namespace tool
{
namespace component
{
class Component;
}

namespace infrastructure
{

class Component_Factory
{
public:
typedef component::Component* (*CreateComponentCallback)();

bool RegisterComponent ( int component_id,
CreateComponentCallback call_func )
{
return m_callbacks.insert ( CallbackMap::value_type ( component_id,
call_func ) ).second;
}

bool UnregisterComponent ( int component_id ) {
return m_callbacks.erase ( component_id ) == 1;
}

component::Component* CreateComponent ( int component_id ) {
CallbackMap::const_iterator pos = m_callbacks.find ( component_id );

if ( pos == m_callbacks.end() )
{
std::cerr << "Unknown Component ID" << std::endl; abort();
}

return (pos->second)();
}

private:
typedef std::map<int, CreateComponentCallbackCallbackMap;

CallbackMap m_callbacks;
};

}

namespace component
{
class Component
{};
class Apple : public Component
{
public:

static const int ID = 1;

Component* operator()()
{
return new Apple;
}
};

class Blueberry : public Component
{
public:

static const int ID = 5;

Component* operator()()
{
return new Blueberry;
}
};
}
}
int main (int, char**)
{
tool::infrastructure::Component_Factory fact_ref;

/* LINE 88 */
fact_ref.RegisterComponent ( tool::component::Apple::ID,
tool::component::Apple );

fact_ref.UnregisterComponent ( tool::component::Apple::ID );

return 0;
}
Sep 8 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Stephen Torri wrote:
Here is my attempt at implementing a object factory. The purpose of this
is to replace a large switch statement in a factory class with the
functors. I get an error at line 88, marked, "expected primary-expression
before ')' token". I am using Modern C++ Design chapter 8 as a guide.

Stephen

---------------------

#include <map>
#include <iostream>

namespace tool
{
namespace component
{
class Component;
}

namespace infrastructure
{

class Component_Factory
{
public:
typedef component::Component* (*CreateComponentCallback)();

bool RegisterComponent ( int component_id,
CreateComponentCallback call_func )
{
return m_callbacks.insert ( CallbackMap::value_type ( component_id,
call_func ) ).second;
}

bool UnregisterComponent ( int component_id ) {
return m_callbacks.erase ( component_id ) == 1;
}

component::Component* CreateComponent ( int component_id ) {
CallbackMap::const_iterator pos = m_callbacks.find ( component_id );

if ( pos == m_callbacks.end() )
{
std::cerr << "Unknown Component ID" << std::endl; abort();
}

return (pos->second)();
}

private:
typedef std::map<int, CreateComponentCallbackCallbackMap;

CallbackMap m_callbacks;
};

}

namespace component
{
class Component
{};
class Apple : public Component
{
public:

static const int ID = 1;

Component* operator()()
{
return new Apple;
}
};

class Blueberry : public Component
{
public:

static const int ID = 5;

Component* operator()()
{
return new Blueberry;
}
};
}
}
int main (int, char**)
{
tool::infrastructure::Component_Factory fact_ref;

/* LINE 88 */
fact_ref.RegisterComponent ( tool::component::Apple::ID,
tool::component::Apple );
the second param of Registercomponent is typeof
typedef component::Component* (*CreateComponentCallback)();
which is a free function.
now you trying to assign it with a class type, which is never valid
anywhere to use type as a function actual parameter.

so a quick and simple change is
class Apple : public Component
{
public:

static const int ID = 1;

Component* operator()()
static Component* Create() // factory method
{
return new Apple;
}
};
fact_ref.RegisterComponent ( tool::component::Apple::ID,
tool::component::Apple );
fact_ref.RegisterComponent ( tool::component::Apple::ID,
&tool::component::Apple::Create );
>
fact_ref.UnregisterComponent ( tool::component::Apple::ID );

return 0;
}

--
Thanks
Barry
Sep 8 '07 #2

P: n/a
On Sat, 08 Sep 2007 11:15:25 +0800, Barry wrote:
>
>int main (int, char**)
{
tool::infrastructure::Component_Factory fact_ref;

/* LINE 88 */
fact_ref.RegisterComponent ( tool::component::Apple::ID,
tool::component::Apple );

the second param of Registercomponent is typeof typedef
component::Component* (*CreateComponentCallback)(); which is a free
function.
now you trying to assign it with a class type, which is never valid
anywhere to use type as a function actual parameter.
Thanks.

Is there a way to have each class that will be a part of a object factory automatically register itself?

Stephen
Sep 8 '07 #3

P: n/a
Stephen Torri wrote:
On Sat, 08 Sep 2007 11:15:25 +0800, Barry wrote:
>>int main (int, char**)
{
tool::infrastructure::Component_Factory fact_ref;

/* LINE 88 */
fact_ref.RegisterComponent ( tool::component::Apple::ID,
tool::component::Apple );
the second param of Registercomponent is typeof typedef
component::Component* (*CreateComponentCallback)(); which is a free
function.
now you trying to assign it with a class type, which is never valid
anywhere to use type as a function actual parameter.

Thanks.

Is there a way to have each class that will be a part of a object factory automatically register itself?
it depends on how you define "automatically",
if you meant register as you write the class, I think there's not.
if you meant easier registering, maybe you can apply macro, then you can
write less code, but since your code is not so complicated and the
reg/unreg is clean and easy to use, moreover, the macro is so notorious,
so don't fix it if it ain't broken.

--
Thanks
Barry
Sep 9 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.