2014-05-03 20:46:10 -04:00
|
|
|
__if_comp__ __C__ __simple_passthrough__ """
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
"""
|
|
|
|
|
|
2014-07-28 01:52:15 -07:00
|
|
|
/* we have a template versions so we don't have to cast (because we don't have that yet) */
|
2014-05-03 20:46:10 -04:00
|
|
|
|
2014-08-01 00:45:48 -07:00
|
|
|
template <T> |T*| malloc(|int| size) {
|
|
|
|
|
|T*| memPtr = 0;
|
2014-05-03 20:46:10 -04:00
|
|
|
__if_comp__ __C__ {
|
|
|
|
|
__simple_passthrough__ """
|
|
|
|
|
memPtr = malloc(size);
|
|
|
|
|
"""
|
|
|
|
|
}
|
|
|
|
|
return memPtr;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-01 00:45:48 -07:00
|
|
|
template <T> |void| free(|T*| memPtr) {
|
2014-05-19 20:00:35 -04:00
|
|
|
__if_comp__ __C__ {
|
|
|
|
|
__simple_passthrough__ """
|
|
|
|
|
free(memPtr);
|
|
|
|
|
"""
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-01 00:45:48 -07:00
|
|
|
template <T> |int| sizeof() {
|
|
|
|
|
|int| result = 0;
|
|
|
|
|
|T| testObj;
|
2014-05-19 20:00:35 -04:00
|
|
|
__if_comp__ __C__ {
|
|
|
|
|
__simple_passthrough__ """
|
|
|
|
|
result = sizeof(testObj);
|
|
|
|
|
"""
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-01 00:45:48 -07:00
|
|
|
template <T> |T*| new(|int| count) {
|
2014-07-28 01:52:15 -07:00
|
|
|
return malloc<T>( sizeof<T>() * count );
|
2014-05-19 20:00:35 -04:00
|
|
|
}
|
|
|
|
|
|
2014-08-01 00:45:48 -07:00
|
|
|
template <T> |T*| new() {
|
2014-05-19 20:00:35 -04:00
|
|
|
return new<T>(1);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-20 20:42:26 -07:00
|
|
|
/* We specilize on the trait Destructable to decide on whether or not the destructor should be called */
|
2014-08-01 00:45:48 -07:00
|
|
|
template <T> |void| delete(|T*| toDelete, |int| itemCount) {
|
2014-07-20 20:42:26 -07:00
|
|
|
delete<T>(toDelete);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-28 01:52:15 -07:00
|
|
|
/* Calling this with itemCount = 0 allows you to delete destructable objects without calling their destructors. */
|
2014-08-01 00:45:48 -07:00
|
|
|
template <T(Destructable)> |void| delete(|T*| toDelete, |int| itemCount) {
|
|
|
|
|
for (|int| i = 0; i < itemCount; i++;)
|
2014-06-30 01:57:50 -07:00
|
|
|
toDelete[i].destruct();
|
2014-07-02 01:18:27 -07:00
|
|
|
delete<T>(toDelete);
|
2014-06-30 01:57:50 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-20 20:42:26 -07:00
|
|
|
/* We specilize on the trait Destructable to decide on whether or not the destructor should be called */
|
2014-08-01 00:45:48 -07:00
|
|
|
template <T> |void| delete(|T*| toDelete) {
|
2014-07-20 20:42:26 -07:00
|
|
|
free(toDelete);
|
2014-07-06 23:42:25 -07:00
|
|
|
}
|
|
|
|
|
|
2014-08-01 00:45:48 -07:00
|
|
|
template <T(Destructable)> |void| delete(|T*| toDelete) {
|
2014-07-23 02:23:21 -07:00
|
|
|
toDelete->destruct();
|
2014-07-20 20:42:26 -07:00
|
|
|
free(toDelete);
|
2014-05-19 20:00:35 -04:00
|
|
|
}
|