Skip to content

Commit

Permalink
Merge pull request #10 from GrahamBriggs/IDisposable
Browse files Browse the repository at this point in the history
Implemented IDisposable and Dispose pattern
  • Loading branch information
dmedine authored Mar 30, 2021
2 parents 21065ca + 94d5712 commit 3fe3201
Showing 1 changed file with 83 additions and 11 deletions.
94 changes: 83 additions & 11 deletions LSL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,26 @@ public class StreamInfo
public StreamInfo(IntPtr handle) { obj = handle; }

/// Destroy a previously created streaminfo object.
~StreamInfo() { dll.lsl_destroy_streaminfo(obj); }
~StreamInfo() { Dispose(false); }

/// Dispose of the object
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private bool _disposed = false;
protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}

dll.lsl_destroy_streaminfo(obj);
obj = IntPtr.Zero;
_disposed = true;
}

// ========================
// === Core Information ===
Expand Down Expand Up @@ -306,11 +325,29 @@ public class StreamOutlet
*/
public StreamOutlet(StreamInfo info, int chunk_size = 0, int max_buffered = 360) { obj = dll.lsl_create_outlet(info.handle(), chunk_size, max_buffered); }

~StreamOutlet() { Dispose(false); }

/**
* Destructor.
* The stream will no longer be discoverable after destruction and all paired inlets will stop delivering data.
* Dispose.
* The stream will no longer be discoverable after disposal (or when disposed by the GC) and all paired inlets will stop delivering data.
*/
~StreamOutlet() { dll.lsl_destroy_outlet(obj); }
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private bool _disposed = false;
protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}

dll.lsl_destroy_outlet(obj);
obj = IntPtr.Zero;
_disposed = true;
}


// ========================================
Expand Down Expand Up @@ -497,11 +534,29 @@ public StreamInlet(StreamInfo info, int max_buflen = 360, int max_chunklen = 0,
dll.lsl_set_postprocessing(obj, postproc_flags);
}

~StreamInlet() { Dispose(false); }

/**
* Destructor.
* The inlet will automatically disconnect if destroyed.
* Dispose
* The inlet will be disconnect after disposal (or when disposed by the GC)
*/
~StreamInlet() { dll.lsl_destroy_inlet(obj); }
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private bool _disposed = false;
protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}

dll.lsl_destroy_inlet(obj);
obj = IntPtr.Zero;
_disposed = true;
}

/**
* Retrieve the complete information of the given stream, including the extended description.
Expand Down Expand Up @@ -799,10 +854,27 @@ public class ContinuousResolver
public ContinuousResolver(string pred) { obj = dll.lsl_create_continuous_resolver_bypred(pred, 5.0); }
public ContinuousResolver(string pred, double forget_after) { obj = dll.lsl_create_continuous_resolver_bypred(pred, forget_after); }

/**
* Destructor.
*/
~ContinuousResolver() { dll.lsl_destroy_continuous_resolver(obj); }
~ContinuousResolver() { Dispose(false); }

/// Dispose of the object
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private bool _disposed = false;
protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}

dll.lsl_destroy_continuous_resolver(obj);
obj = IntPtr.Zero;
_disposed = true;
}


/**
* Obtain the set of currently present streams on the network (i.e. resolve result).
Expand Down

0 comments on commit 3fe3201

Please sign in to comment.