diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-06-14 15:23:21 +0200 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-06-14 15:23:21 +0200 |
commit | 149d6d6d16b10da9915e3018237405f6fe8749cf (patch) | |
tree | 3bf7ef659b5efd3d61bfd80eef2f48b9eff8d5ec /src/keeper.cpp | |
parent | 792128255b6c6add22f97ea60734181cb915f2ae (diff) | |
download | lanes-149d6d6d16b10da9915e3018237405f6fe8749cf.tar.gz lanes-149d6d6d16b10da9915e3018237405f6fe8749cf.tar.bz2 lanes-149d6d6d16b10da9915e3018237405f6fe8749cf.zip |
More char* -> std::string_view conversions
Diffstat (limited to 'src/keeper.cpp')
-rw-r--r-- | src/keeper.cpp | 142 |
1 files changed, 73 insertions, 69 deletions
diff --git a/src/keeper.cpp b/src/keeper.cpp index 4175d84..6aeea45 100644 --- a/src/keeper.cpp +++ b/src/keeper.cpp | |||
@@ -51,6 +51,8 @@ | |||
51 | // where key is a key used in the Lua Linda API to exchange data, and KeyUD is a full userdata with a table uservalue | 51 | // where key is a key used in the Lua Linda API to exchange data, and KeyUD is a full userdata with a table uservalue |
52 | // the table uservalue is the actual fifo, where elements are added and removed. | 52 | // the table uservalue is the actual fifo, where elements are added and removed. |
53 | 53 | ||
54 | namespace { | ||
55 | |||
54 | // ################################################################################################# | 56 | // ################################################################################################# |
55 | // ################################################################################################# | 57 | // ################################################################################################# |
56 | // ############################################ KeyUD ############################################## | 58 | // ############################################ KeyUD ############################################## |
@@ -77,10 +79,10 @@ class KeyUD | |||
77 | [[nodiscard]] bool changeLimit(int limit_); | 79 | [[nodiscard]] bool changeLimit(int limit_); |
78 | [[nodiscard]] static KeyUD* Create(KeeperState K_); | 80 | [[nodiscard]] static KeyUD* Create(KeeperState K_); |
79 | [[nodiscard]] static KeyUD* GetPtr(KeeperState K_, int idx_); | 81 | [[nodiscard]] static KeyUD* GetPtr(KeeperState K_, int idx_); |
80 | void peek(KeeperState K_, int count_); | 82 | void peek(KeeperState K_, int count_) const; // keepercall_get |
81 | [[nodiscard]] int pop(KeeperState K_, int minCount_, int maxCount_); | 83 | [[nodiscard]] int pop(KeeperState K_, int minCount_, int maxCount_); // keepercall_receive[_batched] |
82 | void prepareAccess(KeeperState K_, int idx_); | 84 | void prepareAccess(KeeperState K_, int idx_) const; |
83 | [[nodiscard]] bool push(KeeperState K_, int count_); | 85 | [[nodiscard]] bool push(KeeperState K_, int count_); // keepercall_send |
84 | [[nodiscard]] bool reset(KeeperState K_); | 86 | [[nodiscard]] bool reset(KeeperState K_); |
85 | }; | 87 | }; |
86 | 88 | ||
@@ -126,7 +128,7 @@ KeyUD* KeyUD::GetPtr(KeeperState const K_, int idx_) | |||
126 | // out: bool ... | 128 | // out: bool ... |
127 | // pops the fifo, push bool + as much data as is available (up to the specified count) without consuming it | 129 | // pops the fifo, push bool + as much data as is available (up to the specified count) without consuming it |
128 | // bool is true if the requested count was served, else false | 130 | // bool is true if the requested count was served, else false |
129 | void KeyUD::peek(KeeperState const K_, int const count_) | 131 | void KeyUD::peek(KeeperState const K_, int const count_) const |
130 | { | 132 | { |
131 | STACK_CHECK_START_ABS(K_, 1); | 133 | STACK_CHECK_START_ABS(K_, 1); |
132 | LUA_ASSERT(K_, KeyUD::GetPtr(K_, -1) == this); // K_: KeyUD | 134 | LUA_ASSERT(K_, KeyUD::GetPtr(K_, -1) == this); // K_: KeyUD |
@@ -194,7 +196,7 @@ int KeyUD::pop(KeeperState const K_, int const minCount_, int const maxCount_) | |||
194 | 196 | ||
195 | // expects 'this' at the specified index | 197 | // expects 'this' at the specified index |
196 | // replaces it by its uservalue on the stack (the table holding the fifo values) | 198 | // replaces it by its uservalue on the stack (the table holding the fifo values) |
197 | void KeyUD::prepareAccess(KeeperState const K_, int const idx_) | 199 | void KeyUD::prepareAccess(KeeperState const K_, int const idx_) const |
198 | { | 200 | { |
199 | int const _idx{ luaG_absindex(K_, idx_) }; | 201 | int const _idx{ luaG_absindex(K_, idx_) }; |
200 | LUA_ASSERT(K_, KeyUD::GetPtr(K_, idx_) == this); | 202 | LUA_ASSERT(K_, KeyUD::GetPtr(K_, idx_) == this); |
@@ -273,68 +275,7 @@ static void PushKeysDB(KeeperState const K_, int const idx_) | |||
273 | STACK_CHECK(K_, 1); | 275 | STACK_CHECK(K_, 1); |
274 | } | 276 | } |
275 | 277 | ||
276 | // ################################################################################################# | 278 | } // namespace |
277 | |||
278 | // only used by linda:dump() and linda:__towatch() for debugging purposes | ||
279 | int keeper_push_linda_storage(Linda& linda_, DestState L_) | ||
280 | { | ||
281 | Keeper* const _keeper{ linda_.whichKeeper() }; | ||
282 | KeeperState const _K{ _keeper ? _keeper->K : nullptr }; | ||
283 | if (_K == nullptr) { | ||
284 | return 0; | ||
285 | } | ||
286 | STACK_GROW(_K, 4); | ||
287 | STACK_CHECK_START_REL(_K, 0); | ||
288 | kLindasRegKey.pushValue(_K); // _K: LindasDB L_: | ||
289 | lua_pushlightuserdata(_K, &linda_); // _K: LindasDB linda L_: | ||
290 | lua_rawget(_K, -2); // _K: LindasDB KeysDB L_: | ||
291 | lua_remove(_K, -2); // _K: KeysDB L_: | ||
292 | if (!lua_istable(_K, -1)) { // possible if we didn't send anything through that linda | ||
293 | lua_pop(_K, 1); // _K: L_: | ||
294 | STACK_CHECK(_K, 0); | ||
295 | return 0; | ||
296 | } | ||
297 | // move data from keeper to destination state | ||
298 | STACK_GROW(L_, 5); | ||
299 | STACK_CHECK_START_REL(L_, 0); | ||
300 | lua_newtable(L_); // _K: KeysDB L_: out | ||
301 | InterCopyContext _c{ linda_.U, L_, SourceState{ _K }, {}, {}, {}, LookupMode::FromKeeper, {} }; | ||
302 | lua_pushnil(_K); // _K: KeysDB nil L_: out | ||
303 | while (lua_next(_K, -2)) { // _K: KeysDB key KeyUD L_: out | ||
304 | KeyUD* const _key{ KeyUD::GetPtr(_K, -1) }; | ||
305 | _key->prepareAccess(_K, -1); // _K: KeysDB key fifo L_: out | ||
306 | lua_pushvalue(_K, -2); // _K: KeysDB key fifo key L_: out | ||
307 | if (_c.interMove(1) != InterCopyResult::Success) { // _K: KeysDB key fifo L_: out key | ||
308 | raise_luaL_error(L_, "Internal error reading Keeper contents"); | ||
309 | } | ||
310 | STACK_CHECK(L_, 2); | ||
311 | lua_newtable(L_); // _K: KeysDB key fifo L_: out key keyout | ||
312 | if (_c.interMove(1) != InterCopyResult::Success) { // _K: KeysDB key L_: out key keyout fifo | ||
313 | raise_luaL_error(L_, "Internal error reading Keeper contents"); | ||
314 | } | ||
315 | // keyout.first | ||
316 | lua_pushinteger(L_, _key->first); // _K: KeysDB key L_: out key keyout fifo first | ||
317 | STACK_CHECK(L_, 5); | ||
318 | lua_setfield(L_, -3, "first"); // _K: KeysDB key L_: out key keyout fifo | ||
319 | // keyout.count | ||
320 | lua_pushinteger(L_, _key->count); // _K: KeysDB key L_: out key keyout fifo count | ||
321 | STACK_CHECK(L_, 5); | ||
322 | lua_setfield(L_, -3, "count"); // _K: KeysDB key L_: out key keyout fifo | ||
323 | // keyout.limit | ||
324 | lua_pushinteger(L_, _key->limit); // _K: KeysDB key L_: out key keyout fifo limit | ||
325 | STACK_CHECK(L_, 5); | ||
326 | lua_setfield(L_, -3, "limit"); // _K: KeysDB key L_: out key keyout fifo | ||
327 | // keyout.fifo | ||
328 | lua_setfield(L_, -2, "fifo"); // _K: KeysDB key L_: out key keyout | ||
329 | // out[key] = keyout | ||
330 | lua_rawset(L_, -3); // _K: KeysDB key L_: out | ||
331 | STACK_CHECK(L_, 1); | ||
332 | } // _K: KeysDB L_: out | ||
333 | STACK_CHECK(L_, 1); | ||
334 | lua_pop(_K, 1); // _K: L_: out | ||
335 | STACK_CHECK(_K, 0); | ||
336 | return 1; | ||
337 | } | ||
338 | 279 | ||
339 | // ################################################################################################# | 280 | // ################################################################################################# |
340 | // ################################################################################################# | 281 | // ################################################################################################# |
@@ -437,7 +378,7 @@ int keepercall_get(lua_State* const L_) | |||
437 | lua_replace(_K, 1); // _K: KeysDB key | 378 | lua_replace(_K, 1); // _K: KeysDB key |
438 | lua_rawget(_K, 1); // _K: KeysDB KeyUD | 379 | lua_rawget(_K, 1); // _K: KeysDB KeyUD |
439 | lua_remove(_K, 1); // _K: KeyUD | 380 | lua_remove(_K, 1); // _K: KeyUD |
440 | KeyUD* const _key{ KeyUD::GetPtr(_K, -1) }; | 381 | KeyUD const* const _key{ KeyUD::GetPtr(_K, -1) }; |
441 | if (_key != nullptr) { | 382 | if (_key != nullptr) { |
442 | _key->peek(_K, _count); // _K: N val... | 383 | _key->peek(_K, _count); // _K: N val... |
443 | } else { | 384 | } else { |
@@ -713,6 +654,69 @@ void Keeper::operator delete[](void* p_, Universe* U_) | |||
713 | } | 654 | } |
714 | 655 | ||
715 | // ################################################################################################# | 656 | // ################################################################################################# |
657 | |||
658 | // only used by linda:dump() and linda:__towatch() for debugging purposes | ||
659 | int Keeper::PushLindaStorage(Linda& linda_, DestState const L_) | ||
660 | { | ||
661 | Keeper* const _keeper{ linda_.whichKeeper() }; | ||
662 | KeeperState const _K{ _keeper ? _keeper->K : nullptr }; | ||
663 | if (_K == nullptr) { | ||
664 | return 0; | ||
665 | } | ||
666 | STACK_GROW(_K, 4); | ||
667 | STACK_CHECK_START_REL(_K, 0); | ||
668 | kLindasRegKey.pushValue(_K); // _K: LindasDB L_: | ||
669 | lua_pushlightuserdata(_K, &linda_); // _K: LindasDB linda L_: | ||
670 | lua_rawget(_K, -2); // _K: LindasDB KeysDB L_: | ||
671 | lua_remove(_K, -2); // _K: KeysDB L_: | ||
672 | if (!lua_istable(_K, -1)) { // possible if we didn't send anything through that linda | ||
673 | lua_pop(_K, 1); // _K: L_: | ||
674 | STACK_CHECK(_K, 0); | ||
675 | return 0; | ||
676 | } | ||
677 | // move data from keeper to destination state | ||
678 | STACK_GROW(L_, 5); | ||
679 | STACK_CHECK_START_REL(L_, 0); | ||
680 | lua_newtable(L_); // _K: KeysDB L_: out | ||
681 | InterCopyContext _c{ linda_.U, L_, SourceState{ _K }, {}, {}, {}, LookupMode::FromKeeper, {} }; | ||
682 | lua_pushnil(_K); // _K: KeysDB nil L_: out | ||
683 | while (lua_next(_K, -2)) { // _K: KeysDB key KeyUD L_: out | ||
684 | KeyUD* const _key{ KeyUD::GetPtr(_K, -1) }; | ||
685 | _key->prepareAccess(_K, -1); // _K: KeysDB key fifo L_: out | ||
686 | lua_pushvalue(_K, -2); // _K: KeysDB key fifo key L_: out | ||
687 | if (_c.interMove(1) != InterCopyResult::Success) { // _K: KeysDB key fifo L_: out key | ||
688 | raise_luaL_error(L_, "Internal error reading Keeper contents"); | ||
689 | } | ||
690 | STACK_CHECK(L_, 2); | ||
691 | lua_newtable(L_); // _K: KeysDB key fifo L_: out key keyout | ||
692 | if (_c.interMove(1) != InterCopyResult::Success) { // _K: KeysDB key L_: out key keyout fifo | ||
693 | raise_luaL_error(L_, "Internal error reading Keeper contents"); | ||
694 | } | ||
695 | // keyout.first | ||
696 | lua_pushinteger(L_, _key->first); // _K: KeysDB key L_: out key keyout fifo first | ||
697 | STACK_CHECK(L_, 5); | ||
698 | lua_setfield(L_, -3, "first"); // _K: KeysDB key L_: out key keyout fifo | ||
699 | // keyout.count | ||
700 | lua_pushinteger(L_, _key->count); // _K: KeysDB key L_: out key keyout fifo count | ||
701 | STACK_CHECK(L_, 5); | ||
702 | lua_setfield(L_, -3, "count"); // _K: KeysDB key L_: out key keyout fifo | ||
703 | // keyout.limit | ||
704 | lua_pushinteger(L_, _key->limit); // _K: KeysDB key L_: out key keyout fifo limit | ||
705 | STACK_CHECK(L_, 5); | ||
706 | lua_setfield(L_, -3, "limit"); // _K: KeysDB key L_: out key keyout fifo | ||
707 | // keyout.fifo | ||
708 | lua_setfield(L_, -2, "fifo"); // _K: KeysDB key L_: out key keyout | ||
709 | // out[key] = keyout | ||
710 | lua_rawset(L_, -3); // _K: KeysDB key L_: out | ||
711 | STACK_CHECK(L_, 1); | ||
712 | } // _K: KeysDB L_: out | ||
713 | STACK_CHECK(L_, 1); | ||
714 | lua_pop(_K, 1); // _K: L_: out | ||
715 | STACK_CHECK(_K, 0); | ||
716 | return 1; | ||
717 | } | ||
718 | |||
719 | // ################################################################################################# | ||
716 | // ################################################################################################# | 720 | // ################################################################################################# |
717 | // ########################################## Keepers ############################################## | 721 | // ########################################## Keepers ############################################## |
718 | // ################################################################################################# | 722 | // ################################################################################################# |