dtoh: Provide operator[] for _d_dynamicArray ...

... s.t. it can be used like a normal array (+ bounds checking)
This commit is contained in:
MoonlightSentinel 2020-10-18 18:14:44 +02:00
parent aeb3dbd1f4
commit 3a426c842a
No known key found for this signature in database
GPG key ID: 1A1A60AECDC956AB
20 changed files with 211 additions and 1 deletions

View file

@ -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
`);

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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));