Wiki Roadmap Timeline Tickets New Ticket Source Search Help / Guide About Trac Login

root/gen/utils.h

Revision 1262:ec1d9dc1d32a, 1.3 kB (checked in by Tomas Lindquist Olsen <tomas.l.olsen gmail com>, 3 years ago)

Fixed struct default initializers.

Line 
1 #ifndef __LDC_GEN_UTILS_H__
2 #define __LDC_GEN_UTILS_H__
3
4 #include "root.h"
5
6 /// Very simple templated iterator for DMD ArrayS.
7 template<class C>
8 struct ArrayIter
9 {
10     Array* array;
11     size_t index;
12
13     ArrayIter(Array& arr, size_t idx = 0)
14     :   array(&arr), index(idx)
15     { }
16     ArrayIter(Array* arr, size_t idx = 0)
17     :   array(arr), index(idx)
18     { assert(arr && "null array"); }
19
20     ArrayIter<C>& operator=(const Array& arr)
21     {
22         array = &arr;
23         index = 0;
24         return *this;
25     }
26     ArrayIter<C>& operator=(const Array* arr)
27     {
28         assert(arr && "null array");
29         array = arr;
30         index = 0;
31         return *this;
32     }
33
34     bool done()
35     {
36         return index >= array->dim;
37     }
38     bool more()
39     {
40         return index < array->dim;
41     }
42
43     C* get() {
44         return static_cast<C*>(array->data[index]);
45     }
46     C* operator->() {
47         return get();
48     }
49     C* operator*() {
50         return get();
51     }
52
53     void next()
54     {
55         ++index;
56     }
57
58     bool operator==(const ArrayIter<C>& other) {
59         return &array->data[index] == &other.array->data[other.index];
60     }
61 };
62
63 // some aliases
64 typedef ArrayIter<Dsymbol> DsymbolIter;
65 typedef ArrayIter<FuncDeclaration> FuncDeclarationIter;
66 typedef ArrayIter<VarDeclaration> VarDeclarationIter;
67
68 #endif
Note: See TracBrowser for help on using the browser.
Copyright © 2008, LDC Development Team.