-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
box.cr
47 lines (44 loc) · 1.55 KB
/
box.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# A Box allows turning any object to a `Void*` and back.
#
# A Box's purpose is passing data to C as a `Void*` and then converting that
# back to the original data type.
#
# For an example usage, see `Proc`'s explanation about sending Procs to C.
class Box(T)
# Returns the original object
getter object : T
# Creates a `Box` with the given object.
#
# This method isn't usually used directly. Instead, `Box.box` is used.
def initialize(@object : T)
end
# Turns *object* into a `Void*`.
#
# If `T` is not a reference type, nor a union between reference types and
# `Nil`, this method effectively copies *object* to the dynamic heap.
#
# NOTE: The returned pointer might not be a null pointer even when *object* is
# `nil`.
def self.box(object : T) : Void*
{% if T.union_types.all? { |t| t == Nil || t < Reference } %}
object.as(Void*)
{% else %}
# NOTE: if `T` is explicitly specified and `typeof(object) < T` (e.g.
# `Box(Int32?).box(1)`, then `.new` will perform the appropriate upcast
new(object).as(Void*)
{% end %}
end
# Unboxes a `Void*` into an object of type `T`. Note that for this you must
# specify T: `Box(T).unbox(data)`.
#
# WARNING: It is undefined behavior to box an object in one type and unbox it
# via a different type; in particular, when boxing a `T` and unboxing it as a
# `T?`, or vice-versa.
def self.unbox(pointer : Void*) : T
{% if T.union_types.all? { |t| t == Nil || t < Reference } %}
pointer.as(T)
{% else %}
pointer.as(self).object
{% end %}
end
end