summaryrefslogtreecommitdiff
path: root/rockworkd/libpebble/watchdatawriter.h
blob: 8e4addec14cb2481e529436bdb8c6a796e612d64 (plain)
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
#ifndef WATCHDATAWRITER_H
#define WATCHDATAWRITER_H

#include <QtEndian>
#include <QByteArray>
#include <QString>
#include <QUuid>
#include <QVariantMap>
#include <QLoggingCategory>

class WatchDataWriter
{
public:
    WatchDataWriter(QByteArray *buf);

    template <typename T>
    void write(T v);

    template <typename T>
    void writeLE(T v);

    void writeBytes(int n, const QByteArray &b);

    void writeFixedString(int n, const QString &s);

    void writeCString(const QString &s);

    void writePascalString(const QString &s);

    void writeUuid(const QUuid &uuid);

    void writeDict(const QMap<int, QVariant> &d);

private:
    char *p(int n);
    uchar *up(int n);
    QByteArray *_buf;
};

inline WatchDataWriter::WatchDataWriter(QByteArray *buf)
    : _buf(buf)
{
}

template <typename T>
void WatchDataWriter::write(T v)
{
    qToBigEndian(v, up(sizeof(T)));
}

template <typename T>
void WatchDataWriter::writeLE(T v)
{
    qToLittleEndian(v, up(sizeof(T)));
}

inline char * WatchDataWriter::p(int n)
{
    int size = _buf->size();
    _buf->resize(size + n);
    return &_buf->data()[size];
}

inline uchar * WatchDataWriter::up(int n)
{
    return reinterpret_cast<uchar *>(p(n));
}

#endif