blob: 2cd65e9cb4cdee5e47a9ea62686a3f25a77da138 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/sksl/SkSLOutputStream.h"
Ethan Nicholasfc994162019-06-06 10:04:27 -040012#include "src/sksl/SkSLString.h"
Ethan Nicholas0df1b042017-03-31 13:56:23 -040013
14#ifdef SKSL_STANDALONE
15
16namespace SkSL {
17
18class StringStream : public OutputStream {
19public:
20 void write8(uint8_t b) override {
21 fBuffer += (char) b;
22 }
23
24 void writeText(const char* s) override {
25 fBuffer += s;
26 }
27
28 void write(const void* s, size_t size) override {
29 fBuffer.append((const char*) s, size);
30 }
31
Ethan Nicholas762466e2017-06-29 10:03:38 -040032 const String& str() const {
33 return fBuffer;
Ethan Nicholas0df1b042017-03-31 13:56:23 -040034 }
35
36 void reset() {
37 fBuffer = "";
38 }
39
40private:
41 String fBuffer;
42};
43
44#else
45
Mike Kleinc0bd9f92019-04-23 12:05:21 -050046#include "include/core/SkData.h"
47#include "include/core/SkStream.h"
Ethan Nicholas0df1b042017-03-31 13:56:23 -040048
49namespace SkSL {
50
51class StringStream : public OutputStream {
52public:
53 void write8(uint8_t b) override {
Ethan Nicholas0df1b042017-03-31 13:56:23 -040054 fStream.write8(b);
55 }
56
57 void writeText(const char* s) override {
Ethan Nicholas0df1b042017-03-31 13:56:23 -040058 fStream.writeText(s);
59 }
60
61 void write(const void* s, size_t size) override {
Ethan Nicholas0df1b042017-03-31 13:56:23 -040062 fStream.write(s, size);
63 }
64
Ethan Nicholas762466e2017-06-29 10:03:38 -040065 const String& str() const {
66 if (!fString.size()) {
67 sk_sp<SkData> data = fStream.detachAsData();
68 fString = String((const char*) data->data(), data->size());
Ethan Nicholas0df1b042017-03-31 13:56:23 -040069 }
Ethan Nicholas762466e2017-06-29 10:03:38 -040070 return fString;
Ethan Nicholas0df1b042017-03-31 13:56:23 -040071 }
72
73 void reset() {
74 fStream.reset();
Ethan Nicholas762466e2017-06-29 10:03:38 -040075 fString = "";
Ethan Nicholas0df1b042017-03-31 13:56:23 -040076 }
77
78private:
79 mutable SkDynamicMemoryWStream fStream;
Ethan Nicholas762466e2017-06-29 10:03:38 -040080 mutable String fString;
Ethan Nicholas0df1b042017-03-31 13:56:23 -040081};
82
83#endif // SKSL_STANDALONE
84
John Stilesa6841be2020-08-06 14:11:56 -040085} // namespace SkSL
Ethan Nicholas0df1b042017-03-31 13:56:23 -040086
87#endif