473,396 Members | 1,826 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Factories and conversion from Base to Derived...

Hi all,

I want to know whether the following is possible. I started out
with an idea called a CreationAbstractor. Basically just a
factory function wrapper for all types.

It looks like this:

template <class T>
class CreationAbstractor
{
public:
virtual T* create() const = 0;
virtual ~CreationAbstractor(){ }

protected:
CreationAbstractor(){ }
};

Typically, it would be used as argument to a class constructor like
this:

Driver::Driver( const CreationAbstractor<Car>& );

I attempted to make CreationAbstractor generic of up to N arguments:

template <class BaseT, class DerivedT = BaseT>
class Creator: public CreationAbstractor<BaseT>
{
private:
typedef boost::scoped_ptr<CreationAbstractor<BaseT ImplT;

ImplT impl_;

struct NoArgImpl: public CreationAbstractor<BaseT>
{ virtual BaseT* create() const{ return new DerivedT; } };

template <class Fstruct Impl;

template <class A1>
struct Impl<void(A1)>: public CreationAbstractor<BaseT>
{
Impl( A1 a1 ): a1_( a1 ){}
virtual BaseT* create() const{ return new DerivedT( a1_ ); }
A1 a1_;
};

//etc...

public:

virtual BaseT* create() const
{
return impl_->create();
}

Creator(): impl_( new NoArgImpl ){ }
template <class A1>
Creator( A1 a ): impl_( new Impl<void(A1)>( a ) ){ }
//etc..
};

This seems to work fine, although I suppose you guys could
give me some pointers as to how to implement it better. The
problem that I have is that my original implementation had
only on type. Therefore:

template <class T>
class Creator: public CreationAbstractor<T>......(1)

This prohibit me from converting Creator<Derivedto
CreationAbstractor<Base>, despite my best efforts.

Does anybody know of a better implementation using nifty
conversions where a declaration as in (1) may suffice to do?

IOW:

const CreationAbstractor<Base>& a = const
CreationAbstractor<Derived>&...;compiles fine.

Regards,

Werner

Aug 27 '07 #1
2 1699

werasm wrote:

Excuse my fast fingers. See below:
This seems to work fine, although I suppose you guys could
give me some pointers as to how to implement it better. The
problem that I have is that my original implementation had
only on type. Therefore:
only <onetype
>
template <class T>
class Creator: public CreationAbstractor<T>......(1)

This prohibit me from converting Creator<Derivedto
CreationAbstractor<Base>, despite my best efforts.
This prohibits..

Regards again,

W

Aug 27 '07 #2

werasm wrote:
Hi all,

I want to know whether the following is possible. I started out
with an idea called a CreationAbstractor. Basically just a
factory function wrapper for all types.

It looks like this:

template <class T>
class CreationAbstractor
{
public:
virtual T* create() const = 0;
virtual ~CreationAbstractor(){ }

protected:
CreationAbstractor(){ }
};

Typically, it would be used as argument to a class constructor like
this:

Driver::Driver( const CreationAbstractor<Car>& );
This seems to be a monologue. Anyway, yesterday after writing this
code I contemplated (after forgetting why) why I could not simply have
done

Driver::Driver( std::auto_ptr<Car);

[Excuse my example... It is the only simple real world case I can
think of now.]

This would have solved the problem. Alternatively I could have used
something
like Loki Factories (I've read the chapter, yes). The reason the
auto_ptr would
not suffice, is that given the following:

Car( std::auto_ptr<Engine>, std::auto_ptr<Interior>...), the client
would be
inclined to try this:

Car( new ConcreteEngine(...), new LeatherInterior(...) );

Which is also frowned upon... not exception safe, so to speak. For
this reason the notion of a factory does not seem like a bad idea.

Loki factories (whilst I have great respect for the ideas) also have
its problems. The book gives to examples - OpNewFactoryUnit
that creates by calling new T(), and PrototypeFactoryUnit, that
uses Clone on prototypes. This implies either default or copy
constructors are required. This also does not cater for the
possibility of implementation classes being dependent on
other classes. There is another issue. Often creator objects
only need to live as long as required. Using prototypes
imply they live forever, and not using prototypes imply
default constructors are required. Another issue that I
contemplated is creating OpAbstractCreatorFactoryUnit. What
I don't like about this, is the fact that sometimes the creator may
have references to temporaries (like strings), and it may not live
longer than required (It ceases to live once the constructors scope
ceases). Storing these as variant to prototypes would imply they
would have to live indefinitely, meaning the creator may not contain
references to temporaries.

Therefore, back to the car example, this would make sense to
me:
Car( const CreationAbstractor<Engine>&, const
CreationAbstractor<Interior>&...)...

This does not burden the implementor with the need to copy construct.
The actual creation is delayed until the actual point of calling,
which would
be in the constructor initializer list - most likely, or in that of a
member, in
which case it would be forwarded. Also, its exception safe, as usually
the
concrete class is created on the stack.

Monologue complete, thank you.

Any implementation tips (or critique) would be welcome.

Kind regards,

Werner

Aug 28 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Robert Ferrell | last post by:
I'm wondering how Python cognoscenti use/implement factory patterns in Python. In my limited C++ experience, one reason to use a factory method is to make it easier to create instances of classes...
15
by: G. Peter | last post by:
Hi there, I've a 'funny' error message of my compiler (g++ 2.95.4) that tells me: robot.cpp: In method `Robot::Robot()': robot.cpp:19: warning: deprecated conversion from string constant to...
5
by: pw4getter | last post by:
Is there any rationale to prohibit implicit conversion from derived** to base** ? To illustrate my question I submit couple lines below: class base { public: base(){} virtual ~base(){} };...
2
by: CD | last post by:
Is this possible: class base; class derived; //:public base vector <base*> bList; vector<derived*> dList; //add some derived class pointer entries to dList;
5
by: Jeff Greenberg | last post by:
Not an experienced c++ programmer here and I've gotten myself a bit stuck. I'm trying to implement a class lib and I've run into a sticky problem that I can't solve. I'd appreciate any help that I...
9
by: Codemonkey | last post by:
Hi, Sorry for a stupid question, but is it possible to do a narrowing conversion with an object array with Option Strict On in VB? E.g: ------------------ Dim aBase as Base() = {New...
3
by: shuisheng | last post by:
Dear All, Such as I have a template class Base<intwhich can automaticaaly convert to base<douoble>. Base<int <= Base<double> And also I have a derived class Derived<intwhich can...
5
by: Scott M. | last post by:
Why will this fail: (short) txtQty.Text and this succeed? Convert.ToInt16(txtQty.Text)
3
by: jared.grubb | last post by:
Can a private Base class method convert to/from a Derived* using static_cast? The CPL book says that a Derived method is allowed to convert to/from Base, but says nothing about whether a Base...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.