C/C++ API Reference
Loading...
Searching...
No Matches
method_info.h
1// Copyright 2022 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_info.h"
17#include "pw_rpc/method_id.h"
18#include "pw_rpc/service_id.h"
19
20namespace pw::rpc {
21
23
24// Collection of various helpers for RPC calls introspection. For now contains
25// only MethodRequestType/MethodResponseTypes types to obtain information about
26// RPC methods request/response types.
27//
28// Example. We have an RPC service:
29//
30// package some.package;
31// service SpecialService {
32// rpc MyMethod(MyMethodRequest) returns (MyMethodResponse) {}
33// }
34//
35// We also have a templated Storage type alias:
36//
37// template <auto kMethod>
38// using Storage =
39// std::pair<MethodRequestType<kMethod>, MethodResponseType<kMethod>>;
40//
41// Storage<some::package::pw_rpc::pwpb::SpecialService::MyMethod> will
42// instantiate as:
43//
44// std::pair<some::package::MyMethodRequest::Message,
45// some::package::MyMethodResponse::Message>;
46
47// Request type for given kMethod.
48template <auto kMethod>
49using MethodRequestType = typename internal::MethodInfo<kMethod>::Request;
50
51// Response type for given kMethod.
52template <auto kMethod>
53using MethodResponseType = typename internal::MethodInfo<kMethod>::Response;
54
55// Function which returns a serializer for given kMethod.
56// For e.g. `pwpb` methods, this returns a `const PwpbMethodSerde&`.
57template <auto kMethod>
58constexpr const auto& MethodSerde() {
59 return internal::MethodInfo<kMethod>::serde();
60}
61
62// Returns the identifier for this particular method.
63//
64// Identifiers are not guaranteed to be unique across services, so this should
65// be paired with a service ID when checking against packets which could target
66// different services.
67template <auto kMethod>
68constexpr MethodId GetMethodId() {
69 return internal::WrapMethodId(internal::MethodInfo<kMethod>::kMethodId);
70}
71
72// Returns the identifier for the service this method belongs to.
73template <auto kMethod>
74constexpr ServiceId GetServiceIdForMethod() {
75 return internal::WrapServiceId(internal::MethodInfo<kMethod>::kServiceId);
76}
77
79
80} // namespace pw::rpc