View Javadoc
1   /*
2    * Copyright 2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.jna;
6   
7   import com.sun.jna.NativeLong;
8   import com.sun.jna.platform.unix.LibCAPI.size_t;
9   import com.sun.jna.platform.win32.BaseTSD.ULONG_PTRByReference;
10  import com.sun.jna.platform.win32.Tlhelp32.PROCESSENTRY32;
11  import com.sun.jna.platform.win32.WinDef.LONGLONGByReference;
12  import com.sun.jna.platform.win32.WinNT.HANDLEByReference;
13  import com.sun.jna.ptr.IntByReference;
14  import com.sun.jna.ptr.LongByReference;
15  import com.sun.jna.ptr.NativeLongByReference;
16  import com.sun.jna.ptr.PointerByReference;
17  
18  import oshi.util.Util;
19  
20  /**
21   * Wrapper classes for JNA clases which extend {@link com.sun.jna.ptr.ByReference} intended for use in
22   * try-with-resources blocks.
23   */
24  public interface ByRef {
25  
26      class CloseableIntByReference extends IntByReference implements AutoCloseable {
27          public CloseableIntByReference() {
28              super();
29          }
30  
31          public CloseableIntByReference(int value) {
32              super(value);
33          }
34  
35          @Override
36          public void close() {
37              Util.freeMemory(getPointer());
38          }
39      }
40  
41      class CloseableLongByReference extends LongByReference implements AutoCloseable {
42          public CloseableLongByReference() {
43              super();
44          }
45  
46          public CloseableLongByReference(long value) {
47              super(value);
48          }
49  
50          @Override
51          public void close() {
52              Util.freeMemory(getPointer());
53          }
54      }
55  
56      class CloseableNativeLongByReference extends NativeLongByReference implements AutoCloseable {
57          public CloseableNativeLongByReference() {
58              super();
59          }
60  
61          public CloseableNativeLongByReference(NativeLong nativeLong) {
62              super(nativeLong);
63          }
64  
65          @Override
66          public void close() {
67              Util.freeMemory(getPointer());
68          }
69      }
70  
71      class CloseablePointerByReference extends PointerByReference implements AutoCloseable {
72          @Override
73          public void close() {
74              Util.freeMemory(getPointer());
75          }
76      }
77  
78      class CloseableLONGLONGByReference extends LONGLONGByReference implements AutoCloseable {
79          @Override
80          public void close() {
81              Util.freeMemory(getPointer());
82          }
83      }
84  
85      class CloseableULONGptrByReference extends ULONG_PTRByReference implements AutoCloseable {
86          @Override
87          public void close() {
88              Util.freeMemory(getPointer());
89          }
90      }
91  
92      class CloseableHANDLEByReference extends HANDLEByReference implements AutoCloseable {
93          @Override
94          public void close() {
95              Util.freeMemory(getPointer());
96          }
97      }
98  
99      class CloseableSizeTByReference extends size_t.ByReference implements AutoCloseable {
100         public CloseableSizeTByReference() {
101             super();
102         }
103 
104         public CloseableSizeTByReference(long value) {
105             super(value);
106         }
107 
108         @Override
109         public void close() {
110             Util.freeMemory(getPointer());
111         }
112     }
113 
114     class CloseablePROCESSENTRY32ByReference extends PROCESSENTRY32.ByReference implements AutoCloseable {
115         @Override
116         public void close() {
117             Util.freeMemory(getPointer());
118         }
119     }
120 }