blob: a8690668d2196a7a98fe82e4424bf4f210c80af4 [file] [log] [blame]
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001/*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SKSL_STRINGSTREAM
9#define SKSL_STRINGSTREAM
10
11#include "SkSLOutputStream.h"
12
13#ifdef SKSL_STANDALONE
14
15namespace SkSL {
16
17class StringStream : public OutputStream {
18public:
19 void write8(uint8_t b) override {
20 fBuffer += (char) b;
21 }
22
23 void writeText(const char* s) override {
24 fBuffer += s;
25 }
26
27 void write(const void* s, size_t size) override {
28 fBuffer.append((const char*) s, size);
29 }
30
Ethan Nicholas762466e2017-06-29 10:03:38 -040031 const String& str() const {
32 return fBuffer;
Ethan Nicholas0df1b042017-03-31 13:56:23 -040033 }
34
35 void reset() {
36 fBuffer = "";
37 }
38
39private:
40 String fBuffer;
41};
42
43#else
44
45#include "SkData.h"
46#include "SkStream.h"
47
48namespace SkSL {
49
50class StringStream : public OutputStream {
51public:
52 void write8(uint8_t b) override {
Ethan Nicholas0df1b042017-03-31 13:56:23 -040053 fStream.write8(b);
54 }
55
56 void writeText(const char* s) override {
Ethan Nicholas0df1b042017-03-31 13:56:23 -040057 fStream.writeText(s);
58 }
59
60 void write(const void* s, size_t size) override {
Ethan Nicholas0df1b042017-03-31 13:56:23 -040061 fStream.write(s, size);
62 }
63
Ethan Nicholas762466e2017-06-29 10:03:38 -040064 const String& str() const {
65 if (!fString.size()) {
66 sk_sp<SkData> data = fStream.detachAsData();
67 fString = String((const char*) data->data(), data->size());
Ethan Nicholas0df1b042017-03-31 13:56:23 -040068 }
Ethan Nicholas762466e2017-06-29 10:03:38 -040069 return fString;
Ethan Nicholas0df1b042017-03-31 13:56:23 -040070 }
71
72 void reset() {
73 fStream.reset();
Ethan Nicholas762466e2017-06-29 10:03:38 -040074 fString = "";
Ethan Nicholas0df1b042017-03-31 13:56:23 -040075 }
76
77private:
78 mutable SkDynamicMemoryWStream fStream;
Ethan Nicholas762466e2017-06-29 10:03:38 -040079 mutable String fString;
Ethan Nicholas0df1b042017-03-31 13:56:23 -040080};
81
82#endif // SKSL_STANDALONE
83
84} // namespace
85
86#endif