Giter Site home page Giter Site logo

cppdelegates's Introduction

C++ Delegates

The point of this repository is just to experiment and create delegates in C++. These are not standard in C++ but are a great way for communication and it's widely known in other programming languages like C#. Unreal Engine is worth noting as it has its own implementation of Delegates in C++ too.

Classes

  • Delegate<RetVal, Args>
  • MulticastDelegate<Args>

Features

  • Support for:
    • Static/Global methods
    • Member functions
    • Lambda's
    • std::shared_ptr
  • Delegate object is allocated inline if it is under 32 bytes
  • Add payload to delegate during bind-time
  • Move operations enable optimization

Example Usage

Delegate

Delegate<int, float> del;
del.BindLambda([](float a, int payload)
{
	std::cout << "Lambda delegate parameter: " << a << std::endl;
	std::cout << "Lambda delegate payload: " << payload << std::endl;
	return 10;
}, 50);
std::cout << "Lambda delegate return value: " << del.Execute(20) << std::endl;
Output:
Lambda delegate parameter: 20
Lambda delegate payload: 50
Lambda delegate return value: 10

MulticastDelegate

struct Foo
{
	void Bar(float a, int payload)
	{
		std::cout << "Raw delegate parameter: " << a << std::endl;
		std::cout << "Raw delegate payload: " << payload << std::endl;
	}
};
MulticastDelegate<float> del;
del.AddLambda([](float a, int payload)
{
	std::cout << "Lambda delegate parameter: " << a << std::endl;
	std::cout << "Lambda delegate payload: " << payload << std::endl;
}, 90);

Foo foo;
del.AddRaw(&foo, &Foo::Bar, 10);
del.Broadcast(20);
Output:
Lambda delegate parameter: 20
Lambda delegate payload: 90
Raw delegate parameter: 20
Raw delegate payload: 10

cppdelegates's People

Contributors

simco50 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

cppdelegates's Issues

String arguments not completely working with `Broadcast(...)`

When I use a multicast delegate in 'raw' mode and one of the passed arguments to broadcast is a std::string only the first instance of the collection gets the value, others get an empty string:

TestClass t1 = TestClass();
TestClass t2 = TestClass();
MulticastDelegate<std::string> del2;

del2.AddRaw(&t1, &TestClass::DoSomething);
del2.AddRaw(&t2, &TestClass::DoSomething);

del2.Broadcast("Test");

Output:

TestClass Did Something. Argument: "Test"
TestClass Did Something. Argument: ""

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.