Skip to content
This repository has been archived by the owner on Jan 7, 2023. It is now read-only.

Add RefCount!T, Unique!T, improve documentation

Compare
Choose a tag to compare
@hmmdyl hmmdyl released this 19 Jun 11:03
· 47 commits to master since this release

RefCount and Unique are designed specifically for LWDR. The implementation is inspired by AutoMem.

struct Point { int x, y; }

{
  auto rc0 = RefCount!(Point*)(new Point(3, 4)); // or RefCount!(Point*)(3, 4); works too
  {
    auto rc1 = rc0; // increase reference count
  } // decrease reference count
} // decrease reference count and free

It works for classes, interfaces, pointers and dynamic arrays.

struct Point { int x, y; }

{
  auto u0 = Unique!(Point*)(new Point(3, 4)); // Unique!(Point*)(3, 4); works too
  /+ ERROR +/ auto u1 = u0; // error -- cannot copy
  auto u2 = u0.move; // works
  u0.hasPayload; // false
  u1.hasPayload; // true
} // u1 will deallocate the payload

It works for classes, interfaces, pointers and dynamic arrays.

Dynamic arrays can be constructor via RefCount's and Unique's constructors. Example:

auto rcArray0 = RefCount!(int[])(4, 5, 6); // pass the array elements to RefCount's ctor
auto rcArray1 = RefCount!(int[])([4, 5, 6]); // allocate the array and then pass it to RefCount

The documentation of the source code has been improved. This is too make the codebase easier to explore and reason about.