Skip to content

Commit 5bae17b

Browse files
committed
view: stop using gotos
1 parent d88aba1 commit 5bae17b

File tree

2 files changed

+109
-105
lines changed

2 files changed

+109
-105
lines changed

cmake/StaticAnalyzers.cmake

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ macro(cloysterhpc_enable_cppcheck WARNINGS_AS_ERRORS CPPCHECK_OPTIONS)
3636
"${CMAKE_CXX_STANDARD}"
3737
STREQUAL
3838
"")
39-
set(CMAKE_CXX_CPPCHECK ${CMAKE_CXX_CPPCHECK} --std=c++${CMAKE_CXX_STANDARD})
39+
40+
if ("${CMAKE_CXX_STANDARD}" STREQUAL "23")
41+
# cppcheck does not support c++23 for some reason (in the cloyster dev server)
42+
set(CMAKE_CXX_CPPCHECK ${CMAKE_CXX_CPPCHECK} --std=c++20)
43+
else()
44+
set(CMAKE_CXX_CPPCHECK ${CMAKE_CXX_CPPCHECK} --std=c++${CMAKE_CXX_STANDARD})
45+
endif()
4046
endif()
4147
if(${WARNINGS_AS_ERRORS})
4248
list(APPEND CMAKE_CXX_CPPCHECK --error-exitcode=2)

include/cloysterhpc/view/newt.h

