diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-12-03 17:13:35 +0100 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-12-03 17:13:35 +0100 |
commit | d8580e14fec64dd5bf3dd492a4811b290cbe4aec (patch) | |
tree | 8dcd3117f7427671e34509badd4c08957d8f0914 /src/cancel.cpp | |
parent | 307fd830eb168005a3ba3d557343284814757eff (diff) | |
download | lanes-d8580e14fec64dd5bf3dd492a4811b290cbe4aec.tar.gz lanes-d8580e14fec64dd5bf3dd492a4811b290cbe4aec.tar.bz2 lanes-d8580e14fec64dd5bf3dd492a4811b290cbe4aec.zip |
Internal rework of an enum bad practice usage
Diffstat (limited to 'src/cancel.cpp')
-rw-r--r-- | src/cancel.cpp | 50 |
1 files changed, 26 insertions, 24 deletions
diff --git a/src/cancel.cpp b/src/cancel.cpp index 31656c4..73ee6f3 100644 --- a/src/cancel.cpp +++ b/src/cancel.cpp | |||
@@ -81,25 +81,24 @@ CancelRequest CheckCancelRequest(lua_State* const L_) | |||
81 | // ################################################################################################# | 81 | // ################################################################################################# |
82 | // ################################################################################################# | 82 | // ################################################################################################# |
83 | 83 | ||
84 | CancelOp WhichCancelOp(std::string_view const& opString_) | 84 | static std::optional<CancelOp> WhichCancelOp(std::string_view const& opString_) |
85 | { | 85 | { |
86 | auto _op{ CancelOp::Invalid }; | 86 | if (opString_ == "soft") { |
87 | if (opString_ == "hard") { | 87 | return std::make_optional<CancelOp>(CancelRequest::Soft, LuaHookMask::None); |
88 | _op = CancelOp::Hard; | 88 | } else if (opString_ == "hard") { |
89 | } else if (opString_ == "soft") { | 89 | return std::make_optional<CancelOp>(CancelRequest::Hard, LuaHookMask::None); |
90 | _op = CancelOp::Soft; | ||
91 | } else if (opString_== "call") { | 90 | } else if (opString_== "call") { |
92 | _op = CancelOp::MaskCall; | 91 | return std::make_optional<CancelOp>(CancelRequest::Hard, LuaHookMask::Call); |
93 | } else if (opString_ == "ret") { | 92 | } else if (opString_ == "ret") { |
94 | _op = CancelOp::MaskRet; | 93 | return std::make_optional<CancelOp>(CancelRequest::Hard, LuaHookMask::Ret); |
95 | } else if (opString_ == "line") { | 94 | } else if (opString_ == "line") { |
96 | _op = CancelOp::MaskLine; | 95 | return std::make_optional<CancelOp>(CancelRequest::Hard, LuaHookMask::Line); |
97 | } else if (opString_ == "count") { | 96 | } else if (opString_ == "count") { |
98 | _op = CancelOp::MaskCount; | 97 | return std::make_optional<CancelOp>(CancelRequest::Hard, LuaHookMask::Count); |
99 | } else if (opString_ == "all") { | 98 | } else if (opString_ == "all") { |
100 | _op = CancelOp::MaskAll; | 99 | return std::make_optional<CancelOp>(CancelRequest::Hard, LuaHookMask::All); |
101 | } | 100 | } |
102 | return _op; | 101 | return std::nullopt; |
103 | } | 102 | } |
104 | 103 | ||
105 | // ################################################################################################# | 104 | // ################################################################################################# |
@@ -109,14 +108,14 @@ static CancelOp WhichCancelOp(lua_State* const L_, StackIndex const idx_) | |||
109 | { | 108 | { |
110 | if (luaG_type(L_, idx_) == LuaType::STRING) { | 109 | if (luaG_type(L_, idx_) == LuaType::STRING) { |
111 | std::string_view const _str{ luaG_tostring(L_, idx_) }; | 110 | std::string_view const _str{ luaG_tostring(L_, idx_) }; |
112 | CancelOp _op{ WhichCancelOp(_str) }; | 111 | auto const _op{ WhichCancelOp(_str) }; |
113 | lua_remove(L_, idx_); // argument is processed, remove it | 112 | lua_remove(L_, idx_); // argument is processed, remove it |
114 | if (_op == CancelOp::Invalid) { | 113 | if (!_op.has_value()) { |
115 | raise_luaL_error(L_, "invalid hook option %s", _str.data()); | 114 | raise_luaL_error(L_, "Invalid cancel option %s", _str.data()); |
116 | } | 115 | } |
117 | return _op; | 116 | return _op.value(); |
118 | } | 117 | } |
119 | return CancelOp::Hard; | 118 | return CancelOp{ CancelRequest::Hard, LuaHookMask::None }; |
120 | } | 119 | } |
121 | 120 | ||
122 | // ################################################################################################# | 121 | // ################################################################################################# |
@@ -133,7 +132,7 @@ static CancelOp WhichCancelOp(lua_State* const L_, StackIndex const idx_) | |||
133 | // | 132 | // |
134 | LUAG_FUNC(cancel_test) | 133 | LUAG_FUNC(cancel_test) |
135 | { | 134 | { |
136 | CancelRequest _test{ CheckCancelRequest(L_) }; | 135 | CancelRequest const _test{ CheckCancelRequest(L_) }; |
137 | lua_pushboolean(L_, _test != CancelRequest::None); | 136 | lua_pushboolean(L_, _test != CancelRequest::None); |
138 | return 1; | 137 | return 1; |
139 | } | 138 | } |
@@ -146,14 +145,17 @@ LUAG_FUNC(lane_cancel) | |||
146 | Lane* const _lane{ ToLane(L_, StackIndex{ 1 }) }; | 145 | Lane* const _lane{ ToLane(L_, StackIndex{ 1 }) }; |
147 | CancelOp const _op{ WhichCancelOp(L_, StackIndex{ 2 }) }; // this removes the cancel_op string from the stack | 146 | CancelOp const _op{ WhichCancelOp(L_, StackIndex{ 2 }) }; // this removes the cancel_op string from the stack |
148 | 147 | ||
149 | int _hook_count{ 0 }; | 148 | int const _hook_count = std::invoke([_op, L_]() { |
150 | if (_op > CancelOp::Soft) { // hook is requested | 149 | if (_op.hookMask == LuaHookMask::None) { |
151 | _hook_count = static_cast<int>(luaL_checkinteger(L_, 2)); | 150 | return 0; |
151 | } | ||
152 | auto const _hook_count{ static_cast<int>(luaL_checkinteger(L_, 2)) }; | ||
152 | lua_remove(L_, 2); // argument is processed, remove it | 153 | lua_remove(L_, 2); // argument is processed, remove it |
153 | if (_hook_count < 1) { | 154 | if (_hook_count < 1) { |
154 | raise_luaL_error(L_, "hook count cannot be < 1"); | 155 | raise_luaL_error(L_, "Hook count cannot be < 1"); |
155 | } | 156 | } |
156 | } | 157 | return _hook_count; |
158 | }); | ||
157 | 159 | ||
158 | std::chrono::time_point<std::chrono::steady_clock> _until{ std::chrono::time_point<std::chrono::steady_clock>::max() }; | 160 | std::chrono::time_point<std::chrono::steady_clock> _until{ std::chrono::time_point<std::chrono::steady_clock>::max() }; |
159 | if (luaG_type(L_, StackIndex{ 2 }) == LuaType::NUMBER) { // we don't want to use lua_isnumber() because of autocoercion | 161 | if (luaG_type(L_, StackIndex{ 2 }) == LuaType::NUMBER) { // we don't want to use lua_isnumber() because of autocoercion |
@@ -169,7 +171,7 @@ LUAG_FUNC(lane_cancel) | |||
169 | } | 171 | } |
170 | 172 | ||
171 | // we wake by default in "hard" mode (remember that hook is hard too), but this can be turned off if desired | 173 | // we wake by default in "hard" mode (remember that hook is hard too), but this can be turned off if desired |
172 | WakeLane _wake_lane{ _op != CancelOp::Soft ? WakeLane::Yes : WakeLane::No }; | 174 | WakeLane _wake_lane{ (_op.mode == CancelRequest::Hard) ? WakeLane::Yes : WakeLane::No }; |
173 | if (lua_gettop(L_) >= 2) { | 175 | if (lua_gettop(L_) >= 2) { |
174 | if (!lua_isboolean(L_, 2)) { | 176 | if (!lua_isboolean(L_, 2)) { |
175 | raise_luaL_error(L_, "wake_lane argument is not a boolean"); | 177 | raise_luaL_error(L_, "wake_lane argument is not a boolean"); |