Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/linksplatform/delegates
LinksPlatform's Platform.Delegates Template Library
https://github.com/linksplatform/delegates
Last synced: about 2 months ago
JSON representation
LinksPlatform's Platform.Delegates Template Library
- Host: GitHub
- URL: https://github.com/linksplatform/delegates
- Owner: linksplatform
- License: unlicense
- Created: 2020-01-01T18:27:25.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2024-01-02T16:45:02.000Z (about 1 year ago)
- Last Synced: 2024-04-16T00:51:28.587Z (9 months ago)
- Language: C++
- Homepage:
- Size: 215 KB
- Stars: 2
- Watchers: 4
- Forks: 3
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![NuGet Version and Downloads count](https://img.shields.io/nuget/v/Platform.Delegates.TemplateLibrary?label=nuget&style=flat)](https://www.nuget.org/packages/Platform.Delegates.TemplateLibrary)
[![ConanCenter package](https://repology.org/badge/version-for-repo/conancenter/platform.delegates.svg)](https://conan.io/center/platform.delegates)# Delegates
LinksPlatform's Platform::Delegates Template Class LibraryDelegate and MulticastDelegate classes are modeled after [.NET Delegates](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/).
Delegate is a container for callable function. Delegate can contain:
* Pointer to simple function (represented by a single pointer).
* Pointer to an instanse of class and to a member method of this class.
* Pointer to std::function instance.Unlike std::function Delegate class supports primitive by reference comparision for equality.
MulticastDelegate represents a thread-safe collection of Delegates that are callable at the same time. If you call MulticastDelegate all Delegates added to its collection will be called. MulticastDelegate can be used as a basis for simple event model (similar of that you can find in .NET). Because Delegate class supports the comparision for equality you can both subscribe and unsubscribe any Delegate instance to MulticastDelegate. To be able to unsubscribe from event represented by MulticastDelegate instance you should store Delegate instance somewhere. Due to its thread safety MulticastDelegate instance can be global/static object to which every thread is safe to subscribe.
NuGet package: [Platform.Delegates.TemplateLibrary](https://www.nuget.org/packages/Platform.Delegates.TemplateLibrary)
Conan package: [platform.delegates](http://www.conan.io/center/platform.delegates)
## Example
```C++
#includevoid function(const char *str)
{
std::cout << "function(" << str << ")" << std::endl;
}struct Object
{
void Method(const char *str)
{
std::cout << "Object::Method(" << str << ")" << std::endl;
}
};int main()
{
MulticastDelegate event;Delegate memberMethod(std::make_shared(), &Object::Method);
auto lambda = std::make_shared>([](const char *str)
{
std::cout << "lambda(" << str << ")" << std::endl;
});// Subscribe
event += function;
event += lambda;
event += memberMethod;// Raise the event
event("value");// Unsubscribe
event -= function;
event -= lambda;
event -= memberMethod;
}
```