RAUL  0.5.1
List.hpp
1 /* This file is part of Raul.
2  * Copyright (C) 2007 Dave Robillard <http://drobilla.net>
3  *
4  * Raul is free software; you can redistribute it and/or modify it under the
5  * terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * Raul is distributed in the hope that it will be useful, but WITHOUT ANY
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16  */
17 
18 #ifndef RAUL_LIST_HPP
19 #define RAUL_LIST_HPP
20 
21 #include <cstddef>
22 #include <cassert>
23 #include <boost/utility.hpp>
24 #include <raul/Deletable.hpp>
25 #include <raul/AtomicPtr.hpp>
26 #include <raul/AtomicInt.hpp>
27 
28 namespace Raul {
29 
30 
37 template <typename T>
38 class List : public Raul::Deletable, public boost::noncopyable
39 {
40 public:
41 
48  class Node : public Raul::Deletable {
49  public:
50  Node(T elem) : _elem(elem) {}
51  virtual ~Node() {}
52 
53  template <typename Y>
54  Node(const typename List<Y>::Node& copy)
55  : _elem(copy._elem), _prev(copy._prev), _next(copy._next)
56  {}
57 
58  Node* prev() const { return _prev.get(); }
59  void prev(Node* ln) { _prev = ln; }
60  Node* next() const { return _next.get(); }
61  void next(Node* ln) { _next = ln; }
62  T& elem() { return _elem;}
63  const T& elem() const { return _elem; }
64 
65  private:
66  T _elem;
67  AtomicPtr<Node> _prev;
68  AtomicPtr<Node> _next;
69  };
70 
71 
72  List() : _size(0), _end_iter(this), _const_end_iter(this) {
73  _end_iter._listnode = NULL;
74  _const_end_iter._listnode = NULL;
75  }
76 
77  ~List();
78 
79  void push_back(Node* elem);
80  void push_back(T& elem);
81 
82  void append(List<T>& list);
83 
84  void clear();
85 
87  unsigned size() const { return (unsigned)_size.get(); }
88 
90  bool empty() { return (_head.get() == NULL); }
91 
92  class iterator;
93 
96  public:
97  const_iterator(const List<T>* const list);
98  const_iterator(const iterator& i)
99  : _list(i._list), _listnode(i._listnode) {}
100 
101  inline const T& operator*();
102  inline const T* operator->();
103  inline const_iterator& operator++();
104  inline bool operator!=(const const_iterator& iter) const;
105  inline bool operator!=(const iterator& iter) const;
106  inline bool operator==(const const_iterator& iter) const;
107  inline bool operator==(const iterator& iter) const;
108 
109  friend class List<T>;
110 
111  private:
112  const List<T>* _list;
113  const typename List<T>::Node* _listnode;
114  };
115 
116 
118  class iterator {
119  public:
120  iterator(List<T>* const list);
121 
122  inline T& operator*();
123  inline T* operator->();
124  inline iterator& operator++();
125  inline bool operator!=(const iterator& iter) const;
126  inline bool operator!=(const const_iterator& iter) const;
127  inline bool operator==(const iterator& iter) const;
128  inline bool operator==(const const_iterator& iter) const;
129 
130  friend class List<T>;
131  friend class List<T>::const_iterator;
132 
133  private:
134  const List<T>* _list;
135  typename List<T>::Node* _listnode;
136  };
137 
138 
139  Node* erase(const iterator iter);
140 
141  iterator find(const T& val);
142 
143  iterator begin();
144  const_iterator begin() const;
145  const iterator end() const;
146 
147 private:
148  AtomicPtr<Node> _head;
149  AtomicPtr<Node> _tail;
150  AtomicInt _size;
151  iterator _end_iter;
152  const_iterator _const_end_iter;
153 };
154 
155 
156 } // namespace Raul
157 
158 #include "ListImpl.hpp"
159 
160 #endif // RAUL_LIST_HPP
Something with a virtual destructor.
Definition: Deletable.hpp:28
void clear()
Clear the list, deleting all Nodes contained (but NOT their contents!)
Definition: ListImpl.hpp:37
Realtime safe iterator for a List.
Definition: List.hpp:118
unsigned size() const
Valid only in the write thread.
Definition: List.hpp:87
A node in a List.
Definition: List.hpp:48
A realtime safe, (partially) thread safe doubly-linked list.
Definition: List.hpp:38
void append(List< T > &list)
Append a list to this list.
Definition: ListImpl.hpp:120
Node * erase(const iterator iter)
Remove an element from the list using an iterator.
Definition: ListImpl.hpp:176
Realtime safe const iterator for a List.
Definition: List.hpp:95
void push_back(Node *elem)
Realtime Safe.
Definition: ListImpl.hpp:61
bool empty()
Valid for any thread.
Definition: List.hpp:90
iterator find(const T &val)
Find an element in the list.
Definition: ListImpl.hpp:157
const_iterator(const List< T > *const list)
const_iterator stuff ///
Definition: ListImpl.hpp:302