Skip to content

Commit f69da5b

Browse files
committed
Replace usages of StdStringstream with custom TextWriter.
Reasons: - May possibly mitigate ABI issues on some Linux distros #842 - Less heavy than std::stringstream - Less header includes - Customizability
1 parent bf484b0 commit f69da5b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+488
-204
lines changed

engine/gpu/gpu_storage_buffer_pool.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#include "gpu_storage_buffer_pool.h"
22
#include "../../util/dstack.h"
33
#include "../../util/io/log.h"
4+
#include "../../util/io/std_string_text_writer.h"
45
#include "../../util/math/funcs.h"
56
#include "../../util/profiling.h"
67
#include "../../util/string/format.h"
7-
#include "../../util/string/std_stringstream.h"
88

99
#include <algorithm>
1010
#include <limits>
@@ -152,7 +152,7 @@ void GPUStorageBufferPool::recycle(GPUStorageBuffer b) {
152152
}
153153

154154
void GPUStorageBufferPool::debug_print() const {
155-
StdStringStream ss;
155+
StdStringTextWriter ss;
156156
ss << "---- GPUStorageBufferPool ----\n";
157157
for (unsigned int i = 0; i < _pools.size(); ++i) {
158158
const Pool &pool = _pools[i];
@@ -164,7 +164,7 @@ void GPUStorageBufferPool::debug_print() const {
164164
<< ", capacity: " << pool.buffers.capacity() << "\n";
165165
}
166166
ss << "----";
167-
print_line(ss.str());
167+
print_line(ss.get_written());
168168
}
169169

170170
} // namespace zylann::voxel

engine/ids.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#include "ids.h"
2-
#include <sstream>
2+
#include "../util/io/text_writer.h"
33

