]> git.saurik.com Git - cycript.git/blob - List.hpp
Automatically generate FFI bridges using libclang.
[cycript.git] / List.hpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
3 */
4
5 /* GNU Affero General Public License, Version 3 {{{ */
6 /*
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #ifndef CYCRIPT_LIST_HPP
23 #define CYCRIPT_LIST_HPP
24
25 #include "Exception.hpp"
26
27 template <typename Type_>
28 struct CYNext {
29 Type_ *next_;
30
31 CYNext() :
32 next_(NULL)
33 {
34 }
35
36 CYNext(Type_ *next) :
37 next_(next)
38 {
39 }
40
41 void SetNext(Type_ *next) {
42 next_ = next;
43 }
44 };
45
46 template <typename Type_>
47 Type_ *&CYSetLast(Type_ *&list) {
48 if (list == NULL)
49 return list;
50
51 Type_ *next(list);
52 while (next->next_ != NULL)
53 next = next->next_;
54 return next->next_;
55 }
56
57 template <typename Type_>
58 Type_ *&CYGetLast(Type_ *&list) {
59 if (list == NULL)
60 return list;
61
62 Type_ **next(&list);
63 while ((*next)->next_ != NULL)
64 next = &(*next)->next_;
65 return *next;
66 }
67
68 template <typename Type_>
69 struct CYList {
70 Type_ *first_;
71 Type_ *last_;
72
73 CYList() :
74 first_(NULL),
75 last_(NULL)
76 {
77 }
78
79 CYList(Type_ *first) :
80 first_(first),
81 last_(CYGetLast(first))
82 {
83 }
84
85 CYList(Type_ *first, Type_ *last) :
86 first_(first),
87 last_(last)
88 {
89 }
90
91 operator Type_ *() const {
92 return first_;
93 }
94
95 Type_ *operator ->() const {
96 return first_;
97 }
98
99 CYList &operator ->*(Type_ *next) {
100 if (next != NULL)
101 if (first_ == NULL) {
102 first_ = next;
103 last_ = next;
104 } else {
105 _assert(last_->next_ == NULL);
106 last_->next_ = next;
107 last_ = next;
108 }
109 return *this;
110 }
111
112 CYList &operator ->*(CYList &next) {
113 if (*this == NULL)
114 *this = next;
115 else if (next != NULL) {
116 last_->next_ = next.first_;
117 last_ = next.last_;
118 }
119 return *this;
120 }
121 };
122
123 #define CYForEach(value, list) \
124 for (__typeof__(*list) *value(list); value != NULL; value = value->next_)
125
126 #endif/*CYCRIPT_LIST_HPP*/