memory/sharedptr.bi

Summary
memory/sharedptr.bi
LicenseCopyright © 2007-2011, FreeBASIC Extended Library Development Group
Macros
FBEXT_DECLARE_SHAREDPTRDefines the SharedPtr object class for a particular type, and any declarations it needs.
SharedPtrA generic reference-counting smart pointer class.
Functions
default constructorConstructs a fbext_SharedPtr(T_) with the address of a resource.
constructorConstructs a fbext_SharedPtr(T_) with the address of a resource.
copy constructorConstructs a fbext_SharedPtr(T_) by sharing the resource of another.
destructorDestroys a fbext_SharedPtr(T_).
letShares a resource with another fbext_SharedPtr(T_).
ResetForces the fbext_SharedPtr(T_) to reference another resource.
ResetForces the fbext_SharedPtr(T_) to reference another resource.
GetRetrieves the wrapped pointer.
Swap_Swaps the value of this fbext_SharedPtr(T_) with another.
Macros
FBEXT_DEFINE_SHAREDPTRDefines the fbext_SharedPtr(T_) object class implementation.

License

Copyright © 2007-2011, FreeBASIC Extended Library Development Group

Distributed under the FreeBASIC Extended Library Group license.  See accompanying file LICENSE.txt or copy at http://code.google.com/p/fb-extended-lib/wiki/License

Macros

FBEXT_DECLARE_SHAREDPTR

Defines the SharedPtr object class for a particular type, and any declarations it needs.

Parameters

fbext_TypeName(T_)The type of SharedPtr to define.

SharedPtr

A generic reference-counting smart pointer class.

Description

SharedPtr objects reference resources, and are responsible for freeing that resource when it’s no longer referenced.  Copying a SharedPtr results in another SharedPtr that shares the resource, if any, of the original.  When all SharedPtr objects that share a particular resource are destroyed, the resource is also destroyed, using ..DELETE, or a user-defined destruction procedure.

See Also

<SharedPtr example>

Summary
Functions
default constructorConstructs a fbext_SharedPtr(T_) with the address of a resource.
constructorConstructs a fbext_SharedPtr(T_) with the address of a resource.
copy constructorConstructs a fbext_SharedPtr(T_) by sharing the resource of another.
destructorDestroys a fbext_SharedPtr(T_).
letShares a resource with another fbext_SharedPtr(T_).
ResetForces the fbext_SharedPtr(T_) to reference another resource.
ResetForces the fbext_SharedPtr(T_) to reference another resource.
GetRetrieves the wrapped pointer.
Swap_Swaps the value of this fbext_SharedPtr(T_) with another.
Macros
FBEXT_DEFINE_SHAREDPTRDefines the fbext_SharedPtr(T_) object class implementation.

Functions

default constructor

Constructs a fbext_SharedPtr(T_) with the address of a resource.

Parameters

pThe address of the resource to reference.

Description

p defaults to //null// if not specified, meaning the fbext_SharedPtr(T_) references no resource.  When the last fbext_SharedPtr(T_) referencing the resource is destroyed, ..DELETE will be called to free the resource.

constructor

declare constructor (byval p as fbext_TypeName(T_) ptr,
byval d as fbext_DestroyProc(T_))

Constructs a fbext_SharedPtr(T_) with the address of a resource.

Parameters

pThe address of the resource to reference.
destroyThe address of a procedure that will be passed the wrapped pointer when no more fbext_SharedPtr(T_) objects reference it.

Description

p defaults to //null// if not specified, meaning the fbext_SharedPtr(T_) references no resource.  When the last fbext_SharedPtr(T_) referencing the resource is destroyed, d will be called to handle freeing the resource.  If d is //null//, ..DELETE will be called instead.

copy constructor

Constructs a fbext_SharedPtr(T_) by sharing the resource of another.

Parameters

xThe other fbext_SharedPtr(T_).

Description

The newly constructed fbext_SharedPtr(T_) will share the resource, if any, referenced by x.  Any destruction procedure bound to the resource is also copied.

destructor

declare destructor ( )

Destroys a fbext_SharedPtr(T_).

Description

If no other fbext_SharedPtr(T_) references this resource, it is destroyed using ..DELETE, or the user-supplied destruction procedure.

let

declare operator let (byref x as fbext_SharedPtr(T_))

Shares a resource with another fbext_SharedPtr(T_).

Parameters

xThe other fbext_SharedPtr(T_).

Description

If no other fbext_SharedPtr(T_) references this resource, it is destroyed using ..DELETE, or the user-supplied destruction procedure.  The fbext_SharedPtr(T_) will then share the resource, if any, of x.

Behaves like this.Swap_(fbext_SharedPtr(T_)(x)).

Reset

declare sub Reset (byval p as fbext_TypeName(T_) ptr =  null)

Forces the fbext_SharedPtr(T_) to reference another resource.

Parameters

pThe address of the new resource.

Description

If no other fbext_SharedPtr(T_) references this resource, it is destroyed using ..DELETE, or the user-supplied destruction procedure.  The fbext_SharedPtr(T_) will then reference the resource pointed to by p, or no resource if p is //null//.

Behaves like this.Swap_(fbext_SharedPtr(T_)(p)).

Reset

declare sub Reset (byval p as fbext_TypeName(T_) ptr,
byval d as fbext_DestroyProc(T_))

Forces the fbext_SharedPtr(T_) to reference another resource.

Parameters

pThe address of the new resource.
destroyThe address of a procedure that will be passed the wrapped pointer when no more fbext_SharedPtr(T_) objects reference it.

Description

If no other fbext_SharedPtr(T_) references this resource, it is destroyed using ..DELETE, or the user-supplied destruction procedure.  The fbext_SharedPtr(T_) will then reference the resource pointed to by p, or no resource if p is //null//.

Behaves like this.Swap_(fbext_SharedPtr(T_)(p, d)).

Get

declare function Get () as fbext_TypeName(T_) ptr

Retrieves the wrapped pointer.

Returns

Returns the address of the referenced resource.

Swap_

declare sub Swap_ (byref x as fbext_SharedPtr(T_))

Swaps the value of this fbext_SharedPtr(T_) with another.

Description

The two fbext_SharedPtr(T_) objects will then reference each other’s resources, if any.

Macros

FBEXT_DEFINE_SHAREDPTR

Defines the fbext_SharedPtr(T_) object class implementation.

Parameters

fbext_TypeName(T_)The type of SharedPtr implementation to define.
declare constructor (byval p as fbext_TypeName(T_) ptr,
byval d as fbext_DestroyProc(T_))
Constructs a fbext_SharedPtr(T_) with the address of a resource.
declare destructor ( )
Destroys a fbext_SharedPtr(T_).
declare operator let (byref x as fbext_SharedPtr(T_))
Shares a resource with another fbext_SharedPtr(T_).
declare sub Reset (byval p as fbext_TypeName(T_) ptr =  null)
Forces the fbext_SharedPtr(T_) to reference another resource.
declare function Get () as fbext_TypeName(T_) ptr
Retrieves the wrapped pointer.
declare sub Swap_ (byref x as fbext_SharedPtr(T_))
Swaps the value of this fbext_SharedPtr(T_) with another.