bpmn++
A BPMN parser library, written in C++
Loading...
Searching...
No Matches
Element.h
Go to the documentation of this file.
1#ifndef BPMN_Element_H
2#define BPMN_Element_H
3
5
6namespace BPMN {
7
8/**
9 * @brief Abstract base class for all elements in a BPMN model.
10 *
11 * The class provides conveniance methods to cast the element to concrete types.
12 */
13class Element {
14public:
15 virtual ~Element() = default;
16
17 /**
18 * @brief Attempts to cast the element to the specified type T.
19 * @return A pointer to casted object or `nullptr` if the cast fails
20 */
21 template<typename T> T* represents() {
22 return dynamic_cast<T*>(this);
23 }
24
25 /**
26 * @brief Attempts to cast the element to the specified type T.
27 * @return A pointer to casted object or `nullptr` if the cast fails
28 */
29 template<typename T> const T* represents() const {
30 return dynamic_cast<const T*>(this);
31 }
32
33 /**
34 * @brief Casts the element to the specified type T.
35 * @return A pointer to casted object
36 * @throws std::runtime_error if cast fails
37 */
38 template<typename T> T* as() {
39 T* ptr = dynamic_cast<T*>(this);
40 if ( ptr == nullptr ) {
41 throw std::runtime_error("Element: Illegal cast of element");
42 }
43 return ptr;
44 }
45
46 /**
47 * @brief Casts the element to the specified type T.
48 * @return A pointer to casted object
49 * @throws std::runtime_error if cast fails
50 */
51 template<typename T> const T* as() const {
52 const T* ptr = dynamic_cast<const T*>(this);
53 if ( ptr == nullptr ) {
54 throw std::runtime_error("Element: Illegal cast of element");
55 }
56 return ptr;
57 }
58};
59
60} // namespace BPMN
61
62#endif // BPMN_Element_H
Abstract base class for all elements in a BPMN model.
Definition Element.h:13
const T * represents() const
Attempts to cast the element to the specified type T.
Definition Element.h:29
const T * as() const
Casts the element to the specified type T.
Definition Element.h:51
T * as()
Casts the element to the specified type T.
Definition Element.h:38
virtual ~Element()=default
T * represents()
Attempts to cast the element to the specified type T.
Definition Element.h:21
The BPMN namespace contains linked classes representing a BPMN model.