Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_rpc / public / pw_rpc / service.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 <cstdint>
17 #include <limits>
18 #include <span>
19
20 #include "pw_containers/intrusive_list.h"
21 #include "pw_rpc/internal/method.h"
22 #include "pw_rpc/internal/method_union.h"
23
24 namespace pw::rpc {
25
26 // Base class for all RPC services. This cannot be instantiated directly; use a
27 // generated subclass instead.
28 //
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 {
33  public:
34   uint32_t id() const { return id_; }
35
36  protected:
37   template <typename T, size_t method_count>
38   constexpr Service(uint32_t id, const std::array<T, method_count>& methods)
39       : id_(id),
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());
44   }
45
46   // For use by tests with only one method.
47   template <typename T>
48   constexpr Service(uint32_t id, const T& method)
49       : id_(id), methods_(&method), method_size_(sizeof(T)), method_count_(1) {}
50
51  private:
52   friend class Server;
53   friend class ServiceTestHelper;
54
55   // Finds the method with the provided method_id. Returns nullptr if no match.
56   const internal::Method* FindMethod(uint32_t method_id) const;
57
58   const uint32_t id_;
59   const internal::MethodUnion* const methods_;
60   const uint16_t method_size_;
61   const uint16_t method_count_;
62 };
63
64 }  // namespace pw::rpc