473,785 Members | 2,882 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_Facto ry
{
public:
typedef component::Comp onent* (*CreateCompone ntCallback)();

bool RegisterCompone nt ( int component_id,
CreateComponent Callback call_func )
{
return m_callbacks.ins ert ( CallbackMap::va lue_type ( component_id,
call_func ) ).second;
}

bool UnregisterCompo nent ( int component_id ) {
return m_callbacks.era se ( component_id ) == 1;
}

component::Comp onent* CreateComponent ( int component_id ) {
CallbackMap::co nst_iterator pos = m_callbacks.fin d ( component_id );

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

return (pos->second)();
}

private:
typedef std::map<int, CreateComponent CallbackCallbac kMap;

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::infrastru cture::Componen t_Factory fact_ref;

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

fact_ref.Unregi sterComponent ( tool::component ::Apple::ID );

return 0;
}
Sep 8 '07 #1
3 1775
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_Facto ry
{
public:
typedef component::Comp onent* (*CreateCompone ntCallback)();

bool RegisterCompone nt ( int component_id,
CreateComponent Callback call_func )
{
return m_callbacks.ins ert ( CallbackMap::va lue_type ( component_id,
call_func ) ).second;
}

bool UnregisterCompo nent ( int component_id ) {
return m_callbacks.era se ( component_id ) == 1;
}

component::Comp onent* CreateComponent ( int component_id ) {
CallbackMap::co nst_iterator pos = m_callbacks.fin d ( component_id );

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

return (pos->second)();
}

private:
typedef std::map<int, CreateComponent CallbackCallbac kMap;

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::infrastru cture::Componen t_Factory fact_ref;

/* LINE 88 */
fact_ref.Regist erComponent ( tool::component ::Apple::ID,
tool::component ::Apple );
the second param of Registercompone nt is typeof
typedef component::Comp onent* (*CreateCompone ntCallback)();
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.Regist erComponent ( tool::component ::Apple::ID,
tool::component ::Apple );
fact_ref.Regist erComponent ( tool::component ::Apple::ID,
&tool::componen t::Apple::Creat e );
>
fact_ref.Unregi sterComponent ( 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::infrastru cture::Componen t_Factory fact_ref;

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

the second param of Registercompone nt is typeof typedef
component::Comp onent* (*CreateCompone ntCallback)(); 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::infrastru cture::Componen t_Factory fact_ref;

/* LINE 88 */
fact_ref.Regist erComponent ( tool::component ::Apple::ID,
tool::component ::Apple );
the second param of Registercompone nt is typeof typedef
component::Com ponent* (*CreateCompone ntCallback)(); 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
16278
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 pythoncom. The problem is that all requests to the server seem to be serialized by COM. To demonstrate the problem, I'm including a simple test server that exposes one interface object, PyComThreads.Application, with a single method sleep(delay)....
6
2761
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 the Controller classe of a Sequence Diagram should be implemented as a private class within a component. However, my Programmer has said that since the ASP code lives outside the
8
1841
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 to the program. Where and how do I declare and initialise this object? For instance, it would have property implementation like this: //******************************************* public MyClass1 Class1 {
6
2668
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 the object and set default blank/null values), and then does all the Db access and number crunching to populate the new object. The factory returns the fully populated object to the caller. All the fields in the object are private, but have...
1
3278
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 : IHttpHandlerFactory, System.Web.SessionState.IRequiresSessionState { public virtual IHttpHandler GetHandler (HttpContext context, string requestType, string url, string path) { //some stuff here context.Session = "aaa"; // here Session object is...
2
1268
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 the created objects as plain objects. The caller method casts the object returned by the object factory into appropriate class object and uses it. All this works fine when exe is invoked directly. But when the application is deployed on the...
1
3607
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 to a function of a ùregistry vlass that hold all sub-classes... /* ------Neuron.h--------*/ #include <math.h> namespace Mnetlib { class Neuron
8
20434
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 class to the type of the object to be created. Here it is the code, which is a modification of the wikipedia C++ factory example code: ----------------------------------8<--------------------------------
13
2295
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 problem is that classes in several modules share a common base class which needs to implement a factory method to return instances of these same classes.
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10151
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8973
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3647
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.