I’m extracting files from zip and rar archives into raw buffers. I created the following to wrap minizip and unrarlib:
Archive.hpp – Used to access everything. If I could make all the functions in the other classes inaccessible from the outside, I would. (Actually, I suppose I could friend all the other classes in Archive and use private function callbacks…, but that’s soo roundabout.)
#include "ArchiveBase.hpp"
#include "ArchiveDerived.hpp"
class Archive {
public:
Archive(string path) {
/* logic here to determine type */
switch(type) {
case RAR:
archive_ = new ArchiveRar(path);
break;
case ZIP:
archive_ = new ArchiveZip(path);
break;
case UNKNOWN_ARCHIVE:
throw;
break;
}
}
Archive(Archive& other) {
archive_ = // how do I copy an abstract class?
}
~Archive() { delete archive_; }
void passThrough(ArchiveBase::Data& data) { archive_->passThrough(data); }
Archive& operator = (Archive& other) {
if (this == &other) return *this;
ArchiveBase* newArchive = // can't instantiate....
delete archive_;
archive_ = newArchive;
return *this;
}
private:
ArchiveBase* archive_;
}
ArchiveBase.hpp
class ArchiveBase {
public:
// Is there any way to put this struct in Archive instead,
// so that outside classes instantiating one could use
// Archive::Data instead of ArchiveBase::Data?
struct Data {
int field;
};
virtual void passThrough(Data& data) = 0;
/* more methods */
}
ArchiveDerived.hpp “Derived” being “Zip” or “Rar”
#include "ArchiveBase.hpp"
class ArchiveDerived : public ArchiveBase {
public:
ArchiveDerived(string path);
void passThrough(ArchiveBase::Data& data);
private:
/* fields needed by minizip/unrarlib */
// example zip:
unzFile zipFile_;
// example rar:
RARHANDLE rarFile_;
}
ArchiveDerived.cpp
#include "ArchiveDerived.hpp"
ArchiveDerived::ArchiveDerived(string path) { //implement }
ArchiveDerived::passThrough(ArchiveBase::Data& data) { //implement }
Somebody had suggested I use this design so that I could do:
Archive archiveFile(pathToZipOrRar);
archiveFile.passThrough(extractParams); // yay polymorphism!
-
How do I write a cctor for Archive?
-
What about op= for Archive?
-
What can I do about “renaming”
ArchiveBase::DatatoArchive::Data? (Both minizip and unrarlib use such structs for input and output. Data is generic for Zip & Rar and later is used to create the respective library’s struct.) Everything else is accessed viaArchive, and I’d like to make declaringDatain an outside class this way as well.
I know I could throw away my current class Archive, name ArchiveBase into Archive, and use a global factory function. However, I wanted to avoid using the global function.
First of all you can’t “copy” an abstract class because you can’t instantiate one. Instead, what you should do is set up a std::tr1::shared_ptr of that class and pass in a pointer.
Use a factory function outside of the Archive class for instantiation.
For the = operator, simply holding onto a smart pointer such as this and using the “=” will perform the safe transfer of knowledge between classes. It performs reference counting and will delete the pointer so you don’t have to and only when it’s safe to do so.
Let the smart pointers worry about deleting, copying, and all that for you.