sempr-tk / sempr

SEMPR - Semantic Environment Mapping, Processing and Reasoning
BSD 3-Clause "New" or "Revised" License
7 stars 1 forks source link

feature: CallbackModule #65

Closed niniemann closed 5 years ago

niniemann commented 5 years ago

This PR adds a new ProcessingModule for lazy people like me: The CallbackModule. It allows you to easily add callbacks arbitrary event types that are handled in sempr. Please refrain from using it too excessively, it is meant for small, application specific triggers. In my case, I used it to collect information about changed SpatialObjects to update a visualization later on.

You can use it e.g. with lambdas, like this, but other function- and member-function-pointers should work, too:

auto myCallbackModule = sempr::processing::CreateCallbackModule(
    [](sempr::core::EntityEvent<sempr::entity::SpatialObject>::Ptr event)
    {
        std::cout << "myCallbackModule reacting to a SpatialObject-Event!" << std::endl;
    }
);

sempr::core::Core core;
core.addModule(myCallbackModule);

The sempr::core::CreateCallbackModule(Func f) is a helper function that infers the needed template arguments for you, so you don't have to write:

sempr::processing::CallbackModule<sempr::core::EntityEvent<sempr::entity::SpatialObject>>::Ptr
    myCallbackModule(
        new sempr::processing::CallbackModule<sempr::core::EntityEvent<sempr::entity::SpatialObject>>(
            [](sempr::core::EntityEvent<sempr::entity::SpatialObject>::Ptr event)
                {
                    std::cout << "myCallbackModule reacting to SpatialObjectEvent!" << std::endl;
                }
        )
    );

I also extended Utility.hpp provide the template magic needed for this simplification: