tags:

views:

59

answers:

3

I came a across a new problem when using templates. This is me being a little creative from a book I recently read, expanding his ideas, and it has this code example.

Say you have a templated Array2D class. And you have this method (Array2D::WriteFile):

    bool WriteFile( const char* p_filename )
{
    FILE* outfile = 0;
    int written = 0;

    // open the file
    outfile = fopen( p_filename, "wb" );

    // return if it couldn't be opened
    if( outfile == 0 )
        return false;

    // write the array and close thef ile
    written = fwrite( m_array, sizeof( Datatype ), m_size, outfile );
    fclose( outfile );

    // if we didn't write the number of items we expected,
    // return failure
    if( written != m_size )
        return false;

    // return success.
    return true;
}

This works fine for basic data types such as int, char, float, etc. But what if you get a little creative making the data type a compound variable such as SLinkedList (singly linked list). It would be Array2D. Then you call your old WriteFile() function. It'll save it, but you just only saved one node. The rest of the nodes went into oblivion never to return.

A few ideas came in my head: 1. Twiddle your thumbs watching the clock go by towards your deadline. 2. Make Array2D::WriteFile() a pure virtual function causing a more specific class to save it the way it should. But then I can't use Array2D by itself in other stand-alone situations. 3. Write a function pointer to save, but I think it could get messy because you don't know what data your passing into til the time comes. It could vary based on the data type. 4. Realize templates aren't the solution, perhaps.

The template class isn't solving every case scenario for me based on the data type. So what would be a good solution in your opinion? Thanks!

+2  A: 

You could simply use the iostreams equivalent. Instead of using fwrite which is a C function and only works with C datatypes, use the C++ equivalent which can be overloaded and specialized for custom types.

std::ofstream file(p_filename);
file << m_array;

operator << works for all built-in types, and for custom types, you can define it yourself.

jalf
+3  A: 

Your objects need to be serializable. Think about what actually happens under the hood when you try to write out the head of a linked list this way - of course it just writes the first node; the write function has no idea what it's writing, so it has no idea how to follow the pointers to the next node and write that too. It just sees bits and writes them out until the end of the current object, which is in that case just the first node.

Serialization basically means, for each non-trivial type, writing a method that packages up that type into a flat stream of bytes suitable for writing out (and ideally also a second method that reads that format back in).

Now, your question is tagged C++, but the code you've written is very C-like. The normal way to implement a serializable object in C++ is to override the << operator with a std::ostream& argument and return value, e.g.

 std::ostream& MyType::operator<< (std::ostream& out)
 {
    // Here, write out the logical contents of this object in whatever format you
    // feel is appropriate (keep in mind endianness and floating point representation
    // if you want portability!)
    return out << this->field1 << this->field2 << this->field3;
 }

And symmetrically

 std::istream& MyType::operator>> (std::istream& in)
 {
    // Here, read in the logical contents of this object in the same format
    return in >> this->field1 >> this->field2 >> this->field3;
 }

Now, you can just do something like this:

 MyType t;
 std::ofstream outputFile("myoutputfile.dat");

 outputFile << t;

Note that this approach of overriding the stream operators means that if, for example MyType::field2 happens to itself be a complex object, it will still be serialized and deserialized properly in the above code as long as its stream operators are overridden.

But if you want to continue using C-style file I/O as in your original post, things aren't going to be quite so clean. If your code is really supposed to be C++ though, you should be using the iostream libraries to do your file I/O.

Tyler McHenry
I got the output to work, but not exactly sure how to read in a 2d array with each cell being a singly linked list. It's all digits at that point :) And should it be in binary output? It's unfortunate keeping a generic reusable class like this and then declaring multiple overridden forms of '<<' to every complex variable I think of that may be written. Those complex variables may not be in every project making this class constantly needing adjustments. This got me started in a good direction though so thanks!
Phil
+1  A: 

My immediate reaction is that you're looking at a problem that's been looked at (and solved, to at least some degree) before. Yes, it can still use templates pretty heavily. I'd take a look at std::copy along with std::ostream_iterator, as well as some serialization libraries, such as Boost serialization.

Jerry Coffin