Embedded Template Library 1.0
successor.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2021 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_SUCCESSOR_INCLUDED
32#define ETL_SUCCESSOR_INCLUDED
33
34#include "platform.h"
35#include "nullptr.h"
36#include "exception.h"
37#include "error_handler.h"
38#include "file_error_numbers.h"
39
40namespace etl
41{
42 //***************************************************************************
44 //***************************************************************************
46 {
47 public:
48
49 successor_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
50 : exception(reason_, file_name_, line_number_)
51 {
52 }
53 };
54
55 //***************************************************************************
57 //***************************************************************************
59 {
60 public:
61
62 successor_invalid(string_type file_name_, numeric_type line_number_)
63 : etl::successor_exception(ETL_ERROR_TEXT("successor:invalid", ETL_SUCCESSOR_FILE_ID"A"), file_name_, line_number_)
64 {
65 }
66 };
67
68 //***************************************************************************
70 //***************************************************************************
71 template <typename T>
73 {
74 public:
75
76 typedef T successor_type;
77
78 //*************************************************************************
80 //*************************************************************************
82 : p_successor(ETL_NULLPTR)
83 {
84 }
85
86 //*************************************************************************
88 //*************************************************************************
89 successor(successor_type& s)
90 : p_successor(&s)
91 {
92 }
93
94 //*************************************************************************
96 //*************************************************************************
97 void set_successor(successor_type& s)
98 {
99 p_successor = &s;
100 }
101
102#if ETL_CPP11_SUPPORTED
103 //*************************************************************************
105 //*************************************************************************
106 template <typename... TSuccessors>
107 void set_successor(successor_type& s, TSuccessors&... rest)
108 {
109 set_successor(s);
110 s.set_successor(rest...);
111 }
112#endif
113
114 //*************************************************************************
116 //*************************************************************************
117 template <typename TSuccessor>
118 void append_successor(TSuccessor& s)
119 {
120 if (has_successor())
121 {
122 get_successor().append_successor(s);
123 }
124 else
125 {
126 set_successor(s);
127 }
128 }
129
130#if ETL_CPP11_SUPPORTED
131 //*************************************************************************
133 //*************************************************************************
134 template <typename TSuccessor, typename... TSuccessors>
135 void append_successor(TSuccessor& s, TSuccessors&... rest)
136 {
137 if (has_successor())
138 {
139 get_successor().append_successor(s);
140 get_successor().append_successor(rest...);
141 }
142 else
143 {
144 set_successor(s);
145 append_successor(rest...);
146 }
147 }
148#endif
149
150 //*************************************************************************
152 //*************************************************************************
154 {
155 p_successor = ETL_NULLPTR;
156 }
157
158 //*************************************************************************
160 //*************************************************************************
162 {
163 if (has_successor())
164 {
165 get_successor().clear_successor_chain();
167 }
168 }
169
170 //*************************************************************************
173 //*************************************************************************
174 successor_type& get_successor() const
175 {
177
178 return *p_successor;
179 }
180
181 //*************************************************************************
183 //*************************************************************************
184 bool has_successor() const
185 {
186 return (p_successor != ETL_NULLPTR);
187 }
188
189 private:
190
191 successor_type* p_successor;
192 };
193}
194
195#endif
Exception for the successor.
Definition: successor.h:46
Invalid exception for successor.
Definition: successor.h:59
Adds successor traits to a class.
Definition: successor.h:73
void clear_successor()
Clear the successor.
Definition: successor.h:153
void set_successor(successor_type &s)
Set the successor.
Definition: successor.h:97
bool has_successor() const
Does this have a successor?
Definition: successor.h:184
void clear_successor_chain()
Clear the successor chain.
Definition: successor.h:161
void append_successor(TSuccessor &s)
Append a successor.
Definition: successor.h:118
successor_type & get_successor() const
Definition: successor.h:174
successor(successor_type &s)
Construct from a successor type.
Definition: successor.h:89
successor()
Default constructor.
Definition: successor.h:81
#define ETL_ASSERT(b, e)
Definition: error_handler.h:316
ETL_CONSTEXPR exception(string_type reason_, string_type, numeric_type line_)
Constructor.
Definition: exception.h:69
Definition: exception.h:47
bitset_ext
Definition: absolute.h:38