BPMN-OS
BPMN for optimization and simulation
Loading...
Searching...
No Matches
erase.h
Go to the documentation of this file.
1#ifndef BPMNOS_Execution_erase_H
2#define BPMNOS_Execution_erase_H
3
4namespace BPMNOS::Execution {
5
6/**
7 * @brief Erase a specific element from a vector of unique pointers.
8 *
9 * This function searches for a specified element pointer in a vector of unique pointers
10 * and removes the corresponding element from the vector.
11 *
12 * @tparam T The type of elements stored in the vector.
13 * @param container The vector from which the element will be removed.
14 * @param elementPtr The pointer to the element that needs to be removed.
15 *
16 * @throws std::logic_error if the specified element is not found in the vector.
17 */
18template<typename T>
19void erase_ptr(std::vector<std::unique_ptr<T>>& container, const T* elementPtr) {
20 auto it = std::find_if(container.begin(), container.end(),
21 [elementPtr](const std::unique_ptr<T>& uniquePtr)
22 {
23 return uniquePtr.get() == elementPtr;
24 }
25 );
26
27 if (it == container.end()) {
28 throw std::logic_error("erase_ptr: cannot find unique pointer to be removed");
29 }
30 container.erase(it);
31}
32
33/**
34 * @brief Erase a specific element from a vector of shared pointers.
35 *
36 * This function searches for a specified element pointer in a vector of shared pointers
37 * and removes the corresponding element from the vector.
38 *
39 * @tparam T The type of elements stored in the vector.
40 * @param container The vector from which the element will be removed.
41 * @param elementPtr The pointer to the element that needs to be removed.
42 *
43 * @throws std::logic_error if the specified element is not found in the vector.
44 */
45template<typename T>
46void erase_ptr(std::vector<std::shared_ptr<T>>& container, const T* elementPtr) {
47 auto it = std::find_if(container.begin(), container.end(),
48 [elementPtr](const std::shared_ptr<T>& sharedPtr)
49 {
50 return sharedPtr.get() == elementPtr;
51 }
52 );
53
54 if (it == container.end()) {
55 throw std::logic_error("erase_ptr: cannot find shared pointer to be removed");
56 }
57 container.erase(it);
58}
59
60/**
61 * @brief Erase a specific element from a vector of pointers.
62 *
63 * This function searches for a specified element pointer in a vector of pointers
64 * and removes the corresponding element from the vector.
65 *
66 * @tparam T The type of elements stored in the vector.
67 * @param container The vector from which the element will be removed.
68 * @param elementPtr The pointer to the element that needs to be removed.
69 *
70 * @throws std::logic_error if the specified element is not found in the vector.
71 */
72template<typename T>
73void erase_ptr(std::vector<T*>& container, const T* elementPtr) {
74 auto it = std::find_if(container.begin(), container.end(),
75 [elementPtr](const T* ptr)
76 {
77 return ptr == elementPtr;
78 }
79 );
80
81 if (it == container.end()) {
82 throw std::logic_error("erase_ptr: cannot find pointer to be removed");
83 }
84 container.erase(it);
85}
86
87/**
88 * @brief Erase a specific element from a set of key-value pairs where the value is a pointer to the element.
89 *
90 * This function searches for a specified element pointer in a set of key-value pairs
91 * and removes the corresponding element from the set.
92 *
93 * @tparam T The type of elements stored in the set.
94 * @param container The set from which the element will be removed.
95 * @param elementPtr The pointer to the element that needs to be removed.
96 *
97 * @throws std::logic_error if the specified element is not found in the set.
98 */
99template<typename K, typename T, typename Comparator>
100void erase_pair(std::set< std::pair<K,T*>, Comparator >& container, const T* elementPtr) {
101 auto it = std::find_if(container.begin(), container.end(),
102 [elementPtr](const std::pair<K,T*>& element)
103 {
104 return element.second == elementPtr;
105 }
106 );
107
108 if (it == container.end()) {
109 throw std::logic_error("erase_pair: cannot find element to be removed");
110 }
111 container.erase(it);
112}
113
114} // namespace BPMNOS::Execution
115#endif // BPMNOS_Execution_erase_H
116
void erase_pair(std::set< std::pair< K, T * >, Comparator > &container, const T *elementPtr)
Erase a specific element from a set of key-value pairs where the value is a pointer to the element.
Definition erase.h:100
void erase_ptr(std::vector< std::unique_ptr< T > > &container, const T *elementPtr)
Erase a specific element from a vector of unique pointers.
Definition erase.h:19