Kev Mann / mbed-dev-OS5_10_4
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         if (!temp) {
00054             return NULL;
00055         }
00056         temp->next = NULL;
00057         if (_head == NULL) {
00058             _head = temp;
00059         } else {
00060             _tail->next = temp;
00061         }
00062         _tail = temp;
00063 
00064         return _tail;
00065     }
00066 
00067     void delete_last()
00068     {
00069         T *previous = NULL;
00070         T *current = _head;
00071 
00072         if (!current) {
00073             return;
00074         }
00075 
00076         while (current->next != NULL) {
00077             previous = current;
00078             current = current->next;
00079         }
00080 
00081         if (previous) {
00082             _tail = previous;
00083             previous->next = NULL;
00084         } else {
00085             _head = NULL;
00086             _tail = NULL;
00087         }
00088 
00089         delete current;
00090     }
00091 
00092     void delete_all()
00093     {
00094         T *temp = _head;
00095         while (temp) {
00096             _head = _head->next;
00097             delete temp;
00098             temp = _head;
00099         }
00100         _tail = NULL;
00101     }
00102 
00103 
00104     T *get_head()
00105     {
00106         return _head;
00107     }
00108 };
00109 
00110 } // namespace mbed
00111 
00112 #endif // CELLULAR_LIST_H_