ROS 2 rclcpp + rcl - rolling  rolling-20536064
ROS 2 C++ Client Library with ROS Client Library
static_storage.hpp
1 // Copyright 2020 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://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,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef RCLCPP__WAIT_SET_POLICIES__STATIC_STORAGE_HPP_
16 #define RCLCPP__WAIT_SET_POLICIES__STATIC_STORAGE_HPP_
17 
18 #include <array>
19 #include <memory>
20 #include <utility>
21 
22 #include "rclcpp/client.hpp"
23 #include "rclcpp/guard_condition.hpp"
24 #include "rclcpp/macros.hpp"
25 #include "rclcpp/service.hpp"
26 #include "rclcpp/subscription_base.hpp"
27 #include "rclcpp/subscription_wait_set_mask.hpp"
28 #include "rclcpp/visibility_control.hpp"
29 #include "rclcpp/wait_set_policies/detail/storage_policy_common.hpp"
30 #include "rclcpp/waitable.hpp"
31 
32 namespace rclcpp
33 {
34 namespace wait_set_policies
35 {
36 
38 
42 template<
43  std::size_t NumberOfSubscriptions,
44  std::size_t NumberOfGuardCondtions,
45  std::size_t NumberOfTimers,
46  std::size_t NumberOfClients,
47  std::size_t NumberOfServices,
48  std::size_t NumberOfWaitables
49 >
51 {
52 protected:
53  using is_mutable = std::false_type;
54 
56  {
57 public:
58  std::shared_ptr<rclcpp::SubscriptionBase> subscription;
60 
63  std::shared_ptr<rclcpp::SubscriptionBase> subscription_in = nullptr,
65  : subscription(std::move(subscription_in)),
66  mask(mask_in)
67  {}
68  };
69  using ArrayOfSubscriptions = std::array<
70  SubscriptionEntry,
71  NumberOfSubscriptions
72  >;
73  using SubscriptionsIterable = ArrayOfSubscriptions;
74 
75  using ArrayOfGuardConditions = std::array<
76  std::shared_ptr<rclcpp::GuardCondition>,
77  NumberOfGuardCondtions
78  >;
79  using GuardConditionsIterable = ArrayOfGuardConditions;
80 
81  using ArrayOfTimers = std::array<
82  std::shared_ptr<rclcpp::TimerBase>,
83  NumberOfTimers
84  >;
85  using TimersIterable = ArrayOfTimers;
86 
87  using ArrayOfClients = std::array<
88  std::shared_ptr<rclcpp::ClientBase>,
89  NumberOfClients
90  >;
91  using ClientsIterable = ArrayOfClients;
92 
93  using ArrayOfServices = std::array<
94  std::shared_ptr<rclcpp::ServiceBase>,
95  NumberOfServices
96  >;
97  using ServicesIterable = ArrayOfServices;
98 
100  {
103  std::shared_ptr<rclcpp::Waitable> waitable_in = nullptr,
104  std::shared_ptr<void> associated_entity_in = nullptr) noexcept
105  : waitable(std::move(waitable_in)),
106  associated_entity(std::move(associated_entity_in))
107  {}
108 
109  std::shared_ptr<rclcpp::Waitable> waitable;
110  std::shared_ptr<void> associated_entity;
111  };
112  using ArrayOfWaitables = std::array<
113  WaitableEntry,
114  NumberOfWaitables
115  >;
116  using WaitablesIterable = ArrayOfWaitables;
117 
118  template<class ArrayOfExtraGuardConditions>
119  explicit
121  const ArrayOfSubscriptions & subscriptions,
122  const ArrayOfGuardConditions & guard_conditions,
123  const ArrayOfExtraGuardConditions & extra_guard_conditions,
124  const ArrayOfTimers & timers,
125  const ArrayOfClients & clients,
126  const ArrayOfServices & services,
127  const ArrayOfWaitables & waitables,
128  rclcpp::Context::SharedPtr context
129  )
130  : StoragePolicyCommon(
131  subscriptions,
132  guard_conditions,
133  extra_guard_conditions,
134  timers,
135  clients,
136  services,
137  waitables,
138  context),
139  subscriptions_(subscriptions),
140  guard_conditions_(guard_conditions),
141  timers_(timers),
142  clients_(clients),
143  services_(services),
144  waitables_(waitables)
145  {}
146 
147  ~StaticStorage() = default;
148 
149  template<class ArrayOfExtraGuardConditions>
150  void
151  storage_rebuild_rcl_wait_set(const ArrayOfExtraGuardConditions & extra_guard_conditions)
152  {
154  subscriptions_,
155  guard_conditions_,
156  extra_guard_conditions,
157  timers_,
158  clients_,
159  services_,
160  waitables_
161  );
162 
163  if(this->needs_pruning_) {
164  // we need to throw here, as the indexing of the rcl_waitset is broken,
165  // in case of invalid entries
166 
167  throw std::runtime_error(
168  "StaticStorage::storage_rebuild_rcl_wait_set(): entity weak_ptr "
169  "unexpectedly expired in static entity storage");
170  }
171  }
172 
173  // storage_add_subscription() explicitly not declared here
174  // storage_remove_subscription() explicitly not declared here
175  // storage_add_guard_condition() explicitly not declared here
176  // storage_remove_guard_condition() explicitly not declared here
177  // storage_add_timer() explicitly not declared here
178  // storage_remove_timer() explicitly not declared here
179  // storage_add_client() explicitly not declared here
180  // storage_remove_client() explicitly not declared here
181  // storage_add_service() explicitly not declared here
182  // storage_remove_service() explicitly not declared here
183  // storage_add_waitable() explicitly not declared here
184  // storage_remove_waitable() explicitly not declared here
185  // storage_prune_deleted_entities() explicitly not declared here
186 
187  void
188  storage_acquire_ownerships()
189  {
190  // Explicitly do nothing.
191  }
192 
193  void
194  storage_release_ownerships()
195  {
196  // Explicitly do nothing.
197  }
198 
199  size_t size_of_subscriptions() const
200  {
201  return subscriptions_.size();
202  }
203 
204  size_t size_of_timers() const
205  {
206  return timers_.size();
207  }
208 
209  size_t size_of_clients() const
210  {
211  return clients_.size();
212  }
213 
214  size_t size_of_services() const
215  {
216  return services_.size();
217  }
218 
219  size_t size_of_waitables() const
220  {
221  return waitables_.size();
222  }
223 
224  typename ArrayOfSubscriptions::value_type
225  subscriptions(size_t ii) const
226  {
227  return subscriptions_[ii];
228  }
229 
230  typename ArrayOfTimers::value_type
231  timers(size_t ii) const
232  {
233  return timers_[ii];
234  }
235 
236  typename ArrayOfClients::value_type
237  clients(size_t ii) const
238  {
239  return clients_[ii];
240  }
241 
242  typename ArrayOfServices::value_type
243  services(size_t ii) const
244  {
245  return services_[ii];
246  }
247 
248  typename ArrayOfWaitables::value_type
249  waitables(size_t ii) const
250  {
251  return waitables_[ii];
252  }
253 
254  const ArrayOfSubscriptions subscriptions_;
255  const ArrayOfGuardConditions guard_conditions_;
256  const ArrayOfTimers timers_;
257  const ArrayOfClients clients_;
258  const ArrayOfServices services_;
259  const ArrayOfWaitables waitables_;
260 };
261 
262 } // namespace wait_set_policies
263 } // namespace rclcpp
264 
265 #endif // RCLCPP__WAIT_SET_POLICIES__STATIC_STORAGE_HPP_
Options used to determine what parts of a subscription get added to or removed from a wait set.
SubscriptionEntry(std::shared_ptr< rclcpp::SubscriptionBase > subscription_in=nullptr, rclcpp::SubscriptionWaitSetMask mask_in={})
Conversion constructor, which is intentionally not marked explicit.
WaitSet policy that explicitly provides fixed sized storage only.
Common structure for storage policies, which provides rcl wait set access.
void storage_rebuild_rcl_wait_set_with_sets(const SubscriptionsIterable &subscriptions, const GuardConditionsIterable &guard_conditions, const ExtraGuardConditionsIterable &extra_guard_conditions, const TimersIterable &timers, const ClientsIterable &clients, const ServicesIterable &services, const WaitablesIterable &waitables)
Rebuild the wait set, preparing it for the next wait call.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
WaitableEntry(std::shared_ptr< rclcpp::Waitable > waitable_in=nullptr, std::shared_ptr< void > associated_entity_in=nullptr) noexcept
Conversion constructor, which is intentionally not marked explicit.