LinkedList.h revision 034c475931e8e4da54b499c0056121490f029865
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef LIBMEMUNREACHABLE_LINKED_LIST_H_
18#define LIBMEMUNREACHABLE_LINKED_LIST_H_
19
20template<class T>
21class LinkedList {
22public:
23    LinkedList() : next_(this), prev_(this), data_() {}
24    explicit LinkedList(T data) : LinkedList() {
25        data_ = data;
26    }
27    ~LinkedList() {}
28    void insert(LinkedList<T>& node) {
29        assert(node.empty());
30        node.next_ = this->next_;
31        node.next_->prev_ = &node;
32        this->next_ = &node;
33        node.prev_ = this;
34    }
35    void remove() {
36        this->next_->prev_ = this->prev_;
37        this->prev_->next_ = this->next_;
38        this->next_ = this;
39        this->prev_ = this;
40    }
41    T data() { return data_; }
42    bool empty() { return next_ == this && prev_ == this; }
43    LinkedList<T> *next() { return next_; }
44private:
45    LinkedList<T> *next_;
46    LinkedList<T> *prev_;
47    T data_;
48};
49
50template<class T>
51class LinkedListHead {
52public:
53    LinkedListHead() : node_() {}
54    ~LinkedListHead() {}
55
56private:
57    LinkedList<T> node_;
58};
59
60#endif
61