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
20 #include "pw_containers/intrusive_list.h"
21 #include "pw_rpc/internal/method.h"
22 #include "pw_rpc/internal/method_union.h"
26 // Base class for all RPC services. This cannot be instantiated directly; use a
27 // generated subclass instead.
29 // Services store a span of concrete method implementation classes. To support
30 // different Method implementations, Service stores a base MethodUnion* and the
31 // size of the concrete MethodUnion object.
32 class Service : public IntrusiveList<Service>::Item {
34 uint32_t id() const { return id_; }
37 template <typename T, size_t method_count>
38 constexpr Service(uint32_t id, const std::array<T, method_count>& methods)
40 methods_(methods.data()),
41 method_size_(sizeof(T)),
42 method_count_(static_cast<uint16_t>(method_count)) {
43 static_assert(method_count <= std::numeric_limits<uint16_t>::max());
46 // For use by tests with only one method.
48 constexpr Service(uint32_t id, const T& method)
49 : id_(id), methods_(&method), method_size_(sizeof(T)), method_count_(1) {}
53 friend class ServiceTestHelper;
55 // Finds the method with the provided method_id. Returns nullptr if no match.
56 const internal::Method* FindMethod(uint32_t method_id) const;
59 const internal::MethodUnion* const methods_;
60 const uint16_t method_size_;
61 const uint16_t method_count_;
64 } // namespace pw::rpc