Skip to content

pylon/InstantInterface.h#

Namespaces#

Name
Pylon
Contains definitions of pylon types.

Classes#

Name
class Pylon::CInstantInterface
Provides convenient access to an interface.

Source code#

//------------------------------------------------------------------------------
//  Basler pylon SDK
//  Copyright (c) 2019-2022 Basler AG
//  http://www.baslerweb.com
//  Author:  DV
//------------------------------------------------------------------------------

#pragma once

#include <pylon/Platform.h>

#ifdef _MSC_VER
#   pragma pack(push, PYLON_PACKING)
#endif /* _MSC_VER */

#include <pylon/stdinclude.h>
#include <pylon/PylonBase.h>
#include <pylon/InterfaceInfo.h>
#include <pylon/DeviceInfo.h>
#include <pylon/TransportLayer.h>
#include <pylon/Interface.h>
#include <pylon/_BaslerUniversalInterfaceParams.h>


namespace Pylon
{
    template <typename T>
    class CInstantInterface : public T, public IInterface
    {
    public:
        CInstantInterface( const Pylon::CInfoBase& info ) : T()
            , m_pTransportLayer( NULL )
            , m_pInterface( NULL )
        {
            if (info.IsDeviceClassAvailable())
            {
                // find transport layer by DeviceClass
                CTlFactory& theFactory = CTlFactory::GetInstance();
                m_pTransportLayer = theFactory.CreateTl( info.GetDeviceClass() );
            }
            else if (info.IsTLTypeAvailable())
            {
                // find transport layer by TlType (choose first)
                TlInfoList_t tlList;
                CTlFactory& theFactory = Pylon::CTlFactory::GetInstance();
                theFactory.EnumerateTls( tlList );
                for (TlInfoList_t::const_iterator it = tlList.begin(); it != tlList.end(); ++it)
                {
                    const CTlInfo& tinfo = *it;
                    if (tinfo.GetTLType() == info.GetTLType())
                    {
                        m_pTransportLayer = theFactory.CreateTl( tinfo.GetDeviceClass() );
                        break;
                    }
                }
            }
            else
            {
                throw RUNTIME_EXCEPTION( "Not enough information to create the desired transport layer." );
            }

            if (m_pTransportLayer == NULL)
            {
                throw RUNTIME_EXCEPTION( "Cannot create transport layer." );
            }

            const CInterfaceInfo* pInterfaceInfo = dynamic_cast<const CInterfaceInfo*>(&info);
            const CDeviceInfo* pDeviceInfo = dynamic_cast<const CDeviceInfo*>(&info);
            if (pInterfaceInfo != NULL)
            {
                // parameter might be CInterfaceInfo
                m_pInterface = m_pTransportLayer->CreateInterface( *pInterfaceInfo );
            }
            else if ((pDeviceInfo != NULL) && (pDeviceInfo->IsInterfaceIDAvailable()))
            {
                // parameter might be CDeviceInfo
                CInterfaceInfo ii;
                ii.SetDeviceClass( info.GetDeviceClass() );
                ii.SetInterfaceID( pDeviceInfo->GetInterfaceID() );
                m_pInterface = m_pTransportLayer->CreateInterface( ii );
            }
            else
            {
                // open first interface
                InterfaceInfoList infoList;
                m_pTransportLayer->EnumerateInterfaces( infoList );
                m_pInterface = m_pTransportLayer->CreateInterface( infoList.at( 0 ) );
            }

            if (m_pInterface == NULL)
            {
                throw RUNTIME_EXCEPTION( "Cannot create interface." );
            }
        }

        ~CInstantInterface()
        {
            if (m_pInterface != NULL)
            {
                if (IsOpen())
                {
                    Close();
                }

                m_pTransportLayer->DestroyInterface( m_pInterface );
            }

            if (m_pTransportLayer != NULL)
            {
                CTlFactory& theFactory = CTlFactory::GetInstance();
                theFactory.ReleaseTl( m_pTransportLayer );
            }
        }

        // IInterface

        virtual void Open()
        {
            m_pInterface->Open();
            this->_Initialize( m_pInterface->GetNodeMap() );
        }

        virtual bool IsOpen() const
        {
            bool isOpen = false;
            if (m_pInterface != NULL)
            {
                isOpen = m_pInterface->IsOpen();
            }
            return isOpen;
        }

        virtual void Close()
        {
            if (m_pInterface != NULL)
            {
                m_pInterface->Close();
            }
            this->_Initialize( NULL );
        }

        virtual const CInterfaceInfo& GetInterfaceInfo() const
        {
            return m_pInterface->GetInterfaceInfo();
        }

        virtual GenApi::INodeMap* GetNodeMap()
        {
            return m_pInterface->GetNodeMap();
        }

        // IDeviceFactory

        virtual int EnumerateDevices( DeviceInfoList_t& list, bool addToList = false )
        {
            return m_pInterface->EnumerateDevices( list, addToList );
        }

        virtual int EnumerateDevices( DeviceInfoList_t& list, const DeviceInfoList_t& filter, bool addToList = false )
        {
            return m_pInterface->EnumerateDevices( list, filter, addToList );
        }

        virtual IPylonDevice* CreateDevice( const CDeviceInfo& di )
        {
            return m_pInterface->CreateDevice( di );
        }

        virtual IPylonDevice* CreateFirstDevice( const CDeviceInfo& di = CDeviceInfo() )
        {
            return m_pInterface->CreateFirstDevice( di );
        }

        virtual IPylonDevice* CreateDevice( const CDeviceInfo& di, const StringList_t& InjectedXmlStrings )
        {
            return m_pInterface->CreateDevice( di, InjectedXmlStrings );
        }

        virtual IPylonDevice* CreateFirstDevice( const CDeviceInfo& di, const StringList_t& InjectedXmlStrings )
        {
            return m_pInterface->CreateFirstDevice( di, InjectedXmlStrings );
        }

        virtual IPylonDevice* CreateDevice( const String_t& s )
        {
            return m_pInterface->CreateDevice( s );
        }

        virtual void DestroyDevice( IPylonDevice* pDevice )
        {
            return m_pInterface->DestroyDevice( pDevice );
        }

        virtual bool IsDeviceAccessible( const CDeviceInfo& deviceInfo, AccessModeSet mode = Control, EDeviceAccessiblityInfo* pAccessibilityInfo = NULL )
        {
            return m_pInterface->IsDeviceAccessible( deviceInfo, mode, pAccessibilityInfo );
        }

    private:
        // Do not use
        CInstantInterface();

        ITransportLayer* m_pTransportLayer;
        IInterface* m_pInterface;
    };

    typedef CInstantInterface<Basler_UniversalInterfaceParams::CUniversalInterfaceParams_Params> CUniversalInstantInterface;

} // namespace Pylon

#ifdef _MSC_VER
#   pragma pack(pop)
#endif /* _MSC_VER */

Updated on 5 July 2022 at 15:30:01