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

Base class for diagram generators. More...

Detailed Description

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

Base class for diagram generators.

Template Parameters
ConfigTypeConfiguration type
DiagramTypeDiagram model type

Definition at line 54 of file generator.h.

#include <generator.h>

Public Member Functions

 generator (ConfigType &config, DiagramType &model)
 Constructor.
 
 ~generator () override=default
 
void generate (std::ostream &ostr) const override
 Generate diagram.
 
virtual void generate_diagram (std::ostream &ostr) const =0
 Generate diagram specific part.
 
void generate_config_layout_hints (std::ostream &ostr) const
 Generate diagram layout hints.
 
void generate_plantuml_directives (std::ostream &ostr, const std::vector< std::string > &directives) const
 Generate PlantUML directives from config file.
 
void generate_notes (std::ostream &ostr, const model::element &element) const
 Generate diagram notes.
 
void generate_style (std::ostream &ostr, const std::string &element_type, const model::stylable_element &el) const
 Generate diagram element PlantUML style.
 
void generate_metadata (std::ostream &ostr) const
 Generate comment with diagram metadata.
 
void generate_title (std::ostream &ostr) const
 Generate diagram title.
 
template<typename E >
void generate_link (std::ostream &ostr, const E &e) const
 Generate hyper link to element.
 
void generate_link (std::ostream &ostr, const relationship &e) const
 generate_link specialization for relationship
 
void print_debug (const common::model::source_location &e, std::ostream &ostr) const
 Print debug information in diagram comments.
 
- Public Member Functions inherited from clanguml::common::generators::generator< ConfigType, DiagramType >
 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.
 
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
 
std::optional< std::string > render_link (const common::model::diagram_element &e) const
 
std::optional< std::string > render_link (const common::model::relationship &e) const
 
std::optional< std::string > render_tooltip (const common::model::diagram_element &e) const
 
std::optional< std::string > render_tooltip (const common::model::relationship &e) 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

std::set< std::string > m_generated_aliases
 
- Protected Attributes inherited from clanguml::common::generators::generator< ConfigType, DiagramType >
inja::json m_context
 
inja::Environment m_env
 

Private Member Functions

void generate_row_column_hints (std::ostream &ostr, const std::string &entity_name, const config::layout_hint &hint) const
 
void generate_position_hints (std::ostream &ostr, const std::string &entity_name, const config::layout_hint &hint) const
 

Constructor & Destructor Documentation

◆ generator()

template<typename ConfigType , typename DiagramType >
clanguml::common::generators::plantuml::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 63 of file generator.h.

◆ ~generator()

template<typename ConfigType , typename DiagramType >
clanguml::common::generators::plantuml::generator< ConfigType, DiagramType >::~generator ( )
overridevirtualdefault

Member Function Documentation

◆ generate()

template<typename C , typename D >
void clanguml::common::generators::plantuml::generator< C, D >::generate ( std::ostream &  ostr) const
overridevirtual

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

Implements clanguml::common::generators::generator< ConfigType, DiagramType >.

Definition at line 200 of file generator.h.

201{
204
206
207 if (!config.allow_empty_diagrams() && model.is_empty() &&
208 config.puml().before.empty() && config.puml().after.empty()) {
210 "Diagram configuration resulted in empty diagram."};
211 }
212
213 ostr << "@startuml" << '\n';
214
215 generate_title(ostr);
216
217 // Generate PlantUML directives before auto generated content
218 generate_plantuml_directives(ostr, config.puml().before);
219
220 generate_diagram(ostr);
221
222 // Generate PlantUML directives after auto generated content
223 generate_plantuml_directives(ostr, config.puml().after);
224
225 generate_metadata(ostr);
226
227 ostr << "@enduml" << '\n';
228}

◆ generate_config_layout_hints()

template<typename C , typename D >
void clanguml::common::generators::plantuml::generator< C, D >::generate_config_layout_hints ( std::ostream &  ostr) const

Generate diagram layout hints.

This method adds to the diagram any layout hints that were provided in the configuration file.

Parameters
ostrOutput stream

Definition at line 231 of file generator.h.

232{
233 using namespace clanguml::util;
234
236
237 // Generate layout hints
238 for (const auto &[entity_name, hints] : config.layout()) {
239 for (const auto &hint : hints) {
240 try {
241 if (hint.hint == config::hint_t::together) {
242 // 'together' layout hint is handled separately
243 }
244 else if (hint.hint == config::hint_t::row ||
245 hint.hint == config::hint_t::column) {
246 generate_row_column_hints(ostr, entity_name, hint);
247 }
248 else {
249 generate_position_hints(ostr, entity_name, hint);
250 }
251 }
253 LOG_DBG("=== Skipping layout hint '{}' from {} due "
254 "to: {}",
255 to_string(hint.hint), entity_name, e.what());
256 }
257 }
258 }
259}

