2014-05-03 20:46:10 -04:00
|
|
|
|
2016-04-30 15:38:28 -04:00
|
|
|
ext fun malloc(size: ulong): *void
|
2016-04-29 16:19:23 -04:00
|
|
|
ext fun free(size: *void)
|
2016-05-15 18:36:13 -07:00
|
|
|
ext fun memmove(dest: *void, src: *void, size: ulong): *void
|
2014-05-19 20:00:35 -04:00
|
|
|
|
2016-04-30 15:38:28 -04:00
|
|
|
fun null<T>(): *T
|
|
|
|
|
return (0) cast *T
|
|
|
|
|
|
2016-04-29 16:19:23 -04:00
|
|
|
fun new<T>(count: int): *T
|
2016-04-30 15:38:28 -04:00
|
|
|
return (malloc( (#sizeof<T> * count ) cast ulong )) cast *T
|
2014-05-19 20:00:35 -04:00
|
|
|
|
2016-04-29 16:19:23 -04:00
|
|
|
fun new<T>(): *T
|
|
|
|
|
return new<T>(1)
|
2014-05-19 20:00:35 -04: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 */
|
2016-04-29 16:19:23 -04:00
|
|
|
fun delete<T>(toDelete: *T, itemCount: int)
|
|
|
|
|
delete<T>(toDelete)
|
2014-07-20 20:42:26 -07:00
|
|
|
|
2014-07-28 01:52:15 -07:00
|
|
|
/* Calling this with itemCount = 0 allows you to delete destructable objects without calling their destructors. */
|
2015-07-04 17:02:51 -04:00
|
|
|
fun delete<T(Object)>(toDelete: *T, itemCount: int): void {
|
2015-06-05 00:34:24 -04:00
|
|
|
// 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();
|
2016-04-29 16:19:23 -04:00
|
|
|
free((toDelete) cast *void);
|
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 */
|
2016-04-29 16:19:23 -04:00
|
|
|
fun delete<T>(toDelete: *T)
|
|
|
|
|
free((toDelete) cast *void)
|
2014-07-06 23:42:25 -07:00
|
|
|
|
2015-07-04 17:02:51 -04:00
|
|
|
fun delete<T(Object)>(toDelete: *T): void {
|
2014-07-23 02:23:21 -07:00
|
|
|
toDelete->destruct();
|
2016-04-29 16:19:23 -04:00
|
|
|
free((toDelete) cast *void);
|
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
|
2016-04-29 16:19:23 -04:00
|
|
|
fun maybe_construct<T>(it:*T):*T
|
2015-06-14 11:13:30 -04:00
|
|
|
return it
|
|
|
|
|
|
2016-04-29 16:19:23 -04:00
|
|
|
fun maybe_construct<T(Object)>(it:*T):*T
|
2015-06-14 11:13:30 -04:00
|
|
|
return it->construct()
|
|
|
|
|
|
2015-06-05 00:34:24 -04:00
|
|
|
// a wrapper for copy constructing if it has the Object trait
|
2016-04-29 16:19:23 -04:00
|
|
|
fun maybe_copy_construct<T>(to:*T, from:*T)
|
2015-06-05 00:34:24 -04:00
|
|
|
*to = *from
|
|
|
|
|
|
2016-04-29 16:19:23 -04:00
|
|
|
|
|
|
|
|
fun maybe_copy_construct<T(Object)>(to:*T, from:*T)
|
2015-06-05 00:34:24 -04:00
|
|
|
to->copy_construct(from)
|
|
|
|
|
|
2015-06-14 11:13:30 -04:00
|
|
|
// a wrapper for destruct if it has the Object trait
|
2016-04-29 16:19:23 -04:00
|
|
|
fun maybe_destruct<T>(it:*T) {}
|
2015-06-14 11:13:30 -04:00
|
|
|
|
2016-04-29 16:19:23 -04:00
|
|
|
fun maybe_destruct<T(Object)>(it:*T)
|
2015-06-14 11:13:30 -04:00
|
|
|
it->destruct()
|
|
|
|
|
|
2016-01-11 19:25:09 -05:00
|
|
|
obj shared_ptr<T> (Object){
|
|
|
|
|
var data: *T;
|
|
|
|
|
var refCount: int;
|
|
|
|
|
|
|
|
|
|
fun construct(): *shared_ptr<T> {
|
|
|
|
|
data = 0;
|
|
|
|
|
refCount = 1;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun construct(newPtr: *T): *shared_ptr<T> {
|
|
|
|
|
data = newPtr;
|
|
|
|
|
refCount = 1;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun construct(newPtr: ref shared_ptr<T>): *shared_ptr<T> {
|
|
|
|
|
data = newPtr.data;
|
|
|
|
|
refCount = newPtr.refCount;
|
|
|
|
|
refCount++;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun destruct(): void {
|
|
|
|
|
if(refCount == 1){
|
|
|
|
|
delete(data,1);
|
|
|
|
|
refCount--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun operator*(): ref T {
|
|
|
|
|
return *data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun operator->(): *T {
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun operator=(newPtr: ref shared_ptr<T>): ref shared_ptr<T> {
|
|
|
|
|
if(this != &newPtr){
|
|
|
|
|
if(refCount == 1){
|
|
|
|
|
delete(data,1);
|
|
|
|
|
refCount--;
|
|
|
|
|
}
|
|
|
|
|
//use copy constructor here???
|
|
|
|
|
data = newPtr.data;
|
|
|
|
|
refCount = newPtr.refCount;
|
|
|
|
|
refCount++;
|
|
|
|
|
}//end self-assignment check
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2016-01-11 19:52:11 -05:00
|
|
|
|
|
|
|
|
fun operator=(newPtr: ref *T): ref shared_ptr<T> {
|
|
|
|
|
data = newPtr;
|
|
|
|
|
refCount = 1;
|
|
|
|
|
delete(newPtr,1);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2016-01-11 19:25:09 -05:00
|
|
|
|
|
|
|
|
}; //end shared_ptr class
|
|
|
|
|
|