File: async-qt.cpp

package info (click to toggle)
pcp 7.1.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 252,748 kB
  • sloc: ansic: 1,483,656; sh: 182,366; xml: 160,462; cpp: 83,813; python: 24,980; perl: 18,327; yacc: 6,877; lex: 2,864; makefile: 2,738; awk: 165; fortran: 60; java: 52
file content (47 lines) | stat: -rw-r--r-- 1,057 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
#include <iostream>
using namespace std;

#include "async-qt.h"

#include <QCoreApplication>
#include <QTimer>

void getCallback(valkeyAsyncContext *, void *r, void *privdata) {

    valkeyReply *reply = static_cast<valkeyReply *>(r);
    ExampleQt *ex = static_cast<ExampleQt *>(privdata);
    if (reply == nullptr || ex == nullptr)
        return;

    cout << "key: " << reply->str << endl;

    ex->finish();
}

void ExampleQt::run() {

    m_ctx = valkeyAsyncConnect("localhost", 6379);

    if (m_ctx->err) {
        cerr << "Error: " << m_ctx->errstr << endl;
        valkeyAsyncFree(m_ctx);
        emit finished();
    }

    m_adapter.setContext(m_ctx);

    valkeyAsyncCommand(m_ctx, NULL, NULL, "SET key %s", m_value);
    valkeyAsyncCommand(m_ctx, getCallback, this, "GET key");
}

int main(int argc, char **argv) {

    QCoreApplication app(argc, argv);

    ExampleQt example(argv[argc - 1]);

    QObject::connect(&example, SIGNAL(finished()), &app, SLOT(quit()));
    QTimer::singleShot(0, &example, SLOT(run()));

    return app.exec();
}