Skip to content

Commit

Permalink
feat: add new initial stat observer pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
branylagaffe committed May 27, 2024
1 parent 8cdfe5f commit b9af1f2
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
11 changes: 11 additions & 0 deletions core/stats/IStatObserver.hpp
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
16 changes: 16 additions & 0 deletions core/stats/IStatSubject.hpp
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
33 changes: 33 additions & 0 deletions core/stats/Stat.hpp
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
18 changes: 18 additions & 0 deletions core/stats/StatManager.cpp
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_;
}
32 changes: 32 additions & 0 deletions core/stats/StatManager.hpp
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

0 comments on commit b9af1f2

Please sign in to comment.