Skip to content

GenApi/NodeCallback.h#

Implementation helpers for CNodeCallback Defines a bunch of templates for creating callback-functors taking a INode pointer and returning void. More…

Namespaces#

Name
GenApi
Contains definitions of the types of GenICam GenApi modules.

Classes#

Name
class GenApi::CNodeCallback
callback body instance for INode pointers
class GenApi::Function_NodeCallback
Container for a function pointer.
class GenApi::Member_NodeCallback
Container for a member function pointer.

Detailed Description#

Implementation helpers for CNodeCallback Defines a bunch of templates for creating callback-functors taking a INode pointer and returning void.

Note: does not work with all stl containers, i.e. std::map

Source code#

//-----------------------------------------------------------------------------
//  (c) 2006 by Basler Vision Technologies
//  Section: Vision Components
//  Project: GenApi
//  Author:  Hartmut Nebelung
//  $Header$
//
//  License: This file is published under the license of the EMVA GenICam  Standard Group.
//  A text file describing the legal terms is included in  your installation as 'GenICam_license.pdf'.
//  If for some reason you are missing  this file please contact the EMVA or visit the website
//  (http://www.genicam.org) for a full copy.
//
//  THIS SOFTWARE IS PROVIDED BY THE EMVA GENICAM STANDARD GROUP "AS IS"
//  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
//  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
//  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE EMVA GENICAM STANDARD  GROUP
//  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,  SPECIAL,
//  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT  LIMITED TO,
//  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,  DATA, OR PROFITS;
//  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY  THEORY OF LIABILITY,
//  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT  (INCLUDING NEGLIGENCE OR OTHERWISE)
//  ARISING IN ANY WAY OUT OF THE USE  OF THIS SOFTWARE, EVEN IF ADVISED OF THE
//  POSSIBILITY OF SUCH DAMAGE.
//-----------------------------------------------------------------------------
#ifndef GENAPI_NODECALLBACK_H
#define GENAPI_NODECALLBACK_H
#include <GenApi/INode.h>

namespace GENAPI_NAMESPACE
{

    typedef enum _ECallbackType
    {
        cbPostInsideLock = 1,   
        cbPostOutsideLock = 2   
    } ECallbackType;

    class CNodeCallback
    {
    public:
        CNodeCallback( INode *pNode, ECallbackType CallbackType ) :
            m_pNode(pNode),
            m_CallbackType( CallbackType )
        {}

        virtual ~CNodeCallback()
        {};

        virtual void operator()( ECallbackType CallbackType ) const = 0;

        virtual void Destroy() = 0;

        INode* GetNode()      
        {      
            return m_pNode;      
        }  

    protected:
        INode *m_pNode;

        ECallbackType m_CallbackType;
    };

    /***************************************************************************/
    // C Functions as callbacks
    /***************************************************************************/

    template <class Function>
    class Function_NodeCallback : public CNodeCallback
    {
    public:
        Function_NodeCallback( INode *pNode, const Function& function, ECallbackType CallbackType ) :
            CNodeCallback( pNode, CallbackType ),
            m_pFunction( function )
        {}

        virtual void operator()( ECallbackType CallbackType ) const
        {
            if (m_pFunction && m_CallbackType == CallbackType)
                m_pFunction( m_pNode );
        }

        virtual void Destroy()
        {
            delete this;
        }

    private:
        const Function m_pFunction;

        Function_NodeCallback& operator=(Function_NodeCallback&);
    };

    /*-----------------------------------------------------------------------------*/

    template <class Function>
    CNodeCallback *make_NodeCallback( INode *pNode, Function function, ECallbackType CallbackType )
    {
        return static_cast<CNodeCallback*>( new Function_NodeCallback<Function>(pNode, function, CallbackType) );
    }

    /*-----------------------------------------------------------------------------*/


    template<class Function>
    intptr_t Register( INode* pNode, Function f, ECallbackType CallbackType = cbPostInsideLock )
    {
        CNodeCallback *pCallback(make_NodeCallback(pNode, f, CallbackType));
        return pNode->RegisterCallback(pCallback);
    }


    /***************************************************************************/
    // C++ Member functions as callbacks
    /***************************************************************************/

    template <class Client, class Member>
    class Member_NodeCallback : public CNodeCallback
    {
    public:
        typedef void (Client::*PMEMBERFUNC)(INode*);

        Member_NodeCallback( INode *pNode, Client& client, Member member, ECallbackType CallbackType ) :
            CNodeCallback( pNode, CallbackType ),
            m_Client(client),
            m_pMemberFunc(member)
        {}

        virtual void operator()( ECallbackType CallbackType ) const
        {
            if (m_pMemberFunc && m_CallbackType == CallbackType)
                (m_Client.*m_pMemberFunc)( m_pNode );
        }

        virtual void Destroy()
        {
            delete this;
        }

    private:
        Client& m_Client;

        PMEMBERFUNC m_pMemberFunc;

        Member_NodeCallback& operator=(Member_NodeCallback&);
    };

    /*-----------------------------------------------------------------------------*/

    template <class Client, class Member>
    CNodeCallback *make_NodeCallback( INode *pNode, Client& client, Member member, ECallbackType CallbackType )
    {
        return static_cast<CNodeCallback*>( new Member_NodeCallback<Client,Member>(pNode, client, member, CallbackType) );
    }

    /*-----------------------------------------------------------------------------*/

    template<class Client, class Member>
    intptr_t Register( INode* pNode, Client &c, Member m, ECallbackType CallbackType = cbPostInsideLock  )
    {
        CNodeCallback *pCallback(make_NodeCallback(pNode, c, m, CallbackType));
        return pNode->RegisterCallback(pCallback);
    }


    // definition in Node.cpp
    GENAPI_DECL void Deregister (GENAPI_NAMESPACE::CallbackHandleType pCallbackInfo );

}

#endif // GENAPI_NODECALLBACK_H

Updated on 5 July 2022 at 15:30:01