0.5.4
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
Public Member Functions | Protected Attributes | Private Attributes | List of all members
clanguml::common::generators::generator< ConfigType, DiagramType > Class Template Referenceabstract

Common diagram generator interface. More...

Detailed Description

template<typename ConfigType, typename DiagramType>
class clanguml::common::generators::generator< ConfigType, DiagramType >

Common diagram generator interface.

This class defines common interface for all diagram generators.

Definition at line 41 of file generator.h.

#include <generator.h>

Public Member Functions

 generator (ConfigType &config, DiagramType &model)
 Constructor.
 
virtual ~generator ()=default
 
virtual void generate (std::ostream &ostr) const =0
 Generate diagram.
 
const ConfigType & config () const
 Get reference to diagram config.
 
const DiagramType & model () const
 Get reference to diagram model.
 
template<typename E >
inja::json element_context (const E &e) const
 
std::optional< std::pair< std::string, std::string > > get_link_pattern (const common::model::source_location &sl) const
 
std::optional< std::pair< std::string, std::string > > get_tooltip_pattern (const common::model::source_location &sl) const
 
void init_context ()
 Initialize diagram Jinja context.
 
void update_context () const
 Update diagram Jinja context.
 
void init_env ()
 
const inja::json & context () const
 
inja::Environment & env () const
 

Protected Attributes

inja::json m_context
 
inja::Environment m_env
 

Private Attributes

ConfigType & config_
 
DiagramType & model_
 

Constructor & Destructor Documentation

◆ generator()

template<typename ConfigType , typename DiagramType >
clanguml::common::generators::generator< ConfigType, DiagramType >::generator ( ConfigType &  config,
DiagramType &  model 
)
inline

Constructor.

Parameters
configReference to instance of model Reference to instance of clanguml::model::diagram

Definition at line 49 of file generator.h.

51 , model_{model}
52 {
54 init_env();
55 }

◆ ~generator()

template<typename ConfigType , typename DiagramType >
virtual clanguml::common::generators::generator< ConfigType, DiagramType >::~generator ( )
virtualdefault

Member Function Documentation

◆ config()

template<typename ConfigType , typename DiagramType >
const ConfigType & clanguml::common::generators::generator< ConfigType, DiagramType >::config ( ) const
inline

Get reference to diagram config.

Returns
Diagram config

Definition at line 76 of file generator.h.

76{ return config_; }

◆ context()

template<typename C , typename D >
const inja::json & clanguml::common::generators::generator< C, D >::context

Definition at line 140 of file generator.h.

141{
142 return m_context;
143}

◆ element_context()

template<typename C , typename D >
template<typename E >
inja::json clanguml::common::generators::generator< C, D >::element_context ( const E &  e) const

Definition at line 259 of file generator.h.

