0.6.0
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 44 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.
 
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

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 52 of file generator.h.

54 , model_{model}
55 {
57 init_env();
58 }

◆ ~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 79 of file generator.h.

79{ return config_; }

◆ context()

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

Definition at line 152 of file generator.h.

153{
154 return m_context;
155}

◆ env()

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

Definition at line 158 of file generator.h.

159{
160 return m_env;
161}

◆ 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::graphml::generator< ConfigType, DiagramType >, 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 274 of file generator.h.

276{
277 if (sl.file_relative().empty()) {
278 return config().generate_links().get_link_pattern(sl.file());
279 }
280
281 return config().generate_links().get_link_pattern(sl.file_relative());
282}

◆ 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 286 of file generator.h.

288{
289 if (sl.file_relative().empty()) {
290 return config().generate_links().get_tooltip_pattern(sl.file());
291 }
292
293 return config().generate_links().get_tooltip_pattern(sl.file_relative());
294}

◆ init_context()

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

Initialize diagram Jinja context.

Definition at line 135 of file generator.h.

136{
138
139 if (config.git) {
140 m_context["git"]["branch"] = config.git().branch;
141 m_context["git"]["revision"] = config.git().revision;
142 m_context["git"]["commit"] = config.git().commit;
143 m_context["git"]["toplevel"] = config.git().toplevel;
144 }
145
146 if (config.user_data) {
147 m_context["user_data"] = config.user_data();
148 }
149}

◆ init_env()

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

Definition at line 163 of file generator.h.

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

◆ 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 86 of file generator.h.

86{ return model_; }

◆ render_link() [1/2]

template<typename C , typename D >
std::optional< std::string > clanguml::common::generators::generator< C, D >::render_link ( const common::model::diagram_element e) const

Definition at line 297 of file generator.h.

299{
302
303 if (e.file().empty() && e.file_relative().empty())
304 return {};
305
306 auto maybe_link_pattern = generators::generator<C, D>::get_link_pattern(e);
307
308 if (!maybe_link_pattern)
309 return {};
310
311 const auto &[link_prefix, link_pattern] = *maybe_link_pattern;
312
313 inja::json ec = jinja::element_context<common::model::diagram_element>(
315
316 make_context_source_relative(ec, link_prefix);
317
318 return render_template(
319 generators::generator<C, D>::env(), ec, link_pattern);
320}

◆ render_link() [2/2]

template<typename C , typename D >
std::optional< std::string > clanguml::common::generators::generator< C, D >::render_link ( const common::model::relationship e) const

Definition at line 323 of file generator.h.

325{
328
329 if (e.file().empty() && e.file_relative().empty())
330 return {};
331
332 auto maybe_link_pattern = generators::generator<C, D>::get_link_pattern(e);
333
334 if (!maybe_link_pattern)
335 return {};
336
337 const auto &[link_prefix, link_pattern] = *maybe_link_pattern;
338
339 inja::json ec = jinja::element_context<common::model::relationship>(
341
342 make_context_source_relative(ec, link_prefix);
343
344 return render_template(
345 generators::generator<C, D>::env(), ec, link_pattern);
346}

◆ render_tooltip() [1/2]

template<typename C , typename D >
std::optional< std::string > clanguml::common::generators::generator< C, D >::render_tooltip ( const common::model::diagram_element e) const

Definition at line 349 of file generator.h.

351{
354
355 if (e.file().empty() && e.file_relative().empty())
356 return {};
357
358 auto maybe_tooltip_pattern =
360
361 if (!maybe_tooltip_pattern)
362 return {};
363
364 const auto &[tooltip_prefix, tooltip_pattern] = *maybe_tooltip_pattern;
365
366 inja::json ec = jinja::element_context<common::model::diagram_element>(
368
369 make_context_source_relative(ec, tooltip_prefix);
370
371 return render_template(
372 generators::generator<C, D>::env(), ec, tooltip_pattern);
373}

◆ render_tooltip() [2/2]

template<typename C , typename D >
std::optional< std::string > clanguml::common::generators::generator< C, D >::render_tooltip ( const common::model::relationship e) const

Definition at line 376 of file generator.h.

378{
381
382 if (e.file().empty() && e.file_relative().empty())
383 return {};
384
385 auto maybe_tooltip_pattern =
387
388 if (!maybe_tooltip_pattern)
389 return {};
390
391 const auto &[tooltip_prefix, tooltip_pattern] = *maybe_tooltip_pattern;
392
393 inja::json ec = jinja::element_context<common::model::relationship>(
395
396 make_context_source_relative(ec, tooltip_prefix);
397
398 return render_template(
399 generators::generator<C, D>::env(), ec, tooltip_pattern);
400}

◆ update_context()

template<typename C , typename D >
template void clanguml::common::generators::generator< ConfigType, DiagramType >::update_context ( ) const

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 10 of file generator.cc.

11{
12 m_context["diagram"] = common::jinja::diagram_context<D>(model());
13}

Member Data Documentation

◆ config_

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

Definition at line 131 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 127 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 128 of file generator.h.

◆ model_

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

Definition at line 132 of file generator.h.


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