1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2026 Fernando Sahmkow
#include <whiteout/models/m3/writer.h>
#include "../../common/binary_writer.h"
#include "../../common/streams.h"
#include "../../common/unicode_path.h"
#include "binary_writer_visitor.h"
#include <fstream>
#include <stdexcept>
namespace whiteout {
namespace m3 {
using common::BinaryWriter;
// ============================================================================
// WriterImpl - Implementation class using PImpl idiom
// ============================================================================
class Writer::Impl {
public:
void writeToWriter(BinaryWriter& writer, const Model& model);
};
void Writer::Impl::writeToWriter(BinaryWriter& writer, const Model& model) {
BinaryWriterVisitor visitor(writer);
visitor.write(model);
}
// ============================================================================
// Writer Public Interface (using PImpl)
// ============================================================================
Writer::Writer() : pImpl(std::make_unique<Impl>()) {}
Writer::~Writer() = default;
void Writer::write(const std::string& filePath, const Model& model) {
auto file = common::open_ofstream(filePath, std::ios::binary);
if (!file.is_open()) {
// Non-throwing: silently fail. Caller can verify via filesystem if needed.
return;
}
BinaryWriter writer(file);
pImpl->writeToWriter(writer, model);
}
std::vector<u8> Writer::write(const Model& model) {
// Estimate output size from header
u32 const estimatedSize = 1 * 1024 * 1024; // Start with 2 MB
std::vector<u8> buffer;
buffer.reserve(estimatedSize);
common::vector_streambuf streambuf(buffer);
std::ostream out(&streambuf);
BinaryWriter writer(out);
pImpl->writeToWriter(writer, model);
return buffer;
}
} // namespace m3
} // namespace whiteout