Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CellularList.h Source File

CellularList.h

00001 /*
00002  * Copyright (c) 2017, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #ifndef CELLULAR_LIST_H_
00019 #define CELLULAR_LIST_H_
00020 
00021 #include <stddef.h>
00022 
00023 namespace mbed {
00024 
00025 /** Class CellularList
00026  *
00027  *  Templated linked list class for common usage.
00028  *
00029  */
00030 template <class T> class CellularList {
00031 private:
00032     T *_head, *_tail;
00033 public:
00034     CellularList()
00035     {
00036         _head = NULL;
00037         _tail = NULL;
00038     }
00039 
00040     ~CellularList()
00041     {
00042         T *temp = _head;
00043         while (temp) {
00044             _head = _head->next;
00045             delete temp;
00046             temp = _head;
00047         }
00048     }
00049 
00050     T *add_new()
00051     {
00052         T *temp = new T;
00053         temp->next = NULL;
00054         if (_head == NULL) {
00055             _head = temp;
00056         } else {
00057             _tail->next = temp;
00058         }
00059         _tail = temp;
00060 
00061         return _tail;
00062     }
00063 
00064     void delete_last()
00065     {
00066         T *previous = NULL;
00067         T *current = _head;
00068 
00069         if (!current) {
00070             return;
00071         }
00072 
00073         while (current->next != NULL) {
00074             previous = current;
00075             current = current->next;
00076         }
00077 
00078         if (previous) {
00079             _tail = previous;
00080             previous->next = NULL;
00081         } else {
00082             _head = NULL;
00083             _tail = NULL;
00084         }
00085 
00086         delete current;
00087     }
00088 
00089     void delete_all()
00090     {
00091         T *temp = _head;
00092         while (temp) {
00093             _head = _head->next;
00094             delete temp;
00095             temp = _head;
00096         }
00097         _tail = NULL;
00098     }
00099 
00100 
00101     T *get_head()
00102     {
00103         return _head;
00104     }
00105 };
00106 
00107 } // namespace mbed
00108 
00109 #endif // CELLULAR_LIST_H_