QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
QGCFileWriter.h
Go to the documentation of this file.
1#pragma once
2
3#include <QtCore/QByteArray>
4#include <QtCore/QList>
5#include <QtCore/QMutex>
6#include <QtCore/QObject>
7#include <QtCore/QString>
8#include <QtCore/QWaitCondition>
9
10#include <atomic>
11#include <functional>
12
13class QThread;
14
15class QGCFileWriter : public QObject
16{
17 Q_OBJECT
18
19public:
20 explicit QGCFileWriter(QObject *parent = nullptr);
21 ~QGCFileWriter() override;
22
23 void setFilePath(const QString &path);
24 QString filePath() const;
25
26 bool isRunning() const;
27 bool isOpen() const { return _isOpen.load(std::memory_order_relaxed); }
28 bool hasError() const { return _hasError.load(std::memory_order_relaxed); }
29 QString lastError() const;
30
31 qint64 fileSize() const { return _fileSize.load(std::memory_order_relaxed); }
32 qint64 pendingBytes() const { return _pendingBytes.load(std::memory_order_relaxed); }
33
34 using FormatFunc = std::function<QByteArray()>;
35
36 void write(const QByteArray &data);
37 void writeDeferred(FormatFunc formatter);
38 bool flush(int timeoutMs = 5000);
39 void close();
40 void clearError();
41
42 void setMaxPendingBytes(qint64 max) { _maxPendingBytes = max; }
43 qint64 maxPendingBytes() const { return _maxPendingBytes; }
44
45signals:
46 void errorOccurred(const QString &message);
47 void fileSizeChanged(qint64 size);
48
49private:
50 void _workerLoop();
51 void _startLocked();
52 void _stop();
53
54 struct WorkItem {
55 QByteArray data;
56 FormatFunc formatter;
57 };
58
59 mutable QMutex _mutex;
60 QWaitCondition _condition;
61 QList<WorkItem> _queue;
62
63 QString _filePath;
64 QString _lastError;
65
66 QThread *_thread = nullptr;
67 std::atomic<bool> _isOpen{false};
68 std::atomic<bool> _hasError{false};
69 std::atomic<bool> _quit{false};
70 std::atomic<qint64> _fileSize{0};
71 std::atomic<qint64> _pendingBytes{0};
72 std::atomic<quint64> _flushSeq{0};
73 std::atomic<quint64> _writeSeq{0};
74 qint64 _maxPendingBytes = 50LL * 1024 * 1024;
75};
void setMaxPendingBytes(qint64 max)
void writeDeferred(FormatFunc formatter)
~QGCFileWriter() override
qint64 maxPendingBytes() const
bool flush(int timeoutMs=5000)
void fileSizeChanged(qint64 size)
bool hasError() const
QString lastError() const
qint64 fileSize() const
bool isOpen() const
bool isRunning() const
qint64 pendingBytes() const
void write(const QByteArray &data)
void setFilePath(const QString &path)
void errorOccurred(const QString &message)
QString filePath() const
std::function< QByteArray()> FormatFunc