Hi,
I have the following code:
BinaryEncodedDictionary
* BinaryMetaDataDecoder
::parse(QIODevice *sourceDevice
) { qDebug() << "Received a QIODevice to parse from!";
if(sourceDevice->peek(1) != "d") {
qDebug() << "Input stream contains an invalid input file";
return 0;
}
else {
qDebug() << "Found a dictionary, valid file";
return parseDictionary(sourceDevice);
}
}
BinaryEncodedDictionary
* BinaryMetaDataDecoder
::parseDictionary(QIODevice *stream
) { //... Implementation will follow here
return new BinaryEncodedDictionary;
}
BinaryEncodedInteger
* BinaryMetaDataDecoder
::parseInteger(QIODevice *stream
) { //... Implementation will follow here
return new BinaryEncodedInteger;
}
BinaryEncodedString
* BinaryMetaDataDecoder
::parseString(QIODevice *stream
) { //... Implementation will follow here
return new BinaryEncodedString;
}
BinaryEncodedList
* BinaryMetaDataDecoder
::parseList(QIODevice *stream
) { //... Implementation will follow here
return new BinaryEncodedList;
}
BinaryEncodedDictionary* BinaryMetaDataDecoder::parse(QIODevice *sourceDevice) {
qDebug() << "Received a QIODevice to parse from!";
if(sourceDevice->peek(1) != "d") {
qDebug() << "Input stream contains an invalid input file";
return 0;
}
else {
qDebug() << "Found a dictionary, valid file";
return parseDictionary(sourceDevice);
}
}
BinaryEncodedDictionary* BinaryMetaDataDecoder::parseDictionary(QIODevice *stream) {
//... Implementation will follow here
return new BinaryEncodedDictionary;
}
BinaryEncodedInteger* BinaryMetaDataDecoder::parseInteger(QIODevice *stream) {
//... Implementation will follow here
return new BinaryEncodedInteger;
}
BinaryEncodedString* BinaryMetaDataDecoder::parseString(QIODevice *stream) {
//... Implementation will follow here
return new BinaryEncodedString;
}
BinaryEncodedList* BinaryMetaDataDecoder::parseList(QIODevice *stream) {
//... Implementation will follow here
return new BinaryEncodedList;
}
To copy to clipboard, switch view to plain text mode
All of the classes above are plain old C++ classes (not inheriting from QObject) and part of
the following class hierarchy:
Pure virtual (super) class: BinaryEncodedAbstractType
Child (inheriting classes): BinaryEncodedDictionary, BinaryEncodedInteger, BinaryEncodedString, BinaryEncodedList
BinaryEncodedDictionary is implemented like this:
//binaryencodeddictionary.h
class BinaryEncodedDictionary : public BinaryEncodedAbstractType {
public:
BinaryEncodedDictionary();
//TODO: Delete pointers contained by hashMap
~BinaryEncodedDictionary();
private:
QHash<BinaryEncodedString*, BinaryEncodedAbstractType*> hashMap;
};
//binaryencodeddictionary.cpp
BinaryEncodedDictionary::BinaryEncodedDictionary() {
hashMap = new QHash<BinaryEncodedString*, BinaryEncodedAbstractType*>();
}
BinaryEncodedDictionary::~BinaryEncodedDictionary() {
//TODO: We need to remove dynamically allocated values (needed for polymorphism)
}
//binaryencodeddictionary.h
class BinaryEncodedDictionary : public BinaryEncodedAbstractType {
public:
BinaryEncodedDictionary();
//TODO: Delete pointers contained by hashMap
~BinaryEncodedDictionary();
private:
QHash<BinaryEncodedString*, BinaryEncodedAbstractType*> hashMap;
};
//binaryencodeddictionary.cpp
BinaryEncodedDictionary::BinaryEncodedDictionary() {
hashMap = new QHash<BinaryEncodedString*, BinaryEncodedAbstractType*>();
}
BinaryEncodedDictionary::~BinaryEncodedDictionary() {
//TODO: We need to remove dynamically allocated values (needed for polymorphism)
}
To copy to clipboard, switch view to plain text mode
The hashMap member above contains the heap-allocated return values from calls to parse*() functions as keys
and values. Without going further into implementation details (if not needed), my main concern is the
deletion of objects allocated by the parse*() functions. The returned BinaryEncodedDictionary from parse()
function contains the pointers to these objects through the hashMap member variable.
I am trying to make sure that the objects are de-allocated when BinaryEncodedDictionary goes out of scope eventually,
and at the same time, I would like to make use of QScopedPointer and/or QSharedPointer in order not to have
to deal with manually freeing the memory with delete.
How could I use above in my code? I am planing to return QScopedPointers instead of raw pointers as the return
values from the functions above, but will it be enough or do I have to do anything more? Any other suggestions?
I am very thankfull to any kind of desing improvement suggestions as well.
Thank you in advance.
Kind Regards,
Veroslav
Bookmarks