BREWのファイル管理(3)

こんなのはどうだろう

class InputStream{
public:
    int read() = 0;
    int read( Array< byte > buf , int off , int len );
    int close() = 0;
};
class OutputStream{
public:
    int write( int v ) = 0;
    int write( Array< byte > buf , int off , int len );
    int close() = 0;
};
class FileInputStream : public InputStream{
public:
    String getName() = 0;
};
class FileOutputStream : public OutputStream{
public:
    String getName() = 0;
};
class RandomAccessFileInputStream : FileInputStream{
public:
    Array< byte > getBuffer();
};
class RandomAccessFileOutputStream : FileOutputStream{
public:
    Array< byte > getBuffer();
};
class SequentiallyFileInputStream : FileInputStream{
};
class SequentiallyFileOutputStream : FileOutputStream{
};

どうせファイルオブジェクトを作るとそれ用のストリームが必要になってくるので、ファイルオブジェクトとストリームオブジェクトを分けずに、ファイル用のストリームクラスだけで解決する。
でも、これだとCRCとかその辺を付けようとすると、新しいストリームが必要になってくる。これはめんどい。
それに、ストリームから名前を取得するというのもおかしな気がする。
やっぱりファイルオブジェクトとストリームオブジェクトは分けるべきだろう。


ストリームのread()やwrite()を、ファイルオブジェクトに委譲してみるのはどうだろう。

class FileInputStream : public InputStream{
public:
    FileInputStream( File file ) : _file( file ){}
    int read(){ return _file.read(); }
    int read( Array< byte > buf , int off , int len )
        { return _file.read( buf , off , len ); }
    int close(){ return _file.close(); }
protected:
    File _file;
};
class File{
public:
    int read() = 0;
    int read( Array< byte > buf , int off , int len ) = 0;
    int write( int v ) = 0;
    int write( Array< byte > buf , int off , int len ) = 0;
    int close() = 0;
    String getName() = 0;
};
class RandomAccessFile : public File{
};
class SequentiallyFile : public File{
};

これだと、CRCを付けた場合だろうと何だろうと、Fileインターフェースを満たしていればストリームオブジェクトを変更する必要はない。
read()とwrite()を同じクラスのインターフェースにしているのが非常に格好悪いが、だからといってInput用のFileクラスとOutput用のFileクラスを作ると、同じファイルを表すオブジェクトが複数出来ることになる。これはもっと格好悪い。
でも、Read Onlyのつもりで開いているときにwrite()が出来てしまうのは問題な気がする。
なので、read(),write()は非公開にしておくべきだろう。


で、最後にそれぞれのファイルを開く為のManagerクラスを作る。

class FileManager{
public:
    static File openRandomAccessFile( const String& fileName );
    static File openSequentiallyFile( const String& fileName );
};

Singletonにすることも考えたけれども、メンバが無いのでこれで十分。


これにて完成!!


ということで、この設計で作ってみることにします。。