473,322 Members | 1,610 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,322 software developers and data experts.

Is this the easiest way to backup objects?

Problem:

I have a properties dialog. X objects build the dialog, but a subclass
of X, such as Y, can add more options to the dialog for Y specific
properties. I would like to write code for the dialog that creates a
backup of the current object when the dialog pops up, and sets the
current modified object to the backup when the user clicks cancel (or
discards the backup if the user clicks OK). X already has a Clone
method.

(I realize this way of making backups isn't always practical for large
objects, but the ones I'm dealing with are fairly lightweight)

Solutions:

0. Simply write the code using X pointers. Problem: Then only the X
members get set because the assignment operator can't be virtual.

1. Template the dialog class so you can do Dialog<Xor Dialog<Y>,
causing a backup of the appropriate type to be made. Problem: The
dialog code gets a lot uglier just to make 1 line work.

2. Solution below seemed best, but I'm looking for feedback.

This is a compileable test case.

#include <iostream>

template<class T>
class CanBackup {
public:
CanBackup();
~CanBackup();
void MakeBackup();
void RestoreBackup();

virtual CanBackup<T>* Clone() = 0;

private:
T* backup;
};

template<class T>
CanBackup<T>::CanBackup()
: backup(NULL)
{
}

template<class T>
CanBackup<T>::~CanBackup()
{
if(backup)
delete backup;
}

template<class T>
void CanBackup<T>::MakeBackup()
{
backup = (T*)Clone();
}

template<class T>
void CanBackup<T>::RestoreBackup()
{
*((T*)this) = *backup;
}

class A : public CanBackup<A{
public:
A();
~A();
A* Clone();
int x;
};

A::A() {}
A::~A() {}

A* A::Clone()
{
return new A(*this);
}

// Should print: 3 5 3
// Actually prints: 3 5 5
int main() {
A foo;
foo.x = 3;
std::cout << foo.x << std::endl;
foo.MakeBackup();
foo.x = 5;
std::cout << foo.x << std::endl;
foo.RestoreBackup();
std::cout << foo.x << std::endl;
}

Aug 12 '06 #1
6 1715
k0*****@gmail.com wrote:
Problem:

I have a properties dialog. X objects build the dialog, but a subclass
of X, such as Y, can add more options to the dialog for Y specific
properties. I would like to write code for the dialog that creates a
backup of the current object when the dialog pops up, and sets the
current modified object to the backup when the user clicks cancel (or
discards the backup if the user clicks OK). X already has a Clone
method.

(I realize this way of making backups isn't always practical for large
objects, but the ones I'm dealing with are fairly lightweight)

Solutions:

0. Simply write the code using X pointers. Problem: Then only the X
members get set because the assignment operator can't be virtual.
Says who? Have you tried making it virtual? Did it not work?
1. Template the dialog class so you can do Dialog<Xor Dialog<Y>,
causing a backup of the appropriate type to be made. Problem: The
dialog code gets a lot uglier just to make 1 line work.

2. Solution below seemed best, but I'm looking for feedback.

This is a compileable test case.

#include <iostream>

template<class T>
class CanBackup {
public:
CanBackup();
~CanBackup();
void MakeBackup();
void RestoreBackup();

virtual CanBackup<T>* Clone() = 0;
Please drop the "<T>" inside the template. It only confuses
the readers of your code and adds nothing to it.
>
private:
T* backup;
};

template<class T>
CanBackup<T>::CanBackup()
: backup(NULL)
{
}

template<class T>
CanBackup<T>::~CanBackup()
{
if(backup)
delete backup;
There is no need to test 'backup' before deleting. "delete NULL;" is
a NOP.
}

template<class T>
void CanBackup<T>::MakeBackup()
{
backup = (T*)Clone();
Why do you need to cast? I would rather expect either 'Clone' to
return a T* or 'backup' to be of type 'CanBackup*'. See below.
}

template<class T>
void CanBackup<T>::RestoreBackup()
{
*((T*)this) = *backup;
This is utterly dangerous. Who told you that it's even going
to work? Casting 'this' like that is an invitation for a big
trouble.

If you make 'backup' to be of type 'CanBackup*', then you can
use 'dynamic_cast<T*>' here and only assign if you get non-null
ponter back...
}

class A : public CanBackup<A{
public:
A();
~A();
You don't need those.
A* Clone();
^^^^^^^^^^^
This should probably be private.
int x;
};

A::A() {}
A::~A() {}

A* A::Clone()
{
return new A(*this);
}

// Should print: 3 5 3
// Actually prints: 3 5 5
Of course. You're using some very dangerous constructs.
int main() {
A foo;
foo.x = 3;
std::cout << foo.x << std::endl;
foo.MakeBackup();
foo.x = 5;
std::cout << foo.x << std::endl;
foo.RestoreBackup();
std::cout << foo.x << std::endl;
}
So, I'd rewrite it as

template<class T>
class CanBackup {
public:
CanBackup() : backup(NULL) {}
virtual ~CanBackup() { delete backup; }
void MakeBackup();
void RestoreBackup();

virtual CanBackup* Clone() = 0;

private:
CanBackup* backup;
};

template<class T>
void CanBackup<T>::MakeBackup()
{
backup = Clone();
}

template<class T>
void CanBackup<T>::RestoreBackup()
{
T* that = dynamic_cast<T*>(backup);
T* self = dynamic_cast<T*>(this);
if (that && self)
*self = *that;
}

class A : public CanBackup<A{
public:
CanBackup<A>* Clone();
int x;
};

CanBackup<A>* A::Clone()
{
return new A(*this);
}

Now, for me this (along with your 'main') will print 3 5 3.
There is still some protection you need to do, probably, to ensure
that nobody does this:

class A { ... };
class B : public CanBackup<A{ ...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 12 '06 #2
k0*****@gmail.com wrote:
Problem:

I have a properties dialog. X objects build the dialog, but a subclass
of X, such as Y, can add more options to the dialog for Y specific
properties. I would like to write code for the dialog that creates a
backup of the current object when the dialog pops up, and sets the
current modified object to the backup when the user clicks cancel (or
discards the backup if the user clicks OK). X already has a Clone
method.

(I realize this way of making backups isn't always practical for large
objects, but the ones I'm dealing with are fairly lightweight)
[snip]

Consider using standard serialization/unserialization techniques. See
these FAQs:

http://www.parashift.com/c++-faq-lit...alization.html

and this handy library:

http://boost.org/libs/serialization/doc/index.html

Cheers! --M

Aug 12 '06 #3
Victor Bazarov wrote:
k0*****@gmail.com wrote:
0. Simply write the code using X pointers. Problem: Then only the X
members get set because the assignment operator can't be virtual.

Says who? Have you tried making it virtual? Did it not work?
Sorry, I should have explained more. The idea is that this can be used
with the compiler generated assignment operator, which is not virtual.
Now that I think about it the Clone method is probably not needed do to
the template either...
>
There is no need to test 'backup' before deleting. "delete NULL;" is
a NOP.
I'd read so much stuff telling me not to free() NULL pointers I thought
this carried over ;P Thanks :)
>
template<class T>
void CanBackup<T>::MakeBackup()
{
backup = (T*)Clone();

Why do you need to cast? I would rather expect either 'Clone' to
return a T* or 'backup' to be of type 'CanBackup*'. See below.
Without it the code doesn't compile. You can't implicitly cast
CanBackup<X*to X*.
>
template<class T>
void CanBackup<T>::RestoreBackup()
{
*((T*)this) = *backup;

This is utterly dangerous. Who told you that it's even going
to work? Casting 'this' like that is an invitation for a big
trouble.

If you make 'backup' to be of type 'CanBackup*', then you can
use 'dynamic_cast<T*>' here and only assign if you get non-null
ponter back...
I've rewritten it to this:

dynamic_cast<T&>(*this) = *backup;

This way an exception gets thrown because you can't have a NULL
reference.
>
A* Clone();
^^^^^^^^^^^
This should probably be private.
Good point :P
// Should print: 3 5 3
// Actually prints: 3 5 5

Of course. You're using some very dangerous constructs.
Whoops, it does actually print 3 5 3. I forgot to remove those old
comments from before I had it working :)

Aug 12 '06 #4
k0*****@gmail.com schrieb:
Victor Bazarov wrote:
>k0*****@gmail.com wrote:
>>template<class T>
void CanBackup<T>::MakeBackup()
{
backup = (T*)Clone();
Why do you need to cast? I would rather expect either 'Clone' to
return a T* or 'backup' to be of type 'CanBackup*'. See below.

Without it the code doesn't compile. You can't implicitly cast
CanBackup<X*to X*.
You can't even _explicitly_ cast CanBackup<X*to X* and get a sane result.
And you cast a CanBackup<X*>* to X*, what makes it even worser.

What makes you think that this should work?

--
Thomas
Aug 12 '06 #5
k0*****@gmail.com wrote:
>
>>There is no need to test 'backup' before deleting. "delete NULL;" is
a NOP.


I'd read so much stuff telling me not to free() NULL pointers I thought
this carried over ;P Thanks :)
Calling free() with a null pointer is safe.

--
Ian Collins.
Aug 12 '06 #6

Thomas J. Gritzan wrote:
k0*****@gmail.com schrieb:
Victor Bazarov wrote:
k0*****@gmail.com wrote:
template<class T>
void CanBackup<T>::MakeBackup()
{
backup = (T*)Clone();
Why do you need to cast? I would rather expect either 'Clone' to
return a T* or 'backup' to be of type 'CanBackup*'. See below.
Without it the code doesn't compile. You can't implicitly cast
CanBackup<X*to X*.

You can't even _explicitly_ cast CanBackup<X*to X* and get a sane result.
And you cast a CanBackup<X*>* to X*, what makes it even worser.

What makes you think that this should work?
Well, that it does work. I cast CanBackup<X>* to X*, not CanBackup<X*>*
to X* (note only one asterisk in the first). In practice X should
always be a subclass of CanBackup<X(that's intended usage, I could
probably add safeguards to enforce this).

Aug 13 '06 #7

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

Similar topics

3
by: Cristina | last post by:
Hallo i am a beginner into Oracle Technologies.I would like to make backup of my database,but i dont know how.Is there tools?Can i schedule the backup plan? thanks Cristina
1
by: Leader | last post by:
Hi, I want to take backup of database logfile periodically and automatically. What should i do then..... Thanks Hoque
3
by: anuke | last post by:
Hi there, I use shared space MSSQL server in my hosting server. And I can't backup my DB to my remote server. Please help how can I do it. Thank you
3
by: Colin Finck | last post by:
Hello! I need to backup a MySQL database (MySQL 4.0). But it is on a shared-hosting web server and so I don't have direct server access. I also have no phpMyAdmin installed. How can I backup the...
5
by: Kevin | last post by:
Can anyone recommend the easiest way to get a full copy of a database from one server to another. The servers are not part of the same organization or network. I have received a backup of the...
5
by: Jim | last post by:
Hello, I have a broken server that we are going to be moving off to a new server with a new version of DB2 but here is what I have right now: RedHat 7.0 (2.2.24smp) DB2 v6.1.0.40 I am...
6
by: Eric Herber | last post by:
I've a question regarding db2 (V8.1) and database backups going to a storage manager like TSM for example. As I can see in the storage manager if I backup the complete database over the TSM API...
14
by: vince | last post by:
Can I add (append) to an xml file that already contains a serialized object, and be able to deserialize to either or both objects from the same file...??? How is this done...?? thanks, vince
6
by: michael.spoden | last post by:
Hi, how can I fix lock-waits during an online backup? Is an online backup in DB2 V8.2 not realy online? I'm using DB2 V8.2 Fixpak 15 on Linux. The command to perform the backup is: db2 backup...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.