• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
15 namespace SkSL {
16 
17 class StringStream : public OutputStream {
18 public:
write8(uint8_t b)19     void write8(uint8_t b) override {
20         fBuffer += (char) b;
21     }
22 
writeText(const char * s)23     void writeText(const char* s) override {
24         fBuffer += s;
25     }
26 
write(const void * s,size_t size)27     void write(const void* s, size_t size) override {
28         fBuffer.append((const char*) s, size);
29     }
30 
str()31     const String& str() const {
32         return fBuffer;
33     }
34 
reset()35     void reset() {
36         fBuffer = "";
37     }
38 
39 private:
40     String fBuffer;
41 };
42 
43 #else
44 
45 #include "SkData.h"
46 #include "SkStream.h"
47 
48 namespace SkSL {
49 
50 class StringStream : public OutputStream {
51 public:
52     void write8(uint8_t b) override {
53         fStream.write8(b);
54     }
55 
56     void writeText(const char* s) override {
57         fStream.writeText(s);
58     }
59 
60     void write(const void* s, size_t size) override {
61         fStream.write(s, size);
62     }
63 
64     const String& str() const {
65         if (!fString.size()) {
66             sk_sp<SkData> data = fStream.detachAsData();
67             fString = String((const char*) data->data(), data->size());
68         }
69         return fString;
70     }
71 
72     void reset() {
73         fStream.reset();
74         fString = "";
75     }
76 
77 private:
78     mutable SkDynamicMemoryWStream fStream;
79     mutable String fString;
80 };
81 
82 #endif // SKSL_STANDALONE
83 
84 } // namespace
85 
86 #endif
87