mirror of
https://github.com/dlang/dmd.git
synced 2025-05-03 16:40:32 +03:00
dtoh: Provide operator[] for _d_dynamicArray ...
... s.t. it can be used like a normal array (+ bounds checking)
This commit is contained in:
parent
aeb3dbd1f4
commit
3a426c842a
20 changed files with 211 additions and 1 deletions
|
@ -126,7 +126,7 @@ extern(C++) void genCppHdrFiles(ref Modules ms)
|
||||||
buf.writenl();
|
buf.writenl();
|
||||||
buf.writestringln("#pragma once");
|
buf.writestringln("#pragma once");
|
||||||
buf.writenl();
|
buf.writenl();
|
||||||
// buf.writestring("#include <assert.h>\n");
|
hashInclude(buf, "<assert.h>");
|
||||||
hashInclude(buf, "<stddef.h>");
|
hashInclude(buf, "<stddef.h>");
|
||||||
hashInclude(buf, "<stdint.h>");
|
hashInclude(buf, "<stdint.h>");
|
||||||
// buf.writestring(buf, "#include <stdio.h>\n");
|
// buf.writestring(buf, "#include <stdio.h>\n");
|
||||||
|
@ -149,6 +149,16 @@ struct _d_dynamicArray
|
||||||
|
|
||||||
_d_dynamicArray(size_t length_in, T *ptr_in)
|
_d_dynamicArray(size_t length_in, T *ptr_in)
|
||||||
: length(length_in), ptr(ptr_in) { }
|
: length(length_in), ptr(ptr_in) { }
|
||||||
|
|
||||||
|
T& operator[](const size_t idx) {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
const T& operator[](const size_t idx) const {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
`);
|
`);
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
@ -19,6 +20,16 @@ struct _d_dynamicArray
|
||||||
|
|
||||||
_d_dynamicArray(size_t length_in, T *ptr_in)
|
_d_dynamicArray(size_t length_in, T *ptr_in)
|
||||||
: length(length_in), ptr(ptr_in) { }
|
: length(length_in), ptr(ptr_in) { }
|
||||||
|
|
||||||
|
T& operator[](const size_t idx) {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
const T& operator[](const size_t idx) const {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
#if !defined(_d_real)
|
#if !defined(_d_real)
|
||||||
|
|
|
@ -7,6 +7,7 @@ TEST_OUTPUT:
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
@ -24,6 +25,16 @@ struct _d_dynamicArray
|
||||||
|
|
||||||
_d_dynamicArray(size_t length_in, T *ptr_in)
|
_d_dynamicArray(size_t length_in, T *ptr_in)
|
||||||
: length(length_in), ptr(ptr_in) { }
|
: length(length_in), ptr(ptr_in) { }
|
||||||
|
|
||||||
|
T& operator[](const size_t idx) {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
const T& operator[](const size_t idx) const {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ TEST_OUTPUT:
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
@ -24,6 +25,16 @@ struct _d_dynamicArray
|
||||||
|
|
||||||
_d_dynamicArray(size_t length_in, T *ptr_in)
|
_d_dynamicArray(size_t length_in, T *ptr_in)
|
||||||
: length(length_in), ptr(ptr_in) { }
|
: length(length_in), ptr(ptr_in) { }
|
||||||
|
|
||||||
|
T& operator[](const size_t idx) {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
const T& operator[](const size_t idx) const {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ TEST_OUTPUT:
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
@ -23,6 +24,16 @@ struct _d_dynamicArray
|
||||||
|
|
||||||
_d_dynamicArray(size_t length_in, T *ptr_in)
|
_d_dynamicArray(size_t length_in, T *ptr_in)
|
||||||
: length(length_in), ptr(ptr_in) { }
|
: length(length_in), ptr(ptr_in) { }
|
||||||
|
|
||||||
|
T& operator[](const size_t idx) {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
const T& operator[](const size_t idx) const {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ TEST_OUTPUT:
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
@ -24,6 +25,16 @@ struct _d_dynamicArray
|
||||||
|
|
||||||
_d_dynamicArray(size_t length_in, T *ptr_in)
|
_d_dynamicArray(size_t length_in, T *ptr_in)
|
||||||
: length(length_in), ptr(ptr_in) { }
|
: length(length_in), ptr(ptr_in) { }
|
||||||
|
|
||||||
|
T& operator[](const size_t idx) {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
const T& operator[](const size_t idx) const {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ TEST_OUTPUT:
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
@ -24,6 +25,16 @@ struct _d_dynamicArray
|
||||||
|
|
||||||
_d_dynamicArray(size_t length_in, T *ptr_in)
|
_d_dynamicArray(size_t length_in, T *ptr_in)
|
||||||
: length(length_in), ptr(ptr_in) { }
|
: length(length_in), ptr(ptr_in) { }
|
||||||
|
|
||||||
|
T& operator[](const size_t idx) {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
const T& operator[](const size_t idx) const {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ TEST_OUTPUT:
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
@ -24,6 +25,16 @@ struct _d_dynamicArray
|
||||||
|
|
||||||
_d_dynamicArray(size_t length_in, T *ptr_in)
|
_d_dynamicArray(size_t length_in, T *ptr_in)
|
||||||
: length(length_in), ptr(ptr_in) { }
|
: length(length_in), ptr(ptr_in) { }
|
||||||
|
|
||||||
|
T& operator[](const size_t idx) {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
const T& operator[](const size_t idx) const {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ TEST_OUTPUT:
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
@ -24,6 +25,16 @@ struct _d_dynamicArray
|
||||||
|
|
||||||
_d_dynamicArray(size_t length_in, T *ptr_in)
|
_d_dynamicArray(size_t length_in, T *ptr_in)
|
||||||
: length(length_in), ptr(ptr_in) { }
|
: length(length_in), ptr(ptr_in) { }
|
||||||
|
|
||||||
|
T& operator[](const size_t idx) {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
const T& operator[](const size_t idx) const {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ TEST_OUTPUT:
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
@ -24,6 +25,16 @@ struct _d_dynamicArray
|
||||||
|
|
||||||
_d_dynamicArray(size_t length_in, T *ptr_in)
|
_d_dynamicArray(size_t length_in, T *ptr_in)
|
||||||
: length(length_in), ptr(ptr_in) { }
|
: length(length_in), ptr(ptr_in) { }
|
||||||
|
|
||||||
|
T& operator[](const size_t idx) {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
const T& operator[](const size_t idx) const {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ TEST_OUTPUT:
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
@ -24,6 +25,16 @@ struct _d_dynamicArray
|
||||||
|
|
||||||
_d_dynamicArray(size_t length_in, T *ptr_in)
|
_d_dynamicArray(size_t length_in, T *ptr_in)
|
||||||
: length(length_in), ptr(ptr_in) { }
|
: length(length_in), ptr(ptr_in) { }
|
||||||
|
|
||||||
|
T& operator[](const size_t idx) {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
const T& operator[](const size_t idx) const {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
#if !defined(_d_real)
|
#if !defined(_d_real)
|
||||||
|
|
|
@ -7,6 +7,7 @@ TEST_OUTPUT:
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
@ -24,6 +25,16 @@ struct _d_dynamicArray
|
||||||
|
|
||||||
_d_dynamicArray(size_t length_in, T *ptr_in)
|
_d_dynamicArray(size_t length_in, T *ptr_in)
|
||||||
: length(length_in), ptr(ptr_in) { }
|
: length(length_in), ptr(ptr_in) { }
|
||||||
|
|
||||||
|
T& operator[](const size_t idx) {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
const T& operator[](const size_t idx) const {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ TEST_OUTPUT:
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
@ -24,6 +25,16 @@ struct _d_dynamicArray
|
||||||
|
|
||||||
_d_dynamicArray(size_t length_in, T *ptr_in)
|
_d_dynamicArray(size_t length_in, T *ptr_in)
|
||||||
: length(length_in), ptr(ptr_in) { }
|
: length(length_in), ptr(ptr_in) { }
|
||||||
|
|
||||||
|
T& operator[](const size_t idx) {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
const T& operator[](const size_t idx) const {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ TEST_OUTPUT:
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
@ -24,6 +25,16 @@ struct _d_dynamicArray
|
||||||
|
|
||||||
_d_dynamicArray(size_t length_in, T *ptr_in)
|
_d_dynamicArray(size_t length_in, T *ptr_in)
|
||||||
: length(length_in), ptr(ptr_in) { }
|
: length(length_in), ptr(ptr_in) { }
|
||||||
|
|
||||||
|
T& operator[](const size_t idx) {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
const T& operator[](const size_t idx) const {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ TEST_OUTPUT:
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
@ -25,6 +26,16 @@ struct _d_dynamicArray
|
||||||
|
|
||||||
_d_dynamicArray(size_t length_in, T *ptr_in)
|
_d_dynamicArray(size_t length_in, T *ptr_in)
|
||||||
: length(length_in), ptr(ptr_in) { }
|
: length(length_in), ptr(ptr_in) { }
|
||||||
|
|
||||||
|
T& operator[](const size_t idx) {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
const T& operator[](const size_t idx) const {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ TEST_OUTPUT:
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
@ -24,6 +25,16 @@ struct _d_dynamicArray
|
||||||
|
|
||||||
_d_dynamicArray(size_t length_in, T *ptr_in)
|
_d_dynamicArray(size_t length_in, T *ptr_in)
|
||||||
: length(length_in), ptr(ptr_in) { }
|
: length(length_in), ptr(ptr_in) { }
|
||||||
|
|
||||||
|
T& operator[](const size_t idx) {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
const T& operator[](const size_t idx) const {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ TEST_OUTPUT:
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
@ -25,6 +26,16 @@ struct _d_dynamicArray
|
||||||
|
|
||||||
_d_dynamicArray(size_t length_in, T *ptr_in)
|
_d_dynamicArray(size_t length_in, T *ptr_in)
|
||||||
: length(length_in), ptr(ptr_in) { }
|
: length(length_in), ptr(ptr_in) { }
|
||||||
|
|
||||||
|
T& operator[](const size_t idx) {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
const T& operator[](const size_t idx) const {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ TEST_OUTPUT:
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
@ -24,6 +25,16 @@ struct _d_dynamicArray
|
||||||
|
|
||||||
_d_dynamicArray(size_t length_in, T *ptr_in)
|
_d_dynamicArray(size_t length_in, T *ptr_in)
|
||||||
: length(length_in), ptr(ptr_in) { }
|
: length(length_in), ptr(ptr_in) { }
|
||||||
|
|
||||||
|
T& operator[](const size_t idx) {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
const T& operator[](const size_t idx) const {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ TEST_OUTPUT:
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
@ -23,6 +24,16 @@ struct _d_dynamicArray
|
||||||
|
|
||||||
_d_dynamicArray(size_t length_in, T *ptr_in)
|
_d_dynamicArray(size_t length_in, T *ptr_in)
|
||||||
: length(length_in), ptr(ptr_in) { }
|
: length(length_in), ptr(ptr_in) { }
|
||||||
|
|
||||||
|
T& operator[](const size_t idx) {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
const T& operator[](const size_t idx) const {
|
||||||
|
assert(idx < length);
|
||||||
|
return ptr[idx];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,8 @@ int main()
|
||||||
assert(!c->s.b);
|
assert(!c->s.b);
|
||||||
assert(c->name.ptr == name);
|
assert(c->name.ptr == name);
|
||||||
assert(c->name.length == length);
|
assert(c->name.length == length);
|
||||||
|
assert(c->name[1] == 'e');
|
||||||
|
assert(const_cast<const C*>(c)->name[2] == 'a');
|
||||||
c->verify();
|
c->verify();
|
||||||
|
|
||||||
assert(foo(c->s) == bar(c));
|
assert(foo(c->s) == bar(c));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue