pw_rpc: Remove GeneratedService alias
- GeneratedService was used to find the generated service base class
from a service implementation. The MethodLookup class is friended by
the generated service base, so MethodLookup can directly access the
methods array from the derived service implementation, without using
GeneratedService.
- Rename kMethods to kPwRpcMethods avoid potential conflicts with
members in derived service classes.
Change-Id: Ifc80177df195e96f6046a17846673fe89d6f861c
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/61797
Commit-Queue: Wyatt Hepler <hepler@google.com>
Pigweed-Auto-Submit: Wyatt Hepler <hepler@google.com>
Reviewed-by: Alexei Frolov <frolv@google.com>
diff --git a/pw_rpc/public/pw_rpc/internal/method.h b/pw_rpc/public/pw_rpc/internal/method.h
index e5db6df..da88d39 100644
--- a/pw_rpc/public/pw_rpc/internal/method.h
+++ b/pw_rpc/public/pw_rpc/internal/method.h
@@ -128,15 +128,5 @@
}
}
-// Identifies a base class from a member function it defines. This should be
-// used with decltype to retrieve the base service class.
-template <typename T, typename U>
-T BaseFromMember(U T::*);
-
-// The base generated service of an RPC service class.
-template <typename Service>
-using GeneratedService =
- decltype(BaseFromMember(&Service::_PwRpcInternalGeneratedBase));
-
} // namespace internal
} // namespace pw::rpc
diff --git a/pw_rpc/public/pw_rpc/internal/method_lookup.h b/pw_rpc/public/pw_rpc/internal/method_lookup.h
index 7383a01..be07455 100644
--- a/pw_rpc/public/pw_rpc/internal/method_lookup.h
+++ b/pw_rpc/public/pw_rpc/internal/method_lookup.h
@@ -25,6 +25,8 @@
// union member in a constant expression, so this results in a compiler error.
class MethodLookup {
public:
+ MethodLookup() = delete;
+
template <typename Service, uint32_t kMethodId>
static constexpr const auto& GetRawMethod() {
const auto& method = GetMethodUnion<Service, kMethodId>().raw_method();
@@ -49,12 +51,11 @@
}
template <typename Service>
- static constexpr
- typename decltype(GeneratedService<Service>::kMethods)::const_pointer
- GetMethodUnionPointer(uint32_t kMethodId) {
- for (size_t i = 0; i < GeneratedService<Service>::kMethodIds.size(); ++i) {
- if (GeneratedService<Service>::kMethodIds[i] == kMethodId) {
- return &GeneratedService<Service>::kMethods[i];
+ static constexpr typename decltype(Service::kPwRpcMethods)::const_pointer
+ GetMethodUnionPointer(uint32_t kMethodId) {
+ for (size_t i = 0; i < Service::kPwRpcMethodIds.size(); ++i) {
+ if (Service::kPwRpcMethodIds[i] == kMethodId) {
+ return &Service::kPwRpcMethods[i];
}
}
return nullptr;
diff --git a/pw_rpc/py/pw_rpc/codegen.py b/pw_rpc/py/pw_rpc/codegen.py
index e28643c..554adef 100644
--- a/pw_rpc/py/pw_rpc/codegen.py
+++ b/pw_rpc/py/pw_rpc/codegen.py
@@ -299,16 +299,12 @@
f'{{ return "{service.name()}"; }}')
gen.line()
- gen.line(
- '// Used by MethodLookup to identify the generated service base.')
- gen.line('constexpr void _PwRpcInternalGeneratedBase() const {}')
- gen.line()
gen.line(' protected:')
with gen.indent():
- gen.line(
- f'constexpr Service() : {base_class}(kServiceId, kMethods) {{}}')
+ gen.line('constexpr Service() : '
+ f'{base_class}(kServiceId, kPwRpcMethods) {{}}')
gen.line()
gen.line(' private:')
@@ -320,7 +316,7 @@
# Generate the method table
gen.line('static constexpr std::array<'
f'{RPC_NAMESPACE}::internal::{gen.method_union_name()},'
- f' {len(service.methods())}> kMethods = {{')
+ f' {len(service.methods())}> kPwRpcMethods = {{')
with gen.indent(4):
for method in service.methods():
@@ -338,7 +334,7 @@
def _method_lookup_table(gen: CodeGenerator, service: ProtoService) -> None:
"""Generates array of method IDs for looking up methods at compile time."""
gen.line('static constexpr std::array<uint32_t, '
- f'{len(service.methods())}> kMethodIds = {{')
+ f'{len(service.methods())}> kPwRpcMethodIds = {{')
with gen.indent(4):
for method in service.methods():