aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2024-05-21 08:57:03 +0200
committerBenoit Germain <benoit.germain@ubisoft.com>2024-05-21 08:57:03 +0200
commite972ee3b65bc85dbee8e1e7f74594490037dcb67 (patch)
treec3c7021b1cd66d359a071a38d7397f34db451ef3 /src
parente17ab4ffe5b675b0163368cd893d2e703c874675 (diff)
downloadlanes-e972ee3b65bc85dbee8e1e7f74594490037dcb67.tar.gz
lanes-e972ee3b65bc85dbee8e1e7f74594490037dcb67.tar.bz2
lanes-e972ee3b65bc85dbee8e1e7f74594490037dcb67.zip
More string_view
Diffstat (limited to 'src')
-rw-r--r--src/cancel.cpp2
-rw-r--r--src/lane.cpp38
-rw-r--r--src/lane.h9
-rw-r--r--src/tracker.cpp2
4 files changed, 26 insertions, 25 deletions
diff --git a/src/cancel.cpp b/src/cancel.cpp
index f093905..2fdd1ef 100644
--- a/src/cancel.cpp
+++ b/src/cancel.cpp
@@ -249,7 +249,7 @@ LUAG_FUNC(thread_cancel)
249 249
250 case CancelResult::Cancelled: 250 case CancelResult::Cancelled:
251 lua_pushboolean(L_, 1); // true 251 lua_pushboolean(L_, 1); // true
252 _lane->pushThreadStatus(L_); // true status 252 std::ignore = _lane->pushThreadStatus(L_); // true status
253 break; 253 break;
254 } 254 }
255 STACK_CHECK(L_, 2); 255 STACK_CHECK(L_, 2);
diff --git a/src/lane.cpp b/src/lane.cpp
index e838b7d..d68da76 100644
--- a/src/lane.cpp
+++ b/src/lane.cpp
@@ -216,7 +216,7 @@ static int thread_index_number(lua_State* L_)
216 // this is an internal error, we probably never get here 216 // this is an internal error, we probably never get here
217 lua_settop(L_, 0); // L_: 217 lua_settop(L_, 0); // L_:
218 lua_pushliteral(L_, "Unexpected status: "); // L_: "Unexpected status: " 218 lua_pushliteral(L_, "Unexpected status: "); // L_: "Unexpected status: "
219 _lane->pushThreadStatus(L_); // L_: "Unexpected status: " "<status>" 219 std::ignore = _lane->pushThreadStatus(L_); // L_: "Unexpected status: " "<status>"
220 lua_concat(L_, 2); // L_: "Unexpected status: <status>" 220 lua_concat(L_, 2); // L_: "Unexpected status: <status>"
221 raise_lua_error(L_); 221 raise_lua_error(L_);
222 222
@@ -300,14 +300,14 @@ static int thread_index_string(lua_State* L_)
300 Lane* const _lane{ ToLane(L_, kSelf) }; 300 Lane* const _lane{ ToLane(L_, kSelf) };
301 LUA_ASSERT(L_, lua_gettop(L_) == 2); // L_: lane "key" 301 LUA_ASSERT(L_, lua_gettop(L_) == 2); // L_: lane "key"
302 302
303 char const* const _keystr{ lua_tostring(L_, kKey) }; 303 std::string_view const _keystr{ lua_tostringview(L_, kKey) };
304 lua_settop(L_, 2); // keep only our original arguments on the stack 304 lua_settop(L_, 2); // keep only our original arguments on the stack
305 if (strcmp(_keystr, "status") == 0) { 305 if (_keystr == "status") {
306 _lane->pushThreadStatus(L_); // L_: lane "key" "<status>" 306 std::ignore = _lane->pushThreadStatus(L_); // L_: lane "key" "<status>"
307 return 1; 307 return 1;
308 } 308 }
309 if (strcmp(_keystr, "error_trace_level") == 0) { 309 if (_keystr == "error_trace_level") {
310 _lane->pushErrorTraceLevel(L_); // L_: lane "key" "<level>" 310 std::ignore = _lane->pushErrorTraceLevel(L_); // L_: lane "key" "<level>"
311 return 1; 311 return 1;
312 } 312 }
313 // return self.metatable[key] 313 // return self.metatable[key]
@@ -316,7 +316,7 @@ static int thread_index_string(lua_State* L_)
316 lua_rawget(L_, -2); // L_: mt value 316 lua_rawget(L_, -2); // L_: mt value
317 // only "cancel" and "join" are registered as functions, any other string will raise an error 317 // only "cancel" and "join" are registered as functions, any other string will raise an error
318 if (!lua_iscfunction(L_, -1)) { 318 if (!lua_iscfunction(L_, -1)) {
319 raise_luaL_error(L_, "can't index a lane with '%s'", _keystr); 319 raise_luaL_error(L_, "can't index a lane with '%s'", _keystr.data());
320 } 320 }
321 return 1; 321 return 1;
322} 322}
@@ -833,9 +833,9 @@ void Lane::changeDebugName(int nameIdx_)
833// / "error" finished at an error, error value is there 833// / "error" finished at an error, error value is there
834// / "cancelled" execution cancelled by M (state gone) 834// / "cancelled" execution cancelled by M (state gone)
835// 835//
836[[nodiscard]] char const* Lane::errorTraceLevelString() const 836[[nodiscard]] std::string_view Lane::errorTraceLevelString() const
837{ 837{
838 char const* const _str{ 838 std::string_view const _str{
839 (errorTraceLevel == ErrorTraceLevel::Minimal) ? "minimal" : 839 (errorTraceLevel == ErrorTraceLevel::Minimal) ? "minimal" :
840 (errorTraceLevel == ErrorTraceLevel::Basic) ? "basic" : 840 (errorTraceLevel == ErrorTraceLevel::Basic) ? "basic" :
841 (errorTraceLevel == ErrorTraceLevel::Extended) ? "extended" : 841 (errorTraceLevel == ErrorTraceLevel::Extended) ? "extended" :
@@ -890,22 +890,22 @@ void Lane::PushMetatable(lua_State* L_)
890 890
891// ################################################################################################# 891// #################################################################################################
892 892
893void Lane::pushThreadStatus(lua_State* L_) const 893[[nodiscard]] std::string_view Lane::pushThreadStatus(lua_State* L_) const
894{ 894{
895 char const* const _str{ threadStatusString() }; 895 std::string_view const _str{ threadStatusString() };
896 LUA_ASSERT(L_, _str); 896 LUA_ASSERT(L_, !_str.empty());
897 897
898 lua_pushstring(L_, _str); 898 return lua_pushstringview(L_, _str);
899} 899}
900 900
901// ################################################################################################# 901// #################################################################################################
902 902
903void Lane::pushErrorTraceLevel(lua_State* L_) const 903[[nodiscard]] std::string_view Lane::pushErrorTraceLevel(lua_State* L_) const
904{ 904{
905 char const* const _str{ errorTraceLevelString() }; 905 std::string_view const _str{ errorTraceLevelString() };
906 LUA_ASSERT(L_, _str); 906 LUA_ASSERT(L_, !_str.empty());
907 907
908 lua_pushstring(L_, _str); 908 return lua_pushstringview(L_, _str);
909} 909}
910 910
911// ################################################################################################# 911// #################################################################################################
@@ -948,9 +948,9 @@ void Lane::startThread(int priority_)
948// / "error" finished at an error, error value is there 948// / "error" finished at an error, error value is there
949// / "cancelled" execution cancelled by M (state gone) 949// / "cancelled" execution cancelled by M (state gone)
950// 950//
951[[nodiscard]] char const* Lane::threadStatusString() const 951[[nodiscard]] std::string_view Lane::threadStatusString() const
952{ 952{
953 char const* const _str{ 953 std::string_view const _str{
954 (status == Lane::Pending) ? "pending" : 954 (status == Lane::Pending) ? "pending" :
955 (status == Lane::Running) ? "running" : // like in 'co.status()' 955 (status == Lane::Running) ? "running" : // like in 'co.status()'
956 (status == Lane::Waiting) ? "waiting" : 956 (status == Lane::Waiting) ? "waiting" :
diff --git a/src/lane.h b/src/lane.h
index 10045c8..3c1ab58 100644
--- a/src/lane.h
+++ b/src/lane.h
@@ -8,6 +8,7 @@
8#include <condition_variable> 8#include <condition_variable>
9#include <latch> 9#include <latch>
10#include <stop_token> 10#include <stop_token>
11#include <string_view>
11#include <thread> 12#include <thread>
12 13
13// ################################################################################################# 14// #################################################################################################
@@ -127,14 +128,14 @@ class Lane
127 128
128 void changeDebugName(int nameIdx_); 129 void changeDebugName(int nameIdx_);
129 void close() { lua_State* _L{ L }; L = nullptr; lua_close(_L); } 130 void close() { lua_State* _L{ L }; L = nullptr; lua_close(_L); }
130 [[nodiscard]] char const* errorTraceLevelString() const; 131 [[nodiscard]] std::string_view errorTraceLevelString() const;
131 [[nodiscard]] int pushErrorHandler() const; 132 [[nodiscard]] int pushErrorHandler() const;
132 void pushErrorTraceLevel(lua_State* L_) const; 133 [[nodiscard]] std::string_view pushErrorTraceLevel(lua_State* L_) const;
133 static void PushMetatable(lua_State* L_); 134 static void PushMetatable(lua_State* L_);
134 void pushThreadStatus(lua_State* L_) const; 135 [[nodiscard]] std::string_view pushThreadStatus(lua_State* L_) const;
135 void securizeDebugName(lua_State* L_); 136 void securizeDebugName(lua_State* L_);
136 void startThread(int priority_); 137 void startThread(int priority_);
137 [[nodiscard]] char const* threadStatusString() const; 138 [[nodiscard]] std::string_view threadStatusString() const;
138 [[nodiscard]] bool waitForCompletion(std::chrono::time_point<std::chrono::steady_clock> until_); 139 [[nodiscard]] bool waitForCompletion(std::chrono::time_point<std::chrono::steady_clock> until_);
139}; 140};
140 141
diff --git a/src/tracker.cpp b/src/tracker.cpp
index 3040154..69cd90c 100644
--- a/src/tracker.cpp
+++ b/src/tracker.cpp
@@ -96,7 +96,7 @@ void LaneTracker::tracking_add(Lane* lane_)
96 lua_createtable(L_, 0, 2); // L_: {} {} 96 lua_createtable(L_, 0, 2); // L_: {} {}
97 std::ignore = lua_pushstringview(L_, _lane->debugName); // L_: {} {} "name" 97 std::ignore = lua_pushstringview(L_, _lane->debugName); // L_: {} {} "name"
98 lua_setfield(L_, -2, "name"); // L_: {} {} 98 lua_setfield(L_, -2, "name"); // L_: {} {}
99 _lane->pushThreadStatus(L_); // L_: {} {} "status" 99 std::ignore = _lane->pushThreadStatus(L_); // L_: {} {} "status"
100 lua_setfield(L_, -2, "status"); // L_: {} {} 100 lua_setfield(L_, -2, "status"); // L_: {} {}
101 lua_rawseti(L_, -2, ++_index); // L_: {} 101 lua_rawseti(L_, -2, ++_index); // L_: {}
102 _lane = _lane->tracking_next; 102 _lane = _lane->tracking_next;