0

I wrote this code sample and found that it definitely hasn't looking good but I struggled trying to optimize it. Maybe somebody can help me with it. Qt5.

int MainWindow::readVariable(QVector<double> &value, QVector<double> &time, QString type, QFile *f)
{
    int buffer_size = 0;
    quint64 read_bytes = 0;
    while (1) 
    {
        if (type == "u_int32_t")
        {
            buffer_size = sizeof(unsigned);
            unsigned dest = 0;
            read_bytes = f->read(reinterpret_cast<char*>(&dest), buffer_size);
            value.append(dest);
             break; 
        }
        if (type == "int32_t")
        {
            buffer_size = sizeof(int32_t);
            int32_t dest = 0;
            read_bytes = f->read(reinterpret_cast<char*>(&dest), buffer_size);
            value.append(dest);
            break;
        }

        /* ... and so on for many-many variable types */
    }
}

"type" value I read from the XML-file earlier.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
user3243625
  • 49
  • 2
  • 3
  • 2
    it is impossible to fix bad design. Without details of actual problem impossible to propose alternative. Answers with template and polymorphism might be a solution but not necessary to your secret problem. – Marek R Jan 28 '14 at 09:11
  • I would add to Marek's comment that XML is a structured data format, which, together with a well designed schema and XML library, should eliminate the need for code like this. – Skizz Jan 28 '14 at 09:22
  • Try using [compile time hashing][1] and a `switch` statement. [1]: http://stackoverflow.com/questions/2111667/compile-time-string-hashing/2112111#2112111 – user1095108 Jan 28 '14 at 10:09
  • 3
    This question appears to be off-topic because it is about reviewing working code to make it better. – László Papp Jan 28 '14 at 10:22
  • @LaszloPapp how is that offtopic? I'm pretty sure there is a better way to make a universal parser. – Loïc Faure-Lacroix Jan 28 '14 at 11:18
  • 1
    @LoïcFaure-Lacroix that might be true but currently it is just a question asking for (better) code. If it would address for example a performance issue or diskutilization issue and aks to solve that then this question would be on-topic. – rene Jan 28 '14 at 11:23
  • You state that you use Qt5, is there any reason for not using the [QVariant](http://qt-project.org/doc/qt-5.0/qtcore/qvariant.html) class? – pauluss86 Jan 28 '14 at 09:27

2 Answers2

1

Might be an idea to use a template like

template< typename T >
void readVariable( QVector<double>& value, QVector<double>& time, QFile* f)
{
  int buffer_size = 0;
  quint64 read_bytes = 0;

  buffer_size = sizeof( T );
  T dest = 0;
  read_bytes = f->read( reinterpret_cast<char*>(&dest), buffer_size );
  value.append(dest);
}

which u can use like this

QVector<double> v;
QVector<double> t;
QFile* f = nullptr;

readVariable<int>( v, t, f );

where int is the type

codencandy
  • 1,701
  • 1
  • 10
  • 20
0

You can use polymorphism instead of if and reduce the duplicated code.

lsbbo
  • 304
  • 3
  • 12