ROS 2 rclcpp + rcl - kilted  kilted
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/timer.hpp"
29 #include "rclcpp/visibility_control.hpp"
30 #include "rclcpp/wait_set_policies/detail/storage_policy_common.hpp"
31 #include "rclcpp/waitable.hpp"
32 
33 namespace rclcpp
34 {
35 namespace wait_set_policies
36 {
37 
39 
43 template<
44  std::size_t NumberOfSubscriptions,
45  std::size_t NumberOfGuardCondtions,
46  std::size_t NumberOfTimers,
47  std::size_t NumberOfClients,
48  std::size_t NumberOfServices,
49  std::size_t NumberOfWaitables
50 >
52 {
53 protected:
54  using is_mutable = std::false_type;
55 
57  {
58 public:
59  std::shared_ptr<rclcpp::SubscriptionBase> subscription;
61 
64  std::shared_ptr<rclcpp::SubscriptionBase> subscription_in = nullptr,
66  : subscription(std::move(subscription_in)),
67  mask(mask_in)
68  {}
69  };
70  using ArrayOfSubscriptions = std::array<
71  SubscriptionEntry,
72  NumberOfSubscriptions
73  >;
74  using SubscriptionsIterable = ArrayOfSubscriptions;
75 
76  using ArrayOfGuardConditions = std::array<
77  std::shared_ptr<rclcpp::GuardCondition>,
78  NumberOfGuardCondtions
79  >;
80  using GuardConditionsIterable = ArrayOfGuardConditions;
81 
82  using ArrayOfTimers = std::array<
83  std::shared_ptr<rclcpp::TimerBase>,
84  NumberOfTimers
85  >;
86  using TimersIterable = ArrayOfTimers;
87 
88  using ArrayOfClients = std::array<
89  std::shared_ptr<rclcpp::ClientBase>,
90  NumberOfClients
91  >;
92  using ClientsIterable = ArrayOfClients;
93 
94  using ArrayOfServices = std::array<
95  std::shared_ptr<rclcpp::ServiceBase>,
96  NumberOfServices
97  >;
98  using ServicesIterable = ArrayOfServices;
99 
101  {
104  std::shared_ptr<rclcpp::Waitable> waitable_in = nullptr,
105  std::shared_ptr<void> associated_entity_in = nullptr) noexcept
106  : waitable(std::move(waitable_in)),
107  associated_entity(std::move(associated_entity_in))
108  {}
109 
110  std::shared_ptr<rclcpp::Waitable> waitable;
111  std::shared_ptr<void> associated_entity;
112  };
113  using ArrayOfWaitables = std::array<
114  WaitableEntry,
115  NumberOfWaitables
116  >;
117  using WaitablesIterable = ArrayOfWaitables;
118 
119  template<class ArrayOfExtraGuardConditions>
120  explicit
122  const ArrayOfSubscriptions & subscriptions,
123  const ArrayOfGuardConditions & guard_conditions,
124  const ArrayOfExtraGuardConditions & extra_guard_conditions,
125  const ArrayOfTimers & timers,
126  const ArrayOfClients & clients,
127  const ArrayOfServices & services,
128  const ArrayOfWaitables & waitables,
129  rclcpp::Context::SharedPtr context
130  )
131  : StoragePolicyCommon(
132  subscriptions,
133  guard_conditions,
134  extra_guard_conditions,
135  timers,
136  clients,
137  services,
138  waitables,
139  context),
140  subscriptions_(subscriptions),
141  guard_conditions_(guard_conditions),
142  timers_(timers),
143  clients_(clients),
144  services_(services),
145  waitables_(waitables)
146  {}
147 
148  ~StaticStorage() = default;
149 
150  template<class ArrayOfExtraGuardConditions>
151  void
152  storage_rebuild_rcl_wait_set(const ArrayOfExtraGuardConditions & extra_guard_conditions)
153  {
155  subscriptions_,
156  guard_conditions_,
157  extra_guard_conditions,
158  timers_,
159  clients_,
160  services_,
161  waitables_
162  );
163 
164  if(this->needs_pruning_) {
165  // we need to throw here, as the indexing of the rcl_waitset is broken,
166  // in case of invalid entries
167 
168  throw std::runtime_error(
169  "StaticStorage::storage_rebuild_rcl_wait_set(): entity weak_ptr "
170  "unexpectedly expired in static entity storage");
171  }
172  }
173 
174  // storage_add_subscription() explicitly not declared here
175  // storage_remove_subscription() explicitly not declared here
176  // storage_add_guard_condition() explicitly not declared here
177  // storage_remove_guard_condition() explicitly not declared here
178  // storage_add_timer() explicitly not declared here
179  // storage_remove_timer() explicitly not declared here
180  // storage_add_client() explicitly not declared here
181  // storage_remove_client() explicitly not declared here
182  // storage_add_service() explicitly not declared here
183  // storage_remove_service() explicitly not declared here
184  // storage_add_waitable() explicitly not declared here
185  // storage_remove_waitable() explicitly not declared here
186  // storage_prune_deleted_entities() explicitly not declared here
187 
188  void
189  storage_acquire_ownerships()
190  {
191  // Explicitly do nothing.
192  }
193 
194  void
195  storage_release_ownerships()
196  {
197  // Explicitly do nothing.
198  }
199 
200  size_t size_of_subscriptions() const
201  {
202  return subscriptions_.size();
203  }
204 
205  size_t size_of_timers() const
206  {
207  return timers_.size();
208  }
209 
210  size_t size_of_clients() const
211  {
212  return clients_.size();
213  }
214 
215  size_t size_of_services() const
216  {
217  return services_.size();
218  }
219 
220  size_t size_of_waitables() const
221  {
222  return waitables_.size();
223  }
224 
225  typename ArrayOfSubscriptions::value_type
226  subscriptions(size_t ii) const
227  {
228  return subscriptions_[ii];
229  }
230 
231  typename ArrayOfTimers::value_type
232  timers(size_t ii) const
233  {
234  return timers_[ii];
235  }
236 
237  typename ArrayOfClients::value_type
238  clients(size_t ii) const
239  {
240  return clients_[ii];
241  }
242 
243  typename ArrayOfServices::value_type
244  services(size_t ii) const
245  {
246  return services_[ii];
247  }
248 
249  typename ArrayOfWaitables::value_type
250  waitables(size_t ii) const
251  {
252  return waitables_[ii];
253  }
254 
255  const ArrayOfSubscriptions subscriptions_;
256  const ArrayOfGuardConditions guard_conditions_;
257  const ArrayOfTimers timers_;
258  const ArrayOfClients clients_;
259  const ArrayOfServices services_;
260  const ArrayOfWaitables waitables_;
261 };
262 
263 } // namespace wait_set_policies
264 } // namespace rclcpp
265 
266 #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.