Generic archive programming

Archive formats such as zip

Also see wxFileSystem for a higher level interface that can handle archive files in a generic way.

The specific archive classes, such as the wxZip classes, inherit from the following abstract classes which can be used to write code that can handle any of the archive types:

wxArchiveInputStream Input stream
wxArchiveOutputStream Output stream
wxArchiveEntry Holds the meta-data for an entry (e.g. filename)

In order to able to write generic code it's necessary to be able to create instances of the classes without knowing which archive type is being used. So there is a class factory for each archive type, derived from wxArchiveClassFactory, which can create the other classes.

For example, given wxArchiveClassFactory* factory, streams and entries can be created like this:

    // create streams without knowing their type
    wxArchiveInputStreamPtr  inarc(factory->NewStream(in));
    wxArchiveOutputStreamPtr outarc(factory->NewStream(out));

    // create an empty entry object
    wxArchiveEntryPtr        entry(factory->NewEntry());

The smart pointer types wxArchiveInputStreamPtr, wxArchiveOutputStreamPtr and wxArchiveEntryPtr would need to have already have been defined, which could be done like this:

    #include <wx/ptr_scpd.h>
    wxDEFINE_SCOPED_PTR_TYPE(wxArchiveInputStream);
    wxDEFINE_SCOPED_PTR_TYPE(wxArchiveOutputStream);
    wxDEFINE_SCOPED_PTR_TYPE(wxArchiveEntry);

The class factory itself can either be created explicitly:

    wxArchiveClassFactory *factory = new wxZipClassFactory;

or using wxWidgets' RTTI:

wxArchiveClassFactory *MakeFactory(const wxString& type)
{
    wxString name = _T("wx") + type.Left(1).Upper() +
                    type.Mid(1).Lower() + _T("ClassFactory");

    wxObject *pObj = wxCreateDynamicObject(name);
    wxArchiveClassFactory *pcf = wxDynamicCast(pObj, wxArchiveClassFactory);

    if (!pcf) {
        wxLogError(_T("can't handle '%s' archives"), type.c_str());
        delete pObj;
    }

    return pcf;
}

ymasuda 平成17年11月19日