Skip to content

File EventBus.h

File List > AIAC > EventSys > EventBus.h

Go to the documentation of this file

// #####################################################################
// >>>>>>>>>>>>>>>>>>>>> BEGINNING OF LEGAL NOTICE >>>>>>>>>>>>>>>>>>>>>
//######################################################################
//
// This source file, along with its associated content, was authored by
// Andrea Settimi, Hong-Bin Yang, Naravich Chutisilp, and numerous other
// contributors. The code was originally developed at the Laboratory for
// Timber Construction (IBOIS, director: Prof. Yves Weinand) at the School of 
// Architecture, Civil and Environmental Engineering (ENAC) at the Swiss
// Federal Institute of Technology in Lausanne (EPFL) for the Doctoral
// Research "Augmented Carpentry" (PhD researcher: Andrea Settimi,
// co-director: Dr. Julien Gamerro, director: Prof. Yves Weinand).
//
// Although the entire repository is distributed under the GPL license,
// these particular source files may also be used under the terms of the
// MIT license. By accessing or using this file, you agree to the following:
//
// 1. You may reproduce, modify, and distribute this file in accordance
//    with the terms of the MIT license.
// 2. You must retain this legal notice in all copies or substantial
//    portions of this file.
// 3. This file is provided "AS IS," without any express or implied
//    warranties, including but not limited to the implied warranties of
//    merchantability and fitness for a particular purpose.
//
// If you cannot or will not comply with the above conditions, you are
// not permitted to use this file. By proceeding, you acknowledge and
// accept all terms and conditions herein.
//
//######################################################################
// <<<<<<<<<<<<<<<<<<<<<<< END OF LEGAL NOTICE <<<<<<<<<<<<<<<<<<<<<<<<
// #####################################################################
//
#pragma once

#include "AIAC/EventSys/Event.h"
#include "AIAC/EventSys/SLAMEvent.h"
#include "AIAC/EventSys/CameraEvent.h"
#include "AIAC/EventSys/ApplicationEvent.h"


namespace AIAC
{
    using EQ = eventpp::EventQueue<EventType, void(const EventPointer&), EventPolicy>;
    class EventBus
    {
    public:
        ~EventBus() = default;

        void Init()
        {
            m_EventQueue.appendListener(EventType::SLAMMapLoaded, [](const EventPointer& event) {
                auto& slamEvent = static_cast<SLAMMapLoadedEvent&>(*event);
                slamEvent.OnSLAMMapLoaded();
            });
            m_EventQueue.appendListener(EventType::SLAMVocabularyLoaded, [](const EventPointer& event) {
                auto& slamEvent = static_cast<SLAMVocabularyLoadedEvent&>(*event);
                slamEvent.OnSLAMVocabularyLoaded();
            });
            m_EventQueue.appendListener(EventType::SLAMStartMapping, [](const EventPointer& event) {
                auto& slamEvent = static_cast<SLAMStartMappingEvent&>(*event);
                slamEvent.OnSLAMStartMapping();
            });
            m_EventQueue.appendListener(EventType::SLAMStopMapping, [](const EventPointer& event) {
                auto& slamEvent = static_cast<SLAMStopMappingEvent&>(*event);
                slamEvent.OnSLAMStopMapping();
            });
            m_EventQueue.appendListener(EventType::SLAMCombineMapEvent, [](const EventPointer& event) {
                auto& slamEvent = static_cast<SLAMCombineMapEvent&>(*event);
                slamEvent.OnSLAMCombineMap();
            });
            m_EventQueue.appendListener(EventType::CameraCalibrationLoaded, [](const EventPointer& event) {
                auto& cameraEvent = static_cast<CameraCalibrationLoadedEvent&>(*event);
                cameraEvent.OnCameraCalibrationFileLoaded();
            });
            m_EventQueue.appendListener(EventType::AppClose, [](const EventPointer& event) {
                auto& appEvent = static_cast<AppCloseEvent&>(*event);
                appEvent.OnAppClose();
            });
        }

        void EnqueueEvent(const EventPointer& sharedPtrEvent)
        {
            m_EventQueue.enqueue(sharedPtrEvent->GetType(), sharedPtrEvent);
        }

        // Asynchronus
        void ProcessQueue() { m_EventQueue.process(); }

        // Synchronus
        void DispatchEvent(const EventPointer& sharedPtrEvent)
        {
            m_EventQueue.dispatch(sharedPtrEvent->GetType(), sharedPtrEvent);
        }

        inline bool IsEventQueueEmpty() const { return m_EventQueue.emptyQueue(); }

    private:
        EQ m_EventQueue;
    };
}