]>
Commit | Line | Data |
---|---|---|
da2af935 | 1 | /* Cycript - Optimizing JavaScript Compiler/Runtime |
c1d3e52e | 2 | * Copyright (C) 2009-2015 Jay Freeman (saurik) |
da2af935 JF |
3 | */ |
4 | ||
f95d2598 | 5 | /* GNU Affero General Public License, Version 3 {{{ */ |
da2af935 | 6 | /* |
f95d2598 JF |
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 | |
c15969fd | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
f95d2598 JF |
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/>. | |
da2af935 JF |
19 | **/ |
20 | /* }}} */ | |
21 | ||
22 | #ifndef CYCRIPT_LIST_HPP | |
23 | #define CYCRIPT_LIST_HPP | |
24 | ||
b3c38c5f JF |
25 | #include "Exception.hpp" |
26 | ||
da2af935 JF |
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_> | |
bf45251b | 47 | Type_ *&CYSetLast(Type_ *&list) { |
da2af935 | 48 | if (list == NULL) |
abadba19 | 49 | return list; |
bf45251b JF |
50 | |
51 | Type_ *next(list); | |
52 | while (next->next_ != NULL) | |
53 | next = next->next_; | |
54 | return next->next_; | |
55 | } | |
56 | ||
57 | template <typename Type_> | |
12e37ba3 | 58 | Type_ *&CYGetLast(Type_ *&list) { |
bf45251b | 59 | if (list == NULL) |
12e37ba3 | 60 | return list; |
bf45251b | 61 | |
12e37ba3 JF |
62 | Type_ **next(&list); |
63 | while ((*next)->next_ != NULL) | |
64 | next = &(*next)->next_; | |
65 | return *next; | |
da2af935 JF |
66 | } |
67 | ||
c5b15840 JF |
68 | template <typename Type_> |
69 | struct CYList { | |
70 | Type_ *first_; | |
71 | Type_ *last_; | |
72 | ||
b3c38c5f JF |
73 | CYList() : |
74 | first_(NULL), | |
75 | last_(NULL) | |
76 | { | |
77 | } | |
78 | ||
79 | CYList(Type_ *first) : | |
c5b15840 JF |
80 | first_(first), |
81 | last_(CYGetLast(first)) | |
82 | { | |
83 | } | |
84 | ||
b3c38c5f JF |
85 | CYList(Type_ *first, Type_ *last) : |
86 | first_(first), | |
87 | last_(last) | |
88 | { | |
89 | } | |
90 | ||
c5b15840 JF |
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; | |
b3c38c5f JF |
104 | } else { |
105 | _assert(last_->next_ == NULL); | |
106 | last_->next_ = next; | |
107 | last_ = next; | |
108 | } | |
c5b15840 JF |
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 | ||
da2af935 JF |
123 | #define CYForEach(value, list) \ |
124 | for (__typeof__(*list) *value(list); value != NULL; value = value->next_) | |
125 | ||
126 | #endif/*CYCRIPT_LIST_HPP*/ |