-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDisposable.cpp
60 lines (49 loc) · 1.53 KB
/
Disposable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//
// Disposable.cpp
// Jako
//
// Created by John Zakharov on 06.08.16.
// Copyright © 2016 Outlaw Studio. All rights reserved.
//
#include "Disposable.hpp"
#include <iostream>
using std::cout;
using std::endl;
std::mutex Mitsoko::Disposable::observersMutex;
std::vector<Mitsoko::Disposable::Observer*> Mitsoko::Disposable::observers;
Mitsoko::Disposable::Observer::~Observer(){
Disposable::unsubscribe(this);
}
Mitsoko::Disposable::Disposable():id(generateId()){}
void Mitsoko::Disposable::dispose(){
for(auto observer : observers) {
const auto id = this->id;
// cout << "observer = " << observer <<endl;
observer->disposableDidDispose(id);
}
}
void Mitsoko::Disposable::subscribe(Observer *observer){
observersMutex.lock();
observers.emplace_back(observer);
// cout << "subscribe " << observer << ", observers count = " << observers.size() << endl;
observersMutex.unlock();
}
void Mitsoko::Disposable::unsubscribe(Observer *observer){
observersMutex.lock();
// cout << "unsubscribe " << observer << ", observers count = " << observers.size() << endl;
const auto it=std::find(observers.begin(),
observers.end(),
observer);
if(observers.end() != it){
observers.erase(it);
}
observersMutex.unlock();
}
Mitsoko::Disposable::Id Mitsoko::Disposable::generateId(){
static Id previousId = 0;
static std::mutex idLock;
idLock.lock();
const auto res = previousId++;
idLock.unlock();
return res;
}