Firmware
SubscriptionArray.hpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2017 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
42 #pragma once
43 
44 #include <uORB/Subscription.hpp>
45 
47 {
48 public:
49  SubscriptionArray() = default;
51 
59  template<class T>
60  bool get(const struct orb_metadata *meta, uORB::Subscription<T> *&subscription, unsigned instance = 0);
61 
65  void update();
66 
70  void forcedUpdate();
71 
72 private:
73  void cleanup();
74 
75  bool resizeSubscriptions();
76 
77  uORB::SubscriptionNode **_subscriptions{nullptr};
78  int _subscriptions_count{0};
79  int _subscriptions_size{0};
80 };
81 
82 
83 template<class T>
84 bool SubscriptionArray::get(const struct orb_metadata *meta, uORB::Subscription<T> *&subscription, unsigned instance)
85 {
86  // does it already exist?
87  for (int i = 0; i < _subscriptions_count; ++i) {
88  if (_subscriptions[i]->getMeta() == meta && _subscriptions[i]->getInstance() == instance) {
89  // we know the type must be correct, so we can use reinterpret_cast (dynamic_cast is not available)
90  subscription = reinterpret_cast<uORB::Subscription<T>*>(_subscriptions[i]);
91  return true;
92  }
93  }
94 
95  // resize if needed
96  if (_subscriptions_count >= _subscriptions_size) {
97  if (!resizeSubscriptions()) {
98  subscription = nullptr;
99  return false;
100  }
101  }
102 
103  subscription = new uORB::Subscription<T>(meta, 0, instance);
104 
105  if (!subscription) {
106  return false;
107  }
108 
109  _subscriptions[_subscriptions_count++] = subscription;
110  return true;
111 }
void update()
update all subscriptions (if new data is available)
Definition: SubscriptionArray.cpp:73
The subscription base class as a list node.
Definition: Subscription.hpp:107
void forcedUpdate()
update all subscriptions
Definition: SubscriptionArray.cpp:80
Object metadata.
Definition: uORB.h:50
Subscription wrapper class.
Definition: Subscription.hpp:143
Definition: SubscriptionArray.hpp:46
bool get(const struct orb_metadata *meta, uORB::Subscription< T > *&subscription, unsigned instance=0)
Get a subscription.
Definition: SubscriptionArray.hpp:84