MiniObject#
- class MiniObject(*args, **kwargs)#
MiniObject
is a simple structure that can be used to implement refcounted
types.
Subclasses will include MiniObject
as the first member in their structure
and then call init()
to initialize the MiniObject
fields.
ref()
and unref()
increment and decrement the
refcount respectively. When the refcount of a mini-object reaches 0, the
dispose function is called first and when this returns True
, the free
function of the miniobject is called.
A copy can be made with copy()
.
is_writable()
will return True
when the refcount of the
object is exactly 1 and there is no parent or a single parent exists and is
writable itself, meaning the current caller has the only reference to the
object. make_writable()
will return a writable version of
the object, which might be a new copy when the refcount was not 1.
Opaque data can be associated with a MiniObject
with
set_qdata()
and get_qdata()
. The data is
meant to be specific to the particular object and is not automatically copied
with copy()
or similar methods.
A weak reference can be added and remove with weak_ref()
and weak_unref()
respectively.
Methods#
- class MiniObject
- add_parent(parent: MiniObject) None #
This adds
parent
as a parent forobject
. Having one ore more parents affects the writability ofobject
: if aparent
is not writable,object
is also not writable, regardless of its refcount.object
is only writable if all the parents are writable and its own refcount is exactly 1.Note: This function does not take ownership of
parent
and also does not take an additional reference. It is the responsibility of the caller to remove the parent again at a later time.Added in version 1.16.
- Parameters:
parent – a parent
MiniObject
- get_qdata(quark: int) Any | None #
This function gets back user data pointers stored via
set_qdata()
.- Parameters:
quark – A
Quark
, naming the user data pointer
- is_writable() bool #
If
mini_object
has the LOCKABLE flag set, check if the current EXCLUSIVE lock onobject
is the only one, this means that changes to the object will not be visible to any other object.If the LOCKABLE flag is not set, check if the refcount of
mini_object
is exactly 1, meaning that no other reference exists to the object and that the object is therefore writable.Modification of a mini-object should only be done after verifying that it is writable.
- lock(flags: LockFlags) bool #
Lock the mini-object with the specified access mode in
flags
.- Parameters:
flags –
LockFlags
- remove_parent(parent: MiniObject) None #
This removes
parent
as a parent forobject
. Seeadd_parent()
.Added in version 1.16.
- Parameters:
parent – a parent
MiniObject
- classmethod replace(newdata: MiniObject | None = None) Tuple[bool, MiniObject | None] #
Atomically modifies a pointer to point to a new mini-object. The reference count of
olddata
is decreased and the reference count ofnewdata
is increased.Either
newdata
and the value pointed to byolddata
may beNone
.- Parameters:
newdata – pointer to new mini-object
- set_qdata(quark: int, data: Any, destroy: Callable[[Any], None]) None #
This sets an opaque, named pointer on a miniobject. The name is specified through a
Quark
(retrieved e.g. viaquark_from_static_string()
), and the pointer can be gotten back from theobject
withget_qdata()
until theobject
is disposed. Setting a previously set user data pointer, overrides (frees) the old pointer set, usingNone
as pointer essentially removes the data stored.destroy
may be specified which is called withdata
as argument when theobject
is disposed, or the data is being overwritten by a call toset_qdata()
with the samequark
.- Parameters:
quark – A
Quark
, naming the user data pointerdata – An opaque user data pointer
destroy – Function to invoke with
data
as argument, whendata
needs to be freed
- classmethod take(newdata: MiniObject) Tuple[bool, MiniObject] #
Modifies a pointer to point to a new mini-object. The modification is done atomically. This version is similar to
replace()
except that it does not increase the refcount ofnewdata
and thus takes ownership ofnewdata
.Either
newdata
and the value pointed to byolddata
may beNone
.- Parameters:
newdata – pointer to new mini-object