File NPCounter.h
File List > core > NPCounter.h
Go to the documentation of this file
#ifndef NPCounter_h
#define NPCounter_h
#include "NPException.h"
#include "NPFunction.h"
#include <map>
#include <memory>
#include <string>
namespace nptool {
class Counter {
public:
Counter() : m_progress(0){};
~Counter(){};
private:
double m_progress;
// map of token vs counter
std::map<std::string, std::shared_ptr<uint64_t>> m_counter;
// map of token vs name (for display)
std::map<std::string, std::string> m_name;
// map of token vs unit (for diplay)
std::map<std::string, std::string> m_unit;
public:
void UpdateProgress(double val) { m_progress = val; };
std::shared_ptr<uint64_t> CreateCounter(std::string token, std::string name, std::string unit = "");
std::shared_ptr<uint64_t> GetCounter(std::string token) {
if (m_counter.find(token) != m_counter.end())
return m_counter[token];
else {
std::string message = "Requested counter " + token + " does not exist";
throw nptool::Error("NPCounter", message, "Check syntax or call SetCounter");
return 0;
}
};
void UpdateCounter(const std::string& token, const unsigned long long& val) {
auto it = m_counter.find(token);
if (it != m_counter.end())
*(it->second) = val;
else {
std::string message = "Requested counter " + token + " does not exist";
throw nptool::Error("NPCounter", message, "Check syntax or call SetCounter");
}
}
void IncrementCounter(const std::string& token) {
auto it = m_counter.find(token);
if (it != m_counter.end())
(*(it->second))++;
else {
std::string message = "Requested counter " + token + " does not exist";
throw nptool::Error("NPCounter", message, "Check syntax or call SetCounter");
}
};
std::string& GetString(const std::string& token) {
thread_local std::string res, unit;
auto it = m_counter.find(token);
if (it != m_counter.end()) {
auto counter = *(it->second);
unit = "";
if (counter > 10e9) {
counter /= 1e9;
unit = "G";
}
else if (counter > 10e6) {
counter /= 1e6;
unit = "M";
}
else if (counter > 10e3) {
counter /= 1e3;
unit = "k";
}
unit += m_unit[token];
res = m_name[token] + ": " + itoa(counter) + unit;
}
else {
std::string message = "Requested counter " + token + " does not exist";
throw nptool::Error("NPCounter", message, "Check syntax or call SetCounter");
}
return res;
}
};
} // namespace nptool
#endif