subclassing QIODevice for QAudioDecoder - c++

Qt 5.7
According to the documentation, QAudioDecoder does not support streaming media. But is accepts either a filename or a QIODevice as source. Came the "bright idea": let's subclass QIODevice to create streaming media support.
But I always get an error:
Unable to start decoding process. Stream contains no data.
I have checked and double checked and debugged. Can't figure out what can be the problem.
I'm only testing it, so the code below is by no means final.
header:
class InputBuffer : public QIODevice
{
public:
InputBuffer(QString fileName);
~InputBuffer();
qint64 readData(char *data, qint64 maxlen);
qint64 writeData(const char *data, qint64 len);
bool isSequential();
qint64 bytesAvailable();
qint64 size();
private:
char* bufferRef;
qint64 length;
qint64 position;
};
implementation:
#include "inputbuffer.h"
InputBuffer::InputBuffer(QString fileName)
{
QFileInfo fileInfo(fileName);
length = fileInfo.size();
position = 0;
bufferRef = (char*)malloc(length);
QFile file(fileName);
file.open(QFile::ReadOnly);
file.read(bufferRef, length);
file.close();
emit readyRead();
}
InputBuffer::~InputBuffer()
{
free(bufferRef);
}
qint64 InputBuffer::readData(char *data, qint64 maxlen)
{
if (position >= length) {
return -1;
}
qint64 readSize = qMin(maxlen, length - position);
memcpy(data, bufferRef + position, readSize);
position += readSize;
return readSize;
}
qint64 InputBuffer::writeData(const char *data, qint64 len)
{
return -1;
}
bool InputBuffer::isSequential()
{
return true;
}
qint64 InputBuffer::bytesAvailable()
{
return length - position;
}
qint64 InputBuffer::size()
{
return length;
}
usage:
inputBufferRef = new InputBuffer("/home/pp/Zenék/Test.mp3");
inputBufferRef->open(QIODevice::ReadOnly);
audioDecoderRef = new QAudioDecoder();
audioDecoderRef->setAudioFormat(audioFormat);
audioDecoderRef->setSourceDevice(inputBufferRef);
connect(audioDecoderRef, SIGNAL(bufferReady()), this, SLOT(decoderBufferReady()));
connect(audioDecoderRef, SIGNAL(finished()), this, SLOT(decoderFinished()));
connect(audioDecoderRef, SIGNAL(error(QAudioDecoder::Error)), this, SLOT(decoderError(QAudioDecoder::Error)));
audioDecoderRef->start();

None of your isSequential(), bytesAvailable(), nor size() methods override the respective method in QIODevice, because the signatures don't match. They are all missing a const qualifier. Using the C++11 override keyword when overriding virtual methods helps.

Related

Block execution program until callback function complete execution

How to block program execution until callback function complete execution ?
From my main() i launch interface.getImage(); function who want to get images from our database. When we receive images the callback function void InterfaceColliseo::dataReceived (std::shared_ptr data) start to execute.
But I have a problem my program main() terminate before my callback function execution ?
main.cpp
int main(){
InterfaceColliseo interface;
IMAGE = true;
interface.getImage();
// want to block program here
return 0;
}
callback function
void InterfaceColliseo::dataReceived (std::shared_ptr<IData> data)
{
if (!data->isValid())
return;
const unsigned char* twoDImageData = data->get2DImageData();
int width2DImageData = data->getWidth2DImageData();
int height2DImageData = data->getHeight2DImageData();
const unsigned char* disparityData = data->getDisparityData();
int widthDisparityData = data->getWidthDisparityData();
int heightDisparityData = data->getHeightDisparityData();
if(IMAGE) {
saveImage(twoDImageData, width2DImageData, height2DImageData,
disparityData, widthDisparityData, heightDisparityData);
IMAGE = false;
}
if(ACQUISATION){
QList<GstObservationBasic> detectorData = data->getObstaclesData();
getObstacles(detectorData);
}
}
I think you just can use thread from std. When you use join, the main thread will wait until joined thread will finish his job.
#include <thread>
//in main
std::thread myThread(interface.getImage);
myThread.join();
#include "interface_colliseo.h"
std::mutex mtx;
std::condition_variable cv;
bool IMAGE;
bool ACQUISATION;
InterfaceColliseo::InterfaceColliseo(){
}
void InterfaceColliseo::startStreaming(){
dataReceiver = _device->getDataReceiver();
start();
}
void InterfaceColliseo::getImage(){
dataReceiver = _device->getDataReceiver();
start();
}
InterfaceColliseo::InterfaceColliseo(QString IP): _IP(IP) {
qDebug() << "INDUS-5: IP server: " << _IP;
qDebug() << "INDUS-5: Connecting to sensor...";
_colliseoClient.setIPServer(_IP);
}
bool InterfaceColliseo::connect2UT(){
QString path = qApp->applicationDirPath()+"/Configuration.ini";
QSettings config(path, QSettings::IniFormat);
_IP = config.value("Connection/IP","10.0.3.144").toString();
_colliseoClient.setIPServer(_IP);
_device = _colliseoClient.getDevice();
_device->connectSensor();
bool connect = _device->isConnected();
return connect;
}
QString InterfaceColliseo::sensorName(){
return _device->getDeviceDiagnostics()->getOperatingData()
->getDeviceInformation()->getOrderNumber();
}
QString InterfaceColliseo::sensorFirmwareVersion(){
return _device->getDeviceDiagnostics()->getOperatingData()
->getDeviceInformation()->getFirmwareVersion();
}
QString InterfaceColliseo::getSensorHeadPN(QString sensor){
return _device->getDeviceDiagnostics()->getOperatingData()
->getDeviceInformation()->getSensorHeadPN(sensor);
}
QString InterfaceColliseo::getEvaluationUnitSN(){
return _device->getDeviceDiagnostics()->getOperatingData()
->getDeviceInformation()->getEvaluationUnitSN();
}
QString InterfaceColliseo::getEvaluationUnitPN(){
return _device->getDeviceDiagnostics()->getOperatingData()
->getDeviceInformation()->getEvaluationUnitPN();
}
QString InterfaceColliseo::getEvaluationUnitFirmwareVersion(){
return _device->getDeviceDiagnostics()->getOperatingData()
->getDeviceInformation()->getEvaluationUnitFirmwareVersion();
}
QString InterfaceColliseo::getEstimatedAngleX(){
return _device->getDeviceDiagnostics()->getOperatingData()
->getDeviceInformation()->getEstimatedAngleX();
}
QString InterfaceColliseo::getEstimatedAngleZ(){
return _device->getDeviceDiagnostics()->getOperatingData()
->getDeviceInformation()->getEstimatedAngleZ();
}
QString InterfaceColliseo::getEstimatedHeight(){
return _device->getDeviceDiagnostics()->getOperatingData()
->getDeviceInformation()->getEstimatedHeight();
}
void InterfaceColliseo::saveImage(const unsigned char*twoDImageData,
int width2DImageData, int height2DImageData,
const unsigned char*disparityData,
int widthDisparityData, int disptHeight){
Configuration configuration;
QString logFolder = configuration.getLogFolder();
QImage imgRight(twoDImageData, width2DImageData, height2DImageData, QImage::Format_Indexed8);
QImage imgDisparity(disparityData, widthDisparityData, disptHeight, QImage::Format_Indexed8);
QPixmap imgRght = QPixmap::fromImage(imgRight);
QPixmap imgDisp = QPixmap::fromImage(imgDisparity);
QString rghtImgPath = logFolder + "raw_image.png";
QString dispImgPath = logFolder + "disp_image.png";
imgRght.save(rghtImgPath, "PNG");
imgDisp.save(dispImgPath, "PNG");
}
void InterfaceColliseo::getObstacles(QList<GstObservationBasic> detectorData){
if (detectorData.size() == 0)
{
qDebug() << "Obstacles: no detected obstacles.";
return;
}
Configuration config;
config.writeLog("***************Obstacles List Acquisation******************");
Q_FOREACH(const GstObservationBasic &obs, detectorData)
{
qDebug() << "Obstacles: " << gstObservationToString(obs);
config.writeLog(gstObservationToString(obs));
}
}
void InterfaceColliseo::dataReceived (std::shared_ptr<IData> data)
{
if (!data->isValid())
return;
const unsigned char* twoDImageData = data->get2DImageData();
int width2DImageData = data->getWidth2DImageData();
int height2DImageData = data->getHeight2DImageData();
const unsigned char* disparityData = data->getDisparityData();
int widthDisparityData = data->getWidthDisparityData();
int heightDisparityData = data->getHeightDisparityData();
if(IMAGE) {
saveImage(twoDImageData, width2DImageData, height2DImageData,
disparityData, widthDisparityData, heightDisparityData);
IMAGE = false;
}
if(ACQUISATION){
QList<GstObservationBasic> detectorData = data->getObstaclesData();
getObstacles(detectorData);
ACQUISATION = false;
}
}
void InterfaceColliseo::start() {
dataReceiver->addListener(this);
if(dataReceiver->isListening())
dataReceiver->stopListening();
dataReceiver->startListening();
_device->triggerSingleImageAcquisition();
}

Sending UDP datagrams using QUdpSocket and QDatastream

The program is able to send .txt files. But any other filetype gets corrupted. I think it has something to do with the datatypes I store my frames in. Sorry for the long post i just have no idea where my problem might be.
I'm new to QT as well as C++. I HAVE TO use QUdpSocket to send any file over a network to another computer. This computer should then reassemble the datagrams in the correct order and save it as a file. (QFiles have to be broken up in segments with sequence number headers and reassembled according to these numbers). Segments are stored with their sequence number in a QByteArray array.
//Setup File
filelocation= opendialog->getOpenFileName(this, "Select File to be sent", "C:/Users/<USER>/Desktop");
SentFile.setFileName(filelocation);
if (!(SentFile.open(QIODevice::ReadOnly))){
return;
}
//Setup Datastream
SentData.setDevice(&SentFile);
SentData.setByteOrder(QDataStream::BigEndian);
SentData.setFloatingPointPrecision(QDataStream::SinglePrecision);
SentData.setVersion(QDataStream::Qt_5_5);
//Calculate number of frames to be sent
max_framesize=200;
int length=SentFile.size();
dsize=length/max_framesize+1;
if((length%max_framesize)!=0)
dsize++;
//Write initial message with dsize and file name
char BeginMessage[max_framesize+16];
char *cfile= new char[filelocation.length()+1];
strcpy(cfile, filelocation.toStdString().c_str());
sprintf(BeginMessage,"%016d%010d%s",0, dsize-1, cfile);
SendArray[0]= BeginMessage;
int i=1;
while(SentData.atEnd()!=true)
{
char s[max_framesize+1];
int len = SentData.readRawData(s, max_framesize);
for(int n=len; n<=max_framesize;n++)
{
s[n]='\0';
}
char tempframe[17];
sprintf(tempframe,"%016d",i);
tempframe[16]='\0';
SendArray[i]=s;
SendArray[i].prepend(tempframe);
i++;
}
The QByteArray Array is then sent over the qudpsocket
void MainWindow::on_pushButton_clicked()
{
for(int l=0; l<dsize; l++)
{
QByteArray datagram = SendArray[l];
socket->writeDatagram(datagram.data(), QHostAddress::Broadcast, 5743);
}
ui->lineEdit->clear();
ui->lineEdit->setFocus();
}
The Reassembly code works as follows
void MainWindow::receiveData()
{
while (socket->hasPendingDatagrams())
{
FramesReceived++;
QByteArray datagram;
datagram.resize(socket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
socket->readDatagram(datagram.data(), datagram.size(),
&sender, &senderPort);
char temp[datagram.size()];
strcpy(temp, datagram);
char cseq_num[17];
for(int n=0; n<16;n++)
{
cseq_num[n]=temp[n];
}
cseq_num[16]='\0';
datagram.remove(0,16);
int seq_num = atoi(cseq_num);
if(seq_num==0)
{
char size[11];
for(int n=0; n<10;n++)
{
size[n]=temp[n+16];
}
size[11]='\0';
rsize = atoi(size);
QByteArray Name=datagram;
Name.remove(0, 10);
strcpy(OriginalName,Name.data());
}
ReceivedArray[seq_num]= datagram;
char message[600];
sprintf(message,"%d of %d Frames Received\nFile name: %s",FramesReceived,rsize, OriginalName);
ui->label->setText(message);
}
}
After all frames have been received
void MainWindow::on_pushButton_3_clicked()
{
filelocation= opendialog->getSaveFileName(this, "Save File");
ReceivedFile.setFileName(filelocation);
if (!(ReceivedFile.open(QIODevice::WriteOnly))){
return;
}
char snum[27];
for(int n=0; n<27;n++)
{
snum[n]=ReceivedArray[0][n];
}
snum[27]='\0';
int len_num = atoi(snum);
ReceivedData.setDevice(&ReceivedFile);
ReceivedData.setByteOrder(QDataStream::BigEndian);
ReceivedData.setFloatingPointPrecision(QDataStream::SinglePrecision);
ReceivedData.setVersion(QDataStream::Qt_5_5);
for(int i=1; i<len_num; i++)
{
ReceivedData.writeRawData(ReceivedArray[i],ReceivedArray[i].length());
}
ReceivedData.writeRawData(ReceivedArray[len_num],ReceivedArray[len_num].length());
ReceivedFile.close();
}

Is there an intra-process local pipe in Qt?

Does Qt have a QIODevice pair that would work for intra-process point-to-point communications?
One could use the concrete QTCPSocket or QLocalSocket, but the server-side connection API is a bit cumbersome, and it seems wasteful to force the data through the OS.
The following is a usable, rudimentary implementation. It uses an internal signal-slot pair to push the data to the other endpoint. That way either end of the connection can live in any thread, and the ends can be moved between threads without losing data or inducing any races.
The private QRingBuffer is used in lieu of reinventing the wheel. Add QT += core-private to the .pro file to make that available. Qt PIMPL is used to gain access to the internal device buffers in Qt versions 5.7 and above.
If you wish to instantiate an open pipe, as may often be the case, you can pass the I/O mode to the constructor. Typical use:
int main(/*…*/)
{
/*…*/
AppPipe end1 { QIODevice::ReadWrite };
AppPipe end2 { &end1, QIODevice::ReadWrite };
AppPipe end3 { &end1, QIODevice::ReadOnly };
// the pipes are open ready to use
/*…*/
}
Whatever you write to one pipe, ends up as readable data in the other, connected pipes, and vice versa. In the above example, data written to end1 is readable from both end2 and end3 independently. Data written to end2 is readable from end1. end3 is effectively a listen-only pipe. Connection of additional pipes is cheap - there's no O(N) cost associated with sending big chunks of data to multiple pipes, because read QRingBuffers of connected pipes store shallow copies of entire byte arrays sent from the originating pipe.
All of the QIODevice semantics hold - you can connect to the readyRead signal, use the pipe with a QDataStream or a QTextStream, etc. As with any QIODevice, you can only use the class from its thread(), but the other endpoint can live in any thread, and both can be moved between threads as desired, without loss of data.
If the other pipe end is not open and readable, the writes are no-ops, even though they succeed. Closing the pipe clears the read and write buffers, so that it can be re-opened for reuse.
The pipe buffers write data by default, and the write buffer can be forcibly flushed using AppPipe::flush(), unless opened in QIODevice::Unbuffered mode.
The hasIncoming and hasOutgoing signals are useful in monitoring the data going over the pipe.
// https://github.com/KubaO/stackoverflown/tree/master/questions/local-pipe-32317081
// This project is compatible with Qt 4 and Qt 5
#include <QtTest>
#include <private/qiodevice_p.h>
#include <private/qringbuffer_p.h>
#include <algorithm>
#include <climits>
#ifndef Q_DECL_OVERRIDE
#define Q_DECL_OVERRIDE
#endif
class AppPipePrivate : public QIODevicePrivate {
public:
#if QT_VERSION < QT_VERSION_CHECK(5,7,0)
QRingBuffer buffer;
QRingBuffer writeBuffer;
int writeBufferChunkSize;
#endif
const QByteArray *writeData;
AppPipePrivate() : writeData(0) { writeBufferChunkSize = 4096; }
};
/// A simple point-to-point intra-process pipe. The other endpoint can live in any
/// thread.
class AppPipe : public QIODevice {
Q_OBJECT
Q_DECLARE_PRIVATE(AppPipe)
static inline int intLen(qint64 len) { return std::min(len, qint64(INT_MAX)); }
Q_SLOT void _a_write(const QByteArray &data) {
Q_D(AppPipe);
if (!(d->openMode & QIODevice::ReadOnly)) return; // We must be readable.
d->buffer.append(data); // This is a chunk shipped from the source.
emit hasIncoming(data);
emit readyRead();
}
void hasOutgoingLong(const char *data, qint64 len) {
while (len) {
int const size = intLen(len);
emit hasOutgoing(QByteArray(data, size));
data += size;
len -= size;
}
}
public:
AppPipe(QIODevice::OpenMode mode, QObject *parent = 0) :
QIODevice(*new AppPipePrivate, parent) {
open(mode);
}
AppPipe(AppPipe *other, QIODevice::OpenMode mode, QObject *parent = 0) :
QIODevice(*new AppPipePrivate, parent) {
open(mode);
addOther(other);
}
AppPipe(AppPipe *other, QObject *parent = 0) :
QIODevice(*new AppPipePrivate, parent) {
addOther(other);
}
~AppPipe() Q_DECL_OVERRIDE {}
void addOther(AppPipe *other) {
if (other) {
connect(this, SIGNAL(hasOutgoing(QByteArray)), other, SLOT(_a_write(QByteArray)), Qt::UniqueConnection);
connect(other, SIGNAL(hasOutgoing(QByteArray)), this, SLOT(_a_write(QByteArray)), Qt::UniqueConnection);
}
}
void removeOther(AppPipe *other) {
disconnect(this, SIGNAL(hasOutgoing(QByteArray)), other, SLOT(_a_write(QByteArray)));
disconnect(other, SIGNAL(hasOutgoing(QByteArray)), this, SLOT(_a_write(QByteArray)));
}
void flush() {
Q_D(AppPipe);
while (!d->writeBuffer.isEmpty()) {
QByteArray const data = d->writeBuffer.read();
emit hasOutgoing(data);
emit bytesWritten(data.size());
}
}
void close() Q_DECL_OVERRIDE {
Q_D(AppPipe);
flush();
QIODevice::close();
d->buffer.clear();
}
qint64 write(const QByteArray &data) { // This is an optional optimization. The base method works OK.
Q_D(AppPipe);
QScopedValueRollback<const QByteArray*> back(d->writeData);
if (!(d->openMode & Text))
d->writeData = &data;
return QIODevice::write(data);
}
qint64 writeData(const char *data, qint64 len) Q_DECL_OVERRIDE {
Q_D(AppPipe);
bool buffered = !(d->openMode & Unbuffered);
if (buffered && (d->writeBuffer.size() + len) > d->writeBufferChunkSize)
flush();
if (!buffered
|| len > d->writeBufferChunkSize
|| (len == d->writeBufferChunkSize && d->writeBuffer.isEmpty()))
{
if (d->writeData && d->writeData->data() == data && d->writeData->size() == len)
emit hasOutgoing(*d->writeData);
else
hasOutgoingLong(data, len);
}
else
memcpy(d->writeBuffer.reserve(len), data, len);
return len;
}
bool isSequential() const Q_DECL_OVERRIDE { return true; }
Q_SIGNAL void hasOutgoing(const QByteArray &);
Q_SIGNAL void hasIncoming(const QByteArray &);
#if QT_VERSION >= QT_VERSION_CHECK(5,7,0)
// all the data is in the read buffer already
qint64 readData(char *, qint64) Q_DECL_OVERRIDE { return 0; }
#else
qint64 readData(char *data, qint64 len) Q_DECL_OVERRIDE {
Q_D(AppPipe);
qint64 hadRead = 0;
while (len && !d->buffer.isEmpty()) {
int size = d->buffer.read(data, intLen(len));
hadRead += size;
data += size;
len -= size;
}
return hadRead;
}
bool canReadLine() const Q_DECL_OVERRIDE {
Q_D(const AppPipe);
return d->buffer.indexOf('\n') != -1 || QIODevice::canReadLine();
}
qint64 bytesAvailable() const Q_DECL_OVERRIDE {
Q_D(const AppPipe);
return QIODevice::bytesAvailable() + d->buffer.size();
}
qint64 bytesToWrite() const Q_DECL_OVERRIDE {
Q_D(const AppPipe);
return QIODevice::bytesToWrite() + d->writeBuffer.size();
}
#endif
};
// ...
#include "main.moc"
A minimal test harness:
class TestAppPipe : public QObject {
Q_OBJECT
QByteArray data1, data2;
struct PipePair {
AppPipe end1, end2;
PipePair(QIODevice::OpenMode mode = QIODevice::NotOpen) :
end1(QIODevice::ReadWrite | mode), end2(&end1, QIODevice::ReadWrite | mode) {}
};
Q_SLOT void initTestCase() {
data1 = randomData();
data2 = randomData();
}
Q_SLOT void sizes() {
QCOMPARE(sizeof(AppPipe), sizeof(QIODevice));
}
Q_SLOT void basic() {
PipePair p;
QVERIFY(p.end1.isOpen() && p.end1.isWritable() && p.end1.isReadable());
QVERIFY(p.end2.isOpen() && p.end2.isWritable() && p.end2.isReadable());
static const char hello[] = "Hello There!";
p.end1.write(hello);
p.end1.flush();
QCOMPARE(p.end2.readAll(), QByteArray(hello));
}
static QByteArray randomData(int const size = 1024*1024*32) {
QByteArray data;
data.resize(size);
char *const d = data.data();
for (char *p = d+data.size()-1; p >= d; --p)
*p = qrand();
Q_ASSERT(data.size() == size);
return data;
}
static void randomChunkWrite(AppPipe *dev, const QByteArray &payload) {
for (int written = 0, left = payload.size(); left; ) {
int const chunk = std::min(qrand() % 82931, left);
dev->write(payload.mid(written, chunk));
left -= chunk; written += chunk;
}
dev->flush();
}
void runBigData(PipePair &p) {
Q_ASSERT(!data1.isEmpty() && !data2.isEmpty());
randomChunkWrite(&p.end1, data1);
randomChunkWrite(&p.end2, data2);
QCOMPARE(p.end1.bytesAvailable(), qint64(data2.size()));
QCOMPARE(p.end2.bytesAvailable(), qint64(data1.size()));
QCOMPARE(p.end1.readAll(), data2);
QCOMPARE(p.end2.readAll(), data1);
}
Q_SLOT void bigDataBuffered() {
PipePair p;
runBigData(p);
}
Q_SLOT void bigDataUnbuffered() {
PipePair p(QIODevice::Unbuffered);
runBigData(p);
}
Q_SLOT void cleanupTestCase() {
data1.clear(); data2.clear();
}
};
QTEST_MAIN(TestAppPipe)
# local-pipe-32317081.pro
QT = core
greaterThan(QT_MAJOR_VERSION, 4): QT = core-private testlib
else: CONFIG += qtestlib
DEFINES += \
QT_DEPRECATED_WARNINGS \
QT_DISABLE_DEPRECATED_BEFORE=0x060000 \
QT_RESTRICTED_CAST_FROM_ASCII
CONFIG += console c++14
CONFIG -= app_bundle
TEMPLATE = app
SOURCES = main.cpp

Random Bytes added to an end of a buffer

I was making a re-creation of some System.IO functions from that class.
When I setup a buffer and allocate n number of bytes it'll read bytes to that and then add random bytes to the end of that buffer.
For example:
My Main:
int main(int argc, char *args[])
{
SetConsoleTitle(TEXT("Stream Test."));
cout<<"Press any Key to begin reading.";
cin.get();
const char* data = File::ReadAllBytes(args[1]);
Stream* stream = new Stream(data);
char* magic = new char[8];
stream->Read(magic, 0, 8);
magic[8] = '\0';
cout<<magic<<endl<<endl;
delete[]data;
cout<<"Press any key to quit.";
cin.get();
return 0;
}
and here is my System::IO namespace + stream class:
namespace System
{
namespace IO
{
class File
{
public:
static char* ReadAllBytes(const char *name)
{
ifstream fl(name, ifstream::in|ifstream::binary);
fl.seekg( 0, ifstream::end );
size_t len = fl.tellg();
char* ret = new char[len+1];
ret[len] = '\0';
fl.seekg(0);
fl.read(ret, len);
fl.close();
return ret;
}
//not sure of this use yet.
static size_t fileSize(const char* filename)
{
ifstream in(filename, ifstream::in | ifstream::binary);
in.seekg(0, ifstream::end);
return in.tellg();
}
};
class Stream
{
public:
const char *_buffer;
__int64 _origin;
__int64 _position;
__int64 _length;
__int64 _capacity;
bool _expandable;
bool _writable;
bool _exposable;
bool _isOpen;
static const int MemStreamMaxLength = 2147483647;
Stream()
{
InitializeInstanceFields();
}
Stream(const char *buffer)
{
_buffer = buffer;
_length = strlen(_buffer);
_capacity = _length;
_position = 0;
_origin = 0;
_expandable = false;
_writable = true;
_exposable = true;
_isOpen = true;
}
int ReadByte()
{
if (_position >= _length)
return -1;
return _buffer[_position++];
}
void Read(char* &buffer, int offset, int length)
{
if((_position + offset + length) <= _length)
{
memcpy( buffer, _buffer + (_position + offset), length );
_position += length;
}
}
private:
void InitializeInstanceFields()
{
_origin = 0;
_position = 0;
_length = 0;
_capacity = 0;
_expandable = false;
_writable = false;
_exposable = false;
_isOpen = false;
}
};
}
}
This is what ends up happening:
Can anyone explain why this happens, how I can fix, or anything else? I'm new to C++ so any explanations would help. Also please don't criticize my scripting, I know it may be bad, outdated, deprecated, etc. but I'm open to learning and any helping advice goes for the better. :)
You can only use operator << (char *) on C-style strings, not arbitrary arrays of characters. How would you expect it to know how many characters to output?
I would guess the file was not opened correctly and thus the magic buffer is not set at all which leaves it with initialized junk data:
If the constructor is not successful in opening the file, the object
is still created although no file is associated to the stream buffer
and the stream's failbit is set (which can be checked with inherited
member fail).
http://www.cplusplus.com/reference/fstream/ifstream/ifstream/
Try adding more error checking along the way (using cout), especially when opening and reading the buffer. Perhaps set the magic buffer to zero or something recognizable that is overwritten when successful.

QDataStream QIODevice memory allocation

Say I have a function that creates a QIODevice (e.g. a QFile), then returns a pointer to a QDataStream constructed from the QIODevice. What is the best way to deal with the memory allocation here? Clearly the QIODevice must be heap allocated to stay available to the QDataStream upon function termination, however the destruction of a QDataStream does not destroy or close the device. Is there a standard way of dealing with this seemingly common problem?
Ideally I want a function that returns an object (not a pointer to an object) that behaves like a QDataStream but upon destruction closes the device. Effectively a standard library input stream.
Example code:
QDataStream* getStream(const QString& filename) {
QFile* file = new QFile(filename); // needs to be explicitly deleted later
file->open(QIODevice::ReadOnly);
QDataStream* out = new QDataStream(&file); // same here
return out;
}
std::shared_ptr<QDataStream> getStream(const QString& filename)
{
QFile* file = new QFile(filename); // needs to be explicitly deleted later
file->open(QIODevice::ReadOnly);
std:shared_ptr<QDataStream> out(new QDataStream(&file), QDSDeleter);
return out;
}
void QDSDeleter(QDataStream* s)
{
QIODevice* device = s->device();
device->close();
delete device;
}
std::unique_ptr is another option depending on your needs; here's a reference for the former if you need it.
Edit: Qt also has the facility for this with its QSharedPointer class, where you can also supply a deleter as a constructor argument. Other pointer wrapper options are given there. Thanks #RA. for the correction.
QDataStream has a handy owndev private member that you can set to true to have the stream effectively own the device. The stream can also be easily made moveable - bringing it very close to your requirement of it behaving like a value. Ideally, you would modify your copy of Qt to implement it, but it can also be worked around.
// https://github.com/KubaO/stackoverflown/tree/master/questions/qdatastream-move-own-13039614
#include <QDataStream>
class DataStream : public QDataStream {
struct Proxy {
QScopedPointer<QDataStreamPrivate> d;
QIODevice *dev;
bool owndev;
bool noswap;
QDataStream::ByteOrder byteorder;
int ver;
QDataStream::Status q_status;
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
virtual ~Proxy();
#endif
};
static Proxy *p(QDataStream *ds) { return reinterpret_cast<Proxy *>(ds); }
static const Proxy *p(const QDataStream *ds) {
return reinterpret_cast<const Proxy *>(ds);
}
#if defined(QT_TESTLIB_LIB) || defined(QT_MODULE_TEST)
friend class DataStreamTest;
#endif
public:
DataStream() = default;
using QDataStream::QDataStream;
DataStream(DataStream &&other) : DataStream(static_cast<QDataStream &&>(other)) {}
DataStream(QDataStream &&other) {
using std::swap;
Proxy &o = *p(&other);
Proxy &t = *p(this);
swap(t.d, o.d);
swap(t.dev, o.dev);
swap(t.owndev, o.owndev);
swap(t.noswap, o.noswap);
swap(t.byteorder, o.byteorder);
swap(t.ver, o.ver);
swap(t.q_status, o.q_status);
}
DataStream &operator=(DataStream &&other) {
return *this = static_cast<QDataStream &&>(other);
}
DataStream &operator=(QDataStream &&other) {
this->~DataStream();
new (this) DataStream(std::move(other));
return *this;
}
void setOwnedDevice(QIODevice *dev) {
setDevice(dev);
p(this)->owndev = true;
}
bool ownsDevice() const { return p(this)->owndev; }
static bool ownsDevice(const QDataStream *ds) { return p(ds)->owndev; }
};
Since QObject has a built-in reference count, we could also have the QDataStream act as a shared pointer for it if we wished to.
The following requirements are tested on both Qt 4.8 and 5.10:
PASS : DataStreamTest::isBinaryCompatible()
PASS : DataStreamTest::streams()
PASS : DataStreamTest::movesFromNotOwnedQDataStream()
PASS : DataStreamTest::movesFromNotOwnedDataStream()
PASS : DataStreamTest::assignsFromNotOwnedQDataStream()
PASS : DataStreamTest::assignsFromNotOwnedDataStream()
PASS : DataStreamTest::returnsFromNotOwnedQDataStream()
PASS : DataStreamTest::returnsFromNotOwnedDataStream()
PASS : DataStreamTest::movesFromOwnedQDataStream()
PASS : DataStreamTest::moveFromOwnedDataStream()
PASS : DataStreamTest::assignsFromOwnedQDataStream()
PASS : DataStreamTest::assignsFromOwnedDataStream()
PASS : DataStreamTest::returnsFromOwnedQDataStream()
PASS : DataStreamTest::returnsFromOwnedDataStream()
The test suite follows. The binary compatibility test is extensive and all but excludes the possibility that the UB we depend on is problematic. Note that QDataStream's layout cannot change within the major Qt version - so the above code will work on all future Qt 5 versions.
#include <QtTest>
class DataStreamTest : public QObject {
Q_OBJECT
static QObjectData *getD(QObject *obj) {
return static_cast<DataStreamTest *>(obj)->d_ptr.data();
}
static bool wasDeleted(QObject *obj) { return getD(obj)->wasDeleted; }
template <typename T, typename... Args>
DataStream make_stream(Args &&... args) {
return T(std::forward<Args>(args)...);
}
static QDataStream::ByteOrder flipped(QDataStream::ByteOrder o) {
return (o == QDataStream::BigEndian) ? QDataStream::LittleEndian
: QDataStream::BigEndian;
}
Q_SLOT void isBinaryCompatible() {
QCOMPARE(sizeof(DataStream), sizeof(QDataStream));
QCOMPARE(sizeof(DataStream::Proxy), sizeof(QDataStream));
struct Test {
QByteArray data;
QDataStream ds{&data, QIODevice::ReadWrite};
void check(int loc = 0) {
if (!loc) {
check(1);
ds.setDevice(nullptr);
check(1);
}
QCOMPARE(!!ds.device(), DataStream::ownsDevice(&ds));
QCOMPARE(ds.device(), DataStream::p(&ds)->dev);
if (!loc) check(2);
bool noswap = DataStream::p(&ds)->noswap;
QCOMPARE(noswap, DataStream::p(&ds)->noswap);
QCOMPARE(ds.byteOrder(), DataStream::p(&ds)->byteorder);
if (loc != 2) {
ds.setByteOrder(flipped(ds.byteOrder()));
noswap = !noswap;
}
if (!loc) check(2);
QCOMPARE(noswap, DataStream::p(&ds)->noswap);
if (!loc) check(3);
QCOMPARE(ds.version(), DataStream::p(&ds)->ver);
if (loc != 3) ds.setVersion(QDataStream::Qt_4_0);
if (!loc) check(3);
if (!loc) check(4);
QCOMPARE(ds.status(), DataStream::p(&ds)->q_status);
if (loc != 4) ds.setStatus(QDataStream::ReadPastEnd);
if (!loc) check(4);
}
} test;
test.check();
}
Q_SLOT void streams() {
QString str{"Hello, world"};
QVector<uint> ints{44, 0xDEADBEEF, 1};
QByteArray data;
DataStream ds(&data, QIODevice::ReadWrite);
ds << str << ints;
ds.device()->reset();
QString str2;
QVector<uint> ints2;
ds >> str2 >> ints2;
QCOMPARE(str2, str);
QCOMPARE(ints2, ints);
}
Q_SLOT void movesFromNotOwnedQDataStream() {
QBuffer buf;
QDataStream ds(&buf);
QVERIFY(ds.device() == &buf);
DataStream ds2(std::move(ds));
QVERIFY(!ds.device());
QVERIFY(ds2.device() == &buf);
QVERIFY(!wasDeleted(&buf));
}
Q_SLOT void movesFromNotOwnedDataStream() {
QBuffer buf;
DataStream ds(&buf);
QVERIFY(ds.device() == &buf);
DataStream ds2(std::move(ds));
QVERIFY(!ds.device());
QVERIFY(ds2.device() == &buf);
QVERIFY(!wasDeleted(&buf));
}
Q_SLOT void assignsFromNotOwnedQDataStream() {
QBuffer buf;
QDataStream ds(&buf);
QVERIFY(ds.device() == &buf);
DataStream ds2;
ds2 = std::move(ds);
QVERIFY(!ds.device());
QVERIFY(ds2.device() == &buf);
QVERIFY(!wasDeleted(&buf));
}
Q_SLOT void assignsFromNotOwnedDataStream() {
QBuffer buf;
DataStream ds(&buf);
QVERIFY(ds.device() == &buf);
DataStream ds2;
ds2 = std::move(ds);
QVERIFY(!ds.device());
QVERIFY(ds2.device() == &buf);
QVERIFY(!wasDeleted(&buf));
}
Q_SLOT void returnsFromNotOwnedQDataStream() {
QBuffer buf;
{
auto ds = make_stream<QDataStream>(&buf);
QVERIFY(ds.device());
QVERIFY(!ds.ownsDevice());
}
QVERIFY(!wasDeleted(&buf));
}
Q_SLOT void returnsFromNotOwnedDataStream() {
QBuffer buf;
buf.open(QIODevice::ReadWrite);
{
auto ds = make_stream<DataStream>(&buf);
QVERIFY(ds.device());
QVERIFY(!ds.ownsDevice());
}
QVERIFY(!wasDeleted(&buf));
}
Q_SLOT void movesFromOwnedQDataStream() {
QPointer<QIODevice> buf;
{
QByteArray data;
QDataStream ds(&data, QIODevice::ReadWrite);
QVERIFY(DataStream::ownsDevice(&ds));
buf = ds.device();
DataStream ds2(std::move(ds));
QVERIFY(!ds.device());
QVERIFY(ds2.device() == buf);
QVERIFY(buf);
}
QVERIFY(!buf);
}
Q_SLOT void moveFromOwnedDataStream() {
QPointer<QBuffer> buf(new QBuffer);
{
DataStream ds;
ds.setOwnedDevice(buf);
QVERIFY(ds.device() == buf);
DataStream ds2(std::move(ds));
QVERIFY(!ds.device());
QVERIFY(ds2.device() == buf);
QVERIFY(buf);
}
QVERIFY(!buf);
}
Q_SLOT void assignsFromOwnedQDataStream() {
QPointer<QIODevice> buf;
{
QByteArray data;
QDataStream ds(&data, QIODevice::ReadWrite);
QVERIFY(DataStream::ownsDevice(&ds));
buf = ds.device();
DataStream ds2;
ds2 = std::move(ds);
QVERIFY(!ds.device());
QVERIFY(ds2.device() == buf);
QVERIFY(buf);
}
QVERIFY(!buf);
}
Q_SLOT void assignsFromOwnedDataStream() {
QPointer<QBuffer> buf(new QBuffer);
{
DataStream ds;
ds.setOwnedDevice(buf);
QVERIFY(ds.device() == buf);
DataStream ds2;
ds2 = std::move(ds);
QVERIFY(!ds.device());
QVERIFY(ds2.device() == buf);
QVERIFY(buf);
}
QVERIFY(!buf);
}
Q_SLOT void returnsFromOwnedQDataStream() {
QPointer<QIODevice> dev;
QByteArray data;
{
auto ds = make_stream<QDataStream>(&data, QIODevice::ReadWrite);
dev = ds.device();
QVERIFY(ds.device());
QVERIFY(ds.ownsDevice());
}
QVERIFY(!dev);
}
Q_SLOT void returnsFromOwnedDataStream() {
QPointer<QIODevice> dev;
QByteArray data;
{
auto ds = make_stream<DataStream>(&data, QIODevice::ReadWrite);
dev = ds.device();
QVERIFY(ds.device());
QVERIFY(ds.ownsDevice());
}
QVERIFY(!dev);
}
};
QTEST_MAIN(DataStreamTest)
#include "main.moc"