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

Base class for diagram generators. More...

Detailed Description

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

Base class for diagram generators.

Template Parameters
ConfigTypeConfiguration type
DiagramTypeDiagram model type

Definition at line 58 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_mermaid_directives (std::ostream &ostr, const std::vector< std::string > &directives) const
 Generate MermaidJS directives from config file.
 
virtual void generate_diagram_type (std::ostream &ostr) const =0
 Generate the diagram type.
 
virtual void generate_notes (std::ostream &ostr, const model::diagram_element &element) const
 Generate diagram notes.
 
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 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
 

Constructor & Destructor Documentation

◆ generator()

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

◆ ~generator()

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

Member Function Documentation

◆ generate()

template<typename C , typename D >
void clanguml::common::generators::mermaid::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 177 of file generator.h.

178{
181
182 if (!config.allow_empty_diagrams() && model.is_empty() &&
183 config.mermaid().before.empty() && config.mermaid().after.empty()) {
185 "Diagram configuration resulted in empty diagram."};
186 }
187
189
190 generate_title(ostr);
191
193
194 generate_mermaid_directives(ostr, config.mermaid().before);
195
196 generate_diagram(ostr);
197
198 generate_mermaid_directives(ostr, config.mermaid().after);
199
200 generate_metadata(ostr);
201}

◆ generate_diagram()

template<typename ConfigType , typename DiagramType >
virtual void clanguml::common::generators::mermaid::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::mermaid::generator, clanguml::include_diagram::generators::mermaid::generator, clanguml::package_diagram::generators::mermaid::generator, and clanguml::sequence_diagram::generators::mermaid::generator.

◆ generate_diagram_type()

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

Generate the diagram type.

This method must be overriden for each diagram type (e.g. it renders a single line classDiagram for Mermaid class diagrams.

Parameters
ostrOutput stream

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

◆ generate_link()

template<typename C , typename D >
template<typename E >
void clanguml::common::generators::mermaid::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 205 of file generator.h.

206{
207 const auto maybe_link = generator<C, D>::render_link(e);
208 const auto maybe_tooltip = generator<C, D>::render_tooltip(e);
209
210 if (!maybe_link && !maybe_tooltip)
211 return;
212
213 if (maybe_link) {
214 ostr << indent(1) << "click " << e.alias() << " href \"";
215
216 if (!maybe_link || maybe_link->empty())
217 ostr << " ";
218 else
219 ostr << *maybe_link;
220
221 ostr << "\"";
222 }
223
224 if (maybe_tooltip && !maybe_tooltip->empty()) {
225 auto tooltip = *maybe_tooltip;
226 ostr << " \"";
227
228 util::replace_all(tooltip, "\"", "&bdquo;");
229 ostr << tooltip;
230
231 ostr << "\"";
232 }
233
234 ostr << "\n";
235}

◆ generate_mermaid_directives()

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

Generate MermaidJS directives from config file.

This method renders the MermaidJS 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 238 of file generator.h.

240{
242
243 for (const auto &d : directives) {
244 auto rendered_directive = common::jinja::render_template(
246 if (rendered_directive)
247 ostr << indent(1) << *rendered_directive << '\n';
248 }
249}

◆ generate_metadata()

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

Generate comment with diagram metadata.

Parameters
ostrOutput stream

Definition at line 267 of file generator.h.

268{
270
271 if (config.generate_metadata()) {
272 ostr << '\n'
273 << "%% Generated with clang-uml, version "
274 << clanguml::version::version() << '\n'
275 << "%% LLVM version " << clang::getClangFullVersion() << '\n';
276 }
277}

◆ generate_notes()

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

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

Reimplemented in clanguml::package_diagram::generators::mermaid::generator.

Definition at line 252 of file generator.h.

254{
256
257 for (const auto &decorator : e.decorators()) {
258 auto note = std::dynamic_pointer_cast<decorators::note>(decorator);
259 if (note && note->applies_to_diagram(config.name)) {
260 ostr << indent(1) << "note for " << e.alias() << " \"" << note->text
261 << "\"" << '\n';
262 }
263 }
264}

◆ generate_title()

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

Generate diagram title.

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

Parameters
ostrOutput stream

Definition at line 280 of file generator.h.

281{
283
284 if (config.title) {
285 ostr << "---\n";
286 ostr << "title: " << config.title() << '\n';
287 ostr << "---\n";
288 }
289}

◆ print_debug()

template<typename C , typename D >
void clanguml::common::generators::mermaid::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 292 of file generator.h.

294{
296
297 if (config.debug_mode()) {
298 if (!e.file_relative().empty()) {
299 ostr << "%% " << e.file_relative() << ":" << e.line() << '\n';
300 }
301 else if (!e.file().empty()) {
302 ostr << "%% " << e.file() << ":" << e.line() << '\n';
303 }
304 }
305}

Member Data Documentation

◆ m_generated_aliases

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

Definition at line 173 of file generator.h.


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