473,511 Members | 15,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem implementing an object factory

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
3 1756
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
16236
by: John Lull | last post by:
I'm writing a multithreaded COM server to manage a pool of hardware resources. All objects are designed to be thread-safe, and I've set sys.coinit_flags to COINIT_MULTITHREADED before importing...
6
2739
by: Martyn Lawson | last post by:
Hi, I am currently working as an Analyst on a .NET Web Project using ASP.NET and C#.NET. I have a couple of, at least what should be, quick questions: 1. My understanding of UML says that...
8
1812
by: Mark Neilson | last post by:
1. What is the best way to make a single instance of my top level class (DLL) internally available to all other members of the assembly? The top level object is where all other access is made in...
6
2651
by: BBM | last post by:
I have an object that has a fairly complex construction sequence, so I have written a dedicated "factory" class that invokes the constructor of my object class (which does nothing but instantiate...
1
3246
by: Bartosz Krzywicki | last post by:
I have the problem with Session object witch is null, when I implement IHttpHandlerFactory. Implementing IRequiresSessionState interface doesn't help. My code is like this: class MyController :...
2
1258
by: Ravi | last post by:
We have an application which dynamically loads an assembly and creates instances of classes. For this we have written a factory class which reflects the assembly and creates the classes and returns...
1
3590
by: neoairus | last post by:
I'm developing a pseudo-library for neural network. To simplify librarary using i wont to implement a sistem to instantiate different implementation of interface(Layer.h Neuron.h) passing a string...
8
20394
by: Stefano Sabatini | last post by:
Hi all, I'm encountering this while trying to implement a factory singleton method to generate objects. The singleton has a static map which binds a static creation function defined in each...
13
2265
by: Rafe | last post by:
Hi, I am in a situation where I feel I am being forced to abandon a clean module structure in favor of a large single module. If anyone can save my sanity here I would be forever grateful. My...
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7418
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
446
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.