a458f447825418afb9c720910a7b8acc7b0acef5
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_rpc / nanopb / public / pw_rpc / internal / service_method_traits.h
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/nanopb_method.h"
17
18 namespace pw::rpc::internal {
19
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::*);
24
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 {
31  public:
32   ServiceMethodTraits() = delete;
33
34   // Type of the service implementation derived class.
35   using Service = typename internal::RpcTraits<decltype(impl_method)>::Service;
36
37   // Type of the generic service base class.
38   using BaseService =
39       decltype(BaseFromMember(&Service::_PwRpcInternalGeneratedBase));
40
41   // Reference to the Method object corresponding to this method.
42   static constexpr const NanopbMethod& method() {
43     return *BaseService::template MethodFor<impl_method>();
44   }
45
46   static_assert(BaseService::template MethodFor<impl_method>() != nullptr,
47                 "The selected function is not an RPC service method");
48 };
49
50 }  // namespace pw::rpc::internal