mbed-os/features/cellular/framework/common/CellularList.h

113 lines
2.2 KiB
C
Raw Normal View History

2018-02-09 11:24:27 +00:00
/*
* Copyright (c) 2017, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* 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
*
* http://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.
*/
#ifndef CELLULAR_LIST_H_
#define CELLULAR_LIST_H_
#include <stddef.h>
namespace mbed {
/** Class CellularList
*
* Templated linked list class for common usage.
*
*/
2018-07-27 12:33:20 +00:00
template <class T> class CellularList {
2018-02-09 11:24:27 +00:00
private:
T *_head, *_tail;
public:
CellularList()
{
2018-07-27 12:33:20 +00:00
_head = NULL;
_tail = NULL;
2018-02-09 11:24:27 +00:00
}
2018-07-27 12:33:20 +00:00
~CellularList()
{
T *temp = _head;
while (temp) {
_head = _head->next;
delete temp;
temp = _head;
}
}
2018-07-27 12:33:20 +00:00
T *add_new()
2018-02-09 11:24:27 +00:00
{
2018-07-27 12:33:20 +00:00
T *temp = new T;
if (!temp) {
return NULL;
}
temp->next = NULL;
if (_head == NULL) {
_head = temp;
} else {
_tail->next = temp;
}
_tail = temp;
return _tail;
2018-02-09 11:24:27 +00:00
}
void delete_last()
{
2018-07-27 12:33:20 +00:00
T *previous = NULL;
T *current = _head;
2018-02-09 11:24:27 +00:00
if (!current) {
return;
}
while (current->next != NULL) {
2018-07-27 12:33:20 +00:00
previous = current;
current = current->next;
2018-02-09 11:24:27 +00:00
}
if (previous) {
2018-07-27 12:33:20 +00:00
_tail = previous;
previous->next = NULL;
2018-02-09 11:24:27 +00:00
} else {
_head = NULL;
_tail = NULL;
}
delete current;
}
void delete_all()
{
T *temp = _head;
while (temp) {
_head = _head->next;
delete temp;
temp = _head;
}
2018-07-27 12:33:20 +00:00
_tail = NULL;
2018-02-09 11:24:27 +00:00
}
T *get_head()
{
return _head;
}
};
} // namespace mbed
#endif // CELLULAR_LIST_H_