$darkmode
simulation_outcome.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2024 Robert Bosch GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
22 #pragma once
23 
24 #include <map> // for map<>
25 
26 #include <fable/enum.hpp> // for ENUM_SERIALIZATION
27 
28 namespace engine {
29 
33 enum class SimulationOutcome {
34  NoStart,
35  Aborted,
36  Stopped,
37  Failure,
38  Success,
39  Probing,
40 };
41 
42 // If possible, the following exit codes should not be used as they are used
43 // by the Bash shell, among others: 1-2, 126-165, and 255. That leaves us
44 // primarily with the range 3-125, which should suffice for our purposes.
45 // The following exit codes should not be considered stable.
46 #define EXIT_OUTCOME_SUCCESS EXIT_SUCCESS // normally 0
47 #define EXIT_OUTCOME_UNKNOWN EXIT_FAILURE // normally 1
48 #define EXIT_OUTCOME_NOSTART 4 // 0b.....1..
49 #define EXIT_OUTCOME_STOPPED 8 // 0b....1...
50 #define EXIT_OUTCOME_FAILURE 9 // 0b....1..1
51 #define EXIT_OUTCOME_ABORTED 16 // 0b...1....
52 
53 // clang-format off
54 ENUM_SERIALIZATION(SimulationOutcome, ({
55  {SimulationOutcome::Aborted, "aborted"},
56  {SimulationOutcome::NoStart, "no-start"},
57  {SimulationOutcome::Failure, "failure"},
58  {SimulationOutcome::Success, "success"},
59  {SimulationOutcome::Stopped, "stopped"},
60  {SimulationOutcome::Probing, "probing"},
61 }))
62 // clang-format on
63 
64 inline int as_exit_code(SimulationOutcome outcome, bool require_success = true) {
65  switch (outcome) {
66  case SimulationOutcome::Success:
67  return EXIT_OUTCOME_SUCCESS;
68  case SimulationOutcome::Stopped:
69  return (require_success ? EXIT_OUTCOME_STOPPED : EXIT_OUTCOME_SUCCESS);
70  case SimulationOutcome::Aborted:
71  return EXIT_OUTCOME_ABORTED;
72  case SimulationOutcome::NoStart:
73  return EXIT_OUTCOME_NOSTART;
74  case SimulationOutcome::Failure:
75  return EXIT_OUTCOME_FAILURE;
76  case SimulationOutcome::Probing:
77  return EXIT_OUTCOME_SUCCESS;
78  default:
79  return EXIT_OUTCOME_UNKNOWN;
80  }
81 }
82 
83 } // namespace engine
#define ENUM_SERIALIZATION(xType, xMap)
Definition: enum.hpp:51
SimulationOutcome
Definition: simulation_outcome.hpp:33
@ Success
Simulation explicitly concluded with success.
@ Aborted
Simulation aborted due to technical problems or interrupt.
@ Stopped
Simulation concluded, but without valuation.
@ Probing
Simulation started briefly to gather specific information.
@ Failure
Simulation explicitly concluded with failure.
@ NoStart
Simulation unable to start.