bpmn++
A BPMN parser library, written in C++
Loading...
Searching...
No Matches
BaseElement.h
Go to the documentation of this file.
1#ifndef BPMN_BaseElement_H
2#define BPMN_BaseElement_H
3
5#include "Element.h"
6
7namespace BPMN {
8
9class ExtensionElements;
10/**
11 * @brief Base class for all core BPMN elements.
12 *
13 * The class allows to access the underlying XML element and may have associated extension elements.
14 */
15class BaseElement : public Element {
16public:
17 /// @brief Constructs a BaseElement object representing a BPMN element.
19
21
22 /// @brief Id of element.
23 std::string id;
24 std::unique_ptr<ExtensionElements> extensionElements;
25
26 /**
27 * @brief Attempts to return the @ref element in the specified type T.
28 * @return A pointer to casted object or `nullptr` if the cast fails
29 */
30 template<typename T> T* is() {
31 return dynamic_cast<T*>(element);
32 }
33
34 /**
35 * @brief Attempts to return the @ref element in the specified type T.
36 * @return A pointer to casted object or `nullptr` if the cast fails
37 */
38 template<typename T> const T* is() const {
39 return dynamic_cast<const T*>(element);
40 }
41
42 /**
43 * @brief Casts the @ref element to the specified type T.
44 * @return A pointer to casted object
45 * @throws std::runtime_error if the cast fails
46 */
47 template<typename T = XML::bpmn::tBaseElement> T* get() {
48 T* ptr = dynamic_cast<T*>(element);
49 if ( ptr == nullptr ) {
50 throw std::runtime_error("Element: Illegal cast of element '" + (element->id.has_value() ? (std::string)element->id->get().value : "") + "'");
51 }
52 return ptr;
53 }
54
55 /**
56 * @brief Casts the element to the specified type T.
57 * @return A pointer to casted object
58 * @throws std::runtime_error if the cast fails
59 */
60 template<typename T = XML::bpmn::tBaseElement> const T* get() const {
61 const T* ptr = dynamic_cast<const T*>(element);
62 if ( ptr == nullptr ) {
63 throw std::runtime_error("Element: Illegal cast of element" + ( element->id.has_value() ? " '" + (std::string)element->id->get().value + "'" : "" ) );
64 }
65 return ptr;
66 }
67};
68
69} // namespace BPMN
70
71#endif // BPMN_BaseElement_H
Base class for all core BPMN elements.
Definition BaseElement.h:15
T * is()
Attempts to return the element in the specified type T.
Definition BaseElement.h:30
std::unique_ptr< ExtensionElements > extensionElements
Definition BaseElement.h:24
const T * get() const
Casts the element to the specified type T.
Definition BaseElement.h:60
std::string id
Id of element.
Definition BaseElement.h:23
const T * is() const
Attempts to return the element in the specified type T.
Definition BaseElement.h:38
XML::bpmn::tBaseElement * element
Definition BaseElement.h:20
T * get()
Casts the element to the specified type T.
Definition BaseElement.h:47
Abstract base class for all elements in a BPMN model.
Definition Element.h:13
std::optional< std::reference_wrapper< Attribute > > id
Attribute value can be expected to be of type 'std::string'.
The BPMN namespace contains linked classes representing a BPMN model.