Skip to content

Commit d591c37

Browse files
dtoh: Provide operator[] for _d_dynamicArray ...
... s.t. it can be used like a normal array (+ bounds checking)
1 parent 4a71f85 commit d591c37

18 files changed

Lines changed: 198 additions & 1 deletion

src/dmd/dtoh.d

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ extern(C++) void genCppHdrFiles(ref Modules ms)
126126
buf.writenl();
127127
buf.writestringln("#pragma once");
128128
buf.writenl();
129-
// buf.writestring("#include <assert.h>\n");
129+
hashInclude(buf, "<assert.h>");
130130
hashInclude(buf, "<stddef.h>");
131131
hashInclude(buf, "<stdint.h>");
132132
// buf.writestring(buf, "#include <stdio.h>\n");
@@ -149,6 +149,16 @@ struct _d_dynamicArray
149149
150150
_d_dynamicArray(size_t length_in, T *ptr_in)
151151
: length(length_in), ptr(ptr_in) { }
152+
153+
T& operator[](const size_t idx) {
154+
assert(idx < length);
155+
return ptr[idx];
156+
}
157+
158+
const T& operator[](const size_t idx) const {
159+
assert(idx < length);
160+
return ptr[idx];
161+
}
152162
};
153163
#endif
154164
`);

src/dmd/frontend.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#pragma once
44

5+
#include <assert.h>
56
#include <stddef.h>
67
#include <stdint.h>
78

@@ -19,6 +20,16 @@ struct _d_dynamicArray
1920

2021
_d_dynamicArray(size_t length_in, T *ptr_in)
2122
: length(length_in), ptr(ptr_in) { }
23+
24+
T& operator[](const size_t idx) {
25+
assert(idx < length);
26+
return ptr[idx];
27+
}
28+
29+
const T& operator[](const size_t idx) const {
30+
assert(idx < length);
31+
return ptr[idx];
32+
}
2233
};
2334
#endif
2435
#if !defined(_d_real)

test/compilable/dtoh_21217.d

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ TEST_OUTPUT:
77
88
#pragma once
99
10+
#include <assert.h>
1011
#include <stddef.h>
1112
#include <stdint.h>
1213
@@ -24,6 +25,16 @@ struct _d_dynamicArray
2425
2526
_d_dynamicArray(size_t length_in, T *ptr_in)
2627
: length(length_in), ptr(ptr_in) { }
28+
29+
T& operator[](const size_t idx) {
30+
assert(idx < length);
31+
return ptr[idx];
32+
}
33+
34+
const T& operator[](const size_t idx) const {
35+
assert(idx < length);
36+
return ptr[idx];
37+
}
2738
};
2839
#endif
2940

test/compilable/dtoh_AliasDeclaration.d

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ TEST_OUTPUT:
77
88
#pragma once
99
10+
#include <assert.h>
1011
#include <stddef.h>
1112
#include <stdint.h>
1213
@@ -24,6 +25,16 @@ struct _d_dynamicArray
2425
2526
_d_dynamicArray(size_t length_in, T *ptr_in)
2627
: length(length_in), ptr(ptr_in) { }
28+
29+
T& operator[](const size_t idx) {
30+
assert(idx < length);
31+
return ptr[idx];
32+
}
33+
34+
const T& operator[](const size_t idx) const {
35+
assert(idx < length);
36+
return ptr[idx];
37+
}
2738
};
2839
#endif
2940

test/compilable/dtoh_AnonDeclaration.d

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ TEST_OUTPUT:
77
88
#pragma once
99
10+
#include <assert.h>
1011
#include <stddef.h>
1112
#include <stdint.h>
1213
@@ -24,6 +25,16 @@ struct _d_dynamicArray
2425
2526
_d_dynamicArray(size_t length_in, T *ptr_in)
2627
: length(length_in), ptr(ptr_in) { }
28+
29+
T& operator[](const size_t idx) {
30+
assert(idx < length);
31+
return ptr[idx];
32+
}
33+
34+
const T& operator[](const size_t idx) const {
35+
assert(idx < length);
36+
return ptr[idx];
37+
}
2738
};
2839
#endif
2940

test/compilable/dtoh_CPPNamespaceDeclaration.d

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ TEST_OUTPUT:
77
88
#pragma once
99
10+
#include <assert.h>
1011
#include <stddef.h>
1112
#include <stdint.h>
1213
@@ -24,6 +25,16 @@ struct _d_dynamicArray
2425
2526
_d_dynamicArray(size_t length_in, T *ptr_in)
2627
: length(length_in), ptr(ptr_in) { }
28+
29+
T& operator[](const size_t idx) {
30+
assert(idx < length);
31+
return ptr[idx];
32+
}
33+
34+
const T& operator[](const size_t idx) const {
35+
assert(idx < length);
36+
return ptr[idx];
37+
}
2738
};
2839
#endif
2940

test/compilable/dtoh_ClassDeclaration.d

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ TEST_OUTPUT:
77
88
#pragma once
99
10+
#include <assert.h>
1011
#include <stddef.h>
1112
#include <stdint.h>
1213
@@ -24,6 +25,16 @@ struct _d_dynamicArray
2425
2526
_d_dynamicArray(size_t length_in, T *ptr_in)
2627
: length(length_in), ptr(ptr_in) { }
28+
29+
T& operator[](const size_t idx) {
30+
assert(idx < length);
31+
return ptr[idx];
32+
}
33+
34+
const T& operator[](const size_t idx) const {
35+
assert(idx < length);
36+
return ptr[idx];
37+
}
2738
};
2839
#endif
2940

test/compilable/dtoh_StructDeclaration.d

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ TEST_OUTPUT:
77
88
#pragma once
99
10+
#include <assert.h>
1011
#include <stddef.h>
1112
#include <stdint.h>
1213
@@ -24,6 +25,16 @@ struct _d_dynamicArray
2425
2526
_d_dynamicArray(size_t length_in, T *ptr_in)
2627
: length(length_in), ptr(ptr_in) { }
28+
29+
T& operator[](const size_t idx) {
30+
assert(idx < length);
31+
return ptr[idx];
32+
}
33+
34+
const T& operator[](const size_t idx) const {
35+
assert(idx < length);
36+
return ptr[idx];
37+
}
2738
};
2839
#endif
2940

test/compilable/dtoh_TemplateDeclaration.d

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ TEST_OUTPUT:
77
88
#pragma once
99
10+
#include <assert.h>
1011
#include <stddef.h>
1112
#include <stdint.h>
1213
@@ -24,6 +25,16 @@ struct _d_dynamicArray
2425
2526
_d_dynamicArray(size_t length_in, T *ptr_in)
2627
: length(length_in), ptr(ptr_in) { }
28+
29+
T& operator[](const size_t idx) {
30+
assert(idx < length);
31+
return ptr[idx];
32+
}
33+
34+
const T& operator[](const size_t idx) const {
35+
assert(idx < length);
36+
return ptr[idx];
37+
}
2738
};
2839
#endif
2940

test/compilable/dtoh_VarDeclaration.d

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ TEST_OUTPUT:
77
88
#pragma once
99
10+
#include <assert.h>
1011
#include <stddef.h>
1112
#include <stdint.h>
1213
@@ -24,6 +25,16 @@ struct _d_dynamicArray
2425
2526
_d_dynamicArray(size_t length_in, T *ptr_in)
2627
: length(length_in), ptr(ptr_in) { }
28+
29+
T& operator[](const size_t idx) {
30+
assert(idx < length);
31+
return ptr[idx];
32+
}
33+
34+
const T& operator[](const size_t idx) const {
35+
assert(idx < length);
36+
return ptr[idx];
37+
}
2738
};
2839
#endif
2940

0 commit comments

Comments
 (0)