Callback functions used to overload various builtin functions.
The functions can be grouped into a few sets:
Object initialization and destruction.
__INIT()
, create()
, destroy()
Unary operator overloading.
`~()
, `!()
,
_values()
, cast()
,
_sizeof()
, _indices()
,
__hash()
Binary asymmetric operator overloading.
`+()
, ``+()
,
`-()
, ``-()
,
`&()
, ``&()
,
`|()
, ``|()
,
`^()
, ``^()
,
`<<()
, ``<<()
,
`>>()
, ``>>()
,
`*()
, ``*()
,
`/()
, ``/()
,
`%()
, ``%()
Binary symmetric operator overloading.
The optimizer will make assumptions about the relations between these functions.
`==()
, _equal()
, `<()
, `>()
Other binary operator overloading.
`[]()
, `[]=()
, `->()
,
`->=()
, `+=()
, `()()
Overloading of other builtin functions.
_is_type()
, _sprintf()
, _m_delete()
,
_get_iterator()
, _search()
Although these functions are called from outside the object they
exist in, they will still be used even if they are declared
protected
. It is in fact recommended to declare them
protected
, since that will hinder them being used for
other purposes.
::
void
__INIT()
Inherit and variable initialization.
This function is generated automatically by the compiler. It's
called just before lfun::create()
when an object is
instantiated.
It first calls any __INIT
functions in inherited classes
(regardless of modifiers on the inherits). It then executes all
the variable initialization expressions in this class, in the
order they occur.
This function can not be overloaded or blocked from executing.
lfun::create()
int
__hash()
Hashing callback.
The main caller of this function is predef::hash_value()
or the low-level equivalent, which get called by various
mapping operations when the object is used as index in a mapping.
It should return an integer that corresponds to the object
in such a way that all values which lfun::`==
considers
equal to the object get the same hash value.
The function predef::hash
does not return hash values that
are compatible with this one.
It's assumed that this function is side-effect free.
lfun::`==
, predef::hash_value()
void
_deserialize(object
o
, function
(function
(mixed
:void
), string
, type
:mixed
) deserializer
)
Dispatch function for Serialization.deserialize()
.
o
Object to serialize. Always a context of the current object.
deserializer
Function to be called once for every variable to serialize.
The deserializer
function expects to be called with three arguments:
setter - Function that sets the symbol value.
symbol - The symbol name.
symbol_type - The type of the symbol.
A default implementation of lfun::_serialize()
and
lfun::_deserialize()
is available in Serializer.Serializable
.
lfun::_serialize()
, Serializer.deserialize()
,
Serializer.Serializable()->_deserialize()
int
_equal(mixed
arg
)
Recursive equality callback.
It's assumed that this function is side-effect free.
predef::equal()
, lfun::`==()
predef::Iterator
_get_iterator()
Iterator creation callback.
The returned predef::Iterator
instance works as a cursor that
references a specific item contained (in some arbitrary sense)
in this one.
It's assumed that this function is side-effect free.
predef::Iterator
, predef::get_iterator
, predef::foreach()
array
_indices(object
|void
context
, int
|void
access
)
List indices callback.
Expected to return an array with the valid indices in the object.
It's assumed that this function is side-effect free.
predef::indices()
, lfun::_values()
, lfun::_types()
,
::_indices()
bool
_is_type(string
basic_type
)
Type comparison callback.
Called by the cast operator to determine if an object simulates a basic type.
basic_type
One of:
|
|
|
|
|
|
|
|
|
|
|
|
The following five shouldn't occurr, but are here for completeness:
|
|
|
|
|
Expected to return 1
if the object is to be regarded as a
simulation of the type specified by basic_type
.
The argument is currently a string with the name of the type, but might in the future be a value of the type type.
It's assumed that this function is side-effect free.
mixed
_m_delete(mixed
arg
)
Delete index callback.
predef::m_delete()
mixed
_random()
Called by random()
. Typical use is when the object implements
a ADT, when a call to this lfun should return a random member of
the ADT or range implied by the ADT.
predef::random()
mixed
_search(mixed
needle
, mixed
|void
start
)
Search callback.
predef::search()
void
_serialize(object
o
, function
(mixed
, string
, type
:void
) serializer
)
Dispatch function for Serializer.serialize()
.
o
Object to serialize. Always a context of the current object.
serializer
Function to be called once for every variable to serialize.
The serializer
function expects to be called with three arguments:
value - The value of the symbol.
symbol - The symbol name.
symbol_type - The type of the symbol.
A default implementation of lfun::_serialize()
and
lfun::_deserialize()
is available in Serializer.Serializable
.
lfun::_deserialize()
, Serializer.serialize()
,
Serializer.Serializable()->_serialize()
int
_size_object()
Debug.size_object()
callback.
Returns an approximation of the memory use in bytes for the object.
Debug.size_object()
, lfun::_sizeof()
int
_sizeof()
Size query callback.
Called by predef::sizeof()
to determine the number of elements
in an object. If this function is not present, the number
of public symbols in the object will be returned.
Expected to return the number of valid indices in the object.
It's assumed that this function is side-effect free.
predef::sizeof()
string
_sprintf(int
conversion_type
, mapping
(string
:int
)|void
params
)
Sprintf callback.
This method is called by predef::sprintf()
to print objects. If it is
not present, printing of the object will not be supported for any
conversion-type except for the %O-conversion-type, which
will output "object"
.
conversion_type
One of:
| Signed binary integer. |
| Signed decimal integer. |
| Unsigned decimal integer. |
| Signed octal integer. |
| Lowercase signed hexadecimal integer. |
| Uppercase signed hexadecimal integer. |
| Character. If a fieldsize has been specified this will output the low-order bytes of the integer in network byte order. |
| Float. |
| Heuristically chosen representation of float. |
| Like %g, but uses uppercase E for exponent. |
| Exponential notation float. |
| Like %e, but uses uppercase E for exponent. |
| String. |
| Any value (debug style). |
| Type of the argument. |
params
Conversion parameters. The following parameters may be supplied:
| Precision. |
| Field width. |
| Indicates that the output should be left-aligned. |
| Indentation level in %O-mode. |
Is expected to return a string describing the object formatted
according to conversion_type
.
_sprintf()
is currently not called for the following
conversion-types:
| Binary IEEE representation of float (%4F gives single precision, %8F gives double precision.) |
This function might be called at odd times, e.g. before
lfun::create
has been called or when an error has occurred.
The reason is typically that it gets called when a backtrace is
being formatted to report an error. It should therefore be very
robust and not make any assumptions about its own internal
state, at least not when conversion_type
is 'O'
.
It's assumed that this function is side-effect free.
predef::sprintf()
mixed
_sqrt()
Called by sqrt when the square root of an object is requested.
_sqrt is not a real lfun, so it must not be defined as static.
predef::sqrt()
array
_types(object
|void
context
, int
|void
access
)
List types callback.
This callback is typically called via predef::types()
.
Expected to return an array with the types corresponding to
the indices returned by lfun::_indices()
.
It's assumed that this function is side-effect free.
predef::types()
was added in Pike 7.9.
predef::types()
, lfun::_indices()
, lfun::_values()
,
::_types()
array
_values(object
|void
context
, int
|void
access
)
List values callback.
Expected to return an array with the values corresponding to
the indices returned by lfun::_indices()
.
It's assumed that this function is side-effect free.
predef::values()
, lfun::_indices()
, lfun::_types()
,
::_values()
int
`!()
Logical not callback.
Returns non-zero if the object should be evaluated as false,
and 0
(zero) otherwise.
It's assumed that this function is side-effect free.
predef::`!()
mixed
`%(zero
... args
)
Left side modulo callback.
It's assumed that this function is side-effect free.
lfun::``%()
, predef::`%()
mixed
`&(zero
... args
)
Left side bitwise and/intersection callback.
It's assumed that this function is side-effect free.
lfun::``&()
, predef::`&()
mixed
`()(zero
... args
)
Apply callback.
predef::`()
mixed
`*(zero
... args
)
Left side multiplication/repetition/implosion callback.
It's assumed that this function is side-effect free.
lfun::``*()
, predef::`*()
mixed
`+(zero
arg
, zero
... rest
)
Left side addition/concatenation callback.
This is used by predef::`+
. It's called with any arguments
that follow this object in the argument list of the call to
predef::`+
. The returned value should be a new instance that
represents the addition/concatenation between this object and
the arguments in the order they are given.
It's assumed that this function is side-effect free.
lfun::``+()
, lfun::`+=()
, predef::`+()
this_program
`+=(zero
arg
, zero
... rest
)
Destructive addition/concatenation callback.
This is used by predef::`+
. It's called with any arguments
that follow this object in the argument list of the call to
predef::`+
. It should update this object to represent the
addition/concatenation between it and the arguments in the order
they are given. It should always return this object.
This function should only be implemented if lfun::`+()
also
is. It should only work as a more optimized alternative to that
one, for the case when it's safe to change the object
destructively and use it directly as the result.
This function is not an lfun for the +=
operator. It's
only whether or not it's safe to do a destructive change that
decides if this function or lfun::`+()
is called; both the
+
operator and the +=
operator can call either
one.
lfun::`+()
, predef::`+()
mixed
`-(void
|zero
arg
)
Negation and left side subtraction/set difference callback.
This is used by predef::`-
. When called without an argument
the result should be a new instance that represents the negation
of this object, otherwise the result should be a new instance
that represents the difference between this object and arg
.
It's assumed that this function is side-effect free.
lfun::``-()
, predef::`-()
mixed
`->(string
index
, object
|void
context
, int
|void
access
)
Arrow index callback.
index
Symbol in context
to access.
context
Context in the current object to start the search from.
If UNDEFINED
or left out, this_program::this
is to be be used (ie start at the current context and ignore
any overloaded symbols).
access
Access permission override. One of the following:
| See only public symbols. |
| |
| See protected symbols as well. |
Returns the value at index
if it exists, and
UNDEFINED
otherwise.
It's assumed that this function is side-effect free.
predef::`->()
, ::`->()
mixed
`->=(string
index
, zero
value
, object
|void
context
, int
|void
access
)
Arrow index assignment callback.
index
Symbol in context
to change the value of.
value
The new value.
context
Context in the current object to start the search from.
If UNDEFINED
or left out, this_program::this
is to be used (ie start at the current context and ignore
any overloaded symbols).
access
Access permission override. One of the following:
| See only public symbols. |
| |
| See protected symbols as well. |
This function is to index the current object with the string index
,
and set it to value
.
Returns the set value
.
predef::`->=()
, ::`->=()
, lfun::`[]=()
mixed
`/(zero
... args
)
Left side division/split callback.
It's assumed that this function is side-effect free.
lfun::``/()
, predef::`/()
bool
`<(mixed
arg
)
Less than test callback.
It's assumed that this function is side-effect free.
predef::`<()
mixed
`<<(zero
arg
)
Left side left shift callback.
It's assumed that this function is side-effect free.
lfun::``<<()
, predef::`<<()
bool
`==(mixed
arg
)
Equality test callback.
If this is implemented it might be necessary to implement
lfun::__hash
too. Otherwise mappings might hold several
objects as indices which are duplicates according to this
function. Various other functions that use hashing also might
not work correctly, e.g. predef::Array.uniq
.
It's assumed that this function is side-effect free.
predef::`==()
, lfun::__hash
bool
`>(mixed
arg
)
Greater than test callback.
It's assumed that this function is side-effect free.
predef::`>()
mixed
`>>(zero
arg
)
Left side right shift callback.
It's assumed that this function is side-effect free.
lfun::``>>()
, predef::`>>()
mixed
`[..](zero
low
, int
low_bound_type
, zero
high
, int
high_bound_type
)
Subrange callback.
It's assumed that this function is side-effect free.
predef::`[..]
mixed
`[](zero
arg1
, zero
|void
arg2
)
Indexing callback.
For compatibility, this is also called to do subranges unless
there is a `[..]
in the class. See predef::`[..]
for
details.
It's assumed that this function is side-effect free.
predef::`[]()
, predef::`[..]
mixed
`[]=(zero
arg1
, zero
arg2
)
Index assignment callback.
predef::`[]=()
, lfun::`->=()
mixed
`^(zero
... args
)
Left side exclusive or callback.
It's assumed that this function is side-effect free.
lfun::``^()
, predef::`^()
mixed
``%(zero
... args
)
Right side modulo callback.
It's assumed that this function is side-effect free.
lfun::`%()
, predef::`%()
mixed
``&(zero
... args
)
Right side bitwise and/intersection callback.
It's assumed that this function is side-effect free.
lfun::`&()
, predef::`&()
mixed
``*(zero
... args
)
Right side multiplication/repetition/implosion callback.
It's assumed that this function is side-effect free.
lfun::`*()
, predef::`*()
mixed
``+(zero
arg
, zero
... rest
)
Right side addition/concatenation callback.
This is used by predef::`+
. It's called with any arguments
that precede this object in the argument list of the call to
predef::`+
. The returned value should be a new instance that
represents the addition/concatenation between the arguments in
the order they are given and this object.
It's assumed that this function is side-effect free.
lfun::`+()
, predef::`+()
mixed
``-(zero
arg
)
Right side subtraction/set difference callback.
This is used by predef::`-
. The result should be a new
instance that represents the difference between arg
and this
object.
It's assumed that this function is side-effect free.
lfun::`-()
, predef::`-()
mixed
``/(zero
... args
)
Right side division/split callback.
It's assumed that this function is side-effect free.
lfun::`/()
, predef::`/()
mixed
``<<(zero
arg
)
Right side left shift callback.
It's assumed that this function is side-effect free.
lfun::`<<()
, predef::`<<()
mixed
``>>(zero
arg
)
Right side right shift callback.
It's assumed that this function is side-effect free.
lfun::`>>()
, predef::`>>()
mixed
``^(zero
... args
)
Right side exclusive or callback.
It's assumed that this function is side-effect free.
lfun::`^()
, predef::`^()
mixed
``|(zero
... args
)
Right side bitwise or/union callback.
It's assumed that this function is side-effect free.
lfun::`|()
, predef::`|()
mixed
`|(zero
... args
)
Left side bitwise or/union callback.
It's assumed that this function is side-effect free.
lfun::``|()
, predef::`|()
mixed
`~()
Complement/inversion callback.
It's assumed that this function is side-effect free.
predef::`~()
mixed
cast(string
requested_type
)
Value cast callback.
requested_type
Type to cast to.
Expected to return the object value-casted (converted) to
the type described by requested_type
.
The argument is currently a string with the name of the type, but might in the future be a value of the type type.
Currently casting between object types is a noop.
If the returned value is not deemed to be of the requested type a runtime error may be thrown.
It's assumed that this function is side-effect free.
void
lfun:create(zero
... args
)
Object creation callback.
This function is called right after lfun::__INIT()
.
args
are the arguments passed when the program was called.
In Pike 7.2 and later this function can be created implicitly by the compiler using the new syntax:
class Foo(int foo) {
int bar;
}
In the above case an implicit lfun::create()
is created, and
it's equivalent to:
class Foo {
int foo;
int bar;
protected void create(int foo)
{
local::foo = foo;
}
}
lfun::__INIT()
, lfun::destroy()
void
destroy(void
|int
reason
)
Object destruction callback.
This function is called right before the object is destructed.
That can happen either through a call to predef::destruct()
,
when there are no more references to the object, or when the
garbage collector discovers that it's part of a cyclic data
structure that has become garbage.
reason
A flag that tells why the object is destructed:
| Destructed explicitly by |
| Destructed due to running out of references. |
| Destructed by the garbage collector. |
| Destructed as part of the cleanup when the pike process exits. Occurs only if Pike has been compiled with the configure option --with-cleanup-on-exit. See note below. |
Objects are normally not destructed when a process exits, so
destroy
functions aren't called then. Use atexit
to get
called when the process exits.
Regarding destruction order during garbage collection:
If an object is destructed by the garbage collector, it's part of
a reference cycle with other things but with no external
references. If there are other objects with destroy
functions in the same cycle, it becomes a problem which to call
first.
E.g. if this object has a variable with another object which (directly or indirectly) points back to this one, you might find that the other object already has been destructed and the variable thus contains zero.
The garbage collector tries to minimize such problems by defining an order as far as possible:
If an object A contains an lfun::destroy
and an object B does
not, then A is destructed before B.
If A references B single way, then A is destructed before B.
If A and B are in a cycle, and there is a reference somewhere from B to A that is weaker than any reference from A to B, then A is destructed before B.
If a cycle is resolved according to the rule above by ignoring a weaker reference, and there is another ambiguous cycle that would get resolved by ignoring the same reference, then the latter cycle will be resolved by ignoring that reference.
Weak references (e.g. set with predef::set_weak_flag()
) are
considered weaker than normal references, and both are
considered weaker than strong references.
Strong references are those from objects to the objects of their lexically surrounding classes. There can never be a cycle consisting only of strong references. (This means the gc never destructs a parent object before all children have been destructed.)
An example with well defined destruct order due to strong references:
class Super {
class Sub {
protected void destroy() {
if (!Super::this)
error ("My parent has been destructed!\n");
}
}
Sub sub = Sub();
protected void destroy() {
if (!sub)
werror ("sub already destructed.\n");
}
}
The garbage collector ensures that these objects are destructed in
an order so that werror
in Super
is called and not
error
in Sub
.
When the garbage collector calls lfun::destroy
, all accessible
non-objects and objects without destroy
functions are
still intact. They are not freed if the destroy
function
adds external references to them. However, all objects with
lfun::destroy
in the cycle are already scheduled for
destruction and will therefore be destroyed even if external
references are added to them.
The garbage collector had completely random destruct order in versions prior to 7.2.
lfun::create()
, predef::destruct()
mixed
lfun::symbol
Symbols implicitly inherited from the virtual base class.
These symbols exist mainly to simplify implementation of the corresponding lfuns.
lfun::
mixed
_indices(object
|void
context
, int
|void
access
)
context
Context in the current object to start the list from.
If UNDEFINED
or left out, this_program::this
will be used (ie start at the current context and ignore
any overloaded symbols).
access
Access permission override. One of the following:
| See only public symbols. |
| |
| See protected symbols as well. |
Builtin function to list the identifiers of an object.
This is useful when lfun::_indices
has been overloaded.
::_values()
, ::_types()
, ::`->()
mixed
_types(object
|void
context
, int
|void
access
)
context
Context in the current object to start the list from.
If UNDEFINED
or left out, this_program::this
will be used (ie start at the current context and ignore
any overloaded symbols).
access
Access permission override. One of the following:
| See only public symbols. |
| |
| See protected symbols as well. |
Builtin function to list the types of the identifiers of an
object. This is useful when lfun::_types
has been overloaded.
::_indices()
, ::_values()
, ::`->()
mixed
_values(object
|void
context
, int
|void
access
)
context
Context in the current object to start the list from.
If UNDEFINED
or left out, this_program::this
will be used (ie start at the current context and ignore
any overloaded symbols).
access
Access permission override. One of the following:
| See only public symbols. |
| |
| See protected symbols as well. |
Builtin function to list the values of the identifiers of an
object. This is useful when lfun::_values
has been overloaded.
::_indices()
, ::_types()
, ::`->()
mixed
`->(string
index
, object
|void
context
, int
|void
access
)
Builtin arrow operator.
index
Symbol in context
to access.
context
Context in the current object to start the search from.
If UNDEFINED
or left out, this_program::this
will be used (ie start at the current context and ignore
any overloaded symbols).
access
Access permission override. One of the following:
| See only public symbols. |
| |
| See protected symbols as well. |
This function indexes the current object with the string index
.
This is useful when the arrow operator has been overloaded.
::`->=()
void
`->=(string
index
, mixed
value
, object
|void
context
, int
|void
access
)
Builtin arrow set operator.
index
Symbol in context
to change the value of.
value
The new value.
context
Context in the current object to start the search from.
If UNDEFINED
or left out, this_program::this
will be used (ie start at the current context and ignore
any overloaded symbols).
access
Access permission override. One of the following:
| See only public symbols. |
| |
| See protected symbols as well. |
This function indexes the current object with the string index
,
and sets it to value
.
This is useful when the arrow set operator has been overloaded.
::`->()