44
namespace zylann {
55

6-
StdStringStream &operator<<(StdStringStream &ss, const SlotMapKey<uint16_t, uint16_t> &v) {
7-
ss << "{i: " << v.index << ", v: " << v.version.value << "}";
8-
return ss;
6+
TextWriter &operator<<(TextWriter &w, const SlotMapKey<uint16_t, uint16_t> &v) {
7+
w << "{i: " << v.index << ", v: " << v.version.value << "}";
8+
return w;
99
}
1010

1111
} // namespace zylann

engine/ids.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define VOXEL_VOLUME_ID_H
33

44
#include "../util/containers/slot_map.h"
5-
#include "../util/string/std_stringstream.h"
65

76
namespace zylann::voxel {
87

@@ -12,7 +11,11 @@ typedef SlotMapKey<uint16_t, uint16_t> ViewerID;
1211
} // namespace zylann::voxel
1312

1413
namespace zylann {
15-
StdStringStream &operator<<(StdStringStream &ss, const SlotMapKey<uint16_t, uint16_t> &v);
14+
15+
class TextWriter;
16+
17+
TextWriter &operator<<(TextWriter &w, const SlotMapKey<uint16_t, uint16_t> &v);
18+
1619
} // namespace zylann
1720

1821
#endif // VOXEL_VOLUME_ID_H

generators/graph/code_gen_helper.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
namespace zylann {
1010

11-
CodeGenHelper::CodeGenHelper(StdStringStream &main_ss, StdStringStream &lib_ss) : _main_ss(main_ss), _lib_ss(lib_ss) {}
12-
1311
void CodeGenHelper::indent() {
1412
++_indent_level;
1513
}
@@ -24,11 +22,11 @@ void CodeGenHelper::add(const char *s, unsigned int len) {
2422
const char c = s[i];
2523
if (_newline) {
2624
for (unsigned int j = 0; j < _indent_level; ++j) {
27-
_main_ss << " ";
25+
_main_code += " ";
2826
}
2927
_newline = false;
3028
}
31-
_main_ss << c;
29+
_main_code += c;
3230
if (c == '\n') {
3331
_newline = true;
3432
}
@@ -39,8 +37,8 @@ void CodeGenHelper::add(const char *s) {
3937
add(s, strlen(s));
4038
}
4139

42-
void CodeGenHelper::add(FwdConstStdString s) {
43-
add(s.s.c_str(), s.s.size());
40+
void CodeGenHelper::add(const StdString &s) {
41+
add(s.c_str(), s.size());
4442
}
4543

4644
void CodeGenHelper::add(float x) {
@@ -68,9 +66,9 @@ void CodeGenHelper::add(int x) {
6866
void CodeGenHelper::require_lib_code(const char *lib_name, const char *code) {
6967
auto p = _included_libs.insert(lib_name);
7068
if (p.second) {
71-
_lib_ss << "\n\n";
72-
_lib_ss << code;
73-
_lib_ss << "\n\n";
69+
_lib_code += "\n\n";
70+
_lib_code += code;
71+
_lib_code += "\n\n";
7472
}
7573
}
7674

@@ -79,23 +77,25 @@ void CodeGenHelper::require_lib_code(const char *lib_name, const char *code) {
7977
void CodeGenHelper::require_lib_code(const char *lib_name, const char **code) {
8078
auto p = _included_libs.insert(lib_name);
8179
if (p.second) {
82-
_lib_ss << "\n\n";
80+
_lib_code += "\n\n";
8381
while (*code != 0) {
84-
_lib_ss << *code;
82+
_lib_code += *code;
8583
++code;
8684
}
87-
_lib_ss << "\n\n";
85+
_lib_code += "\n\n";
8886
}
8987
}
9088

91-
void CodeGenHelper::generate_var_name(FwdMutableStdString out_var_name) {
89+
StdString CodeGenHelper::generate_var_name() {
9290
const StdString s = format("v{}", _next_var_name_id);
9391
++_next_var_name_id;
94-
out_var_name.s = s;
92+
return s;
9593
}
9694

97-
void CodeGenHelper::print(FwdMutableStdString output) {
98-
output.s = _lib_ss.str() + _main_ss.str();
95+
StdString CodeGenHelper::print() const {
96+
StdString out = _lib_code;
97+
out += _main_code;
98+
return out;
9999
}
100100

101101
} // namespace zylann

generators/graph/code_gen_helper.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
#define ZN_CODE_GEN_HELPER_H
33

44
#include "../../util/containers/std_unordered_set.h"
5-
#include "../../util/string/fwd_std_string.h"
6-
#include "../../util/string/std_stringstream.h"
5+
#include "../../util/string/std_string.h"
6+
// Stopped using StdStringstream for the time being, because of issues supporting some Linux distros
7+
// https://github.com/Zylann/godot_voxel/issues/842
8+
// #include "../../util/string/std_stringstream.h"
79

810
namespace zylann {
911

1012
class CodeGenHelper {
1113
public:
12-
CodeGenHelper(StdStringStream &main_ss, StdStringStream &lib_ss);
13-
1414
void indent();
1515
void dedent();
1616

1717
void add(const char *s, unsigned int len);
1818
void add(const char *s);
19-
void add(FwdConstStdString s);
19+
void add(const StdString &s);
2020
void add(double x);
2121
void add(float x);
2222
void add(int x);
@@ -37,9 +37,9 @@ class CodeGenHelper {
3737

3838
void require_lib_code(const char *lib_name, const char *code);
3939
void require_lib_code(const char *lib_name, const char **code);
40-
void generate_var_name(FwdMutableStdString out_var_name);
40+
StdString generate_var_name();
4141

42-
void print(FwdMutableStdString output);
42+
StdString print() const;
4343

4444
private:
4545
template <typename T>
@@ -63,8 +63,8 @@ class CodeGenHelper {
6363
return c;
6464
}
6565

66-
StdStringStream &_main_ss;
67-
StdStringStream &_lib_ss;
66+
StdString _main_code;
67+
StdString _lib_code;
6868
unsigned int _indent_level = 0;
6969
unsigned int _next_var_name_id = 0;
7070
StdUnorderedSet<const char *> _included_libs;

generators/graph/voxel_graph_runtime.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
#ifdef TOOLS_ENABLED
99
#include "../../util/profiling_clock.h"
1010
#endif
11+
#include "../../util/io/std_string_text_writer.h"
1112
#include "node_type_db.h"
1213
#include "voxel_generator_graph.h"
1314

14-
#include <sstream>
1515
#include <unordered_set>
1616

1717
// #ifdef DEBUG_ENABLED
@@ -577,7 +577,7 @@ void Runtime::analyze_range(State &state, Span<const math::Interval> p_inputs) c
577577
void Runtime::debug_print_operations() {
578578
const Span<const uint16_t> operations(_program.operations.data(), 0, _program.operations.size());
579579

580-
StdStringStream ss;
580+
StdStringTextWriter ss;
581581
unsigned int op_index = 0;
582582
uint32_t pc = 0;
583583
while (pc < operations.size()) {
@@ -620,7 +620,7 @@ void Runtime::debug_print_operations() {
620620
++op_index;
621621
}
622622

623-
print_line(ss.str());
623+
print_line(ss.get_written());
624624
}
625625

626626
#endif

generators/graph/voxel_graph_shader_generator.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,7 @@ CompilationResult generate_shader(
8282

8383
expanded_graph.find_dependencies(to_span(terminal_nodes), order);
8484

85-
StdStringStream main_ss;
86-
StdStringStream lib_ss;
87-
CodeGenHelper codegen(main_ss, lib_ss);
85+
CodeGenHelper codegen;
8886

8987
codegen.add("void generate(vec3 pos");
9088

@@ -107,7 +105,8 @@ CompilationResult generate_shader(
107105
outputs.push_back(ShaderOutput{ ShaderOutput::TYPE_TYPE });
108106
break;
109107
default:
110-
ZN_PRINT_WARNING(format("Output type {} is not supported yet in shader generator.", node_type.name)
108+
ZN_PRINT_WARNING(
109+
format("Output type {} is not supported yet in shader generator.", node_type.name)
111110
);
112111
break;
113112
}
@@ -152,8 +151,7 @@ CompilationResult generate_shader(
152151
case VoxelGraphFunction::NODE_CONSTANT: {
153152
ZN_ASSERT(node.outputs.size() == 1);
154153
const ProgramGraph::PortLocation output_port{ node_id, 0 };
155-
StdString name;
156-
codegen.generate_var_name(name);
154+
const StdString name = codegen.generate_var_name();
157155
port_to_var.insert({ output_port, name });
158156
ZN_ASSERT(node.params.size() == 1);
159157
codegen.add_format("float {} = {};\n", name, float(node.params[0]));
@@ -223,15 +221,14 @@ CompilationResult generate_shader(
223221
// No incoming connections to this input. Make up a variable so following code can stay the same.
224222
// It will only be used for this node.
225223
StdString &var_name = unconnected_input_var_names[port_index];
226-
codegen.generate_var_name(var_name);
224+
var_name = codegen.generate_var_name();
227225
input_names[port_index] = var_name.c_str();
228226
codegen.add_format("float {} = {};\n", var_name, float(node.default_inputs[port_index]));
229227
}
230228
}
231229

232230
for (unsigned int port_index = 0; port_index < node.outputs.size(); ++port_index) {
233-
StdString var_name;
234-
codegen.generate_var_name(var_name);
231+
const StdString var_name = codegen.generate_var_name();
235232
auto p = port_to_var.insert({ { node_id, port_index }, var_name });
236233
ZN_ASSERT(p.second); // Conflict with an existing port?
237234
output_names[port_index] = p.first->second.c_str();
@@ -265,7 +262,7 @@ CompilationResult generate_shader(
265262
codegen.dedent();
266263
codegen.add("}\n");
267264

268-
codegen.print(source_code);
265+
source_code.s = codegen.print();
269266

270267
CompilationResult result;
271268
result.success = true;

tests/util/test_threaded_task_runner.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
#include "../../util/godot/classes/time.h"
66
#include "../../util/godot/core/random_pcg.h"
77
#include "../../util/io/log.h"
8+
#include "../../util/io/std_string_text_writer.h"
89
#include "../../util/math/vector3i.h"
910
#include "../../util/memory/memory.h"
1011
#include "../../util/profiling.h"
1112
#include "../../util/string/format.h"
12-
#include "../../util/string/std_stringstream.h"
1313
#include "../../util/tasks/threaded_task_runner.h"
1414
#include "../../util/testing/test_macros.h"
1515

@@ -262,11 +262,11 @@ void test_threaded_task_runner_debug_names() {
262262

263263
// Print how many times each name came up.
264264
// Doing this to check if the test runs as expected and to prevent compiler optimization on getting the names
265-
StdStringStream ss;
265+
StdStringTextWriter ss;
266266
for (auto it = name_counts.begin(); it != name_counts.end(); ++it) {
267267
ss << it->first << ": " << it->second << "; ";
268268
}
269-
print_line(ss.str());
269+
print_line(ss.get_written());
270270
}
271271

272272
void test_task_priority_values() {

tests/voxel/test_util.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include "test_util.h"
22
#include "../../storage/voxel_buffer.h"
33
#include "../../util/io/log.h"
4+
#include "../../util/io/std_string_text_writer.h"
45
#include "../../util/string/std_string.h"
5-
#include "../../util/string/std_stringstream.h"
66
#include <sstream>
77

88
namespace zylann::voxel::tests {
@@ -51,7 +51,7 @@ bool sd_equals_approx(const VoxelBuffer &vb1, const VoxelBuffer &vb2) {
5151
}
5252

5353
void print_channel_as_ascii(const VoxelBuffer &vb, unsigned int channel, const unsigned int padding) {
54-
StdStringStream ss;
54+
StdStringTextWriter ss;
5555

5656
Vector3i pos;
5757
for (pos.y = 0; pos.y < vb.get_size().y; ++pos.y) {
@@ -76,7 +76,7 @@ void print_channel_as_ascii(const VoxelBuffer &vb, unsigned int channel, const u
7676
}
7777
}
7878

79-
print_line(ss.str());
79+
print_line(ss.get_written());
8080
}
8181

8282
} // namespace zylann::voxel::tests

tests/voxel/test_voxel_buffer.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "../../util/io/log.h"
88
#include "../../util/string/format.h"
99
#include "../../util/string/std_string.h"
10-
#include "../../util/string/std_stringstream.h"
1110
#include "../../util/testing/test_macros.h"
1211
#include "test_util.h"
1312
#include <array>

0 commit comments

Comments
 (0)