I have an AbstractIndex
defining the interface for its Item
s and the sorting and query algorithms. Further I have several concrete indizes like in the example below a FileIndex
. Now I want to serialize the indizes. I guess serializeIndex()
works, but I could not test it. In FileIndex::buildIndex()
the operator>>
of AbstractIndexProvider::Item
gets called.I tried to downcast the _index
but I get strange compiletime errors. Using boost I could easily declare the type, but I want to drop the boost dependency. So what do I have to do, to let Qt instanciate the correct class in FileIndex::buildIndex()
?
class AbstractIndex
{
public:
class Item;
void query(const QString &req, QVector<Item*> *res);
protected:
virtual void buildIndex() = 0;
virtual void serializeIndex() const = 0;
QVector<Item*> _index;
};
class AbstractIndexProvider::Item
{
public:
QString _name;
// Several pure virtual functions...
// Serialization
friend QDataStream &operator<<(QDataStream &out, AbstractIndexProvider::Item const * const item);
friend QDataStream &operator>>(QDataStream &in, AbstractIndexProvider::Item *item);
};
/**************************************************************************/
class FileIndex : public AbstractIndexProvider
{
public:
class Item;
protected:
void buildIndex() override;
void serializeIndex() const override;
};
class FileIndex::Item : public AbstractIndexProvider::Item
{
friend class FileIndex;
protected:
QString _path;
friend QDataStream &operator<<(QDataStream &out, const FileIndex::Item &item);
friend QDataStream &operator>>(QDataStream &in, FileIndex::Item &item);
};
/**************************************************************************/
void FileIndex::serializeIndex() const
{
QFile file(_indexFile);
if (file.open(QIODevice::ReadWrite| QIODevice::Text))
{
QDataStream stream( &file );
stream << _index;
return;
}
}
/**************************************************************************/
void FileIndex::buildIndex()
{
QFile file(_indexFile);
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QDataStream stream( &file );
stream >> _index; // WHAT TO DO HERE?
return;
}
}