260{
261 const auto &diagram_context = context();
262
263 inja::json ctx;
264 ctx["element"] = e.context();
265#if _MSC_VER
266 if (ctx.contains("git")) {
267#else
268 if (diagram_context.template contains("git")) {
269#endif
270 ctx["git"] = diagram_context["git"];
271 }
272
273 if (!e.file().empty()) {
274 std::filesystem::path file{e.file()};
275 std::string git_relative_path = file.string();
276 if (!e.file_relative().empty()) {
277#if _MSC_VER
278 if (file.is_absolute() && ctx.contains("git")) {
279#else
280 if (file.is_absolute() &&
281 diagram_context.template contains("git")) {
282#endif
283 git_relative_path = std::filesystem::relative(
284 file, diagram_context["git"]["toplevel"])
285 .string();
286 ctx["element"]["source"]["path"] =
287 util::path_to_url(git_relative_path);
288 }
289 else {
290 ctx["element"]["source"]["path"] = e.file();
291 }
292 }
293 else {
294 git_relative_path = "";
295 ctx["element"]["source"]["path"] = e.file();
296 }
297
298 ctx["element"]["source"]["full_path"] = file.string();
299 ctx["element"]["source"]["name"] = file.filename().string();
300 ctx["element"]["source"]["line"] = e.line();
301 }
302
303 const auto &maybe_comment = e.comment();
304 if (maybe_comment) {
305 ctx["element"]["comment"] = maybe_comment.value();
306 }
307
308 return ctx;
309}

◆ env()

template<typename C , typename D >
inja::Environment & clanguml::common::generators::generator< C, D >::env

Definition at line 146 of file generator.h.

147{
148 return m_env;
149}

◆ generate()

template<typename ConfigType , typename DiagramType >
virtual void clanguml::common::generators::generator< ConfigType, DiagramType >::generate ( std::ostream &  ostr) const
pure virtual

Generate diagram.

This is the main diagram generation entrypoint. It is responsible for calling other methods in appropriate order to generate the diagram into the output stream. It generates diagram elements, that are common to all types of diagrams in a given generator.

Parameters
ostrOutput stream

Implemented in clanguml::common::generators::json::generator< ConfigType, DiagramType >, clanguml::common::generators::mermaid::generator< ConfigType, DiagramType >, and clanguml::common::generators::plantuml::generator< ConfigType, DiagramType >.

◆ get_link_pattern()

template<typename C , typename D >
std::optional< std::pair< std::string, std::string > > clanguml::common::generators::generator< C, D >::get_link_pattern ( const common::model::source_location sl) const

Definition at line 313 of file generator.h.

315{
316 if (sl.file_relative().empty()) {
317 return config().generate_links().get_link_pattern(sl.file());
318 }
319
320 return config().generate_links().get_link_pattern(sl.file_relative());
321}

◆ get_tooltip_pattern()

template<typename C , typename D >
std::optional< std::pair< std::string, std::string > > clanguml::common::generators::generator< C, D >::get_tooltip_pattern ( const common::model::source_location sl) const

Definition at line 325 of file generator.h.

327{
328 if (sl.file_relative().empty()) {
329 return config().generate_links().get_tooltip_pattern(sl.file());
330 }
331
332 return config().generate_links().get_tooltip_pattern(sl.file_relative());
333}

◆ init_context()

template<typename C , typename D >
void clanguml::common::generators::generator< C, D >::init_context

Initialize diagram Jinja context.

Definition at line 122 of file generator.h.

123{
125
126 if (config.git) {
127 m_context["git"]["branch"] = config.git().branch;
128 m_context["git"]["revision"] = config.git().revision;
129 m_context["git"]["commit"] = config.git().commit;
130 m_context["git"]["toplevel"] = config.git().toplevel;
131 }
132}

◆ init_env()

template<typename C , typename D >
void clanguml::common::generators::generator< C, D >::init_env

Definition at line 151 of file generator.h.

152{
155
156 //
157 // Add basic string functions to inja environment
158 //
159
160 // Check if string is empty
161 m_env.add_callback("empty", 1, [](inja::Arguments &args) {
162 return args.at(0)->get<std::string>().empty();
163 });
164
165 // Remove spaces from the left of a string
166 m_env.add_callback("ltrim", 1, [](inja::Arguments &args) {
167 return util::ltrim(args.at(0)->get<std::string>());
168 });
169
170 // Remove trailing spaces from a string
171 m_env.add_callback("rtrim", 1, [](inja::Arguments &args) {
172 return util::rtrim(args.at(0)->get<std::string>());
173 });
174
175 // Remove spaces before and after a string
176 m_env.add_callback("trim", 1, [](inja::Arguments &args) {
177 return util::trim(args.at(0)->get<std::string>());
178 });
179
180 // Make a string shorted with a limit to
181 m_env.add_callback("abbrv", 2, [](inja::Arguments &args) {
182 return util::abbreviate(
183 args.at(0)->get<std::string>(), args.at(1)->get<unsigned>());
184 });
185
186 m_env.add_callback("replace", 3, [](inja::Arguments &args) {
187 std::string result = args[0]->get<std::string>();
188 std::regex pattern(args[1]->get<std::string>());
189 return std::regex_replace(result, pattern, args[2]->get<std::string>());
190 });
191
192 m_env.add_callback("split", 2, [](inja::Arguments &args) {
193 return util::split(
194 args[0]->get<std::string>(), args[1]->get<std::string>());
195 });
196
197 //
198 // Add PlantUML specific functions
199 //
200
201 // Return the entire element JSON context based on element name
202 // e.g.:
203 // {{ element("clanguml::t00050::A").comment }}
204 //
205 m_env.add_callback("element", 1, [&model, &config](inja::Arguments &args) {
206 inja::json res{};
207 auto element_opt = model.get_with_namespace(
208 args[0]->get<std::string>(), config.using_namespace());
209
210 if (element_opt.has_value())
211 res = element_opt.value().context();
212
213 return res;
214 });
215
216 // Convert C++ entity to PlantUML alias, e.g.
217 // "note left of {{ alias("A") }}: This is a note"
218 // Shortcut to:
219 // {{ element("A").alias }}
220 //
221 m_env.add_callback("alias", 1, [&model, &config](inja::Arguments &args) {
222 auto element_opt = model.get_with_namespace(
223 args[0]->get<std::string>(), config.using_namespace());
224
225 if (!element_opt.has_value())
227 args[0]->get<std::string>());
228
229 return element_opt.value().alias();
230 });
231
232 // Get elements' comment:
233 // "note left of {{ alias("A") }}: {{ comment("A") }}"
234 // Shortcut to:
235 // {{ element("A").comment }}
236 //
237 m_env.add_callback("comment", 1, [&model, &config](inja::Arguments &args) {
238 inja::json res{};
239 auto element_opt = model.get_with_namespace(
240 args[0]->get<std::string>(), config.using_namespace());
241
242 if (!element_opt.has_value())
244 args[0]->get<std::string>());
245
246 auto comment = element_opt.value().comment();
247
248 if (comment.has_value()) {
249 assert(comment.value().is_object());
250 res = comment.value();
251 }
252
253 return res;
254 });
255}

◆ model()

template<typename ConfigType , typename DiagramType >
const DiagramType & clanguml::common::generators::generator< ConfigType, DiagramType >::model ( ) const
inline

Get reference to diagram model.

Returns
Diagram model

Definition at line 83 of file generator.h.

83{ return model_; }

◆ update_context()

template<typename C , typename D >
void clanguml::common::generators::generator< C, D >::update_context

Update diagram Jinja context.

This method updates the diagram context with models properties which can be used to render Jinja templates in the diagram (e.g. in notes or links)

Definition at line 134 of file generator.h.

135{
136 m_context["diagram"] = model().context();
137}

Member Data Documentation

◆ config_

template<typename ConfigType , typename DiagramType >
ConfigType& clanguml::common::generators::generator< ConfigType, DiagramType >::config_
private

Definition at line 118 of file generator.h.

◆ m_context

template<typename ConfigType , typename DiagramType >
inja::json clanguml::common::generators::generator< ConfigType, DiagramType >::m_context
mutableprotected

Definition at line 114 of file generator.h.

◆ m_env

template<typename ConfigType , typename DiagramType >
inja::Environment clanguml::common::generators::generator< ConfigType, DiagramType >::m_env
mutableprotected

Definition at line 115 of file generator.h.

◆ model_

template<typename ConfigType , typename DiagramType >
DiagramType& clanguml::common::generators::generator< ConfigType, DiagramType >::model_
private

Definition at line 119 of file generator.h.


The documentation for this class was generated from the following file: