Skip to content

File ManagedInterfaceEventHandler.h

File List > SpinnakerNET > ManagedInterfaceEventHandler.h

Go to the documentation of this file

//=============================================================================
// Copyright (c) 2026 FLIR Integrated Imaging Solutions, Inc. All Rights Reserved.
//
// This software is the confidential and proprietary information of FLIR
// Integrated Imaging Solutions, Inc. ("Confidential Information"). You
// shall not disclose such Confidential Information and shall use it only in
// accordance with the terms of the license agreement you entered into
// with FLIR Integrated Imaging Solutions, Inc. (FLIR)
//
// FLIR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
// SOFTWARE, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE, OR NON-INFRINGEMENT. FLIR SHALL NOT BE LIABLE FOR ANY DAMAGES
// SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
// THIS SOFTWARE OR ITS DERIVATIVES.
//=============================================================================

#ifndef FLIR_SPINNAKER_MANAGED_INTERFACE_EVENT_HANDLER_H
#define FLIR_SPINNAKER_MANAGED_INTERFACE_EVENT_HANDLER_H

#include "ManagedEventHandler.h"
#include "InterfaceEventHandler.h"
#include "ManagedCamera.h"
#include <vcclr.h>

using namespace ::System;

namespace SpinnakerNET
{
    // forward declaration
    ref class ManagedInterfaceEventHandler;

    // Intermediate delegate and function pointer for DeviceArrivalEvent
    delegate void OnDeviceArrivalDelegate(Spinnaker::CameraPtr);
    typedef void(__stdcall *OnDeviceArrivalNative)(Spinnaker::CameraPtr);

    // Intermediate delegate and function pointer for DeviceRemovalEvent
    delegate void OnDeviceRemovalDelegate(Spinnaker::CameraPtr);
    typedef void(__stdcall *OnDeviceRemovalNative)(Spinnaker::CameraPtr);

    // We declare an intermediate class which inherits from InterfaceEventHandler (unmanaged abstract class).
    // We provide definitions for the virtual functions. These functions are the callbacks that
    // will be called from the unmanaged library. These functions call into the managed abstract
    // class.
    class InterfaceEventRedirector : public Spinnaker::InterfaceEventHandler
    {
        public:
            InterfaceEventRedirector(OnDeviceArrivalDelegate^ delegate1, OnDeviceRemovalDelegate^ delegate2)
            {
                // Assign delegates
                m_OnDeviceArrivalDelegate = delegate1;
                m_OnDeviceRemovalDelegate = delegate2;

                // Fetch function pointers.
                m_OnDeviceArrivalNative = (OnDeviceArrivalNative)Marshal::GetFunctionPointerForDelegate(m_OnDeviceArrivalDelegate).ToPointer();
                m_OnDeviceRemovalNative = (OnDeviceRemovalNative)Marshal::GetFunctionPointerForDelegate(m_OnDeviceRemovalDelegate).ToPointer();
            }
        protected:
            virtual void OnDeviceArrival(Spinnaker::CameraPtr pCamera);
            virtual void OnDeviceRemoval(Spinnaker::CameraPtr pCamera);

        private:
            gcroot<OnDeviceArrivalDelegate^> m_OnDeviceArrivalDelegate;
            OnDeviceArrivalNative m_OnDeviceArrivalNative;
            gcroot<OnDeviceRemovalDelegate^> m_OnDeviceRemovalDelegate;
            OnDeviceRemovalNative m_OnDeviceRemovalNative;
    };

    // Managed abstract interface event base class
    // The event callbacks, ie.  On<Event>() functions will be called via the internal
    // CallOn<Event>() calls.
    public ref class ManagedInterfaceEventHandler abstract : public ManagedEventHandler
    {
        public:
            ManagedInterfaceEventHandler();
            ~ManagedInterfaceEventHandler();
            !ManagedInterfaceEventHandler();

        protected:
            // Empty body so user can override only the one's they need
            virtual void OnDeviceArrival(IManagedCamera^ /*camera*/){};
            virtual void OnDeviceRemoval(IManagedCamera^ /*camera*/){};

internal:
            void CallOnDeviceArrival(Spinnaker::CameraPtr camera);
            void CallOnDeviceRemoval(Spinnaker::CameraPtr camera);
    };
}

#endif // FLIR_SPINNAKER_MANAGED_INTERFACE_EVENT_HANDLER_H