Lines changed: 102 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -166,47 +166,54 @@ class Newt : public View {
166166

167167
auto cStrings = convertToNewtList(tempStrings);
168168

169-
// goto implementation
170-
question:
171-
returnValue = newtWinMenu(const_cast<char*>(title),
172-
const_cast<char*>(message), m_suggestedWidth, m_flexDown, m_flexUp,
173-
m_maxListHeight, const_cast<char**>(cStrings.data()), &selector,
174-
const_cast<char*>(TUIText::Buttons::ok),
175-
const_cast<char*>(TUIText::Buttons::cancel),
176-
const_cast<char*>(TUIText::Buttons::add),
177-
const_cast<char*>(TUIText::Buttons::remove),
178-
const_cast<char*>(TUIText::Buttons::help), nullptr);
169+
bool stay = true;
170+
while (stay) {
171+
returnValue = newtWinMenu(const_cast<char*>(title),
172+
const_cast<char*>(message), m_suggestedWidth, m_flexDown,
173+
m_flexUp, m_maxListHeight, const_cast<char**>(cStrings.data()),
174+
&selector, const_cast<char*>(TUIText::Buttons::ok),
175+
const_cast<char*>(TUIText::Buttons::cancel),
176+
const_cast<char*>(TUIText::Buttons::add),
177+
const_cast<char*>(TUIText::Buttons::remove),
178+
const_cast<char*>(TUIText::Buttons::help), nullptr);
179179

180-
switch (returnValue) {
181-
case 0:
182-
/* F12 is pressed, and we don't care; continue to case 1 */
183-
case 1:
184-
return tempStrings;
185-
case 2:
186-
abort();
187-
break;
188-
case 3: { // add
189-
bool ret = addCallback(tempStrings);
190-
if (ret) {
191-
cStrings = convertToNewtList(tempStrings);
192-
goto question;
193-
} else {
180+
stay = false;
181+
182+
switch (returnValue) {
183+
case 0:
184+
/* F12 is pressed, and we don't care; continue to case 1 */
185+
case 1:
194186
return tempStrings;
187+
case 2:
188+
abort();
189+
break;
190+
case 3: { // add
191+
bool ret = addCallback(tempStrings);
192+
if (ret) {
193+
cStrings = convertToNewtList(tempStrings);
194+
stay = true;
195+
} else {
196+
return tempStrings;
197+
}
198+
break;
195199
}
200+
case 4: // remove
201+
if (selector >= 0 && selector < cStrings.size()) {
202+
tempStrings.erase(tempStrings.begin() + selector);
203+
cStrings = convertToNewtList(tempStrings);
204+
}
205+
stay = true;
206+
break;
207+
case 5:
208+
this->helpMessage(helpMessage);
209+
stay = true;
210+
break;
211+
default:
212+
__builtin_unreachable();
196213
}
197-
case 4: // remove
198-
if (selector >= 0 && selector < cStrings.size()) {
199-
tempStrings.erase(tempStrings.begin() + selector);
200-
cStrings = convertToNewtList(tempStrings);
201-
}
202-
203-
goto question;
204-
case 5:
205-
this->helpMessage(helpMessage);
206-
goto question;
207-
default:
208-
__builtin_unreachable();
209214
}
215+
216+
__builtin_unreachable();
210217
}
211218

212219
template <std::ranges::range T>
@@ -226,55 +233,35 @@ class Newt : public View {
226233
// Newt expects a NULL terminated array of C style strings
227234
std::vector<const char*> cStrings = convertToNewtList(tempStrings);
228235

229-
#if 1
230-
// goto implementation
231-
question:
232-
returnValue = newtWinMenu(const_cast<char*>(title),
233-
const_cast<char*>(message), m_suggestedWidth, m_flexDown, m_flexUp,
234-
m_maxListHeight, const_cast<char**>(cStrings.data()), &selector,
235-
const_cast<char*>(TUIText::Buttons::ok),
236-
const_cast<char*>(TUIText::Buttons::cancel),
237-
const_cast<char*>(TUIText::Buttons::help), nullptr);
236+
bool stay = true;
238237

239-
switch (returnValue) {
240-
case 0:
241-
/* F12 is pressed, and we don't care; continue to case 1 */
242-
case 1:
243-
return tempStrings[boost::lexical_cast<std::size_t>(selector)];
244-
case 2:
245-
abort();
246-
break;
247-
case 3:
248-
this->helpMessage(helpMessage);
249-
goto question;
250-
default:
251-
__builtin_unreachable();
252-
}
253-
#else
254-
// gotoless implementation
255-
for (;;) {
238+
while (stay) {
256239
returnValue = newtWinMenu(const_cast<char*>(title),
257-
const_cast<char*>(message), m_suggestedWidth, m_flexUp,
258-
m_flexDown, m_maxListHeight,
259-
const_cast<char**>(cStrings.data()), &selector,
260-
const_cast<char*>(TUIText::Buttons::ok),
240+
const_cast<char*>(message), m_suggestedWidth, m_flexDown,
241+
m_flexUp, m_maxListHeight, const_cast<char**>(cStrings.data()),
242+
&selector, const_cast<char*>(TUIText::Buttons::ok),
261243
const_cast<char*>(TUIText::Buttons::cancel),
262-
const_cast<char*>(TUIText::Buttons::help), NULL);
244+
const_cast<char*>(TUIText::Buttons::help), nullptr);
245+
stay = false;
263246

264247
switch (returnValue) {
265248
case 0:
266249
/* F12 is pressed, and we don't care; continue to case 1 */
267250
case 1:
268-
return items[selector];
251+
return tempStrings[boost::lexical_cast<std::size_t>(
252+
selector)];
269253
case 2:
270-
abortInstall();
254+
abort();
255+
break;
271256
case 3:
272257
this->helpMessage(helpMessage);
273-
continue;
258+
stay = true;
259+
break;
260+
default:
261+
__builtin_unreachable();
274262
}
275-
break; // for (;;)
276263
}
277-
#endif
264+
278265
__builtin_unreachable();
279266
}
280267

@@ -328,40 +315,51 @@ class Newt : public View {
328315

329316
T returnArray;
330317

331-
question:
332-
returnValue = newtWinEntries(const_cast<char*>(title),
333-
const_cast<char*>(message), m_suggestedWidth, m_flexDown, m_flexUp,
334-
m_dataWidth, field.get(), const_cast<char*>(TUIText::Buttons::ok),
335-
const_cast<char*>(TUIText::Buttons::cancel),
336-
const_cast<char*>(TUIText::Buttons::help), nullptr);
318+
bool stay = true;
337319

338-
switch (returnValue) {
339-
case 0:
340-
/* F12 is pressed, and we don't care; continue to case 1 */
341-
case 1:
342-
// TODO: The view should now check for this, it's a passive view
343-
if (hasEmptyField(field.get()))
344-
goto question;
345-
346-
// FIXME: We forgot that we should return size_t sometimes and
347-
// that was triggering an exception on the presenter, so
348-
// basically the std::variant is useless here, we always
349-
// return std:string.
350-
for (std::size_t i = 0; field[i].text; i++) {
351-
returnArray[i] = std::make_pair<std::string, std::string>(
352-
field[i].text, *field[i].value);
353-
}
320+
while (stay) {
321+
returnValue = newtWinEntries(const_cast<char*>(title),
322+
const_cast<char*>(message), m_suggestedWidth, m_flexDown,
323+
m_flexUp, m_dataWidth, field.get(),
324+
const_cast<char*>(TUIText::Buttons::ok),
325+
const_cast<char*>(TUIText::Buttons::cancel),
326+
const_cast<char*>(TUIText::Buttons::help), nullptr);
327+
stay = false;
354328

355-
return returnArray;
356-
case 2:
357-
abort();
358-
break;
359-
case 3:
360-
this->helpMessage(helpMessage);
361-
goto question;
362-
default:
363-
throw std::runtime_error(
364-
"Invalid return value from fields on newt library");
329+
switch (returnValue) {
330+
case 0:
331+
/* F12 is pressed, and we don't care; continue to case 1 */
332+
case 1:
333+
// TODO: The view should now check for this, it's a passive
334+
// view
335+
if (hasEmptyField(field.get())) {
336+
stay = true;
337+
continue;
338+
}
339+
340+
// FIXME: We forgot that we should return size_t sometimes
341+
// and
342+
// that was triggering an exception on the presenter,
343+
// so basically the std::variant is useless here, we
344+
// always return std:string.
345+
for (std::size_t i = 0; field[i].text; i++) {
346+
returnArray[i]
347+
= std::make_pair<std::string, std::string>(
348+
field[i].text, *field[i].value);
349+
}
350+
351+
return returnArray;
352+
case 2:
353+
abort();
354+
break;
355+
case 3:
356+
this->helpMessage(helpMessage);
357+
stay = true;
358+
break;
359+
default:
360+
throw std::runtime_error(
361+
"Invalid return value from fields on newt library");
362+
}
365363
}
366364
throw std::runtime_error("Invalid return path on newt library");
367365
}

0 commit comments

Comments
 (0)