Giter Site home page Giter Site logo

multicast-delegate's Introduction

multicast-delegate

C-Sharp style MulticastDelegate, allows combining and calling multiple delegates into one. Transparently works with third party libraries, works with @safe, nothrow, pure and has APIs to use with @nogc. The library works well with const and immutable as well and the data structure is simply a safe and efficient wrapper around delegate arrays. Offers consistent D-style operator overloading using the ~ concatenation operator as well as C# compatible + and - operators for calling the add and remove functions.

  • Allows you to define APIs with more flexible callbacks
  • Allows you to use this as C# style event system
  • Allows you to write arbitrary depth callbacks with return values and out/ref
  • Uses idiomatic features with full integration of language features

To use in your dub project simply run

dub add multicast-delegate

To use with additional C# compatibility types, add

"subConfigurations": {
	"multicast-delegate": "cs-compat"
}

to your dub.json or add

// dub.sdl
subConfiguration "multicast-delegate" "cs-compat"

to your dub.sdl.

To use elsewhere, just copy the source/multicast_delegate.d file into your project and import it using import multicast_delegate.

Example

import std.stdio;
import multicast_delegate; // I recommend using `public import` for all
                           // your common imports in some helper file

// have documented delegate types for your callbacks
alias SomeDelegate = void delegate();

// define your APIs as usual
void runCallback(SomeDelegate dg) { dg(); }

void main() {
	void func1() { writeln("1"); }
	void func2() { writeln("2"); }

	writeln("\ncalling normal delegate");
		// call like normal
		runCallback(&func1);

	writeln("\ncalling multicast delegates");
		// or call with multiple delegates in a row
		runCallback(multicast(&func1, &func2));

	writeln("\ncalling more delegates");
		// or store more complex delegate groups
		Multicast!SomeDelegate multi = &func1;
		// mutate it!
		multi ~= &func2;

		// or construct it immutable or const
		immutable Multicast!SomeDelegate fixed = multicast(&func2, &func2);

		// concat multiple and call a normal delegate method
		runCallback(multi ~ fixed); // implicitly using Multicast!T as delegate
}

outputs

calling normal delegate
1

calling multicast delegates
1
2

calling more delegates
1
2
2
2

Another simple example porting the following C# code:

using System;

public class Program
{
	public delegate void SomeDelegate();

	public static void Main()
	{
		void Foo() { Console.WriteLine("foo"); }
		void Bar() { Console.WriteLine("bar"); }

		SomeDelegate dg = Foo;
		dg += Foo;
		dg += Foo;
		dg += Bar;
		dg();

		CallCB(dg);
	}

	public static void CallCB(SomeDelegate cb)
	{
		cb();
	}
}

is ported:

import std.stdio;
import multicast_delegate;

alias SomeDelegate = void delegate();

void main()
{
	void foo() { writeln("foo"); }
	void bar() { writeln("bar"); }

	Multicast!SomeDelegate dg = &foo;
	dg ~= &foo;
	dg ~= &foo;
	dg ~= &bar;
	dg();

	callCB(dg);
}

void callCB(SomeDelegate cb)
{
	cb();
}

If you want to use global functions (not delegates) you need to import std.functional : toDelegate and use dg ~= toDelegate(&foo); or you can define SomeDelegate as a void function(). However in this exact code the callCB function argument would break with that.

Using class or struct members and lambdas works without issues.

Documentation

https://multicast-delegate.dpldocs.info

multicast-delegate's People

Contributors

webfreak001 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.