PX4 Firmware
PX4 Autopilot Software http://px4.io
ORBSet.hpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2015 Mark Charlebois. 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 
34 #pragma once
35 
36 class ORBSet
37 {
38 public:
39  struct Node {
40  struct Node *next;
41  const char *node_name;
42  };
43 
44  ORBSet() :
45  _top(nullptr),
46  _end(nullptr)
47  { }
49  {
50  while (_top != nullptr) {
52 
53  if (_top->next == nullptr) {
54  free((void *)_top->node_name);
55  free(_top);
56  _top = nullptr;
57  }
58  }
59  }
60  void insert(const char *node_name)
61  {
62  Node **p;
63 
64  if (_top == nullptr) {
65  p = &_top;
66 
67  } else {
68  p = &_end->next;
69  }
70 
71  *p = (Node *)malloc(sizeof(Node));
72 
73  if (_end) {
74  _end = _end->next;
75 
76  } else {
77  _end = _top;
78  }
79 
80  _end->next = nullptr;
81  _end->node_name = strdup(node_name);
82  }
83 
84  bool find(const char *node_name)
85  {
86  Node *p = _top;
87 
88  while (p) {
89  if (strcmp(p->node_name, node_name) == 0) {
90  return true;
91  }
92 
93  p = p->next;
94  }
95 
96  return false;
97  }
98 
99  bool erase(const char *node_name)
100  {
101  Node *p = _top;
102 
103  if (_top && (strcmp(_top->node_name, node_name) == 0)) {
104  p = _top->next;
105  free((void *)_top->node_name);
106  free(_top);
107  _top = p;
108 
109  if (_top == nullptr) {
110  _end = nullptr;
111  }
112 
113  return true;
114  }
115 
116  while (p->next) {
117  if (strcmp(p->next->node_name, node_name) == 0) {
118  unlinkNext(p);
119  return true;
120  }
121  }
122 
123  return false;
124  }
125 
126 private:
127 
128  void unlinkNext(Node *a)
129  {
130  Node *b = a->next;
131 
132  if (b != nullptr) {
133  if (_end == b) {
134  _end = a;
135  }
136 
137  a->next = b->next;
138  free((void *)b->node_name);
139  free(b);
140  }
141  }
142 
145 };
146 
Node * _end
Definition: ORBSet.hpp:144
Node * _top
Definition: ORBSet.hpp:143
void insert(const char *node_name)
Definition: ORBSet.hpp:60
struct Node * next
Definition: ORBSet.hpp:40
bool find(const char *node_name)
Definition: ORBSet.hpp:84
bool erase(const char *node_name)
Definition: ORBSet.hpp:99
const char * node_name
Definition: ORBSet.hpp:41
ORBSet()
Definition: ORBSet.hpp:44
~ORBSet()
Definition: ORBSet.hpp:48
void unlinkNext(Node *a)
Definition: ORBSet.hpp:128