-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_render_with_output_buffer.cpp
More file actions
58 lines (47 loc) · 1.65 KB
/
example_render_with_output_buffer.cpp
File metadata and controls
58 lines (47 loc) · 1.65 KB
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
#include "frozenchars.hpp"
#include <iostream>
#include <string>
// カスタム出力バッファクラスの例
class StringBuffer {
public:
void append(std::string_view text) {
buffer_.append(text);
}
[[nodiscard]] auto result() const -> std::string {
return buffer_;
}
private:
std::string buffer_;
};
int main() {
// テンプレート例
constexpr auto tmpl = frozenchars::FrozenString{"Hello {{ name }}! Count: {{ count }}"};
// コンテキスト作成
auto context = frozenchars::inja::make_object({
{"name", frozenchars::inja::inja_value{"World"}},
{"count", frozenchars::inja::inja_value{42.0}},
});
// 方法1: 従来の render_template(std::string を返す)
try {
auto result1 = frozenchars::inja::render<tmpl>(context);
std::cout << "Method 1 (std::string): " << result1 << '\n';
} catch (std::exception const& e) {
std::cerr << "Error in method 1: " << e.what() << '\n';
}
// 方法2: カスタム出力バッファを使用(std::expected を返す)
auto buffer = StringBuffer{};
auto result2 = frozenchars::inja::render<tmpl>(context, buffer);
if (result2.has_value()) {
std::cout << "Method 2 (OutputBuffer): " << buffer.result() << '\n';
} else {
std::cerr << "Error in method 2: " << result2.error() << '\n';
}
// エラーケースのテスト: root がオブジェクトでない場合
auto non_obj = frozenchars::inja::inja_value{123.0};
auto buffer_err = StringBuffer{};
auto result_err = frozenchars::inja::render<tmpl>(non_obj, buffer_err);
if (!result_err.has_value()) {
std::cout << "Expected error: " << result_err.error() << '\n';
}
return 0;
}