source: branches/0.16.x/abcl/src/org/armedbear/lisp/SlotDefinition.java

Last change on this file was 11754, checked in by vvoutilainen, 16 years ago

Convert using ClassCastException? to checking instanceof.
Performance tests show this approach to be faster.
Patch by Douglas R. Miles. I modified the patch to
remove tabs, so indentation may be slightly off in places.
That's something that we need to handle separately, abcl
doesn't have a clear indentation policy.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 12.0 KB
Line 
1/*
2 * SlotDefinition.java
3 *
4 * Copyright (C) 2005 Peter Graves
5 * $Id: SlotDefinition.java 11754 2009-04-12 10:53:39Z vvoutilainen $
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
34package org.armedbear.lisp;
35
36public final class SlotDefinition extends StandardObject
37{
38  public SlotDefinition()
39  {
40    super(StandardClass.SLOT_DEFINITION,
41          StandardClass.SLOT_DEFINITION.getClassLayout().getLength());
42    slots[SlotDefinitionClass.SLOT_INDEX_LOCATION] = NIL;
43  }
44
45  public SlotDefinition(LispObject name, LispObject readers)
46  {
47    this();
48    try
49      {
50        Debug.assertTrue(name instanceof Symbol);
51        slots[SlotDefinitionClass.SLOT_INDEX_NAME] = name;
52        slots[SlotDefinitionClass.SLOT_INDEX_INITFUNCTION] = NIL;
53        slots[SlotDefinitionClass.SLOT_INDEX_INITARGS] =
54          new Cons(PACKAGE_KEYWORD.intern(((Symbol)name).getName()));
55        slots[SlotDefinitionClass.SLOT_INDEX_READERS] = readers;
56        slots[SlotDefinitionClass.SLOT_INDEX_ALLOCATION] = Keyword.INSTANCE;
57      }
58    catch (Throwable t)
59      {
60        Debug.trace(t);
61      }
62  }
63
64  public SlotDefinition(LispObject name, LispObject readers,
65                        LispObject initForm)
66  {
67    this();
68    try
69      {
70        Debug.assertTrue(name instanceof Symbol);
71        slots[SlotDefinitionClass.SLOT_INDEX_NAME] = name;
72        slots[SlotDefinitionClass.SLOT_INDEX_INITFUNCTION] = NIL;
73        slots[SlotDefinitionClass.SLOT_INDEX_INITFORM] = initForm;
74        slots[SlotDefinitionClass.SLOT_INDEX_INITARGS] =
75          new Cons(PACKAGE_KEYWORD.intern(((Symbol)name).getName()));
76        slots[SlotDefinitionClass.SLOT_INDEX_READERS] = readers;
77        slots[SlotDefinitionClass.SLOT_INDEX_ALLOCATION] = Keyword.INSTANCE;
78      }
79    catch (Throwable t)
80      {
81        Debug.trace(t);
82      }
83  }
84 
85  public static SlotDefinition checkSlotDefination(LispObject obj) throws ConditionThrowable {
86          if (obj instanceof SlotDefinition) return (SlotDefinition)obj;
87      return (SlotDefinition)type_error(obj, Symbol.SLOT_DEFINITION);     
88  }
89
90  public final LispObject getName()
91  {
92    return slots[SlotDefinitionClass.SLOT_INDEX_NAME];
93  }
94
95  public final void setLocation(int i)
96  {
97    slots[SlotDefinitionClass.SLOT_INDEX_LOCATION] = Fixnum.getInstance(i);
98  }
99
100  @Override
101  public String writeToString() throws ConditionThrowable
102  {
103    FastStringBuffer sb =
104      new FastStringBuffer(Symbol.SLOT_DEFINITION.writeToString());
105    LispObject name = slots[SlotDefinitionClass.SLOT_INDEX_NAME];
106    if (name != null && name != NIL)
107      {
108        sb.append(' ');
109        sb.append(name.writeToString());
110      }
111    return unreadableString(sb.toString());
112  }
113
114  // ### make-slot-definition
115  private static final Primitive MAKE_SLOT_DEFINITION =
116    new Primitive("make-slot-definition", PACKAGE_SYS, true, "")
117    {
118      @Override
119      public LispObject execute() throws ConditionThrowable
120      {
121        return new SlotDefinition();
122      }
123    };
124
125  // ### %slot-definition-name
126  private static final Primitive _SLOT_DEFINITION_NAME =
127    new Primitive(Symbol._SLOT_DEFINITION_NAME, "slot-definition")
128    {
129      @Override
130      public LispObject execute(LispObject arg) throws ConditionThrowable
131      {
132          return checkSlotDefination(arg).slots[SlotDefinitionClass.SLOT_INDEX_NAME];
133      }
134    };
135
136  // ### set-slot-definition-name
137  private static final Primitive SET_SLOT_DEFINITION_NAME =
138    new Primitive("set-slot-definition-name", PACKAGE_SYS, true,
139                  "slot-definition name")
140    {
141      @Override
142      public LispObject execute(LispObject first, LispObject second)
143        throws ConditionThrowable
144      {
145          checkSlotDefination(first).slots[SlotDefinitionClass.SLOT_INDEX_NAME] = second;
146          return second;
147      }
148    };
149
150  // ### %slot-definition-initfunction
151  private static final Primitive _SLOT_DEFINITION_INITFUNCTION =
152    new Primitive(Symbol._SLOT_DEFINITION_INITFUNCTION, "slot-definition")
153    {
154      @Override
155      public LispObject execute(LispObject arg) throws ConditionThrowable
156      {
157          return checkSlotDefination(arg).slots[SlotDefinitionClass.SLOT_INDEX_INITFUNCTION];
158      }
159    };
160
161  // ### set-slot-definition-initfunction
162  private static final Primitive SET_SLOT_DEFINITION_INITFUNCTION =
163    new Primitive("set-slot-definition-initfunction", PACKAGE_SYS, true,
164                  "slot-definition initfunction")
165    {
166      @Override
167      public LispObject execute(LispObject first, LispObject second)
168        throws ConditionThrowable
169      {
170          checkSlotDefination(first).slots[SlotDefinitionClass.SLOT_INDEX_INITFUNCTION] = second;
171          return second;
172      }
173    };
174
175  // ### %slot-definition-initform
176  private static final Primitive _SLOT_DEFINITION_INITFORM =
177    new Primitive("%slot-definition-initform", PACKAGE_SYS, true,
178                  "slot-definition")
179    {
180      @Override
181      public LispObject execute(LispObject arg) throws ConditionThrowable
182      {
183          return checkSlotDefination(arg).slots[SlotDefinitionClass.SLOT_INDEX_INITFORM];
184      }
185    };
186
187  // ### set-slot-definition-initform
188  private static final Primitive SET_SLOT_DEFINITION_INITFORM =
189    new Primitive("set-slot-definition-initform", PACKAGE_SYS, true,
190                  "slot-definition initform")
191    {
192      @Override
193      public LispObject execute(LispObject first, LispObject second)
194        throws ConditionThrowable
195      {
196          checkSlotDefination(first).slots[SlotDefinitionClass.SLOT_INDEX_INITFORM] = second;
197          return second;
198      }
199    };
200
201  // ### %slot-definition-initargs
202  private static final Primitive _SLOT_DEFINITION_INITARGS =
203    new Primitive(Symbol._SLOT_DEFINITION_INITARGS, "slot-definition")
204    {
205      @Override
206      public LispObject execute(LispObject arg) throws ConditionThrowable
207      {
208          return checkSlotDefination(arg).slots[SlotDefinitionClass.SLOT_INDEX_INITARGS];
209      }
210    };
211
212  // ### set-slot-definition-initargs
213  private static final Primitive SET_SLOT_DEFINITION_INITARGS =
214    new Primitive("set-slot-definition-initargs", PACKAGE_SYS, true,
215                  "slot-definition initargs")
216    {
217      @Override
218      public LispObject execute(LispObject first, LispObject second)
219        throws ConditionThrowable
220      {
221          checkSlotDefination(first).slots[SlotDefinitionClass.SLOT_INDEX_INITARGS] = second;
222          return second;
223      }
224    };
225
226  // ### %slot-definition-readers
227  private static final Primitive _SLOT_DEFINITION_READERS =
228    new Primitive("%slot-definition-readers", PACKAGE_SYS, true,
229                  "slot-definition")
230    {
231      @Override
232      public LispObject execute(LispObject arg) throws ConditionThrowable
233      {
234          return checkSlotDefination(arg).slots[SlotDefinitionClass.SLOT_INDEX_READERS];
235      }
236    };
237
238  // ### set-slot-definition-readers
239  private static final Primitive SET_SLOT_DEFINITION_READERS =
240    new Primitive("set-slot-definition-readers", PACKAGE_SYS, true,
241                  "slot-definition readers")
242    {
243      @Override
244      public LispObject execute(LispObject first, LispObject second)
245        throws ConditionThrowable
246      {
247          checkSlotDefination(first).slots[SlotDefinitionClass.SLOT_INDEX_READERS] = second;
248          return second;
249      }
250    };
251
252  // ### %slot-definition-writers
253  private static final Primitive _SLOT_DEFINITION_WRITERS =
254    new Primitive("%slot-definition-writers", PACKAGE_SYS, true,
255                  "slot-definition")
256    {
257      @Override
258      public LispObject execute(LispObject arg) throws ConditionThrowable
259      {
260          return checkSlotDefination(arg).slots[SlotDefinitionClass.SLOT_INDEX_WRITERS];
261      }
262    };
263
264  // ### set-slot-definition-writers
265  private static final Primitive SET_SLOT_DEFINITION_WRITERS =
266    new Primitive("set-slot-definition-writers", PACKAGE_SYS, true,
267                  "slot-definition writers")
268    {
269      @Override
270      public LispObject execute(LispObject first, LispObject second)
271        throws ConditionThrowable
272      {
273          checkSlotDefination(first).slots[SlotDefinitionClass.SLOT_INDEX_WRITERS] = second;
274          return second;
275      }
276    };
277
278  // ### %slot-definition-allocation
279  private static final Primitive _SLOT_DEFINITION_ALLOCATION =
280    new Primitive("%slot-definition-allocation", PACKAGE_SYS, true,
281                  "slot-definition")
282    {
283      @Override
284      public LispObject execute(LispObject arg) throws ConditionThrowable
285      {
286          return checkSlotDefination(arg).slots[SlotDefinitionClass.SLOT_INDEX_ALLOCATION];
287      }
288    };
289
290  // ### set-slot-definition-allocation
291  private static final Primitive SET_SLOT_DEFINITION_ALLOCATION =
292    new Primitive("set-slot-definition-allocation", PACKAGE_SYS, true,
293                  "slot-definition allocation")
294    {
295      @Override
296      public LispObject execute(LispObject first, LispObject second)
297        throws ConditionThrowable
298      {
299          checkSlotDefination(first).slots[SlotDefinitionClass.SLOT_INDEX_ALLOCATION] = second;
300          return second;
301      }
302    };
303
304  // ### %slot-definition-allocation-class
305  private static final Primitive _SLOT_DEFINITION_ALLOCATION_CLASS =
306    new Primitive("%slot-definition-allocation-class", PACKAGE_SYS, true,
307                  "slot-definition")
308    {
309      @Override
310      public LispObject execute(LispObject arg) throws ConditionThrowable
311      {
312          return checkSlotDefination(arg).slots[SlotDefinitionClass.SLOT_INDEX_ALLOCATION_CLASS];
313      }
314    };
315
316  // ### set-slot-definition-allocation-class
317  private static final Primitive SET_SLOT_DEFINITION_ALLOCATION_CLASS =
318    new Primitive("set-slot-definition-allocation-class", PACKAGE_SYS, true,
319                  "slot-definition allocation-class")
320    {
321      @Override
322      public LispObject execute(LispObject first, LispObject second)
323        throws ConditionThrowable
324      {
325          checkSlotDefination(first).slots[SlotDefinitionClass.SLOT_INDEX_ALLOCATION_CLASS] = second;
326          return second;
327      }
328    };
329
330  // ### %slot-definition-location
331  private static final Primitive _SLOT_DEFINITION_LOCATION =
332    new Primitive("%slot-definition-location", PACKAGE_SYS, true, "slot-definition")
333    {
334      @Override
335      public LispObject execute(LispObject arg) throws ConditionThrowable
336      {
337          return checkSlotDefination(arg).slots[SlotDefinitionClass.SLOT_INDEX_LOCATION];
338      }
339    };
340
341  // ### set-slot-definition-location
342  private static final Primitive SET_SLOT_DEFINITION_LOCATION =
343    new Primitive("set-slot-definition-location", PACKAGE_SYS, true, "slot-definition location")
344    {
345      @Override
346      public LispObject execute(LispObject first, LispObject second)
347        throws ConditionThrowable
348      {
349          checkSlotDefination(first).slots[SlotDefinitionClass.SLOT_INDEX_LOCATION] = second;
350          return second;
351      }
352    };
353}
Note: See TracBrowser for help on using the repository browser.