0.6.0
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
jinja_context.h
Go to the documentation of this file.
1/**
2 * @file src/common/model/jinja_context.h
3 *
4 * Copyright (c) 2021-2025 Bartek Kryza <bkryza@gmail.com>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18#pragma once
19
24#include "util/error.h"
25
26#include <inja/inja.hpp>
27
28#include <string>
29
31
32struct diagram_context_tag;
33struct element_context_tag;
34
35/**
36 * @brief Jinja diagram element context wrapper
37 *
38 * This template allows to have custom adl serializers for JSON, which only work
39 * for Jinja templates, and not JSON generator.
40 *
41 * @tparam T Diagram model element
42 */
43template <typename T, typename Tag> class jinja_context {
44public:
45 explicit jinja_context(
46 const T &e, const inja::json &diagram_context) noexcept
47 : value_{e}
49 {
50 }
51
52 explicit jinja_context(const T &e) noexcept
53 : jinja_context(e, {})
54 {
55 }
56
57 const T &get() const noexcept { return value_; }
58
59 template <typename U> const jinja_context<U, Tag> as() const
60 {
62 dynamic_cast<const U &>(value_), diagram_context_);
63 }
64
65 const inja::json &diagram_context() const { return diagram_context_; }
66
67private:
68 const T &value_;
69 const inja::json &diagram_context_;
70};
71
72template <typename T>
74template <typename T>
76
77void to_json(inja::json &ctx,
79
81 inja::json &ctx, const element_context<common::model::diagram_element> &jc);
82
84 inja::json &ctx, const element_context<common::model::element> &jc);
85
86void to_json(
87 inja::json &ctx, const element_context<common::model::relationship> &jc);
88
90 inja::json &ctx, const element_context<common::model::source_file> &jc);
91
93 inja::json &ctx, const element_context<common::model::source_location> &jc);
94
95void to_json(inja::json &ctx,
97
98void to_json(
99 inja::json &ctx, const diagram_context<common::model::diagram_element> &jc);
100
101void to_json(
102 inja::json &ctx, const diagram_context<common::model::element> &jc);
103
105 inja::json &ctx, const diagram_context<common::model::source_file> &jc);
106
108 inja::json &ctx, const diagram_context<common::model::source_location> &jc);
109
110std::optional<std::string> render_template(inja::Environment &env,
111 const inja::json &context, const std::string &jinja_template);
112
113std::optional<std::string> render_template(
114 inja::Environment &env, const std::string &jinja_template);
115
116} // namespace clanguml::common::jinja