Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > CRTP and Factories

Reply
Thread Tools

CRTP and Factories

 
 
alexander.stippler@uni-ulm.de
Guest
Posts: n/a
 
      07-25-2006
Hi

I try to combine two things, which perhaps aren't compatible at all.
But I at least look for alternatives:
I want to instantiate objects where the type is chosen based on some
input parameter (string). So I thought of the Factory pattern. But the
classes I want to instantiate all use the CRTP pattern, so I can't
choose their 'normal' base class as type for the objects created by the
factory, since for that I would need to know the exact type, and that
is 'hidden' in the string. I hope I expressed the problem
understandable.
Are their any ways to get that functionality?

regards,
Alex

 
Reply With Quote
 
 
 
 
Raider
Guest
Posts: n/a
 
      07-25-2006
> I try to combine two things, which perhaps aren't compatible at all.
> But I at least look for alternatives:
> I want to instantiate objects where the type is chosen based on some
> input parameter (string). So I thought of the Factory pattern. But the
> classes I want to instantiate all use the CRTP pattern, so I can't
> choose their 'normal' base class as type for the objects created by the
> factory, since for that I would need to know the exact type, and that
> is 'hidden' in the string. I hope I expressed the problem
> understandable.
> Are their any ways to get that functionality?


Your factory can return the structure like this:

struct ObjectCreateInfo
{
void* Object; // pointer to the created object
enum { ... } ObjectType; // type of the object created (enum or just
std::type_info)

// optional
typedef void Deleter(void*);
Deleter* ObjectDeleter; // function to call do delete object instance
}

So, your factory will be like this:

class YourFactory
{
public:
ObjectCreateInfo CreateObjectFormString(const string&);
}

 
Reply With Quote
 
 
 
 
alexander.stippler@uni-ulm.de
Guest
Posts: n/a
 
      07-25-2006

Raider schrieb:

> > I try to combine two things, which perhaps aren't compatible at all.
> > But I at least look for alternatives:
> > I want to instantiate objects where the type is chosen based on some
> > input parameter (string). So I thought of the Factory pattern. But the
> > classes I want to instantiate all use the CRTP pattern, so I can't
> > choose their 'normal' base class as type for the objects created by the
> > factory, since for that I would need to know the exact type, and that
> > is 'hidden' in the string. I hope I expressed the problem
> > understandable.
> > Are their any ways to get that functionality?

>
> Your factory can return the structure like this:
>
> struct ObjectCreateInfo
> {
> void* Object; // pointer to the created object
> enum { ... } ObjectType; // type of the object created (enum or just
> std::type_info)
>
> // optional
> typedef void Deleter(void*);
> Deleter* ObjectDeleter; // function to call do delete object instance
> }
>
> So, your factory will be like this:
>
> class YourFactory
> {
> public:
> ObjectCreateInfo CreateObjectFormString(const string&);
> }


I do not understand how this solves my problem. How do I call a method
defined for the object type to be created from the string afterwards?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
the crtp and static polymorphism AdlerSam C++ 5 02-18-2011 10:34 AM
CRTP, member typedefs and access control Hicham Mouline C++ 8 03-27-2009 09:53 AM
Design Patterns: Factories VS Abstract Factories tim@nocomment.com Java 1 10-02-2006 08:35 PM
HSSI & cRTP Arthur Lashin Cisco 0 03-19-2005 08:19 AM
ABC vs. CRTP? Mike Smith C++ 7 03-03-2005 05:43 PM



Advertisments