std.typecons.ref_counted - multiple declarations
- Function refCounted
- Struct RefCounted
Function refCounted
Initializes a with RefCounted. The template parameter
valT of is inferred from RefCounted.
This function can be used to move non-copyable values to the heap.
It also disables the valautoInit option of .
RefCounted
Prototype
RefCounted!(T,RefCountedAutoInitialize.no) refCounted(T)( T val );
Parameters
| Name | Description |
|---|---|
| val | The value to be reference counted |
Returns
An initialized containing RefCounted.
val
See Also
Example
static struct File
{
string name;
@disable this(this); // not copyable
~this() { name = null; }
}
auto file = File("name");
assert(file.name == "name");
// file cannot be copied and has unique ownership
static assert(!__traits(compiles, {auto file2 = file;}));
// make the file refcounted to share ownership
import std.algorithm.mutation : move;
auto rcFile = refCounted(move(file));
assert(rcFile.name == "name");
assert(file.name == null);
auto rcFile2 = rcFile;
assert(rcFile.refCountedStore.refCount == 2);
// file gets properly closed when last reference is dropped
Struct RefCounted
Defines a reference-counted object containing a T value as
payload. keeps track of all references of an RefCountedobject,
and when the reference count goes down to zero, frees the underlying
store. uses RefCountedmalloc and free for operation.
is unsafe and should be used with care. RefCountedNo references
to the payload should be escaped outside the RefCountedobject.
The autoInit option makes the object ensure the store is
automatically initialized. Leaving autoInit ==
(the default option) is convenient but
has the cost of a test whenever the payload is accessed. If RefCountedAutoInitialize.yesautoInit == , user code must call either
RefCountedAutoInitialize.norefCountedStore.isInitialized or refCountedStore.ensureInitialized
before attempting to access the payload. Not doing so results in null
pointer dereference.
Constructors
| Name | Description |
|---|---|
this
|
Constructor that initializes the payload. |
Properties
| Name | Type | Description |
|---|---|---|
refCountedStore
[get]
|
inout(RefCounted. |
Returns storage implementation struct. |
Methods
| Name | Description |
|---|---|
opAssign
|
Assignment operators |
refCountedPayload
|
Returns a reference to the payload. If (autoInit ==
RefCountedAutoInitialize.yes), calls refCountedStore.ensureInitialized. Otherwise, just issues assert(refCountedStore.isInitialized). Used with alias
, so callers can just use the
object as a T.
|
Inner structs
| Name | Description |
|---|---|
RefCountedStore
|
storage implementation.
|
Templates
| Name | Description |
|---|---|
this
|
Constructor that initializes the payload. |
Example
// A pair of anintand asize_t- the latter being the // reference count - will be dynamically allocated auto rc1 = RefCounted!int(5); assert(rc1 == 5); // No more allocation, add just one extra reference count auto rc2 = rc1; // Reference semantics rc2 = 42; assert(rc1 == 42); // the pair will be freed when rc1 and rc2 go out of scope
Authors
Andrei Alexandrescu, Bartosz Milewski, Don Clugston, Shin Fujishiro, Kenji Hara