/* CWrapper -- wrapping C structures
* Copyright (C) 2014 Galois, Inc.
*
* This library is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this library. If not, see
This class is intended to work hand-in-hand with Java Native Interface * code that follows a four-stage lifecycle: * *
This class is not intended to be instantiated directly. Only * instantiate its concrete subclasses. */ abstract class CWrapper { /** * C pointer to the underlying data structure. */ protected long cPointer; /////////////////////////// Low-level life cycle /////////////////////////// /** * Reserves memory and resources needed by the structure. * *
Precondition: cPointer
is 0
.
*
*
Postcondition: If allocation was successful,
* cPointer
is not 0
.
*/
abstract protected void allocateCPointer();
/**
* Installs data into the structure to make it ready for use.
*
*
Precondition: Allocation succeeded. */ abstract protected void initializeCPointer(); /** * Cleans up the structure under the assumption that it will not be used * anymore. * *
Precondition: Initialization succeeded. */ abstract protected void finalizeCPointer(); /** * Releases memory and resources needed by the structure. * *
Precondition: cPointer
is not 0
.
* Precondition: Finalization succeeded.
*
*
Postcondition: cPointer
is 0
.
*/
abstract protected void freeCPointer();
////////////////////////// High-level life cycle //////////////////////////
/**
* Allocates and initializes the underlying structure.
*
* @throws OpenedException the underlying structure has already been opened.
*
* @throws NativeOutOfMemoryError allocation failed.
*/
protected synchronized void open() {
if (cPointer != 0) {
throw new OpenedException();
}
allocateCPointer();
if (cPointer == 0) {
throw new NativeOutOfMemoryError();
}
initializeCPointer();
}
/**
* Finalizes and frees the underlying structure.
*
* @throws ClosedException the underlying structure has already been closed.
*/
public synchronized void close() {
if (cPointer == 0) {
throw new ClosedException();
}
finalizeCPointer();
freeCPointer();
cPointer = 0;
}
////////////////////////// Associated exceptions //////////////////////////
/**
* Signals that the underlying structure is already open.
*/
public class OpenedException extends java.lang.IllegalStateException {
static final long serialVersionUID = -7226578680647930527L;
/**
* Constructs a CWrapper.OpenedException
with no detail
* message.
*/
protected OpenedException() {
super();
}
}
/**
* Signals that the underlying structure is already closed.
*/
public class ClosedException extends java.lang.IllegalStateException {
static final long serialVersionUID = -2471059099211958977L;
/**
* Constructs a CWrapper.ClosedException
with no detail
* message.
*/
protected ClosedException() {
super();
}
}
////////////////////////////// Miscellaneous //////////////////////////////
@Override
protected void finalize() {
close();
}
@Override
protected Object clone() throws java.lang.CloneNotSupportedException {
throw new java.lang.CloneNotSupportedException();
}
}