Board index » cppbuilder » Create object by type name (String variable), HOW?!?!?

Create object by type name (String variable), HOW?!?!?

Hello,
Problem:
Type of object (TButton, TEdit,...) ist given as a variable
(TypeName:AnsiString). How can i create such an object
[TypeName].Create.... (can only work with similar objects, derived from
TWinControl)

Solution ????????
other than case-switch.....!

Thanks for Help

 

Re:Create object by type name (String variable), HOW?!?!?


Quote
"Christian Lotze" <_chrissi...@hotmail.com> wrote in message

news:b2a5c2$spc$03$1@news.t-online.com...

Quote
> Hello,
> Problem:
> Type of object (TButton, TEdit,...) ist given as a variable
> (TypeName:AnsiString). How can i create such an object
> [TypeName].Create.... (can only work with similar objects, derived from
> TWinControl)

> Solution ????????
> other than case-switch.....!

Well, you are talking about a factory.  There are many ways to do this.  You
should read about this in the GoF Design Patterns book for some background.

One approach is to create a map of factories, something similar to this...

#include <memory>
#include <map>
class Abstract_TWinControl_Factory
  : public TComponent
{
public:
  __fastcall Abstract_TWinControl_Factory(
      TComponent * owner = 0)
    : TComponent(owner)
  {
  }

  __fastcall virtual ~Abstract_TWinControl_Factory()
  {
  }

  virtual TWinControl * create() = 0;

Quote
};

template <class TYPE>
class Specific_TWinControl_Factory
  : public Abstract_TWinControl_Factory
{
public:
  Specific_TWinControl_Factory(
      TComponent * owner = 0)
    : Abstract_TWinControl_Factory(owner)
  {
  }

  virtual TWinControl * create()
  {
    std::auto_ptr<TWinControl> control(new TYPE(this));
    return control.release();
  }

Quote
};

class TWinControl_Factory
  : public TComponent
{
public:
  __fastcall TWinControl_Factory(
      TComponent * owner = 0)
    : TComponent(owner)
  {
    factories_["TButton"] = new Specific_TWinControl_Factory<TButton>(this);
    factories_["TEdit"] = new Specific_TWinControl_Factory<TEdit>(this);
  }

  TWinControl * create(
      AnsiString const & type_name)
  {
    Factories::iterator iter(factories_.find(type_name));
    if (iter == factories_.end())
    {
      return 0;
    }
    return iter->second->create();
  }

private:
  typedef std::map<AnsiString, Abstract_TWinControl_Factory*> Factories;
  Factories factories_;

Quote
};

Re:Create object by type name (String variable), HOW?!?!?


http://www.respower.com/~earlye/programming/19990917.001.htm

Quote
"Christian Lotze" <_chrissi...@hotmail.com> wrote in message

news:b2a5c2$spc$03$1@news.t-online.com...
Quote
> Hello,
> Problem:
> Type of object (TButton, TEdit,...) ist given as a variable
> (TypeName:AnsiString). How can i create such an object
> [TypeName].Create.... (can only work with similar objects, derived from
> TWinControl)

> Solution ????????
> other than case-switch.....!

> Thanks for Help

Re:Create object by type name (String variable), HOW?!?!?


New, improved!

http://www.respower.com/~earlye/programming/VclObjectFactory.htm

Other Threads