Skip to content

chei90/C--Style-Events

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C--Style-Events

C#-Style Events for C++

Lambdas

//No parameters
{
    //Create Event
    Event e;
    //Register
    auto handle = e += [] () { std::cout << "hi" << std::endl; };
    //Invoke
    e.Invoke();

    //Remove
    e -= handle;
}

//Variable parameters
{
    Event<int, float> e;
    auto handle = e += [] (int i, float f) { 
        std::cout << "I is: " << i << " f is: " << f << std::endl;
    };

    e.Invoke(42, 21.f);
}

Functions

//Events with free functions
void FreeFunc(int a, int& b) {
    b = a;
}

{
    Event<int, int&> evt;
    //Assign free function
    auto handle = evt += FreeFunc;

    int a = 5;
    int b = 3;

    evt.Invoke(a, b);
    ASSERT_EQ(a, b);
}

Methods

class CallableThing
{
    private:
        int a;

    public:
        CallableThing(int a) : a(a) {}
        void AddOffset(int & b) { b = b + a; }
};

{
    //Create Object
    CallableThing c1(2);

    Event<int&> evt1;
    //For bound function use register method of event
    evt1.Register<CallableThing>(&c1, &CallableThing::AddOffset);

    int i = 0;
    evt1.Invoke(i);
    ASSERT_EQ(i, 2);
}

About

C#-Styled Events for C++

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published