#include <baseref.h>
详细描述
template<typename T, typename HANDLER>
class maxon::BaseRef< T, HANDLER >
This is a general template for references of any kind. The characteristic feature of it is that special actions are triggered whenever the reference is set, cleared, changed or destructed. For example, a reference to a reference-counted object has to increase the reference count of the pointee when the pointer is set, and to decrease the count when the reference is cleared. Another example of reference is a unique reference which simply frees the pointee when it is cleared or destructed.
BaseRef
delegates the specific behaviour to static members of the template parameter HANDLER. There are several implementations available to be used as HANDLER:
-
StrongRefHandler
is used for objects which have a reference counter (used by StrongRef).
-
StrongCOWRefHandler
has an additional copy-on-write semantics. I.e., if the pointee is referenced more than once, a copy is made before the pointee is modified through the
BaseRef
(used by StrongCOWRef).
-
UniqueRefHandler
uses DeleteObj to release the memory, so the object's destructor is invoked (used by UniqueRef).
-
PointerHandler
lets the
BaseRef
behave like an ordinary C++ pointer, so nothing is done when the pointer is changed (used by Pointer).
So if you want to have a reference to an object of type MyType with reference counting and copy-on-write semantics, you could use BaseRef<const MyType, StrongCOWRefHandler> which is equivalent to StrongCOWRef<MyType>.
-
Template Parameters
-
T
|
Type of the referenced object.
|
HANDLER
|
Base class for
BaseRef
. This implements the specific behaviour whenever a reference is changed to point to another object, see above.
|
-
注意
-
BaseRef
is neither atomic nor thread-safe.
.
-
After a
BaseRef
is guaranteed not to change anymore multiple threads can safely read from it concurrently.
-
Reading and writing on the same
BaseRef
from different threads will
crash
(the reference to an object might have been released by a writing thread before a reading thread was able to add a reference).
-
Writing to the same
BaseRef
from different threads will also
crash
because this implies a read operation (a write has to read the old value to release the reference to the previous object).
-
If you really have to read and write from multiple threads you could use the
ThreadSafeRef
class.
公共成员函数
|
T *
|
GetPointer
()
|
ConstReferencedType
*
|
GetPointer
() const
|
T *
|
operator->
()
|
ConstReferencedType
*
|
operator->
() const
|
T &
|
operator*
()
|
ConstReferencedType
&
|
operator*
() const
|
|
operator T*
()
|
|
operator ConstReferencedType *
() const
|
|
operator Bool
() const
|
|
operator Bool
()
|
const T &
|
operator[]
(
Int
) const =delete
|
ResultRef
< typename std::remove_const< T >::type >
|
MakeWritable
(
Bool
resetOnError=true)
|
T &
|
GetOrNull
() const
|
|
BaseRef
()
|
MAXON_IMPLICIT
|
BaseRef
(std::nullptr_t)=delete
|
MAXON_IMPLICIT
|
BaseRef
(T *o)
|
template<typename PTR >
|
|
BaseRef
(
ForwardResultPtr
< PTR > o)
|
|
BaseRef
(
ResultPtr
< T > o,
Int
)
|
|
BaseRef
(const
BaseRef
&
src
)
|
template<typename REF , typename = typename std::enable_if<!STD_IS_REPLACEMENT(base_of, BaseRef, REF) && maxon::HasErasedBase<typename REF::ReferencedType, ReferencedType>::value>::type>
|
MAXON_IMPLICIT
|
BaseRef
(const REF &
src
)
|
template<typename REF , typename = typename std::enable_if<!STD_IS_REPLACEMENT(base_of, BaseRef, REF) && maxon::HasErasedBase<typename REF::ReferencedType, ReferencedType>::value>::type>
|
MAXON_IMPLICIT
|
BaseRef
(REF &
src
)
|
BaseRef
&
|
operator=
(T *
src
)
|
BaseRef
&
|
operator=
(std::nullptr_t
src
)
|
BaseRef
&
|
operator=
(const
BaseRef
&
src
)
|
template<typename REF , typename = typename std::enable_if<!STD_IS_REPLACEMENT(base_of, BaseRef, REF) && maxon::HasErasedBase<typename REF::ReferencedType, ReferencedType>::value>::type>
|
BaseRef
&
|
operator=
(const REF &
src
)
|
template<typename REF , typename = typename std::enable_if<!STD_IS_REPLACEMENT(base_of, BaseRef, REF) && maxon::HasErasedBase<typename REF::ReferencedType, ReferencedType>::value>::type>
|
BaseRef
&
|
operator=
(REF &
src
)
|
|
BaseRef
(
BaseRef
&&
src
)
|
template<typename REF , typename = typename std::enable_if<!STD_IS_REPLACEMENT(const, REF) && !STD_IS_REPLACEMENT(base_of, BaseRef, REF) && maxon::HasErasedBase<typename REF::ReferencedType, ReferencedType>::value>::type>
|
MAXON_IMPLICIT
|
BaseRef
(REF &&
src
)
|
BaseRef
&
|
operator=
(
BaseRef
&&
src
)
|
template<typename REF , typename = typename std::enable_if<!STD_IS_REPLACEMENT(const, REF) && !STD_IS_REPLACEMENT(base_of, BaseRef, REF) && maxon::HasErasedBase<typename REF::ReferencedType, ReferencedType>::value>::type>
|
BaseRef
&
|
operator=
(REF &&
src
)
|
|
~BaseRef
()
|
Bool
|
operator==
(const
BaseRef
&b) const
|
Bool
|
operator!=
(const
BaseRef
&b) const
|
Bool
|
operator==
(const T *b) const
|
Bool
|
operator!=
(const T *b) const
|
Bool
|
operator==
(typename std::remove_const< T >::type *b) const
|
Bool
|
operator!=
(typename std::remove_const< T >::type *b) const
|
Bool
|
operator==
(std::nullptr_t) const
|
Bool
|
operator!=
(std::nullptr_t) const
|
HashInt
|
GetHashCode
() const
|
T *
|
Disconnect
()
|
void
|
PrivateSetTarget
(
ResultPtr
< T >
src
)
|
void
|
PrivateSetPointer
(T *ptr)
|
Friends
|
template<typename , typename >
|
class
|
BaseRef
|
Member Typedef Documentation
◆
ReferencedType
A type alias for the type the
BaseRef
points to.
◆
ConstReferencedType
◆
DirectlyReferencedType
◆
SelfType
◆
IsGenericBaseOf
◆
Handler
◆
RefCompareType
构造函数 & 析构函数文档编制
◆
BaseRef()
[1/10]
◆
BaseRef()
[2/10]
◆
BaseRef()
[3/10]
Constructs a
BaseRef
, initializing the pointer with the given argument.
-
参数
-
[in]
|
o
|
Pointer with which the
BaseRef
is initialized, may be nullptr.
|
◆
BaseRef()
[4/10]
Constructs a
BaseRef
with a pointer forwarded from a memory allocation. The template parameter is used to avoid unnecessary conversion from ResultPtr<X> to ResultPtr<const X>.
-
参数
-
[in]
|
o
|
Pointer with which the
BaseRef
is initialized, may be nullptr on OutOfMemory.
|
◆
BaseRef()
[5/10]
Constructs a
BaseRef
with a pointer forwarded from a memory allocation. To prevent accidental assignment from an allocation without error handling a dummy parameter has to be passed as well.
-
参数
-
[in]
|
o
|
Pointer with which the
BaseRef
is initialized, may be nullptr on OutOfMemory.
|
◆
BaseRef()
[6/10]
Constructs a
BaseRef
, initializing the pointer with the pointer of another
BaseRef
.
-
参数
-
[in]
|
src
|
Another
BaseRef
whose internal pointer will be used to initialize this
BaseRef
.
|
◆
BaseRef()
[7/10]
Constructs a
BaseRef
, initializing the pointer with the pointer of another reference object.
-
参数
-
[in]
|
src
|
A reference object whose internal pointer will be used to initialize this
BaseRef
.
|
◆
BaseRef()
[8/10]
◆
BaseRef()
[9/10]
Constructs a new
BaseRef
by moving the pointer from src to this. The pointer of src will be a nullptr afterwards.
-
参数
-
[in]
|
src
|
Another
BaseRef
for the move.
|
◆
BaseRef()
[10/10]
Constructs a new
BaseRef
by moving the pointer from src to this. The pointer of src will be a nullptr afterwards.
-
参数
-
[in]
|
src
|
Another
BaseRef
for the move.
|
◆
~BaseRef()
Destructs this
BaseRef
, including proper release of the reference.
成员函数文档编制
◆
GenericVarianceCheck()
static std::false_type GenericVarianceCheck
|
(
|
const void *
|
|
)
|
|
|
static
|
◆
GetPointer()
[1/2]
Returns the internal pointer of the
BaseRef
. You can also use operator-> to access members of the pointee directly through the
BaseRef
.
-
返回
-
Internal pointer.
◆
GetPointer()
[2/2]
Returns the internal pointer of the
BaseRef
. You can also use operator-> to access members of the pointee directly through the
BaseRef
.
-
返回
-
Internal pointer.
◆
operator->()
[1/2]
Allows direct access to the members of the pointee. You have to check for a nullptr before.
-
返回
-
Internal pointer.
◆
operator->()
[2/2]
Allows direct access to the members of the pointee. You have to check for a nullptr before.
-
返回
-
Internal pointer.
◆
operator*()
[1/2]
Dereferences the
BaseRef
. This yields the pointee as a reference. You have to check for a nullptr before.
-
返回
-
Pointee as a reference.
◆
operator*()
[2/2]
Dereferences the
BaseRef
. This yields the pointee as a reference. You have to check for a nullptr before.
-
返回
-
Pointee as a reference.
◆
operator T*()
User-defined conversion to T*, unpacking the internal pointer.
-
返回
-
Internal pointer.
◆
operator ConstReferencedType *()
User-defined conversion to T*, unpacking the internal pointer.
-
返回
-
Internal pointer.
◆
operator Bool()
[1/2]
Checks if the pointer is valid (i.e., not a nullptr).
-
返回
-
True if the internal pointer is not a nullptr.
◆
operator Bool()
[2/2]
◆
operator[]()
const T& operator[]
|
(
|
Int
|
|
)
|
const
|
|
delete
|
◆
MakeWritable()
ResultRef
<typename std::remove_const<T>::type> MakeWritable
|
(
|
Bool
|
resetOnError
=
true
|
)
|
|
Makes the pointee writable. Depending on the argument for the template parameter HANDLER, this may just return the original pointer, or it may replace the pointer by a new one pointing to a copy of the original pointee, and return the new pointer. The latter is the case for copy-on-write semantics if there is more than one reference to the pointee.
-
参数
-
[in]
|
resetOnError
|
If true (the default), the reference will be reset when the copy fails. If false, the reference will stay the same, but the function returns nullptr.
|
-
返回
-
Internal pointer, which might be a new pointer pointing to a copy of the original pointee, or nullptr if the copy failed.
◆
GetOrNull()
◆
Create()
Creates the referenced type.
-
参数
-
[in]
|
args
|
Arguments for the construcor.
|
-
Template Parameters
-
-
返回
-
OK on success, FAILED only for OutOfMemoryerror.
◆
operator=()
[1/7]
Lets the
BaseRef
point to another object. This performs the necessary operations for removing the reference to the current pointee and adding the reference to the new pointee.
-
参数
-
[in]
|
src
|
New pointer, may be nullptr.
|
◆
operator=()
[2/7]
BaseRef
& operator=
|
(
|
std::nullptr_t
|
src
|
)
|
|
Resets the internal pointer to nullptr, which includes the proper release of the reference.
◆
operator=()
[3/7]
Lets the
BaseRef
point to the object of another
BaseRef
. This performs the necessary operations for removing the reference to the current pointee and adding the reference to the new pointee.
-
参数
-
[in]
|
src
|
BaseRef
of which the pointer will be used.
|
◆
operator=()
[4/7]
BaseRef
& operator=
|
(
|
const REF &
|
src
|
)
|
|
Lets the
BaseRef
point to the object of another reference object. This performs the necessary operations for removing the reference to the current pointee and adding the reference to the new pointee.
-
参数
-
[in]
|
src
|
Another reference of which the pointer will be used.
|
◆
operator=()
[5/7]
◆
operator=()
[6/7]
Moves the internal pointer from src to this. src will be a nullptr reference afterwards.
-
参数
-
[in]
|
src
|
BaseRef
whose internal pointer shall be moved to this
BaseRef
.
|
◆
operator=()
[7/7]
Moves the internal pointer from src to this. src will be a nullptr reference afterwards. Moves from
BaseRef
with different HANDLER as well as interface references.
-
参数
-
[in]
|
src
|
BaseRef
whose internal pointer shall be moved to this
BaseRef
.
|
◆
operator==()
[1/4]
Bool
operator==
|
(
|
const
BaseRef
< T, HANDLER > &
|
b
|
)
|
const
|
Returns true when this and b are equal as determined by
RefCompare
.
-
参数
-
[in]
|
b
|
Reference to compare with.
|
-
返回
-
True when this and b are equal, false otherwise.
◆
operator!=()
[1/4]
Bool
operator!=
|
(
|
const
BaseRef
< T, HANDLER > &
|
b
|
)
|
const
|
Returns true when this and b are not equal as determined by
RefCompare
.
-
参数
-
[in]
|
b
|
Reference to compare with.
|
-
返回
-
True when this and b are not equal, false otherwise.
◆
operator==()
[2/4]
Bool
operator==
|
(
|
const T *
|
b
|
)
|
const
|
Returns true when this and b are equal as determined by
RefCompare
.
-
参数
-
[in]
|
b
|
Pointer to compare with.
|
-
返回
-
True when this and b are equal, false otherwise.
◆
operator!=()
[2/4]
Bool
operator!=
|
(
|
const T *
|
b
|
)
|
const
|
Returns true when this and b are not equal as determined by
RefCompare
.
-
参数
-
[in]
|
b
|
Reference to compare with.
|
-
返回
-
True when this and b are not equal, false otherwise.
◆
operator==()
[3/4]
Bool
operator==
|
(
|
typename std::remove_const< T >::type *
|
b
|
)
|
const
|
Returns true when this and b are equal as determined by
RefCompare
.
-
参数
-
[in]
|
b
|
Pointer to compare with.
|
-
返回
-
True when this and b are equal, false otherwise.
◆
operator!=()
[3/4]
Bool
operator!=
|
(
|
typename std::remove_const< T >::type *
|
b
|
)
|
const
|
Returns true when this and b are not equal as determined by
RefCompare
.
-
参数
-
[in]
|
b
|
Reference to compare with.
|
-
返回
-
True when this and b are not equal, false otherwise.
◆
operator==()
[4/4]
Bool
operator==
|
(
|
std::nullptr_t
|
|
)
|
const
|
Returns true when this and a null reference are equal as determined by
RefCompare
.
-
返回
-
True when this and nullptr are equal, false otherwise.
◆
operator!=()
[4/4]
Bool
operator!=
|
(
|
std::nullptr_t
|
|
)
|
const
|
Returns true when this and a null reference are not equal as determined by
RefCompare
.
-
返回
-
True when this and nullptr are not equal, false otherwise.
◆
GetHashCode()
Returns the hash code of the reference, see
RefCompare
.
-
返回
-
The reference's hash code.
◆
NullValueRef()
static const
BaseRef
& NullValueRef
|
(
|
|
)
|
|
|
static
|
◆
NullValue()
◆
Disconnect()
Sets the internal pointer to nullptr and returns its previous value. This moves the ownership of the object from this UniqueRef to the invoking code, so you have to take care of proper deallocation once the object isn't needed any longer.
◆
PrivateSetTarget()
void PrivateSetTarget
|
(
|
ResultPtr
< T >
|
src
|
)
|
|
Lets the
BaseRef
point to another freshly allocated object. This performs the necessary operations for removing the reference to the current pointee and adding the reference to the new pointee.
-
参数
-
[in]
|
src
|
New pointer, may be nullptr.
|
◆
PrivateSetPointer()
void PrivateSetPointer
|
(
|
T *
|
ptr
|
)
|
|
◆
operator+()
void operator+
|
(
|
Int
|
|
)
|
const
|
|
private
|
◆
operator-()
[1/2]
void operator-
|
(
|
Int
|
|
)
|
const
|
|
private
|
◆
operator-()
[2/2]
void operator-
|
(
|
const
BaseRef
< T, HANDLER > &
|
|
)
|
const
|
|
private
|
◆
PrivateNullValue()
[1/2]
static const
BaseRef
& PrivateNullValue
|
(
|
std::false_type *
|
|
)
|
|
|
static
private
|
◆
PrivateNullValue()
[2/2]
static
BaseRef
PrivateNullValue
|
(
|
std::true_type *
|
|
)
|
|
|
static
private
|
Friends And Related Function Documentation
◆
BaseRef
Member Data Documentation
◆
DIRECT_REF
const
Bool
DIRECT_REF
static
|
|
◆
_object