-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new initial stat observer pattern
- Loading branch information
1 parent
8cdfe5f
commit b9af1f2
Showing
5 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
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,11 @@ | ||
#ifndef ISTATOBSERVER_HPP | ||
#define ISTATOBSERVER_HPP | ||
|
||
class IStatObserver | ||
{ | ||
public: | ||
virtual ~IStatObserver(){}; | ||
// virtual void function_name(smth to transmit) = 0 | ||
}; | ||
|
||
#endif |
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,16 @@ | ||
#ifndef ISTATSUBJECT_HPP | ||
#define ISTATSUBJECT_HPP | ||
|
||
#include "IStatObserver.hpp" | ||
|
||
#include <string> | ||
|
||
class IStatSubject | ||
{ | ||
|
||
public: | ||
virtual ~IStatSubject(){}; | ||
virtual void attach(std::string observer_name, IStatObserver* observer) = 0; | ||
}; | ||
|
||
#endif |
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,33 @@ | ||
#ifndef STAT_HPP | ||
#define STAT_HPP | ||
|
||
#include "IStatObserver.hpp" | ||
#include "StatManager.hpp" | ||
|
||
#include <cstdint> | ||
#include <iterator> | ||
#include <map> | ||
#include <string> | ||
#include <utility> | ||
|
||
class Stat : public IStatObserver | ||
{ | ||
private: | ||
std::map<std::string, uint64_t> stats_map; | ||
|
||
public: | ||
template<typename Iterable> | ||
Stat(const std::string name, const Iterable& list_entries) | ||
{ | ||
for (const auto c : list_entries) { | ||
stats_map.insert(std::make_pair<std::string, uint64_t>(c, 0)); | ||
} | ||
|
||
StatManager::get_instance().attach(name, this); | ||
}; | ||
|
||
void increment(const std::string key) { stats_map.at(key) += 1; } | ||
void add(const std::string key, uint64_t value) { stats_map.at(key) += value; } | ||
}; | ||
|
||
#endif |
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,18 @@ | ||
#include "StatManager.hpp" | ||
|
||
#include <utility> | ||
|
||
using namespace std; | ||
|
||
void | ||
StatManager::attach(string observer_name, IStatObserver* observer) | ||
{ | ||
observer_map_.insert(make_pair<string, IStatObserver*>(move(observer_name), move(observer))); | ||
} | ||
|
||
StatManager& | ||
StatManager::get_instance() | ||
{ | ||
if (instance_ == nullptr) { instance_ = new StatManager; } | ||
return *instance_; | ||
} |
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,32 @@ | ||
|
||
#ifndef STATMANAGER_HPP | ||
#define STATMANAGER_HPP | ||
|
||
#include "IStatObserver.hpp" | ||
#include "IStatSubject.hpp" | ||
|
||
#include <map> | ||
#include <string> | ||
|
||
class StatManager : public IStatSubject | ||
{ | ||
private: | ||
static StatManager* instance_; | ||
std::map<std::string, IStatObserver*> observer_map_; | ||
|
||
private: | ||
StatManager() = default; | ||
|
||
public: | ||
static StatManager& get_instance(); | ||
StatManager(StatManager& instance) = delete; | ||
void operator=(const StatManager&) = delete; | ||
|
||
virtual void attach(std::string observer_name, IStatObserver* observer) override; | ||
|
||
void dump(const std::string filename); | ||
}; | ||
|
||
StatManager* StatManager::instance_ = nullptr; | ||
|
||
#endif |