NSCpp 2.0
NSCpp is a modern C++20 server/client framework designed to simplify network programming in C++. With robust support for both IPv4 and IPv6, as well as TCP and UDP communication protocols.
 
Loading...
Searching...
No Matches
GroupManager.h
Go to the documentation of this file.
1
6
7
8#ifndef _GROUPMANAGER_H
9#define _GROUPMANAGER_H
10
11#include <functional>
12#include <unordered_map>
13#include <shared_mutex>
14#include <memory>
15#include <vector>
16#include <string>
17#include <sstream>
18
20template <typename T>
21auto createCallback(T* ptr, void (T::* method)(const std::string&)) {
22 return [ptr, method](const std::string& s) {
23 (ptr->*method)(s);
24 };
25}
26
27class GroupManager {
28public:
33 static GroupManager& getInstance();
34
41 void subscribe(const std::string& groupName, std::function<void(const std::string&)> callback, const std::string& CUID);
42
48 void unsubscribe(const std::string& groupName , const std::string& CUID);
49
56 void send(const std::string& groupName, const std::string& message);
57
58protected:
64 std::unordered_map<std::string, std::vector<std::pair<std::function<void(const std::string&)>, std::string>>> groups;
65
66private:
67
68 GroupManager() = default;
69 ~GroupManager() = default;
70
71 // Design to be used by multiple threads =>
72 std::shared_mutex mtx;
73
74 // We block the different way to copy or deplace it
75 GroupManager(const GroupManager&) = delete;
76 GroupManager& operator=(const GroupManager&) = delete;
77 GroupManager(GroupManager&&) = delete;
78 GroupManager& operator=(GroupManager&&) = delete;
79};
80
81#endif //_GROUPMANAGER_H
auto createCallback(T *ptr, void(T::*method)(const std::string &))
Project NSCpp.
Definition GroupManager.h:21
void subscribe(const std::string &groupName, std::function< void(const std::string &)> callback, const std::string &CUID)
Let you register a Channel's callback to a group.
void unsubscribe(const std::string &groupName, const std::string &CUID)
Remove a Channel's callback from the given group.
static GroupManager & getInstance()
Return the instance of the GroupManager or create one if it does not exist.
void send(const std::string &groupName, const std::string &message)
Emit the message in the given groupName.
std::unordered_map< std::string, std::vector< std::pair< std::function< void(const std::string &)>, std::string > > > groups
Unordered map of the groups, link the group's name to the callback and CUID.
Definition GroupManager.h:64