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/nanopb_method.h"
18 namespace pw::rpc::internal {
20 // Identifies a base class from a member function it defines. This should be
21 // used with decltype to retrieve the base class.
22 template <typename T, typename U>
23 T BaseFromMember(U T::*);
25 // Gets information about a service and method at compile-time. Uses a pointer
26 // to a member function of the service implementation to identify the service
27 // class, generated service class, and Method object.This class is friended by
28 // the generated service classes to give it access to the internal method list.
29 template <auto impl_method>
30 class ServiceMethodTraits {
32 ServiceMethodTraits() = delete;
34 // Type of the service implementation derived class.
35 using Service = typename internal::RpcTraits<decltype(impl_method)>::Service;
37 // Type of the generic service base class.
39 decltype(BaseFromMember(&Service::_PwRpcInternalGeneratedBase));
41 // Reference to the Method object corresponding to this method.
42 static constexpr const NanopbMethod& method() {
43 return *BaseService::template MethodFor<impl_method>();
46 static_assert(BaseService::template MethodFor<impl_method>() != nullptr,
47 "The selected function is not an RPC service method");
50 } // namespace pw::rpc::internal