| 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 "include/core/SkData.h" |
| 12 |
#include "include/core/SkStream.h" |
| 13 |
#include "src/sksl/SkSLOutputStream.h" |
| 14 |
|
| 15 |
namespace SkSL { |
| 16 |
|
| 17 |
class StringStream : public OutputStream { |
| 18 |
public: |
| 19 |
void write8(uint8_t b) override { |
| 20 |
SkASSERT(fString.empty()); |
| 21 |
fStream.write8(b); |
| 22 |
} |
| 23 |
|
| 24 |
void writeText(const char* s) override { |
| 25 |
SkASSERT(fString.empty()); |
| 26 |
fStream.writeText(s); |
| 27 |
} |
| 28 |
|
| 29 |
void write(const void* s, size_t size) override { |
| 30 |
SkASSERT(fString.empty()); |
| 31 |
fStream.write(s, size); |
| 32 |
} |
| 33 |
|
| 34 |
size_t bytesWritten() const { |
| 35 |
return fStream.bytesWritten(); |
| 36 |
} |
| 37 |
|
| 38 |
const std::string& str() const { |
| 39 |
if (!fString.size()) { |
| 40 |
sk_sp<SkData> data = fStream.detachAsData(); |
| 41 |
fString = std::string((const char*) data->data(), data->size()); |
| 42 |
} |
| 43 |
return fString; |
| 44 |
} |
| 45 |
|
| 46 |
void reset() { |
| 47 |
fStream.reset(); |
| 48 |
fString = ""; |
| 49 |
} |
| 50 |
|
| 51 |
private: |
| 52 |
mutable SkDynamicMemoryWStream fStream; |
| 53 |
mutable std::string fString; |
| 54 |
}; |
| 55 |
|
| 56 |
} // namespace SkSL |
| 57 |
|
| 58 |
#endif |