Fixed some C/C++ compiler errors due to stricter checks.
[rubinius.git] / machine / linkedlist.hpp
blobe76e59f6e20dd4050589caaa8dcaf9e31a341c45
1 #ifndef RBX_LINKEDLIST_HPP
2 #define RBX_LINKEDLIST_HPP
4 #include "spinlock.hpp"
6 #include <stdio.h>
7 #include <assert.h>
8 #include <mutex>
10 namespace rubinius{
11 class LinkedList {
12 public:
14 class Node {
15 private:
16 Node* next_;
17 Node* prev_;
19 public:
20 Node()
21 : next_(NULL)
22 , prev_(NULL)
23 { }
25 Node* next() const {
26 return next_;
29 Node* prev() const {
30 return prev_;
33 void set_next(Node* n) {
34 next_ = n;
37 void set_prev(Node* n) {
38 prev_ = n;
41 void remove_linkage() {
42 if(next_) {
43 assert(next_->prev() == this);
44 next_->set_prev(prev_);
47 if(prev_) {
48 assert(prev_->next() == this);
49 prev_->set_next(next_);
52 next_ = NULL;
53 prev_ = NULL;
57 private:
58 Node* head_;
59 size_t count_;
60 locks::spinlock_mutex lock_;
62 public:
63 LinkedList()
64 : head_(NULL)
65 , count_(0)
66 , lock_()
67 { }
69 Node* head() {
70 std::lock_guard<locks::spinlock_mutex> guard(lock_);
72 return head_;
75 size_t size() {
76 std::lock_guard<locks::spinlock_mutex> guard(lock_);
78 return count_;
81 void add(Node*);
82 void remove(Node*);
84 // Utility templates
85 template <typename Roots, typename Root>
86 class Iterator {
87 Roots& roots_;
88 Root* current_;
90 public:
91 Iterator(Roots& roots)
92 : roots_(roots)
93 , current_(roots.front())
96 bool more() const {
97 return current_ != 0;
100 void advance() {
101 current_ = static_cast<Root*>(current_->next());
104 Root* operator->() const {
105 return current_;
108 Root* current() const {
109 return current_;
112 Root* next() const {
113 Root* ret = current_;
114 if(current_) {
115 current_ = static_cast<Root*>(current_->next());
118 return ret;
126 #endif