◆ generate_diagram()

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

Generate diagram specific part.

This method must be implemented in subclasses for specific diagram types.

Parameters
ostrOutput stream

Implemented in clanguml::class_diagram::generators::plantuml::generator, clanguml::include_diagram::generators::plantuml::generator, clanguml::package_diagram::generators::plantuml::generator, and clanguml::sequence_diagram::generators::plantuml::generator.

◆ generate_link() [1/2]

template<typename C , typename D >
template<typename E >
void clanguml::common::generators::plantuml::generator< C, D >::generate_link ( std::ostream &  ostr,
const E &  e 
) const

Generate hyper link to element.

This method renders links to URL's based on templates provided in the configuration file (e.g. Git browser with specific line and column offset)

Parameters
ostrOutput stream
eReference to diagram element
Template Parameters
EDiagram element type

Definition at line 412 of file generator.h.

413{
414 const auto maybe_link = generator<C, D>::render_link(e);
415 const auto maybe_tooltip = generator<C, D>::render_tooltip(e);
416
417 if (!maybe_link && !maybe_tooltip)
418 return;
419
420 ostr << " [[";
421
422 ostr << maybe_link.value_or("");
423
424 if (maybe_tooltip)
425 ostr << "{" << *maybe_tooltip << "}";
426
427 ostr << "]]";
428}

◆ generate_link() [2/2]

template<typename C , typename D >
void clanguml::common::generators::plantuml::generator< C, D >::generate_link ( std::ostream &  ostr,
const relationship e 
) const

generate_link specialization for relationship

Parameters
ostrOutput stream
eReference to diagram relationship

Definition at line 431 of file generator.h.

433{
434 const auto maybe_link = generator<C, D>::render_link(r);
435 const auto maybe_tooltip = generator<C, D>::render_tooltip(r);
436
437 if (!maybe_link && !maybe_tooltip)
438 return;
439
440 ostr << " [[";
441
442 ostr << maybe_link.value_or("");
443
444 if (maybe_tooltip)
445 ostr << "{" << *maybe_tooltip << "}";
446
447 ostr << "]]";
448}

◆ generate_metadata()

template<typename C , typename D >
void clanguml::common::generators::plantuml::generator< C, D >::generate_metadata ( std::ostream &  ostr) const

Generate comment with diagram metadata.

Parameters
ostrOutput stream

Definition at line 388 of file generator.h.

389{
391
392 if (config.generate_metadata()) {
393 ostr << '\n'
394 << "'Generated with clang-uml, version "
395 << clanguml::version::version() << '\n'
396 << "'LLVM version " << clang::getClangFullVersion() << '\n';
397 }
398}

◆ generate_notes()

template<typename C , typename D >
void clanguml::common::generators::plantuml::generator< C, D >::generate_notes ( std::ostream &  ostr,
const model::element element 
) const

Generate diagram notes.

This method adds any notes in the diagram, which were declared in the code using inline directives

Parameters
ostrOutput stream
elementElement to which the note should be attached

Definition at line 372 of file generator.h.

374{
376
377 for (const auto &decorator : e.decorators()) {
378 auto note = std::dynamic_pointer_cast<decorators::note>(decorator);
379 if (note && note->applies_to_diagram(config.name)) {
380 ostr << "note " << note->position << " of " << e.alias() << '\n'
381 << note->text << '\n'
382 << "end note\n";
383 }
384 }
385}

◆ generate_plantuml_directives()

template<typename C , typename D >
void clanguml::common::generators::plantuml::generator< C, D >::generate_plantuml_directives ( std::ostream &  ostr,
const std::vector< std::string > &  directives 
) const

Generate PlantUML directives from config file.

This method renders the PlantUML directives provided in the configuration file, including resolving any element aliases and Jinja templates.

Parameters
ostrOutput stream
directivesList of directives from the configuration file

Definition at line 340 of file generator.h.

342{
344
345 for (const auto &d : directives) {
346 auto rendered_directive =
349
350 if (rendered_directive)
351 ostr << *rendered_directive << '\n';
352 }
353}

◆ generate_position_hints()

template<typename C , typename D >
void clanguml::common::generators::plantuml::generator< C, D >::generate_position_hints ( std::ostream &  ostr,
const std::string &  entity_name,
const config::layout_hint hint 
) const
private

Definition at line 309 of file generator.h.

311{
314
315 const auto &uns = config.using_namespace();
316
317 const auto &hint_entity = std::get<std::string>(hint.entity);
318
319 auto element_opt = model.get(entity_name);
320 if (!element_opt)
321 element_opt = model.get((uns | entity_name).to_string());
322
323 auto hint_element_opt = model.get(hint_entity);
324 if (!hint_element_opt)
325 hint_element_opt = model.get((uns | hint_entity).to_string());
326
327 if (!element_opt || !hint_element_opt)
328 return;
329
330 std::stringstream hint_str;
331
332 hint_str << element_opt.value().alias() << " -[hidden]"
333 << clanguml::config::to_string(hint.hint) << "- "
334 << hint_element_opt.value().alias() << '\n';
335
336 ostr << hint_str.str();
337}

◆ generate_row_column_hints()

template<typename C , typename D >
void clanguml::common::generators::plantuml::generator< C, D >::generate_row_column_hints ( std::ostream &  ostr,
const std::string &  entity_name,
const config::layout_hint hint 
) const
private

Definition at line 262 of file generator.h.

264{
267
268 const auto &uns = config.using_namespace();
269
270 std::vector<std::string> group_elements;
271 std::vector<std::pair<std::string, std::string>> element_aliases_pairs;
272
273 group_elements.push_back(entity_name);
274 const auto &group_tail = std::get<std::vector<std::string>>(hint.entity);
275 std::copy(group_tail.begin(), group_tail.end(),
276 std::back_inserter(group_elements));
277
278 auto element_opt = model.get(entity_name);
279 if (!element_opt)
280 element_opt = model.get((uns | entity_name).to_string());
281
282 for (auto it = cbegin(group_elements);
283 it != cend(group_elements) && std::next(it) != cend(group_elements);
284 ++it) {
285 const auto &first = *it;
286 const auto &second = *std::next(it);
287
288 auto first_opt = model.get(first);
289 if (!first_opt)
290 first_opt = model.get((uns | first).to_string());
291
292 auto second_opt = model.get(second);
293 if (!second_opt)
294 second_opt = model.get((uns | second).to_string());
295
296 element_aliases_pairs.emplace_back(
297 first_opt.value().alias(), second_opt.value().alias());
298 }
299
300 std::string hint_direction =
301 hint.hint == clanguml::config::hint_t::row ? "right" : "down";
302
303 for (const auto &[f, s] : element_aliases_pairs) {
304 ostr << f << " -[hidden]" << hint_direction << "- " << s << '\n';
305 }
306}

◆ generate_style()

template<typename C , typename D >
void clanguml::common::generators::plantuml::generator< C, D >::generate_style ( std::ostream &  ostr,
const std::string &  element_type,
const model::stylable_element el 
) const

Generate diagram element PlantUML style.

This method renders a style for a specific el element if specified in the config file or inline comment directive.

Parameters
ostrOutput stream
element_typeName of the element type (e.g. "class")
elReference to a stylable diagram element

Definition at line 356 of file generator.h.

358{
360
361 if (el.style() && !el.style().value().empty()) // NOLINT
362 ostr << " " << *el.style(); // NOLINT
363 else if (config.puml) {
364 if (const auto config_style = config.puml().get_style(element_type);
365 config_style) {
366 ostr << " " << *config_style;
367 }
368 }
369}

◆ generate_title()

template<typename C , typename D >
void clanguml::common::generators::plantuml::generator< C, D >::generate_title ( std::ostream &  ostr) const

Generate diagram title.

Generates a PlantUML diagram title directive if diagram title is provided in the diagram configuration.

Parameters
ostrOutput stream

Definition at line 401 of file generator.h.

402{
404
405 if (config.title) {
406 ostr << "title " << config.title() << '\n';
407 }
408}

◆ print_debug()

template<typename C , typename D >
void clanguml::common::generators::plantuml::generator< C, D >::print_debug ( const common::model::source_location e,
std::ostream &  ostr 
) const

Print debug information in diagram comments.

Parameters
mDiagram element to describe
ostrOutput stream

Definition at line 451 of file generator.h.

453{
455
456 if (config.debug_mode()) {
457 if (!e.file_relative().empty()) {
458 ostr << "' " << e.file_relative() << ":" << e.line() << '\n';
459 }
460 else if (!e.file().empty()) {
461 ostr << "' " << e.file() << ":" << e.line() << '\n';
462 }
463 }
464}

Member Data Documentation

◆ m_generated_aliases

template<typename ConfigType , typename DiagramType >
std::set<std::string> clanguml::common::generators::plantuml::generator< ConfigType, DiagramType >::m_generated_aliases
mutableprotected

Definition at line 196 of file generator.h.


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