File: inputqueue.h

package info (click to toggle)
qlogo 0.961-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,452 kB
  • sloc: cpp: 11,166; python: 159; sh: 107; ansic: 79; xml: 20; makefile: 4
file content (72 lines) | stat: -rw-r--r-- 1,911 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#ifndef INPUTQUEUE_H
#define INPUTQUEUE_H

//===-- qlogo/reader.h - Reader class definition -------*- C++ -*-===//
//
// Copyright 2017-2024 Jason Sikes
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted under the conditions specified in the
// license found in the LICENSE file in the project root.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file contains the declarations of the InputQueueThread and InputQueue,
/// which create a separate thread to wait for data and subsequently read data
/// from the input queue.
//===----------------------------------------------------------------------===//

#include <QByteArray>
#include <QEventLoop>
#include <QThread>

class InputQueueThread : public QThread
{
    Q_OBJECT

    void run() Q_DECL_OVERRIDE;

  public:
    /// @brief Constructor
    /// @param parent The Qt parent object
    explicit InputQueueThread(QObject *parent = nullptr);

  signals:
    /// @brief Signal to indicate that a message is available
    void sendMessageSignal();
};

class InputQueue : public QObject
{
    Q_OBJECT
    InputQueueThread thread;
    QByteArray message;
    QEventLoop eventLoop;

  private slots:
    // Connected to sendMessage signal from thread.
    void receiveMessageSlot();

  public:
    /// @brief Constructor
    /// @param parent The Qt parent object
    explicit InputQueue(QObject *parent = nullptr);

    /// @brief Start the input thread.
    void startQueue();

    /// @brief Stop the input thread.
    void stopQueue();

    /// @brief Get a message.
    /// @return The message.
    /// Will halt until message is available.
    QByteArray getMessage();

    /// @brief Ask if there is a message in the queue
    /// @return True if there is a message, false otherwise
    bool isMessageAvailable();
};

#endif // INPUTQUEUE_H