OO Design - polymorphism - how to design for handing streams of different file types
- by Kache4
I've little experience with advanced OO practices, and I want to design this properly as an exercise. I'm thinking of implementing the following, and I'm asking if I'm going about this the right way.
I have a class PImage that holds the raw data and some information I need for an image file. Its header is currently something like this:
#include <boost/filesytem.hpp>
#include <vector>
namespace fs = boost::filesystem;
class PImage
{
public:
PImage(const fs::path& path, const unsigned char* buffer, int bufferLen);
const vector<char> data() const { return data_; }
const char* rawData() const { return &data_[0]; }
/*** other assorted accessors ***/
private:
fs::path path_;
int width_;
int height_;
int filesize_;
vector<char> data_;
}
I want to fill the width_ and height_ by looking through the file's header. The trivial/inelegant solution would be to have a lot of messy control flow that identifies the type of image file (.gif, .jpg, .png, etc) and then parse the header accordingly.
Instead of using vector<char> data_, I was thinking of having PImage use a class, RawImageStream data_ that inherits from vector<char>. Each type of file I plan to support would then inherit from RawImageStream, e.g. RawGifStream, RawPngStream.
Each RawXYZStream would encapsulate the respective header-parsing functions, and PImage would only have to do something like height_ = data_.getHeight();.
Am I thinking this through correctly?
How would I create the proper RawImageStream subclass for data_ to be in the PImage ctor? Is this where I could use an object factory?
Anything I'm forgetting?