mirror of
https://github.com/dlang/dmd.git
synced 2025-05-03 00:20:18 +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.writestringln("#pragma once");
|
||||
buf.writenl();
|
||||
// buf.writestring("#include <assert.h>\n");
|
||||
hashInclude(buf, "<assert.h>");
|
||||
hashInclude(buf, "<stddef.h>");
|
||||
hashInclude(buf, "<stdint.h>");
|
||||
// buf.writestring(buf, "#include <stdio.h>\n");
|
||||
|
@ -149,6 +149,16 @@ struct _d_dynamicArray
|
|||
|
||||
_d_dynamicArray(size_t length_in, T *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
|
||||
`);
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
@ -19,6 +20,16 @@ struct _d_dynamicArray
|
|||
|
||||
_d_dynamicArray(size_t length_in, T *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
|
||||
#if !defined(_d_real)
|
||||
|
|
|
@ -7,6 +7,7 @@ TEST_OUTPUT:
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
@ -24,6 +25,16 @@ struct _d_dynamicArray
|
|||
|
||||
_d_dynamicArray(size_t length_in, T *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
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ TEST_OUTPUT:
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
@ -24,6 +25,16 @@ struct _d_dynamicArray
|
|||
|
||||
_d_dynamicArray(size_t length_in, T *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
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ TEST_OUTPUT:
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
@ -23,6 +24,16 @@ struct _d_dynamicArray
|
|||
|
||||
_d_dynamicArray(size_t length_in, T *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
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ TEST_OUTPUT:
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
@ -24,6 +25,16 @@ struct _d_dynamicArray
|
|||
|
||||
_d_dynamicArray(size_t length_in, T *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
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ TEST_OUTPUT:
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
@ -24,6 +25,16 @@ struct _d_dynamicArray
|
|||
|
||||
_d_dynamicArray(size_t length_in, T *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
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ TEST_OUTPUT:
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
@ -24,6 +25,16 @@ struct _d_dynamicArray
|
|||
|
||||
_d_dynamicArray(size_t length_in, T *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
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ TEST_OUTPUT:
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
@ -24,6 +25,16 @@ struct _d_dynamicArray
|
|||
|
||||
_d_dynamicArray(size_t length_in, T *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
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ TEST_OUTPUT:
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
@ -24,6 +25,16 @@ struct _d_dynamicArray
|
|||
|
||||
_d_dynamicArray(size_t length_in, T *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
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ TEST_OUTPUT:
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
@ -24,6 +25,16 @@ struct _d_dynamicArray
|
|||
|
||||
_d_dynamicArray(size_t length_in, T *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
|
||||
#if !defined(_d_real)
|
||||
|
|
|
@ -7,6 +7,7 @@ TEST_OUTPUT:
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
@ -24,6 +25,16 @@ struct _d_dynamicArray
|
|||
|
||||
_d_dynamicArray(size_t length_in, T *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
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ TEST_OUTPUT:
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
@ -24,6 +25,16 @@ struct _d_dynamicArray
|
|||
|
||||
_d_dynamicArray(size_t length_in, T *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
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ TEST_OUTPUT:
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
@ -24,6 +25,16 @@ struct _d_dynamicArray
|
|||
|
||||
_d_dynamicArray(size_t length_in, T *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
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ TEST_OUTPUT:
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
@ -25,6 +26,16 @@ struct _d_dynamicArray
|
|||
|
||||
_d_dynamicArray(size_t length_in, T *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
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ TEST_OUTPUT:
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
@ -24,6 +25,16 @@ struct _d_dynamicArray
|
|||
|
||||
_d_dynamicArray(size_t length_in, T *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
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ TEST_OUTPUT:
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
@ -25,6 +26,16 @@ struct _d_dynamicArray
|
|||
|
||||
_d_dynamicArray(size_t length_in, T *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
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ TEST_OUTPUT:
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
@ -24,6 +25,16 @@ struct _d_dynamicArray
|
|||
|
||||
_d_dynamicArray(size_t length_in, T *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
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ TEST_OUTPUT:
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
@ -23,6 +24,16 @@ struct _d_dynamicArray
|
|||
|
||||
_d_dynamicArray(size_t length_in, T *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
|
||||
|
||||
|
|
|
@ -13,6 +13,8 @@ int main()
|
|||
assert(!c->s.b);
|
||||
assert(c->name.ptr == name);
|
||||
assert(c->name.length == length);
|
||||
assert(c->name[1] == 'e');
|
||||
assert(const_cast<const C*>(c)->name[2] == 'a');
|
||||
c->verify();
|
||||
|
||||
assert(foo(c->s) == bar(c));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue