Source code: normal/engine/implementation/jni/JNIShareableObject.java
1
2 /**************************************************************************
3 * *
4 * Regina - A normal surface theory calculator *
5 * Java user interface *
6 * *
7 * Copyright (c) 1999-2000, Ben Burton *
8 * For further details contact Ben Burton (benb@acm.org). *
9 * *
10 * This program is free software; you can redistribute it and/or *
11 * modify it under the terms of the GNU General Public License as *
12 * published by the Free Software Foundation; either version 2 of the *
13 * License, or (at your option) any later version. *
14 * *
15 * This program is distributed in the hope that it will be useful, but *
16 * WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
18 * General Public License for more details. *
19 * *
20 * You should have received a copy of the GNU General Public *
21 * License along with this program; if not, write to the Free *
22 * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
23 * MA 02111-1307, USA. *
24 * *
25 **************************************************************************/
26
27 /* end stub */
28
29 package normal.engine.implementation.jni;
30
31 import java.lang.reflect.*;
32 import normal.engine.*;
33
34 /**
35 * Java object associated with an object in an external C++ library
36 * through JNI.
37 * <p>
38 * The protected member <tt>cppPtr</tt> is a memory pointer to the external
39 * C++ object. Through this member variable, a C++ implementation of a
40 * native method that is passed only the Java object can thus access the
41 * corresponding native C++ object.
42 * <p>
43 * When a new derivative of <tt>JNIShareableObject</tt> is created, many
44 * constructors
45 * may be called. The corresponding native C++ object should <i>not</i> be
46 * created in more than one of these constructors. Thus every derivative of
47 * <tt>JNIShareableObject</tt> should provide a constructor taking a single
48 * <tt>Sentry</tt> argument that does absolutely nothing. All initialisation
49 * and creation of native C++ objects should take place in a constructor for
50 * the most derived class, and every constructor should call the do-nothing
51 * constructor for its parent class.
52 *
53 * @see #cppPtr
54 * @see normal.engine.implementation.jni.Sentry
55 */
56 public abstract class JNIShareableObject implements ShareableObject {
57 /**
58 * Memory pointer to the associated native C++ object.
59 * A C++ implementation of a native method should cast
60 * <tt>cppPtr</tt> to a pointer to gain access to the
61 * native C++ object.
62 */
63 protected long cppPtr;
64
65 /**
66 * The do-nothing constructor.
67 *
68 * @param s any <tt>Sentry</tt> object; <tt>Sentry.instance</tt> will do.
69 * @see normal.engine.implementation.jni.JNIShareableObject
70 * @see normal.engine.implementation.jni.Sentry
71 */
72 protected JNIShareableObject(Sentry s) {
73 }
74
75 public boolean sameObject(ShareableObject object) {
76 return (object != null &&
77 cppPtr == ((JNIShareableObject)object).cppPtr);
78 }
79 public boolean equals(Object obj) {
80 if (obj instanceof JNIShareableObject)
81 return sameObject((JNIShareableObject)obj);
82 return false;
83 }
84 public ShareableObject castAs(Class cls) {
85 Class[] parameters = { Sentry.class };
86 Object[] args = { Sentry.instance };
87 try {
88 Constructor c = cls.getConstructor(parameters);
89 return (ShareableObject)c.newInstance(args);
90 } catch (Throwable th) {
91 th.printStackTrace();
92 return null;
93 }
94 }
95
96 public native void destroy();
97
98 public native void writeTextShort();
99 public native void writeTextLong();
100 public native String toString();
101 public native String toStringLong();
102 }