aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/sksl/SkSLStringStream.h
blob: 9061432018d81934c3cd7a047538457c7be10dad (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Copyright 2017 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SKSL_STRINGSTREAM
#define SKSL_STRINGSTREAM

#include "SkSLOutputStream.h"

#ifdef SKSL_STANDALONE

namespace SkSL {

class StringStream : public OutputStream {
public:
    void write8(uint8_t b) override {
        fBuffer += (char) b;
    }

    void writeText(const char* s) override {
        fBuffer += s;
    }

    void write(const void* s, size_t size) override {
        fBuffer.append((const char*) s, size);
    }

    const char* data() const {
        return fBuffer.c_str();
    }

    size_t size() const {
        return fBuffer.size();
    }

    void reset() {
        fBuffer = "";
    }

private:
    String fBuffer;
};

#else

#include "SkData.h"
#include "SkStream.h"

namespace SkSL {

class StringStream : public OutputStream {
public:
    void write8(uint8_t b) override {
        SkASSERT(!fData);
        fStream.write8(b);
    }

    void writeText(const char* s) override {
        SkASSERT(!fData);
        fStream.writeText(s);
    }

    void write(const void* s, size_t size) override {
        SkASSERT(!fData);
        fStream.write(s, size);
    }

    const char* data() const {
        if (!fData) {
            fData = fStream.detachAsData();
        }
        return (const char*) fData->data();
    }

    size_t size() const {
        if (!fData) {
            fData = fStream.detachAsData();
        }
        return fData->size();
    }

    void reset() {
        fStream.reset();
        fData = nullptr;
    }

private:
    mutable SkDynamicMemoryWStream fStream;
    mutable sk_sp<SkData> fData;
};

#endif // SKSL_STANDALONE

} // namespace

#endif