From 6d271c5796eae14d1dc60e778435495ebfb540d8 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Tue, 7 May 2024 17:56:10 +0200 Subject: Linda API changes * timeout clarifications (negative values are no longer accepted, use nil instead) * linda(send, linda.null, key, ...) removed, if you want to send a nil, just do it as usual --- src/linda.cpp | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) (limited to 'src/linda.cpp') diff --git a/src/linda.cpp b/src/linda.cpp index bbfbd69..40ef6c7 100644 --- a/src/linda.cpp +++ b/src/linda.cpp @@ -180,7 +180,7 @@ int Linda::ProtectedCall(lua_State* L_, lua_CFunction f_) // ################################################################################################# /* - * bool= linda_send( linda_ud, [timeout_secs=-1,] [linda.null,] key_num|str|bool|lightuserdata, ... ) + * bool= linda:linda_send([timeout_secs=nil,] key_num|str|bool|lightuserdata, ...) * * Send one or more values to a Linda. If there is a limit, all values must fit. * @@ -192,25 +192,21 @@ LUAG_FUNC(linda_send) { auto send = [](lua_State* L_) { Linda* const linda{ ToLinda(L_, 1) }; - std::chrono::time_point until{ std::chrono::time_point::max() }; int key_i{ 2 }; // index of first key, if timeout not there + std::chrono::time_point until{ std::chrono::time_point::max() }; if (lua_type(L_, 2) == LUA_TNUMBER) { // we don't want to use lua_isnumber() because of autocoercion lua_Duration const duration{ lua_tonumber(L_, 2) }; if (duration.count() >= 0.0) { until = std::chrono::steady_clock::now() + std::chrono::duration_cast(duration); + } else { + raise_luaL_argerror(L_, 2, "duration cannot be < 0"); } ++key_i; } else if (lua_isnil(L_, 2)) { // alternate explicit "infinite timeout" by passing nil before the key ++key_i; } - bool const as_nil_sentinel{ kNilSentinel.equals(L_, key_i) }; // if not nullptr, send() will silently send a single nil if nothing is provided - if (as_nil_sentinel) { - // the real key to send data to is after the kNilSentinel marker - ++key_i; - } - // make sure the key is of a valid type check_key_types(L_, key_i, key_i); @@ -218,12 +214,7 @@ LUAG_FUNC(linda_send) // make sure there is something to send if (lua_gettop(L_) == key_i) { - if (as_nil_sentinel) { - // send a single nil if nothing is provided - kNilSentinel.pushKey(L_); - } else { - raise_luaL_error(L_, "no data to send"); - } + raise_luaL_error(L_, "no data to send"); } // convert nils to some special non-nil sentinel in sent values @@ -322,7 +313,7 @@ LUAG_FUNC(linda_send) /* * 2 modes of operation - * [val, key]= linda_receive( linda_ud, [timeout_secs_num=-1], key_num|str|bool|lightuserdata [, ...] ) + * [val, key]= linda_receive( linda_ud, [timeout_secs_num=nil], key_num|str|bool|lightuserdata [, ...] ) * Consumes a single value from the Linda, in any key. * Returns: received value (which is consumed from the slot), and the key which had it @@ -335,13 +326,15 @@ LUAG_FUNC(linda_receive) { auto receive = [](lua_State* L_) { Linda* const linda{ ToLinda(L_, 1) }; - std::chrono::time_point until{ std::chrono::time_point::max() }; int key_i{ 2 }; // index of first key, if timeout not there + std::chrono::time_point until{ std::chrono::time_point::max() }; if (lua_type(L_, 2) == LUA_TNUMBER) { // we don't want to use lua_isnumber() because of autocoercion lua_Duration const duration{ lua_tonumber(L_, 2) }; if (duration.count() >= 0.0) { until = std::chrono::steady_clock::now() + std::chrono::duration_cast(duration); + } else { + raise_luaL_argerror(L_, 2, "duration cannot be < 0"); } ++key_i; } else if (lua_isnil(L_, 2)) { // alternate explicit "infinite timeout" by passing nil before the key -- cgit v1.2.3-55-g6feb