2015-06-27 18:06:02 -04:00
|
|
|
import set
|
2015-06-05 00:34:24 -04:00
|
|
|
|
2015-04-04 01:32:40 -04:00
|
|
|
__if_comp__ __C__ simple_passthrough """
|
2014-05-03 20:46:10 -04:00
|
|
|
#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
|
|
|
|
2015-05-09 06:24:56 -04:00
|
|
|
fun malloc<T>(size: int): T* {
|
|
|
|
|
var memPtr: T*;
|
2014-05-03 20:46:10 -04:00
|
|
|
__if_comp__ __C__ {
|
2015-04-10 00:37:31 -04:00
|
|
|
simple_passthrough( size = size, memPtr = memPtr : memPtr = memPtr :) """
|
2014-05-03 20:46:10 -04:00
|
|
|
memPtr = malloc(size);
|
|
|
|
|
"""
|
|
|
|
|
}
|
|
|
|
|
return memPtr;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-09 06:24:56 -04:00
|
|
|
fun free<T>(memPtr: T*): void {
|
2014-05-19 20:00:35 -04:00
|
|
|
__if_comp__ __C__ {
|
2015-04-04 01:32:40 -04:00
|
|
|
simple_passthrough(memPtr = memPtr ::) """
|
2014-05-19 20:00:35 -04:00
|
|
|
free(memPtr);
|
|
|
|
|
"""
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-09 06:24:56 -04:00
|
|
|
fun sizeof<T>(): int {
|
2015-05-30 04:43:01 -04:00
|
|
|
var testObj: T*;
|
2015-05-09 06:24:56 -04:00
|
|
|
var result: int;
|
2014-05-19 20:00:35 -04:00
|
|
|
__if_comp__ __C__ {
|
2015-04-04 01:32:40 -04:00
|
|
|
simple_passthrough(testObj = testObj : result = result:) """
|
2015-05-30 04:43:01 -04:00
|
|
|
int result = sizeof(*testObj);
|
2014-05-19 20:00:35 -04:00
|
|
|
"""
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-09 06:24:56 -04:00
|
|
|
fun new<T>(count: int): T* {
|
2014-07-28 01:52:15 -07:00
|
|
|
return malloc<T>( sizeof<T>() * count );
|
2014-05-19 20:00:35 -04:00
|
|
|
}
|
|
|
|
|
|
2015-05-09 06:24:56 -04:00
|
|
|
fun new<T>(): T* {
|
2014-05-19 20:00:35 -04:00
|
|
|
return new<T>(1);
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-05 00:34:24 -04:00
|
|
|
/* We specilize on the trait Object to decide on whether or not the destructor should be called */
|
2015-05-09 06:24:56 -04:00
|
|
|
fun delete<T>(toDelete: T*, itemCount: int): void {
|
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. */
|
2015-06-05 00:34:24 -04:00
|
|
|
fun delete<T(Object)>(toDelete: T*, itemCount: int): void {
|
|
|
|
|
// start at one because the actual delete will call the destructor of the first one as it
|
|
|
|
|
// finishes the pointer
|
2015-06-28 20:25:27 -04:00
|
|
|
for (var i: int = 0; i < itemCount; i++;)
|
2014-06-30 01:57:50 -07:00
|
|
|
toDelete[i].destruct();
|
2015-06-28 20:25:27 -04:00
|
|
|
free<T>(toDelete);
|
|
|
|
|
//delete<T>(toDelete);
|
2014-06-30 01:57:50 -07:00
|
|
|
}
|
|
|
|
|
|
2015-06-05 00:34:24 -04:00
|
|
|
/* We specilize on the trait Object to decide on whether or not the destructor should be called */
|
2015-05-09 06:24:56 -04:00
|
|
|
fun delete<T>(toDelete: T*): void {
|
2015-04-04 01:32:40 -04:00
|
|
|
free<T>(toDelete);
|
2014-07-06 23:42:25 -07:00
|
|
|
}
|
|
|
|
|
|
2015-06-05 00:34:24 -04:00
|
|
|
fun delete<T(Object)>(toDelete: T*): void {
|
2014-07-23 02:23:21 -07:00
|
|
|
toDelete->destruct();
|
2015-04-04 01:32:40 -04:00
|
|
|
free<T>(toDelete);
|
2014-05-19 20:00:35 -04:00
|
|
|
}
|
2015-06-05 00:34:24 -04:00
|
|
|
|
2015-06-14 11:13:30 -04:00
|
|
|
// a wrapper for construct if it has the Object trait
|
|
|
|
|
fun maybe_construct<T>(it:T*):T* {
|
|
|
|
|
return it
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun maybe_construct<T(Object)>(it:T*):T* {
|
|
|
|
|
return it->construct()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-06-05 00:34:24 -04:00
|
|
|
// a wrapper for copy constructing if it has the Object trait
|
|
|
|
|
fun maybe_copy_construct<T>(to:T*, from:T*):void {
|
|
|
|
|
*to = *from
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun maybe_copy_construct<T(Object)>(to:T*, from:T*):void {
|
|
|
|
|
to->copy_construct(from)
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-14 11:13:30 -04:00
|
|
|
// a wrapper for destruct if it has the Object trait
|
|
|
|
|
fun maybe_destruct<T>(it:T*):void {}
|
|
|
|
|
|
|
|
|
|
fun maybe_destruct<T(Object)>(it:T*):void {
|
|
|
|
|
it->destruct()
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-27 18:06:02 -04:00
|
|
|
// a function that allows the safe deletion of recursive and complicated data structures
|
|
|
|
|
fun safe_recursive_delete<T>(first: T*, addingFunc: fun(T*): set::set<T*>) {
|
|
|
|
|
var toDelete = set::set<T*>()
|
|
|
|
|
var next = set::set(first)
|
|
|
|
|
while (toDelete != next) {
|
|
|
|
|
toDelete = next
|
|
|
|
|
toDelete.for_each( fun(it: T*) next.add(addingFunc(it)); )
|
|
|
|
|
}
|
|
|
|
|
toDelete.for_each( fun(it: T*) delete(it); )
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-14 11:13:30 -04:00
|
|
|
|