Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

casting by an id

2 views
Skip to first unread message

O.Petzold

unread,
Apr 19, 2001, 12:11:32 PM4/19/01
to
Hello,

on my process on writing the serialisiation class I hang
on the following problem:

class Serializable { [...]
suid_t suid() const;
}

class Hoomer : public Serializable { [...] }
class Bart : public Serializable { [...] }

class SerializableFactory {
[...]
template <class _Tp> static Serializable* creator() {
return new _Tp;
}
Serializable* create(Serializable::suid_t id) const;
};

int main() {
[...]
is >> id;
Bart* bart = static_cast<Bart*>(factory.create(id));
is >> *bart;
[...]
}

But it could be a Hoomer as well. How can I cast using an id ?
The SerializableFactory::create() creates Barts or Hoomers on
the given id using an allways registered creator. The assoziation
between ids and loaders is done by a map. The solution could
be a template but, how could it look ?

Thanks
Olaf

Neil Butterworth

unread,
Apr 19, 2001, 12:15:28 PM4/19/01
to
"O.Petzold" <opet...@wit.regiocom.net> wrote in message
news:3ADF0E34...@wit.regiocom.net...

Possible:

Serializable * s = factory.create(id);

then use virtual methods.

The solution probably can't be a template, as the types for templates need
to be known at compile-time.

BYW, why are you bothering writing this? There are a lot of existing
solutions to persistent object storage already out there.

NeilB


O.Petzold

unread,
Apr 20, 2001, 3:29:09 AM4/20/01
to
> Possible:
>
> Serializable * s = factory.create(id);
>
> then use virtual methods.
Bart and Hooler are Simpson's, well, but the Flintstone Familie
has completly different Members: Simpson != Flintstone. It's
inpossible to find a base class for this. Therefore I need to know
the type or another way.

> The solution probably can't be a template, as the types for templates need
> to be known at compile-time.

Yep.



> BYW, why are you bothering writing this? There are a lot of existing
> solutions to persistent object storage already out there.

Please, could you give some URL for suitable solutions ? I Have some
articles about, but they give not the secrets I'm looking for (like
the above). Or they give only the way in principle, no working solution.
The only lib which can handle with persistance objects
I know is the commonc++ lib, here a part of the source (persist.cpp):

bool BaseObject::Write(Engine& archive) const
{
// Do nothing
return true; // Successfully
}

and other jokes etc.

BTW, this isn't bothering for me. Here I discuss only parts of
the problem due to the long listing, and I have to be portable,
therefore
no MS MFC solutions (which has inspirated commonc++ persistence objects
?)

Regards
Olaf

Neil Butterworth

unread,
Apr 20, 2001, 3:39:49 AM4/20/01
to
"O.Petzold" <opet...@wit.regiocom.net> wrote in message
news:3ADFE545...@wit.regiocom.net...

>
> > BYW, why are you bothering writing this? There are a lot of existing
> > solutions to persistent object storage already out there.
> Please, could you give some URL for suitable solutions ?

You should evaluate the use of an OO database, which will do everything you
want and more. Examples with free single-user products (alast time I looked)
can be found at www.odi.com and www.poet.com - there are several others.


NeilB


Branimir Maksimovic

unread,
Apr 20, 2001, 4:58:14 PM4/20/01
to

"O.Petzold" <opet...@wit.regiocom.net> wrote in message
news:3ADF0E34...@wit.regiocom.net...

> Hello,
>
> on my process on writing the serialisiation class I hang
> on the following problem:
>
> class Serializable { [...]
> suid_t suid() const;
> }
>
> class Hoomer : public Serializable { [...] }
> class Bart : public Serializable { [...] }
>
> class SerializableFactory {
> [...]
> template <class _Tp> static Serializable* creator() {
> return new _Tp;
> }
> Serializable* create(Serializable::suid_t id) const;
> };
>
> int main() {
> [...]
> is >> id;
> Bart* bart = static_cast<Bart*>(factory.create(id));

Serializable* something = factory.create(id);
> is >> *something;


> [...]
> }
>
> But it could be a Hoomer as well. How can I cast using an id ?

You can't predict what it will be. So you have to work only threw
base class pointers, and later on (when object is loaded
you can perform dynamic_cast to see what type you got).

> The SerializableFactory::create() creates Barts or Hoomers on
> the given id using an allways registered creator. The assoziation
> between ids and loaders is done by a map. The solution could
> be a template but, how could it look ?

You don't need a template in this case.

>
> Thanks
> Olaf

Greetings, Bane.

Kent Dahl

unread,
Apr 19, 2001, 1:58:28 PM4/19/01
to
Neil Butterworth wrote:
> BYW, why are you bothering writing this? There are a lot of existing
> solutions to persistent object storage already out there.

AFAIK, they're all either commercial or don't do quite what you need.
:-P

BTW, you wouldn't happen to know about any Open-Source XML-based
serialization libraries/frameworks worth their salt?

--
<[ Kent Dahl ]>================<[ http://www.stud.ntnu.no/~kentda/ ]>
)____(stud.techn.;ind.øk.data)||(softwareDeveloper.at(Trustix))_(
/"Opinions expressed are mine and not those of my Employer, "\
( "the University, my girlfriend, stray cats, banana fruitflies, " )
\"nor the frontal lobe of my left cerebral hemisphere. "/

0 new messages