xref: /aosp_15_r20/external/pigweed/pw_rpc/public/pw_rpc/internal/method_lookup.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2020 The Pigweed Authors
2 //
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
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
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
13 // the License.
14 #pragma once
15 
16 #include "pw_rpc/internal/method.h"
17 
18 namespace pw::rpc::internal {
19 
20 // Gets a Method object from a generated RPC service class. Getter functions are
21 // provided for each supported method implementation. The
22 //
23 // To ensure the MethodUnion actually holds the requested method type, the
24 // method ID is accessed in a static_assert. It is invalid to access an unset
25 // union member in a constant expression, so this results in a compiler error.
26 class MethodLookup {
27  public:
28   MethodLookup() = delete;
29 
30   template <typename Service, uint32_t kMethodId>
GetRawMethod()31   static constexpr const auto& GetRawMethod() {
32     const auto& method = GetMethodUnion<Service, kMethodId>().raw_method();
33     static_assert(method.id() == kMethodId, "Incorrect method implementation");
34     return method;
35   }
36 
37   template <typename Service, uint32_t kMethodId>
GetPwpbMethod()38   static constexpr const auto& GetPwpbMethod() {
39     const auto& method = GetMethodUnion<Service, kMethodId>().pwpb_method();
40     static_assert(method.id() == kMethodId, "Incorrect method implementation");
41     return method;
42   }
43 
44   template <typename Service, uint32_t kMethodId>
GetNanopbMethod()45   static constexpr const auto& GetNanopbMethod() {
46     const auto& method = GetMethodUnion<Service, kMethodId>().nanopb_method();
47     static_assert(method.id() == kMethodId, "Incorrect method implementation");
48     return method;
49   }
50 
51  private:
52   template <typename Service, uint32_t kMethodId>
GetMethodUnion()53   static constexpr const auto& GetMethodUnion() {
54     constexpr auto method = GetMethodUnionPointer<Service>(kMethodId);
55 // TODO: b/285367496 - Remove this #ifndef guard when the static assert
56 // compiles correctly when using the Andestech RISC-V GCC 10.3.0 and
57 // 12.2.0 toolchains.
58 #if !(defined(__riscv) && defined(__nds_v5) &&       \
59       (((__GNUC__ == 10) && (__GNUC_MINOR__ == 3) && \
60         (__GNUC_PATCHLEVEL__ == 0)) ||               \
61        ((__GNUC__ == 12) && (__GNUC_MINOR__ == 2) && \
62         (__GNUC_PATCHLEVEL__ == 0))))
63     static_assert(method != nullptr,
64                   "The selected function is not an RPC service method");
65 #endif  // !(defined(__riscv) && defined(__nds_v5) && (((__GNUC__ == 10) &&
66         // (__GNUC_MINOR__ == 3) && (__GNUC_PATCHLEVEL__ == 0)) || ((__GNUC__ ==
67         // 12) && (__GNUC_MINOR__ == 2) && (__GNUC_PATCHLEVEL__ == 0))))
68 
69     return *method;
70   }
71 
72   template <typename Service>
decltype(Service::kPwRpcMethods)73   static constexpr typename decltype(Service::kPwRpcMethods)::const_pointer
74   GetMethodUnionPointer(uint32_t kMethodId) {
75     for (size_t i = 0; i < Service::kPwRpcMethodIds.size(); ++i) {
76       if (Service::kPwRpcMethodIds[i] == kMethodId) {
77         return &Service::kPwRpcMethods[i];
78       }
79     }
80     return nullptr;
81   }
82 };
83 
84 }  // namespace pw::rpc::internal
85