Skip to content

Commit c2c1e7c

Browse files
committed
minor fixes based on pr review
1 parent 7e074a4 commit c2c1e7c

File tree

7 files changed

+57
-21
lines changed

7 files changed

+57
-21
lines changed

include/libcyphal/transport/can/delegate.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ class TransportDelegate
225225

226226
}; // CanardConcreteTree
227227

228+
/// Umbrella type for all session-related events.
229+
///
230+
/// These are passed to the `onSessionEvent` method of the transport implementation.
231+
///
228232
struct SessionEvent
229233
{
230234
struct MsgCreated

include/libcyphal/transport/can/rx_session_tree_node.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ namespace detail
2323

2424
class IRxSessionDelegate;
2525

26+
/// Umbrella type for various RX session tree nodes in use at the CAN transport.
27+
///
28+
/// Currently, it contains only one `Response` subtype,
29+
/// but still kept nested to match the UDP transport approach (where there are several subtypes).
30+
///
2631
struct RxSessionTreeNode
2732
{
2833
/// @brief Represents a service response RX session node.

include/libcyphal/transport/session_tree.hpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@ namespace libcyphal
2323
namespace transport
2424
{
2525

26-
/// Internal implementation details of the UDP transport.
26+
/// Internal implementation details of a transport.
2727
/// Not supposed to be used directly by the users of the library.
2828
///
2929
namespace detail
3030
{
3131

32-
/// @brief Defines a tree of sessions for the UDP transport.
32+
/// @brief Defines a tree of sessions for a transport.
33+
///
34+
/// @tparam Node The type of the session node. Expected to be a subclass of `SessionTree::NodeBase`,
35+
/// and to have a method `compareByParams` that compares nodes by its parameters.
3336
///
3437
template <typename Node>
3538
class SessionTree final
@@ -74,10 +77,21 @@ class SessionTree final
7477
return nodes_.empty();
7578
}
7679

80+
/// @brief Ensures that a node for the given parameters exists in the tree.
81+
///
82+
/// @tparam ShouldBeNew If `true`, the function will return an error if node with given
83+
// parametes already exists (see also `Node::compareByParams` method).
84+
/// @tparam Params The type of the parameters to be used to find or create the node.
85+
/// @tparam Args The types of the arguments to be passed to the constructor of the node.
86+
/// @param params The parameters to be used to find or create the node.
87+
/// @param args The extra arguments to be forwarded to the constructor of the node (as a tuple).
88+
/// @return The reference to the node, or an error if the node could not be created.
89+
///
7790
template <bool ShouldBeNew = false, typename Params, typename... Args>
7891
CETL_NODISCARD auto ensureNodeFor(const Params& params,
7992
Args&&... args) -> Expected<typename NodeBase::RefWrapper, AnyFailure>
8093
{
94+
// In c++14 we can't capture `args` with forwarding, so we pack them into a tuple.
8195
auto args_tuple = std::make_tuple(std::forward<Args>(args)...);
8296

8397
const auto node_existing = nodes_.search(
@@ -167,6 +181,9 @@ template <typename RxSessionDelegate>
167181
class ResponseRxSessionNode final : public SessionTree<ResponseRxSessionNode<RxSessionDelegate>>::NodeBase
168182
{
169183
public:
184+
// Empty tuple parameter is used to allow for the same constructor signature
185+
// as for other session nodes (see also `SessionTree::ensureNodeFor` method).
186+
//
170187
explicit ResponseRxSessionNode(const ResponseRxParams& params, const std::tuple<>&)
171188
: service_id_{params.service_id}
172189
, server_node_id{params.server_node_id}

include/libcyphal/transport/udp/delegate.hpp

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ class IMsgRxSessionDelegate : public IRxSessionDelegate
253253
class TransportDelegate
254254
{
255255
public:
256+
/// Umbrella type for all session-related events.
257+
///
258+
/// These are passed to the `onSessionEvent` method of the transport implementation.
259+
///
256260
struct SessionEvent
257261
{
258262
struct MsgDestroyed
@@ -301,24 +305,13 @@ class TransportDelegate
301305
return rx_rpc_dispatcher_;
302306
}
303307

304-
void listenForRxRpcPort(UdpardRxRPCPort& rpc_port, const RequestRxParams& params)
305-
{
306-
const std::int8_t result = ::udpardRxRPCDispatcherListen(&rx_rpc_dispatcher_,
307-
&rpc_port,
308-
params.service_id,
309-
true, // request
310-
params.extent_bytes);
311-
(void) result;
312-
CETL_DEBUG_ASSERT(result >= 0, "There is no way currently to get an error here.");
313-
CETL_DEBUG_ASSERT(result == 1, "A new registration has been expected to be created.");
314-
}
315-
316-
void listenForRxRpcPort(UdpardRxRPCPort& rpc_port, const ResponseRxParams& params)
308+
template <bool IsRequest, typename Params>
309+
void listenForRxRpcPort(UdpardRxRPCPort& rpc_port, const Params& params)
317310
{
318311
const std::int8_t result = ::udpardRxRPCDispatcherListen(&rx_rpc_dispatcher_,
319312
&rpc_port,
320313
params.service_id,
321-
false, // response
314+
IsRequest,
322315
params.extent_bytes);
323316
(void) result;
324317
CETL_DEBUG_ASSERT(result >= 0, "There is no way currently to get an error here.");
@@ -485,7 +478,7 @@ class TransportDelegate
485478
, ref_count_{0}
486479
, port_{}
487480
{
488-
transport_delegate_.listenForRxRpcPort(port_, params);
481+
transport_delegate_.listenForRxRpcPort<false>(port_, params);
489482

490483
// No Sonar `cpp:S5356` b/c we integrate here with C libudpard API.
491484
port_.user_reference = static_cast<IRxSessionDelegate*>(this); // NOSONAR cpp:S5356

include/libcyphal/transport/udp/rx_session_tree_node.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,18 @@ struct SocketState
4646
class IRxSessionDelegate;
4747
class IMsgRxSessionDelegate;
4848

49+
/// Umbrella type for various RX session tree nodes in use at the UDP transport.
50+
///
4951
struct RxSessionTreeNode
5052
{
5153
/// @brief Represents a message RX session node.
5254
///
5355
class Message final : public transport::detail::SessionTree<Message>::NodeBase
5456
{
5557
public:
58+
// Empty tuple parameter is used to allow for the same constructor signature
59+
// as for other session nodes (see also `SessionTree::ensureNodeFor` method).
60+
//
5661
explicit Message(const MessageRxParams& params, const std::tuple<>&)
5762
: subject_id_{params.subject_id}
5863
{
@@ -90,6 +95,9 @@ struct RxSessionTreeNode
9095
class Request final : public transport::detail::SessionTree<Request>::NodeBase
9196
{
9297
public:
98+
// Empty tuple parameter is used to allow for the same constructor signature
99+
// as for other session nodes (see also `SessionTree::ensureNodeFor` method).
100+
//
93101
explicit Request(const RequestRxParams& params, const std::tuple<>&)
94102
: service_id_{params.service_id}
95103
{

include/libcyphal/transport/udp/svc_rx_sessions.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class SvcRequestRxSession final : public SvcRxSessionBase<IRequestRxSession, Req
148148
: Base{delegate, params}
149149
, rpc_port_{}
150150
{
151-
delegate.listenForRxRpcPort(rpc_port_, params);
151+
delegate.listenForRxRpcPort<true>(rpc_port_, params);
152152

153153
// No Sonar `cpp:S5356` b/c we integrate here with C libudpard API.
154154
rpc_port_.user_reference = static_cast<IRxSessionDelegate*>(this); // NOSONAR cpp:S5356

include/libcyphal/types.hpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <chrono>
1616
#include <cstddef>
1717
#include <cstdint>
18+
#include <exception>
1819
#include <memory>
1920
#include <ratio>
2021
#include <type_traits>
@@ -149,8 +150,15 @@ CETL_NODISCARD UpVariant upcastVariant(Variant&& variant)
149150
std::forward<Variant>(variant));
150151
}
151152

152-
template <typename Action>
153-
bool performWithoutThrowing(Action&& action) noexcept
153+
/// @brief Wraps the given action into a lambda and performs it without throwing exceptions.
154+
///
155+
/// In use f.e. for `cetl::visit` which might hypothetically throw an exceptions.
156+
///
157+
/// @return `true` if the action was performed successfully, `false` if an exception was thrown.
158+
/// Always `true` if exceptions are disabled.
159+
///
160+
template <typename Exception = std::exception, typename Action>
161+
CETL_NODISCARD bool performWithoutThrowing(Action&& action) noexcept
154162
{
155163
#if defined(__cpp_exceptions)
156164
try
@@ -159,8 +167,9 @@ bool performWithoutThrowing(Action&& action) noexcept
159167
std::forward<Action>(action)();
160168
return true;
161169
#if defined(__cpp_exceptions)
162-
} catch (...)
170+
} catch (const Exception& ex)
163171
{
172+
CETL_DEBUG_ASSERT(false, ex.what());
164173
return false;
165174
}
166175
#endif

0 commit comments

Comments
 (0)