aboutsummaryrefslogtreecommitdiff
path: root/pw_rpc/public/pw_rpc/internal/channel_list.h
blob: c21224b732e031c4cdb98685c5be8b01b296e4e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2022 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
#pragma once

#include <span>
#include <vector>

#include "pw_rpc/internal/channel.h"
#include "pw_rpc/internal/config.h"

namespace pw::rpc::internal {

#if PW_RPC_DYNAMIC_ALLOCATION && !defined(__cpp_lib_constexpr_vector)
#define _PW_RPC_CONSTEXPR
#else
#define _PW_RPC_CONSTEXPR constexpr
#endif  // PW_RPC_DYNAMIC_ALLOCATION && !defined(__cpp_lib_constexpr_vector)

class ChannelList {
 public:
  _PW_RPC_CONSTEXPR ChannelList(std::span<Channel> channels)
      : channels_(channels.begin(), channels.end()) {}

  // Returns the first channel with the matching ID or nullptr if none match.
  // Except for Channel::kUnassignedChannelId, there should be no duplicate
  // channels.
  const Channel* Get(uint32_t channel_id) const;

  Channel* Get(uint32_t channel_id) {
    return const_cast<Channel*>(
        static_cast<const ChannelList&>(*this).Get(channel_id));
  }

  // Adds the channel with the requested ID to the list. Returns:
  //
  //   OK - the channel was added
  //   ALREADY_EXISTS - a channel with this ID is already present; remove it
  //       first
  //   RESOURCE_EXHAUSTED - no unassigned channels are available; only possible
  //       if PW_RPC_DYNAMIC_ALLOCATION is disabled
  //
  Status Add(uint32_t channel_id, ChannelOutput& output);

  // Removes the channel with the requested ID. Returns:
  //
  //   OK - the channel was removed
  //   NOT_FOUND - no channel with the provided ID was found
  //
  Status Remove(uint32_t channel_id);

#if PW_RPC_DYNAMIC_ALLOCATION
  std::vector<Channel> channels_;
#else
  std::span<Channel> channels_;
#endif  // PW_RPC_DYNAMIC_ALLOCATION
};

}  // namespace pw::rpc::internal