Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How would you organize such structure
Forum Updated to NodeBB v4.3 + New Features

How would you organize such structure

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maksim1979
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        maksim1979
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • M Offline
            M Offline
            maksim1979
            wrote on last edited by
            #5

            Thanks a lot SGaist
            Maxim

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved