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
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
20 /// @brief Destructor.
21 /// @note Declared here and defined out-of-line (in BaseElement.cpp, where
22 /// ExtensionElements is a complete type) so that the destructor of the
23 /// std::unique_ptr<ExtensionElements> member is not instantiated against
24 /// the forward-declared type. Clang requires the complete type at the point
25 /// of instantiation, so an implicit destructor here fails to compile.
27
29
30 /// @brief Id of element.
31 std::string id;
32 std::unique_ptr<ExtensionElements> extensionElements;
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> T* is() {
39 return dynamic_cast<T*>(element);
40 }
41
42 /**
43 * @brief Attempts to return the @ref element in the specified type T.
44 * @return A pointer to casted object or `nullptr` if the cast fails
45 */
46 template<typename T> const T* is() const {
47 return dynamic_cast<const T*>(element);
48 }
49
50 /**
51 * @brief Casts the @ref element to the specified type T.
52 * @return A pointer to casted object
53 * @throws std::runtime_error if the cast fails
54 */
55 template<typename T = XML::bpmn::tBaseElement> T* get() {
56 T* ptr = dynamic_cast<T*>(element);
57 if ( ptr == nullptr ) {
58 throw std::runtime_error("Element: Illegal cast of element '" + (element->id.has_value() ? (std::string)element->id->get().value : "") + "'");
59 }
60 return ptr;
61 }
62
63 /**
64 * @brief Casts the element to the specified type T.
65 * @return A pointer to casted object
66 * @throws std::runtime_error if the cast fails
67 */
68 template<typename T = XML::bpmn::tBaseElement> const T* get() const {
69 const T* ptr = dynamic_cast<const T*>(element);
70 if ( ptr == nullptr ) {
71 throw std::runtime_error("Element: Illegal cast of element" + ( element->id.has_value() ? " '" + (std::string)element->id->get().value + "'" : "" ) );
72 }
73 return ptr;
74 }
75};
76
77} // namespace BPMN
78
79#endif // BPMN_BaseElement_H
T * is()
Attempts to return the element in the specified type T.
Definition BaseElement.h:38
std::unique_ptr< ExtensionElements > extensionElements
Definition BaseElement.h:32
const T * get() const
Casts the element to the specified type T.
Definition BaseElement.h:68
std::string id
Id of element.
Definition BaseElement.h:31
const T * is() const
Attempts to return the element in the specified type T.
Definition BaseElement.h:46
BaseElement(XML::bpmn::tBaseElement *element)
Constructs a BaseElement object representing a BPMN element.
~BaseElement()
Destructor.
XML::bpmn::tBaseElement * element
Definition BaseElement.h:28
T * get()
Casts the element to the specified type T.
Definition BaseElement.h:55
Abstract base class for all elements in a BPMN model.
Definition Element.h:13
Base class for extension elements that may be provided for a BPMN element.
The BPMN namespace contains linked classes representing a BPMN model.