-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9310fd3
commit ec386ee
Showing
31 changed files
with
2,500 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
01_wednesday/thread_safe_observer_pattern_youre_doing_it_wrong/Foo0.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// example ObserverPattern | ||
// Tony Van Eerd | ||
|
||
#include <vector> | ||
#include <algorithm> | ||
#include <string> | ||
|
||
class Foo | ||
{ | ||
std::string s; | ||
|
||
public: | ||
void set(std::string str) { | ||
s = str; | ||
notifyListeners(); | ||
} | ||
std::string get() { return s; } | ||
|
||
struct Listener | ||
{ | ||
virtual void fooChanged(Foo * foo) = 0; | ||
}; | ||
|
||
void addListener(Listener * listener) { | ||
listeners.push_back(listener); | ||
} | ||
|
||
void removeListener(Listener * listener) { | ||
std::remove(listeners.begin(), listeners.end(), listener); | ||
} | ||
|
||
private: | ||
void notifyListeners() { | ||
for (auto & listener : listeners) | ||
listener->fooChanged(this); | ||
} | ||
|
||
std::vector<Listener*> listeners; | ||
}; | ||
|
||
|
44 changes: 44 additions & 0 deletions
44
01_wednesday/thread_safe_observer_pattern_youre_doing_it_wrong/Foo1.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// example ObserverPattern | ||
// Tony Van Eerd | ||
|
||
#include <vector> | ||
#include <algorithm> | ||
#include <string> | ||
|
||
class Foo | ||
{ | ||
std::string s; | ||
|
||
public: | ||
void set(std::string str) { | ||
if (s != str) { | ||
s = str; | ||
notifyListeners(); | ||
} | ||
} | ||
|
||
std::string get() { return s; } | ||
|
||
struct Listener | ||
{ | ||
virtual void fooChanged(Foo * foo) = 0; | ||
}; | ||
|
||
void addListener(Listener * listener) { | ||
listeners.push_back(listener); | ||
} | ||
|
||
void removeListener(Listener * listener) { | ||
listeners.erase(std::remove(listeners.begin(), listeners.end(), listener), listeners.end()); | ||
} | ||
|
||
private: | ||
void notifyListeners() { | ||
for (auto & listener : listeners) | ||
listener->fooChanged(this); | ||
} | ||
|
||
std::vector<Listener*> listeners; | ||
}; | ||
|
||
|
44 changes: 44 additions & 0 deletions
44
01_wednesday/thread_safe_observer_pattern_youre_doing_it_wrong/Foo2.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// example ObserverPattern | ||
// Tony Van Eerd | ||
|
||
#include <vector> | ||
#include <algorithm> | ||
#include <string> | ||
|
||
class Foo | ||
{ | ||
std::string s; | ||
|
||
public: | ||
void set(std::string str) { | ||
if (s != str) { | ||
s = str; | ||
notifyListeners(); | ||
} | ||
} | ||
|
||
std::string get() { return s; } | ||
|
||
struct Listener | ||
{ | ||
virtual void fooChanged(Foo * foo) = 0; | ||
}; | ||
|
||
void addListener(Listener * listener) { | ||
listeners.push_back(listener); | ||
} | ||
|
||
void removeListener(Listener * listener) { | ||
listeners.erase(std::remove(listeners.begin(), listeners.end(), listener)); | ||
} | ||
|
||
private: | ||
void notifyListeners() { | ||
for (auto & listener : listeners) | ||
listener->fooChanged(this); | ||
} | ||
|
||
std::vector<Listener*> listeners; | ||
}; | ||
|
||
|
44 changes: 44 additions & 0 deletions
44
01_wednesday/thread_safe_observer_pattern_youre_doing_it_wrong/Foo3.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// example ObserverPattern | ||
// Tony Van Eerd | ||
|
||
#include <vector> | ||
#include <algorithm> | ||
#include <string> | ||
|
||
class Foo | ||
{ | ||
std::string s; | ||
|
||
public: | ||
void set(std::string str) { | ||
if (s != str) { | ||
s = str; | ||
notifyListeners(); | ||
} | ||
} | ||
|
||
std::string get() { return s; } | ||
|
||
struct Listener | ||
{ | ||
virtual void fooChanged(Foo * foo) = 0; | ||
}; | ||
|
||
void addListener(Listener * listener) { | ||
listeners.push_back(listener); | ||
} | ||
|
||
void removeListener(Listener * listener) { | ||
listeners.erase(std::find(listeners.begin(), listeners.end(), listener)); | ||
} | ||
|
||
private: | ||
void notifyListeners() { | ||
for (auto & listener : listeners) | ||
listener->fooChanged(this); | ||
} | ||
|
||
std::vector<Listener*> listeners; | ||
}; | ||
|
||
|
48 changes: 48 additions & 0 deletions
48
01_wednesday/thread_safe_observer_pattern_youre_doing_it_wrong/Foo4.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// example ObserverPattern | ||
// Tony Van Eerd | ||
|
||
#include <vector> | ||
#include <algorithm> | ||
#include <string> | ||
|
||
class Foo | ||
{ | ||
std::string s; | ||
|
||
public: | ||
void set(std::string str) { | ||
if (s != str) { | ||
s = str; | ||
notifyListeners(); | ||
} | ||
} | ||
|
||
std::string get() { return s; } | ||
|
||
struct Listener | ||
{ | ||
virtual void fooChanged(Foo * foo) = 0; | ||
}; | ||
|
||
void addListener(Listener * listener) { | ||
listeners.push_back(listener); | ||
} | ||
|
||
bool removeListener(Listener * listener) { | ||
auto it = std::find(listeners.begin(), listeners.end(), listener); | ||
if (it != listeners.end()) { | ||
listeners.erase(it); | ||
return true; | ||
} | ||
} | ||
|
||
private: | ||
void notifyListeners() { | ||
for (auto & listener : listeners) | ||
listener->fooChanged(this); | ||
} | ||
|
||
std::vector<Listener*> listeners; | ||
}; | ||
|
||
|
49 changes: 49 additions & 0 deletions
49
01_wednesday/thread_safe_observer_pattern_youre_doing_it_wrong/Foo5.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// example ObserverPattern | ||
// Tony Van Eerd | ||
|
||
#include <vector> | ||
#include <algorithm> | ||
#include <string> | ||
|
||
class Foo | ||
{ | ||
std::string s; | ||
|
||
public: | ||
void set(std::string str) { | ||
if (s != str) { | ||
s = str; | ||
notifyListeners(); | ||
} | ||
} | ||
|
||
std::string get() { return s; } | ||
|
||
struct Listener | ||
{ | ||
virtual void fooChanged(Foo * foo) = 0; | ||
}; | ||
|
||
void addListener(Listener * listener) { | ||
listeners.push_back(listener); | ||
} | ||
|
||
bool removeListener(Listener * listener) { | ||
auto it = std::find(listeners.rbegin(), listeners.rend(), listener); | ||
if (it != listeners.rend()) { | ||
listeners.erase(it); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private: | ||
void notifyListeners() { | ||
for (auto & listener : listeners) | ||
listener->fooChanged(this); | ||
} | ||
|
||
std::vector<Listener*> listeners; | ||
}; | ||
|
||
|
52 changes: 52 additions & 0 deletions
52
01_wednesday/thread_safe_observer_pattern_youre_doing_it_wrong/Foo6.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// example ObserverPattern | ||
// Tony Van Eerd | ||
|
||
#include <vector> | ||
#include <algorithm> | ||
#include <string> | ||
|
||
class Foo | ||
{ | ||
std::string s; | ||
|
||
public: | ||
void set(std::string str) { | ||
if (s != str) { | ||
s = str; | ||
notifyListeners(); | ||
} | ||
} | ||
|
||
std::string get() { return s; } | ||
|
||
struct Listener | ||
{ | ||
virtual void fooChanged(Foo * foo) = 0; | ||
}; | ||
|
||
void addListener(Listener * listener) { | ||
listeners.push_back(listener); | ||
} | ||
|
||
bool removeListener(Listener * listener) { | ||
auto it = listeners.end(); | ||
do { | ||
it--; | ||
if (*it == listener) { | ||
listeners.erase(it); | ||
return true; | ||
} | ||
} while (it != listeners.begin()); | ||
return false; | ||
} | ||
|
||
private: | ||
void notifyListeners() { | ||
for (auto & listener : listeners) | ||
listener->fooChanged(this); | ||
} | ||
|
||
std::vector<Listener*> listeners; | ||
}; | ||
|
||
|
52 changes: 52 additions & 0 deletions
52
01_wednesday/thread_safe_observer_pattern_youre_doing_it_wrong/Foo7.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// example ObserverPattern | ||
// Tony Van Eerd | ||
|
||
#include <vector> | ||
#include <algorithm> | ||
#include <string> | ||
|
||
class Foo | ||
{ | ||
std::string s; | ||
|
||
public: | ||
void set(std::string str) { | ||
if (s != str) { | ||
s = str; | ||
notifyListeners(); | ||
} | ||
} | ||
|
||
std::string get() { return s; } | ||
|
||
struct Listener | ||
{ | ||
virtual void fooChanged(Foo * foo) = 0; | ||
}; | ||
|
||
void addListener(Listener * listener) { | ||
listeners.push_back(listener); | ||
} | ||
|
||
bool removeListener(Listener * listener) { | ||
for (auto it = listeners.end(); it != listeners.begin(); ) | ||
{ | ||
it--; | ||
if (*it == listener) { | ||
listeners.erase(it); | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private: | ||
void notifyListeners() { | ||
for (auto & listener : listeners) | ||
listener->fooChanged(this); | ||
} | ||
|
||
std::vector<Listener*> listeners; | ||
}; | ||
|
||
|
Oops, something went wrong.