1 // Copyright 2020 The Pigweed Authors
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
7 // https://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
16 #include "pw_bytes/span.h"
17 #include "pw_rpc/internal/method_union.h"
18 #include "pw_rpc/internal/nanopb_method.h"
19 #include "pw_rpc/internal/raw_method_union.h"
21 namespace pw::rpc::internal {
23 // Method union which holds either a nanopb or a raw method.
24 class NanopbMethodUnion : public MethodUnion {
26 constexpr NanopbMethodUnion(RawMethod&& method)
27 : impl_({.raw = std::move(method)}) {}
28 constexpr NanopbMethodUnion(NanopbMethod&& method)
29 : impl_({.nanopb = std::move(method)}) {}
31 constexpr const Method& method() const { return impl_.method; }
32 constexpr const RawMethod& raw_method() const { return impl_.raw; }
33 constexpr const NanopbMethod& nanopb_method() const { return impl_.nanopb; }
43 // Returns either a raw or nanopb method object, depending on the implemented
44 // function's signature.
45 template <auto method, MethodType type>
46 constexpr auto GetNanopbOrRawMethodFor(
48 [[maybe_unused]] NanopbMessageDescriptor request_fields,
49 [[maybe_unused]] NanopbMessageDescriptor response_fields) {
50 if constexpr (RawMethod::matches<method>()) {
51 return GetMethodFor<method, RawMethod, type>(id);
52 } else if constexpr (NanopbMethod::matches<method>()) {
53 return GetMethodFor<method, NanopbMethod, type>(
54 id, request_fields, response_fields);
56 return InvalidMethod<method, type, RawMethod>(id);
60 } // namespace pw::rpc::internal