Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How would you organize such structure



  • Hey hey,

    Guys, C++ is still a pain for me, so any advice would be really helpfull. Actually I have to parse a file that has a very simple format, namely it's just a list of records [record 1][...][record N], where each record has this structure:
    [keyword; type; number of elements N; N elements of the specified type]. In other words such file could look like:
    @["key1"; int; 3; {2,7,9}]["key2"; string; 2; {"hello", "world"}]["key3"; float; 1; {3.1415926}]@
    Now I need a structure that represents such format (my parse method would return it).

    So I thought that the parse should return QList<Record> and Record structure would look like:
    @
    template <typename T>
    struct Record {
    QString keyword;
    QList<T> data;
    }
    @
    or something like that. And here the way I use it
    @QList<Record> records = MyParser::parse("/path/to/file")@

    But my compiler said that it would be compile it :(

    One of possible solutions is to add one method for each possible type as here:
    @
    struct Record {
    QString keyword;
    QString type;

    QList<int> getIntData();   
    QList<float> getFloatData();
    

    ...
    QList<QString> getStringData();
    }
    @

    In this approach, I first have to check type and after that use appropriate methos, but looks ugly.

    @
    QList<Record> records = MyParser::parse("/path/to/file");

    foreach(record : records ) {
    if (record.type == "int") {
    QList<int> data = record.getIntData();
    ...
    } else if (record.type == "float") {
    QList<flaot> data = record.getFloatData();
    ...
    } ...
    }
    @

    So any thoughts?

    Thanks a lot,
    Maxim


  • Lifetime Qt Champion

    Hi,

    You might be interested by QVariant to store/retrieve your data

    Hope it helps



  • Yep SGaist, thanks a lot. I've already found this class. I just have one question. Is it usually safe to use it from performance point of you?


  • Lifetime Qt Champion

    The only answer is: you need to test it for your needs. AFAIK, there are no particular hit using this class



  • Thanks a lot SGaist
    Maxim


Log in to reply