Skip to content

Commit c0cc005

Browse files
committed
Reduce code duplication
1 parent ae846d5 commit c0cc005

File tree

3 files changed

+43
-66
lines changed

3 files changed

+43
-66
lines changed

include/libcyphal/transport/can/rx_session_tree_node.hpp

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -34,38 +34,7 @@ struct RxSessionTreeNode
3434
{
3535
/// @brief Represents a service response RX session node.
3636
///
37-
class Response final : public transport::detail::SessionTree<Response>::NodeBase
38-
{
39-
public:
40-
explicit Response(const ResponseRxParams& params, const std::tuple<>&)
41-
: service_id_{params.service_id}
42-
, server_node_id{params.server_node_id}
43-
, delegate_{nullptr}
44-
{
45-
}
46-
47-
CETL_NODISCARD std::int32_t compareByParams(const ResponseRxParams& params) const
48-
{
49-
if (service_id_ != params.service_id)
50-
{
51-
return static_cast<std::int32_t>(service_id_) - static_cast<std::int32_t>(params.service_id);
52-
}
53-
return static_cast<std::int32_t>(server_node_id) - static_cast<std::int32_t>(params.server_node_id);
54-
}
55-
56-
CETL_NODISCARD IRxSessionDelegate*& delegate() noexcept
57-
{
58-
return delegate_;
59-
}
60-
61-
private:
62-
// MARK: Data members:
63-
64-
const PortId service_id_;
65-
const NodeId server_node_id;
66-
IRxSessionDelegate* delegate_;
67-
68-
}; // Response
37+
using Response = transport::detail::ResponseRxSessionNode<IRxSessionDelegate>;
6938

7039
}; // RxSessionTreeNode
7140

include/libcyphal/transport/session_tree.hpp

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "errors.hpp"
1010
#include "libcyphal/common/cavl/cavl.hpp"
1111
#include "libcyphal/types.hpp"
12+
#include "svc_sessions.hpp"
1213

1314
#include <cetl/cetl.hpp>
1415
#include <cetl/pf17/cetlpf.hpp>
@@ -74,8 +75,8 @@ class SessionTree final
7475
}
7576

7677
template <bool ShouldBeNew = false, typename Params, typename... Args>
77-
CETL_NODISCARD auto ensureNodeFor(const Params& params,
78-
Args&&... args) -> Expected<typename NodeBase::RefWrapper, AnyFailure>
78+
CETL_NODISCARD auto ensureNodeFor(const Params& params, Args&&... args)
79+
-> Expected<typename NodeBase::RefWrapper, AnyFailure>
7980
{
8081
auto args_tuple = std::make_tuple(std::forward<Args>(args)...);
8182

@@ -158,6 +159,44 @@ class SessionTree final
158159

159160
}; // SessionTree
160161

162+
// MARK: -
163+
164+
/// @brief Represents a service response RX session node.
165+
///
166+
template <typename RxSessionDelegate>
167+
class ResponseRxSessionNode final : public SessionTree<ResponseRxSessionNode<RxSessionDelegate>>::NodeBase
168+
{
169+
public:
170+
explicit ResponseRxSessionNode(const ResponseRxParams& params, const std::tuple<>&)
171+
: service_id_{params.service_id}
172+
, server_node_id{params.server_node_id}
173+
, delegate_{nullptr}
174+
{
175+
}
176+
177+
CETL_NODISCARD std::int32_t compareByParams(const ResponseRxParams& params) const
178+
{
179+
if (service_id_ != params.service_id)
180+
{
181+
return static_cast<std::int32_t>(service_id_) - static_cast<std::int32_t>(params.service_id);
182+
}
183+
return static_cast<std::int32_t>(server_node_id) - static_cast<std::int32_t>(params.server_node_id);
184+
}
185+
186+
CETL_NODISCARD RxSessionDelegate*& delegate() noexcept
187+
{
188+
return delegate_;
189+
}
190+
191+
private:
192+
// MARK: Data members:
193+
194+
const PortId service_id_;
195+
const NodeId server_node_id;
196+
RxSessionDelegate* delegate_;
197+
198+
}; // ResponseRxSessionNode
199+
161200
} // namespace detail
162201
} // namespace transport
163202
} // namespace libcyphal

include/libcyphal/transport/udp/rx_session_tree_node.hpp

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -110,38 +110,7 @@ struct RxSessionTreeNode
110110

111111
/// @brief Represents a service response RX session node.
112112
///
113-
class Response final : public transport::detail::SessionTree<Response>::NodeBase
114-
{
115-
public:
116-
explicit Response(const ResponseRxParams& params, const std::tuple<>&)
117-
: service_id_{params.service_id}
118-
, server_node_id{params.server_node_id}
119-
, delegate_{nullptr}
120-
{
121-
}
122-
123-
CETL_NODISCARD std::int32_t compareByParams(const ResponseRxParams& params) const
124-
{
125-
if (service_id_ != params.service_id)
126-
{
127-
return static_cast<std::int32_t>(service_id_) - static_cast<std::int32_t>(params.service_id);
128-
}
129-
return static_cast<std::int32_t>(server_node_id) - static_cast<std::int32_t>(params.server_node_id);
130-
}
131-
132-
CETL_NODISCARD IRxSessionDelegate*& delegate() noexcept
133-
{
134-
return delegate_;
135-
}
136-
137-
private:
138-
// MARK: Data members:
139-
140-
const PortId service_id_;
141-
const NodeId server_node_id;
142-
IRxSessionDelegate* delegate_;
143-
144-
}; // Response
113+
using Response = transport::detail::ResponseRxSessionNode<IRxSessionDelegate>;
145114

146115
}; // RxSessionTreeNode
147116

0 commit comments

Comments
 (0)