CRTP and Factories

Discussion in 'C++' started by alexander.stippler@uni-ulm.de, Jul 25, 2006.

  1. Guest

    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
     
    , Jul 25, 2006
    #1
    1. Advertising

  2. Raider Guest

    > 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&);
    }
     
    Raider, Jul 25, 2006
    #2
    1. Advertising

  3. Guest

    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?
     
    , Jul 25, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike Smith

    ABC vs. CRTP?

    Mike Smith, Mar 2, 2005, in forum: C++
    Replies:
    7
    Views:
    917
    Dietmar Kuehl
    Mar 3, 2005
  2. fabioppp

    CRTP question

    fabioppp, Apr 15, 2005, in forum: C++
    Replies:
    2
    Views:
    389
    James Aguilar
    Apr 15, 2005
  3. Replies:
    1
    Views:
    452
    Oliver Wong
    Oct 2, 2006
  4. Hicham Mouline
    Replies:
    8
    Views:
    1,024
    Hicham Mouline
    Mar 27, 2009
  5. AdlerSam
    Replies:
    5
    Views:
    699
    itaj sherman
    Feb 18, 2011
Loading...

Share This Page