Think I'm being dim, actually.  Scratch this one - I've made a mistake
somewhere else :-)

Dave.

> From: Dave Addey <[EMAIL PROTECTED]>
> Reply-To: REALbasic NUG <[email protected]>
> Date: Mon, 24 Apr 2006 15:27:50 +0100
> To: REALbasic NUG <[email protected]>, James Milne
> <[EMAIL PROTECTED]>
> Conversation: How do I subclass a class with an event?
> Subject: Re: How do I subclass a class with an event?
> 
> Hi James,
> 
> Well, I've got the Factory approach pretty much working.  My only problem
> comes from the newly-created subclass instances.  So, I have (on a class
> HIDDeviceMouseFactory, which implements interface HIDDeviceFactory):
> 
> Function CreateInstance() as HIDDevice
>     return new HIDDeviceMouse()
> End Function
> 
> This is registered with a list of factories known to the plugin, as you
> described.  The CreateInstance function is then called from my plugin via
> something like...
> 
> REALobject(*CreateInstanceFuncPtr)(REALobject instance) = NULL;
> 
> CreateInstanceFuncPtr = (REALobject(*)(REALobject
> instance))REALInterfaceRoutine(me->RegisteredDeviceFactories[i],
> "HIDDeviceFactory", "CreateInstance");
> 
> REALobject UserCreatedDevice = NULL;
> 
> // Call the function
> if(CreateInstanceFuncPtr) {
>     UserCreatedDevice =
> CreateInstanceFuncPtr(me->RegisteredDeviceFactories[i]);
>     if (UserCreatedDevice) {
>         REALLockObject(UserCreatedDevice);
>         return UserCreatedDevice;
>     }
> }
> 
> This correctly calls the factory¹s CreateInstance function in RB, and I do
> get a non-NULL UserCreatedDevice back in the plugin (sometimes).
> 
> What (I think) I'm finding, however, is that the new HIDDeviceMouse is
> created in the RB code in the factory, but it is destroyed before I get
> chance to lock it when it is returned to the plugin.  The object needs to go
> ³back in² to the plugin before it is returned from GetNextDevice as a
> HIDDevice.  Is there some way I can ensure it is not destroyed, so that I
> can lock it?
> 
> Thank for the help!
> 
> Dave.
> 
>> From: James Milne <[EMAIL PROTECTED]>
>> Reply-To: REALbasic NUG <[email protected]>
>> Date: Mon, 24 Apr 2006 10:28:42 +0100
>> To: REALbasic NUG <[email protected]>
>> Cc: REALbasic NUG <[email protected]>
>> Subject: Re: How do I subclass a class with an event?
>> 
>> Dave Addey wrote:
>>> Hi NUG,
>>> 
>>> I have an RB plugin, which has a GetNextDevice function that returns a new
>>> instance of a class, called HIDDevice.  A HIDDevice has one event,
>>> DataAvailable.  The HIDDevice class is defined in the plugin too.
>>> 
>>> What I need to do in RB code is to subclass the HIDDevice class, so that I
>>> can enter some custom code in the subclass DataAvailable event.  This is
>>> easy enough.  What I also need to do is to assign the output of
>>> GetNextDevice to an instance of my subclass, and this is causing problems.
>>> 
>>> For example:
>>> 
>>> Dim myHIDDevice as HIDDevice_Mouse // a subclass of HIDDevice
>>> 
>>> myHIDDevice_Mouse = GetNextDevice
>>> 
>>> 
>>> This second line fails on compilation, with the following error:
>>> 
>>> Type mismatch error.  Expected HIDDevice_Mouse, but got HIDDevice.
>>> 
>>> Now, surely, HIDDevice_Mouse is a HIDDevice.  So, shouldn¹t this work?
>>> 
>>> 
>>> I also tried casting the output of GetNextDevice, like this:
>>> 
>>> myHIDDevice = HIDDevice_Mouse(GetNextDevice)
>>> 
>>> This compiles, but causes an IllegalCastException when this code executes in
>>> the application.  I¹m not sure why.
>>> 
>>> How should I do this?
>> 
>> You can't convert one instance of a HIDDevice into a HIDDevice_Mouse. They
>> are
>> different types, even though HIDDevice_Mouse inherits from HIDDevice.
>> 
>> You need to separate your design into two parts. You can, for instance, use a
>> Factory pattern approach. Your plugin defines a HIDDeviceFactory interface.
>> You can implement that interface in classes in your REALbasic code, so that
>> you have a HIDDeviceMouseFactory, HIDDeviceKeyboardFactory, etc. You create
>> instances of these factories and register the instances with your plugin. The
>> interface would have one method, "CreateInstance() as HIDDevice". In the case
>> of the HIDDeviceMouseFactory, you would implement it like so:
>> 
>> Function CreateInstance() as HIDDevice
>> return new HIDDeviceMouse()
>> End Function
>> 
>> Your plugin would expose a method for registering factories with it, ie
>> 
>> RegisterFactory( deviceType as String, factory as HIDDeviceFactory )
>> 
>> When the REALbasic calls the RegisterFactory function, the plugin would add
>> the factory object to an internal list of factories, along with the device
>> type. When the plugin came across a HID device that matched that type, it
>> would call the factory object to create an instance of the appropriate HID
>> Device subtype.
>> 
>> Alternatively, you could split the HIDDevice class into two parts. All
>> instances of HIDDevice would have a device handle property which represented
>> the actual HID device. You would create instances of HIDDeviceMouse, etc on
>> the app side, then attach them to the physical HID device return from the
>> plugin.
>> 
>> Created on the REALbasic side:               Created on the plugin side:
>>             HIDDevice --------------------------> HIDPhysicalDevice
>>               ^
>>              / \
>> HIDDeviceMouse HIDDeviceKeyboard
>> 
>> Imagine you had a HIDDeviceManager class in the plugin  which has a
>> DeviceAttached() event. You create a subclass of this in REALbasic. You would
>> implement this DeviceAttached() event like so:
>> 
>> Class MyHIDDeviceManager
>> Sub DeviceAttached( theDevice as HIDPhysicalDevice )
>> select case theDevice.type
>> case "Mouse"
>> Dim mouse as HIDDeviceMouse(theDevice)
>> ' Save mouse object somewhere
>> 
>> case "Keyboard"
>> Dim keyboard as HIDDeviceKeyboard(theDevice)
>> ' Save keyboard object somewhere
>> end select
>> End Sub
>> 
>> Hope this helps.
>> 
>> --
>> Kind regards,
>> James Milne
>> _______________________________________________
>> Unsubscribe or switch delivery mode:
>> <http://www.realsoftware.com/support/listmanager/>
>> 
>> Search the archives of this list here:
>> <http://support.realsoftware.com/listarchives/lists.html>
> 
> _______________________________________________
> Unsubscribe or switch delivery mode:
> <http://www.realsoftware.com/support/listmanager/>
> 
> Search the archives of this list here:
> <http://support.realsoftware.com/listarchives/lists.html>


_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Reply via email to