| 1 | /* |
| 2 | * $Id: HANDLE.java,v 1.3 2004/08/16 18:39:52 hastings Exp $ |
| 3 | * |
| 4 | * (c) Copyright, Moebius Solutions, Inc., 2004 |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This material may be reproduced by or for the U. S. Government |
| 9 | * pursuant to the copyright license under the clause at |
| 10 | * DFARS 252.227-7014 (OCT 2001). |
| 11 | */ |
| 12 | |
| 13 | package com.moesol.bindings.platform_sdk.windows_api; |
| 14 | |
| 15 | |
| 16 | /** |
| 17 | * An opaque HANDLE that represents a Win32 HANDLE. |
| 18 | * It is opaque from java side, only JNI can set the m_handle field, usually |
| 19 | * by calling the protected constructor. When assertions are turned on |
| 20 | * this class does leak detection. |
| 21 | */ |
| 22 | public class HANDLE { |
| 23 | /** |
| 24 | * Returns the native handle. |
| 25 | */ |
| 26 | public long _getHandle() { |
| 27 | return m_handle; |
| 28 | } |
| 29 | /** |
| 30 | * Returns the native handle of <code>handle</code>. |
| 31 | * Safe to call for null handles, returns 0 in this case. |
| 32 | * |
| 33 | * @param handle |
| 34 | * @return native handle or 0 if handle is null. |
| 35 | */ |
| 36 | public static long _safeGetHandle(HANDLE handle) { |
| 37 | if (handle == null) { |
| 38 | return 0; |
| 39 | } |
| 40 | return handle.m_handle; |
| 41 | } |
| 42 | public boolean equals(Object o) { |
| 43 | if (!(o instanceof HANDLE)) { |
| 44 | return false; |
| 45 | } |
| 46 | HANDLE other = (HANDLE)o; |
| 47 | return m_handle == other.m_handle; |
| 48 | } |
| 49 | public int hashCode() { |
| 50 | return (int)m_handle; |
| 51 | } |
| 52 | public void detach() { |
| 53 | assert(NativeResourceRef.removeCreateLocation(m_handle)); |
| 54 | m_handle = 0; |
| 55 | } |
| 56 | public static long getLeakCount() { |
| 57 | return NativeResourceRef.getLeakCount(); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Create a temporary handle that is not reported as |
| 62 | * leaked if finalize is called and m_handle is non-zero. |
| 63 | * |
| 64 | * @param handle |
| 65 | * @param temporary - if false same as calling HANDLE(long) |
| 66 | */ |
| 67 | protected HANDLE(long handle, boolean temporary) { |
| 68 | m_handle = handle; |
| 69 | assert(temporary || NativeResourceRef.recordCreateLocation(this, m_handle)); |
| 70 | } |
| 71 | /** |
| 72 | * Create a tracked handle that will report as leaked |
| 73 | * if it is finalized befre being detached. |
| 74 | * |
| 75 | * @param handle |
| 76 | */ |
| 77 | protected HANDLE(long handle) { |
| 78 | this(handle, false); |
| 79 | } |
| 80 | |
| 81 | private long m_handle; |
| 82 | } |