tags:

views:

107

answers:

2

I am trying to create a generic class to write and read Objects to/from file. Called it ActiveRecord class

only has one method, which saves the class itself:

void ActiveRecord::saveRecord(){
 string fileName = "data.dat";

 ofstream stream(fileName.c_str(), ios::out);
 if (!stream) {
  cerr << "Error opening file: " << fileName << endl;
  exit(1);
 }
 stream.write(reinterpret_cast<const char *> (this), sizeof(ActiveRecord));
 stream.close();
}

now I'm extending this class with User class:

class User : public ActiveRecord
{
 public:
  User(void);
  ~User(void);
  string name;
  string lastName;
};

to create and save the user I would like to do something like:

User user = User();
user.name = "John";
user.lastName = "Smith"
user.save();

how can I get this ActiveRecord::saveRecord() method to take any object, and class definition so it writes whatever i send it:

to look like:

void ActiveRecord::saveRecord(foo_instance, FooClass){
 string fileName = "data.dat";

 ofstream stream(fileName.c_str(), ios::out);
 if (!stream) {
  cerr << "Error opening file: " << fileName << endl;
  exit(1);
 }
 stream.write(reinterpret_cast<const char *> (foo_instance), sizeof(FooClass));
 stream.close();
}

and while we're at it, what is the default Object type in c++. eg. in objective-c it's id in java it's Object in AS3 it's Object what is it in C++??

+5  A: 
stream.write(reinterpret_cast<const char *> (foo_instance), sizeof(FooClass));

This doesn't work. string allocates its data on the heap (IIRC, when it's larger than 16chars). Your reinterpret cast will not include that heap data.

Don't reinvent the wheel, this is a non-trivial, but solved problem. Use Google Protocol Buffers, XML, or the boost serialization library.

What you have in mind is templates, but you can't "just serialize any object" because outside of POD (plain-old-data) types, their representation isn't obvious.

In addition, using sizeof(BaseClass) with the User subclass will not work. It'll slice the subclass's member data off of the cast and not put it in the file.

And there is no "default Object type in c++".

Stephen
+1 for don't reinvent the wheel.
Michael Aaron Safyan
Thanks guys for the response. The retarded course I'm taking seems tto be as old as c++ itself requiring the use of fstream. So I thought I'll ake it further and abstract bit more. So stuff that wheel like u said, I'll check out those lbraries. Thx
Bach
@user259789 : Since it's for a class, you're probably fine with the streaming solution that Georg outlined below. It's good to learn a bit about serialization before entirely leaning on libraries. The unfortunate part is, you'll need each class to write its own stream writer, you won't be able to write a generic version like you tried.
Stephen
Ended up using Google Protocol Buffers, thanks Stephen for the suggestion.
Bach
+1  A: 

What you are proposing doesn't do a deep serialization, you can't know in general what kind of resources the object holds onto.
To let it store any object, you would use a template function and delegate the actual storing to the class in question or a friend thereof (i.e. operator<<()):

template<class T> void ActiveRecord::saveRecord(const T& foo) {
    // ...
    stream << foo;
    // ...
}

For this to work you need to provide overloads for operator<<() for the classes in question:

class Foo {
    friend std::ostream& operator<<(std::ostream&, const Foo&);
    // ...
    int a, b;
    // ...
};

std::ostream& operator<<(std::ostream& o, const Foo& f) {
    o << f.a << f.b;
    // ...
    return o;
}

But as Stephen said, why not use proven solutions for the serialization instead of rolling your own?

Georg Fritzsche