1 | /* |
---|
2 | * WeakReference.java |
---|
3 | * |
---|
4 | * Copyright (C) 2011 Erik Huelsmann |
---|
5 | * $Id: WeakReference.java 13440 2011-08-05 21:25:10Z ehuelsmann $ |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
20 | * |
---|
21 | * As a special exception, the copyright holders of this library give you |
---|
22 | * permission to link this library with independent modules to produce an |
---|
23 | * executable, regardless of the license terms of these independent |
---|
24 | * modules, and to copy and distribute the resulting executable under |
---|
25 | * terms of your choice, provided that you also meet, for each linked |
---|
26 | * independent module, the terms and conditions of the license of that |
---|
27 | * module. An independent module is a module which is not derived from |
---|
28 | * or based on this library. If you modify this library, you may extend |
---|
29 | * this exception to your version of the library, but you are not |
---|
30 | * obligated to do so. If you do not wish to do so, delete this |
---|
31 | * exception statement from your version. |
---|
32 | */ |
---|
33 | package org.armedbear.lisp; |
---|
34 | |
---|
35 | import static org.armedbear.lisp.Lisp.*; |
---|
36 | |
---|
37 | public class WeakReference extends LispObject { |
---|
38 | |
---|
39 | java.lang.ref.WeakReference<LispObject> ref; |
---|
40 | |
---|
41 | public WeakReference(LispObject ref) { |
---|
42 | this.ref = new java.lang.ref.WeakReference<LispObject>(ref); |
---|
43 | } |
---|
44 | |
---|
45 | @Override |
---|
46 | public LispObject typeOf() { |
---|
47 | return Symbol.WEAK_REFERENCE; |
---|
48 | } |
---|
49 | |
---|
50 | @Override |
---|
51 | public LispObject classOf() { |
---|
52 | return BuiltInClass.WEAK_REFERENCE; |
---|
53 | } |
---|
54 | |
---|
55 | @Override |
---|
56 | public String printObject() { |
---|
57 | return unreadableString("WEAK-REFERENCE " |
---|
58 | + toString()); |
---|
59 | } |
---|
60 | |
---|
61 | @Override |
---|
62 | public LispObject typep(LispObject typeSpecifier) { |
---|
63 | if (typeSpecifier == Symbol.WEAK_REFERENCE) { |
---|
64 | return T; |
---|
65 | } |
---|
66 | if (typeSpecifier == BuiltInClass.WEAK_REFERENCE) { |
---|
67 | return T; |
---|
68 | } |
---|
69 | return super.typep(typeSpecifier); |
---|
70 | } |
---|
71 | |
---|
72 | private static final Primitive MAKE_WEAK_REFERENCE = |
---|
73 | new pf_make_weak_reference(); |
---|
74 | @DocString(name="make-weak-reference", args="obj", |
---|
75 | doc="Creates a weak reference to 'obj'.") |
---|
76 | private static final class pf_make_weak_reference extends Primitive |
---|
77 | { |
---|
78 | pf_make_weak_reference() |
---|
79 | { |
---|
80 | super("make-weak-reference", PACKAGE_EXT, true); |
---|
81 | } |
---|
82 | |
---|
83 | @Override |
---|
84 | public LispObject execute(LispObject obj) { |
---|
85 | return new WeakReference(obj); |
---|
86 | } |
---|
87 | }; |
---|
88 | |
---|
89 | private static final Primitive WEAK_REFERENCE_VALUE = |
---|
90 | new pf_weak_reference_value(); |
---|
91 | @DocString(name="weak-reference-value", args="obj", |
---|
92 | doc="Returns two values, the first being the value of the weak ref," |
---|
93 | + "the second T if the reference is valid, or NIL if it has" |
---|
94 | + "been cleared.") |
---|
95 | private static final class pf_weak_reference_value extends Primitive |
---|
96 | { |
---|
97 | pf_weak_reference_value() |
---|
98 | { |
---|
99 | super("weak-reference-value", PACKAGE_EXT, true); |
---|
100 | } |
---|
101 | |
---|
102 | @Override |
---|
103 | public LispObject execute(LispObject obj) { |
---|
104 | if (! (obj instanceof WeakReference)) |
---|
105 | return Lisp.type_error(obj, Symbol.WEAK_REFERENCE); |
---|
106 | |
---|
107 | LispObject value = ((WeakReference)obj).ref.get(); |
---|
108 | return LispThread.currentThread().setValues(value == null ? NIL : value, |
---|
109 | value == null ? NIL : T); |
---|
110 | } |
---|
111 | }; |
---|
112 | } |
---|