Discussion:
Advice about wrapper class
(too old to reply)
GiuseppeDini
2010-12-09 07:57:46 UTC
Permalink
I’ve written my first wrapper class for calling a c++ function.
The code is as follows and it doesn’t need to be commented because I
think its clear.
Do you think it is ok or that it needs come improvement?



’ like c++ counterpart
Public Structure managedStructure
Public Integer1 As Integer
Public Pointer1 As IntPtr
' ...
End Structure


Class myWrapper
Implements IDisposable

Public Ptr As IntPtr

Public Sub New(ByVal dynamParams As Integer, ByVal measureParams As
Integer, ByVal controlParams As Integer)
Dispose()
‘ allocate and initialize unmanaged Resource
Ptr = Emgu.CV.CvInvoke.allocateResource(dynamParams,
measureParams, controlParams)

GC.AddMemoryPressure(Runtime.InteropServices.Marshal.SizeOf(GetType(Emgu.CV.Structure.managedStructure)))
End Sub

Public Sub Dispose() Implements System.IDisposable.Dispose
If Ptr <> IntPtr.Zero Then
Emgu.CV.CvInvoke.ReleaseResource(Ptr)

GC.RemoveMemoryPressure(Runtime.InteropServices.Marshal.SizeOf(GetType(Emgu.CV.Structure.
managedStructure)))
GC.SuppressFinalize(Me)
Ptr = IntPtr.Zero
End If
End Sub

Protected Overrides Sub Finalize()
Try
Dispose()
Finally
MyBase.Finalize()
End Try
End Sub


Public ReadOnly Property getmanagedStructure() As
Emgu.CV.Structure.managedStructure
Get
Return
CType(Runtime.InteropServices.Marshal.PtrToStructure(Ptr,
GetType(Emgu.CV.Structure. managedStructure)),
Emgu.CV.Structure.managedStructure)
End Get
End Property

End Class
--
Giuseppe
Jason Keats
2010-12-09 12:11:08 UTC
Permalink
Post by GiuseppeDini
I’ve written my first wrapper class for calling a c++ function.
The code is as follows and it doesn’t need to be commented because I
think its clear.
Do you think it is ok or that it needs come improvement?
Does it compile without error?
Does it provide the expected answer?
Does it perform adequately?
Will you understand the code in six months?

If you can answer yes to all of the above, then it does not need
improvement.
GiuseppeDini
2010-12-10 09:32:16 UTC
Permalink
Post by Jason Keats
Post by GiuseppeDini
I’ve written my first wrapper class for calling a c++ function.
The code is as follows and it doesn’t need to be commented because I
think its clear.
Do you think it is ok or that it needs come improvement?
Does it compile without error?
Does it provide the expected answer?
Does it perform adequately?
Will you understand the code in six months?
If you can answer yes to all of the above, then it does not need improvement.
As you know, a bug can be discovered even years after the code is
released.
--
Giuseppe
Loading...