1 /*
2  * Copyright (c) 2002-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef __AMETHYST_UNIQUE_LIST_H__
19 #define __AMETHYST_UNIQUE_LIST_H__
20 
21 #include "list.h"
22 
23 template<class T>
24 class UniqueList : public List<T>
25 {
26 public:
27     UniqueList(T *ptr = 0) : List<T>(ptr) { }
UniqueList(const List<T> & other)28     UniqueList(const List<T> &other) : List<T>(other) { }
29 
next()30     UniqueList *next() { return (UniqueList*) Linkable::next(); }
prev()31     UniqueList *prev() { return (UniqueList*) Linkable::prev(); }
32 
33     T *add(T *ptr);
34 };
35 
36 /**
37  * Call this for the root! The new node is added to the end of the list,
38  * i.e. just before the root.
39  */
40 template<class T>
add(T * ptr)41 T *UniqueList<T>::add(T *ptr)
42 {
43     // Check if this pointer already exists.
44     for (UniqueList *u = next(); !u->isRoot(); u = u->next())
45         if (u->get() == ptr) return ptr; // Already there.
46     return List<T>::add(ptr);
47 }
48 
49 typedef UniqueList<void> UniquePtrList;
50 
51 #endif
52