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_rpc/internal/method.h"
18 namespace pw::rpc::internal {
20 // Gets a Method object from a generated RPC service class. Getter functions are
21 // provided for each supported method implementation. The
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.
28 template <typename Service, uint32_t method_id>
29 static constexpr const auto& GetRawMethod() {
30 const auto& method = GetMethodUnion<Service, method_id>().raw_method();
31 static_assert(method.id() == method_id, "Incorrect method implementation");
35 template <typename Service, uint32_t method_id>
36 static constexpr const auto& GetNanopbMethod() {
37 const auto& method = GetMethodUnion<Service, method_id>().nanopb_method();
38 static_assert(method.id() == method_id, "Incorrect method implementation");
43 template <typename Service, uint32_t method_id>
44 static constexpr const auto& GetMethodUnion() {
45 constexpr auto method = GetMethodUnionPointer<Service>(method_id);
46 static_assert(method != nullptr,
47 "The selected function is not an RPC service method");
51 template <typename Service>
53 typename decltype(GeneratedService<Service>::kMethods)::const_pointer
54 GetMethodUnionPointer(uint32_t method_id) {
55 for (size_t i = 0; i < GeneratedService<Service>::kMethodIds.size(); ++i) {
56 if (GeneratedService<Service>::kMethodIds[i] == method_id) {
57 return &GeneratedService<Service>::kMethods[i];
64 } // namespace pw::rpc::internal