Changeset View
Changeset View
Standalone View
Standalone View
src/backend/datasources/LiveDataSource.cpp
Show First 20 Lines • Show All 243 Lines • ▼ Show 20 Line(s) | 243 | void LiveDataSource::setFileType(AbstractFileFilter::FileType type) { | |||
---|---|---|---|---|---|
244 | m_fileType = type; | 244 | m_fileType = type; | ||
245 | } | 245 | } | ||
246 | 246 | | |||
247 | AbstractFileFilter::FileType LiveDataSource::fileType() const { | 247 | AbstractFileFilter::FileType LiveDataSource::fileType() const { | ||
248 | return m_fileType; | 248 | return m_fileType; | ||
249 | } | 249 | } | ||
250 | 250 | | |||
251 | void LiveDataSource::setFilter(AbstractFileFilter* f) { | 251 | void LiveDataSource::setFilter(AbstractFileFilter* f) { | ||
252 | delete m_filter; | ||||
asemke: this needs only be done if m_filter != nulltpr, | |||||
Actually nothing happens when deleting a nullptr (https://en.cppreference.com/w/cpp/language/delete). The check would be redundant. For newer versions of C++ this assumption seems to change a little, in case a non-standard deallocator is used – which we don't, the standard deallocator just does nothing with a nullptr. croick: Actually nothing happens when deleting a nullptr ([[ https://en.cppreference. | |||||
According to the c++-11 standard (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf, 5.3.5/7), it's an unspecified behavior for the null pointer case. asemke: According to the c++-11 standard (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242. | |||||
Which means, that operator delete(void* ptr) either is called or not. And if it is called, then 18.6.1.1/14 applies, which states, that it does nothing if ptr is a null pointer. croick: Which means, that `operator delete(void* ptr)` either is called or not. And if it is called… | |||||
asemke: It's never too late to learn.. Thanks. | |||||
252 | m_filter = f; | 253 | m_filter = f; | ||
253 | } | 254 | } | ||
254 | 255 | | |||
255 | AbstractFileFilter* LiveDataSource::filter() const { | 256 | AbstractFileFilter* LiveDataSource::filter() const { | ||
256 | return m_filter; | 257 | return m_filter; | ||
257 | } | 258 | } | ||
258 | 259 | | |||
259 | /*! | 260 | /*! | ||
▲ Show 20 Lines • Show All 232 Lines • ▼ Show 20 Line(s) | |||||
492 | //################################# SLOTS #################################### | 493 | //################################# SLOTS #################################### | ||
493 | //############################################################################## | 494 | //############################################################################## | ||
494 | 495 | | |||
495 | /* | 496 | /* | ||
496 | * called periodically or on new data changes (file changed, new data in the socket, etc.) | 497 | * called periodically or on new data changes (file changed, new data in the socket, etc.) | ||
497 | */ | 498 | */ | ||
498 | void LiveDataSource::read() { | 499 | void LiveDataSource::read() { | ||
499 | DEBUG("\nLiveDataSource::read()"); | 500 | DEBUG("\nLiveDataSource::read()"); | ||
500 | if (m_filter == nullptr) | 501 | if (!m_filter) | ||
501 | return; | 502 | return; | ||
502 | 503 | | |||
503 | //initialize the device (file, socket, serial port) when calling this function for the first time | 504 | //initialize the device (file, socket, serial port) when calling this function for the first time | ||
504 | if (!m_prepared) { | 505 | if (!m_prepared) { | ||
505 | DEBUG(" Preparing device: update type = " << ENUM_TO_STRING(LiveDataSource, UpdateType, m_updateType)); | 506 | DEBUG(" Preparing device: update type = " << ENUM_TO_STRING(LiveDataSource, UpdateType, m_updateType)); | ||
506 | switch (m_sourceType) { | 507 | switch (m_sourceType) { | ||
507 | case FileOrPipe: | 508 | case FileOrPipe: | ||
508 | m_file = new QFile(m_fileName); | 509 | m_file = new QFile(m_fileName); | ||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Line(s) | |||||
560 | qint64 bytes = 0; | 561 | qint64 bytes = 0; | ||
561 | 562 | | |||
562 | switch (m_sourceType) { | 563 | switch (m_sourceType) { | ||
563 | case FileOrPipe: | 564 | case FileOrPipe: | ||
564 | DEBUG("Reading FileOrPipe. type = " << ENUM_TO_STRING(AbstractFileFilter, FileType, m_fileType)); | 565 | DEBUG("Reading FileOrPipe. type = " << ENUM_TO_STRING(AbstractFileFilter, FileType, m_fileType)); | ||
565 | switch (m_fileType) { | 566 | switch (m_fileType) { | ||
566 | case AbstractFileFilter::Ascii: | 567 | case AbstractFileFilter::Ascii: | ||
567 | if (m_readingType == LiveDataSource::ReadingType::WholeFile) { | 568 | if (m_readingType == LiveDataSource::ReadingType::WholeFile) { | ||
568 | dynamic_cast<AsciiFilter*>(m_filter)->readFromLiveDevice(*m_file, this, 0); | 569 | static_cast<AsciiFilter*>(m_filter)->readFromLiveDevice(*m_file, this, 0); | ||
569 | } else { | 570 | } else { | ||
570 | bytes = dynamic_cast<AsciiFilter*>(m_filter)->readFromLiveDevice(*m_file, this, m_bytesRead); | 571 | bytes = static_cast<AsciiFilter*>(m_filter)->readFromLiveDevice(*m_file, this, m_bytesRead); | ||
571 | m_bytesRead += bytes; | 572 | m_bytesRead += bytes; | ||
572 | DEBUG("Read " << bytes << " bytes, in total: " << m_bytesRead); | 573 | DEBUG("Read " << bytes << " bytes, in total: " << m_bytesRead); | ||
573 | } | 574 | } | ||
574 | break; | 575 | break; | ||
575 | case AbstractFileFilter::Binary: | 576 | case AbstractFileFilter::Binary: | ||
576 | //TODO: not implemented yet | 577 | //TODO: not implemented yet | ||
577 | // bytes = dynamic_cast<BinaryFilter*>(m_filter)->readFromLiveDevice(*m_file, this, m_bytesRead); | 578 | // bytes = qSharedPointerCast<BinaryFilter>(m_filter)->readFromLiveDevice(*m_file, this, m_bytesRead); | ||
578 | // m_bytesRead += bytes; | 579 | // m_bytesRead += bytes; | ||
579 | //TODO: other types not implemented yet | 580 | //TODO: other types not implemented yet | ||
580 | case AbstractFileFilter::Image: | 581 | case AbstractFileFilter::Image: | ||
581 | case AbstractFileFilter::HDF5: | 582 | case AbstractFileFilter::HDF5: | ||
582 | case AbstractFileFilter::NETCDF: | 583 | case AbstractFileFilter::NETCDF: | ||
583 | case AbstractFileFilter::FITS: | 584 | case AbstractFileFilter::FITS: | ||
584 | case AbstractFileFilter::JSON: | 585 | case AbstractFileFilter::JSON: | ||
585 | case AbstractFileFilter::ROOT: | 586 | case AbstractFileFilter::ROOT: | ||
Show All 11 Lines | 595 | case NetworkTcpSocket: | |||
597 | m_tcpSocket->connectToHost(m_host, m_port, QIODevice::ReadOnly); | 598 | m_tcpSocket->connectToHost(m_host, m_port, QIODevice::ReadOnly); | ||
598 | DEBUG("reading from TCP socket. state after reconnect = " << m_tcpSocket->state()); | 599 | DEBUG("reading from TCP socket. state after reconnect = " << m_tcpSocket->state()); | ||
599 | break; | 600 | break; | ||
600 | case NetworkUdpSocket: | 601 | case NetworkUdpSocket: | ||
601 | DEBUG(" Reading from UDP socket. state = " << m_udpSocket->state()); | 602 | DEBUG(" Reading from UDP socket. state = " << m_udpSocket->state()); | ||
602 | 603 | | |||
603 | // reading data here | 604 | // reading data here | ||
604 | if (m_fileType == AbstractFileFilter::Ascii) | 605 | if (m_fileType == AbstractFileFilter::Ascii) | ||
605 | dynamic_cast<AsciiFilter*>(m_filter)->readFromLiveDeviceNotFile(*m_device, this); | 606 | static_cast<AsciiFilter*>(m_filter)->readFromLiveDeviceNotFile(*m_device, this); | ||
606 | break; | 607 | break; | ||
607 | case LocalSocket: | 608 | case LocalSocket: | ||
608 | DEBUG(" Reading from local socket. state before abort = " << m_localSocket->state()); | 609 | DEBUG(" Reading from local socket. state before abort = " << m_localSocket->state()); | ||
609 | if (m_localSocket->state() == QLocalSocket::ConnectingState) | 610 | if (m_localSocket->state() == QLocalSocket::ConnectingState) | ||
610 | m_localSocket->abort(); | 611 | m_localSocket->abort(); | ||
611 | m_localSocket->connectToServer(m_localSocketName, QLocalSocket::ReadOnly); | 612 | m_localSocket->connectToServer(m_localSocketName, QLocalSocket::ReadOnly); | ||
612 | if (m_localSocket->waitForConnected()) | 613 | if (m_localSocket->waitForConnected()) | ||
613 | m_localSocket->waitForReadyRead(); | 614 | m_localSocket->waitForReadyRead(); | ||
614 | DEBUG(" Reading from local socket. state after reconnect = " << m_localSocket->state()); | 615 | DEBUG(" Reading from local socket. state after reconnect = " << m_localSocket->state()); | ||
615 | break; | 616 | break; | ||
616 | case SerialPort: | 617 | case SerialPort: | ||
617 | DEBUG(" Reading from serial port"); | 618 | DEBUG(" Reading from serial port"); | ||
618 | 619 | | |||
619 | // reading data here | 620 | // reading data here | ||
620 | if (m_fileType == AbstractFileFilter::Ascii) | 621 | if (m_fileType == AbstractFileFilter::Ascii) | ||
621 | dynamic_cast<AsciiFilter*>(m_filter)->readFromLiveDeviceNotFile(*m_device, this); | 622 | static_cast<AsciiFilter*>(m_filter)->readFromLiveDeviceNotFile(*m_device, this); | ||
622 | break; | 623 | break; | ||
623 | case MQTT: | 624 | case MQTT: | ||
624 | break; | 625 | break; | ||
625 | } | 626 | } | ||
626 | } | 627 | } | ||
627 | 628 | | |||
628 | /*! | 629 | /*! | ||
629 | * Slot for the signal that is emitted once every time new data is available for reading from the device (not UDP or Serial). | 630 | * Slot for the signal that is emitted once every time new data is available for reading from the device (not UDP or Serial). | ||
630 | * It will only be emitted again once new data is available, such as when a new payload of network data has arrived on the network socket, | 631 | * It will only be emitted again once new data is available, such as when a new payload of network data has arrived on the network socket, | ||
631 | * or when a new block of data has been appended to your device. | 632 | * or when a new block of data has been appended to your device. | ||
632 | */ | 633 | */ | ||
633 | void LiveDataSource::readyRead() { | 634 | void LiveDataSource::readyRead() { | ||
634 | DEBUG("LiveDataSource::readyRead() update type = " << ENUM_TO_STRING(LiveDataSource,UpdateType,m_updateType)); | 635 | DEBUG("LiveDataSource::readyRead() update type = " << ENUM_TO_STRING(LiveDataSource,UpdateType,m_updateType)); | ||
635 | DEBUG(" REMAINING TIME = " << m_updateTimer->remainingTime()); | 636 | DEBUG(" REMAINING TIME = " << m_updateTimer->remainingTime()); | ||
636 | 637 | | |||
637 | if (m_fileType == AbstractFileFilter::Ascii) | 638 | if (m_fileType == AbstractFileFilter::Ascii) | ||
638 | dynamic_cast<AsciiFilter*>(m_filter)->readFromLiveDeviceNotFile(*m_device, this); | 639 | static_cast<AsciiFilter*>(m_filter)->readFromLiveDeviceNotFile(*m_device, this); | ||
639 | 640 | | |||
640 | //TODO: not implemented yet | 641 | //TODO: not implemented yet | ||
641 | // else if (m_fileType == AbstractFileFilter::Binary) | 642 | // else if (m_fileType == AbstractFileFilter::Binary) | ||
642 | // dynamic_cast<BinaryFilter*>(m_filter)->readFromLiveDeviceNotFile(*m_device, this); | 643 | // dynamic_cast<BinaryFilter*>(m_filter)->readFromLiveDeviceNotFile(*m_device, this); | ||
643 | 644 | | |||
644 | //since we won't have the timer to call read() where we create new connections | 645 | //since we won't have the timer to call read() where we create new connections | ||
645 | //for sequential devices in read() we just request data/connect to servers | 646 | //for sequential devices in read() we just request data/connect to servers | ||
646 | if (m_updateType == NewData) | 647 | if (m_updateType == NewData) | ||
▲ Show 20 Lines • Show All 305 Lines • ▼ Show 20 Line(s) | 952 | case FileOrPipe: | |||
952 | break; | 953 | break; | ||
953 | case LocalSocket: | 954 | case LocalSocket: | ||
954 | break; | 955 | break; | ||
955 | default: | 956 | default: | ||
956 | break; | 957 | break; | ||
957 | } | 958 | } | ||
958 | 959 | | |||
959 | } else if (reader->name() == "asciiFilter") { | 960 | } else if (reader->name() == "asciiFilter") { | ||
960 | m_filter = new AsciiFilter(); | 961 | setFilter(new AsciiFilter); | ||
961 | if (!m_filter->load(reader)) | 962 | if (!m_filter->load(reader)) | ||
962 | return false; | 963 | return false; | ||
963 | } else if (reader->name() == "column") { | 964 | } else if (reader->name() == "column") { | ||
964 | Column* column = new Column("", AbstractColumn::Text); | 965 | Column* column = new Column("", AbstractColumn::Text); | ||
965 | if (!column->load(reader, preview)) { | 966 | if (!column->load(reader, preview)) { | ||
966 | delete column; | 967 | delete column; | ||
967 | setColumnCount(0); | 968 | setColumnCount(0); | ||
968 | return false; | 969 | return false; | ||
Show All 14 Lines |
this needs only be done if m_filter != nulltpr,