Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication8.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <string>
- #include <iostream>
- #include <functional>
- #include <vector>
- using namespace std;
- template <typename ...Args>
- //Simplification for readability
- using delegate = std::function<void(Args...)>;
- template <typename ...Args>
- //Signal class -- holds vector of functions.
- class signal {
- private:
- using fn_t = delegate<Args...>;
- std::vector<fn_t> observers;
- public:
- void connect(fn_t f) {
- observers.push_back(f);
- }
- void disconnect(fn_t f)
- {
- // No code yet.
- }
- void operator ()(Args... a) {
- for (auto i : observers)
- i(a...);
- }
- };
- // Button to send Click event through Signal Update.
- struct button {
- signal<int, int> update;
- void click(int x, int y) {
- update(x, y);
- }
- };
- // Label with PrintNum
- struct label {
- std::string text;
- void PrintNum(int x, int y) {
- std::cout << x << y << std::endl;
- }
- };
- int main() {
- using namespace std::placeholders;
- label label1;
- label label2;
- button button1;
- // Connect buttons, testing two different methods.
- button1.update.connect(std::bind(&label::PrintNum, std::ref(label1), _1, _2));
- button1.update.connect([&](int x, int y) { label1.PrintNum(x, y); });
- //Click event, expecting output '23'
- button1.click(2, 3);
- system("pause");
- return 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement