explicit std namespace

This commit is contained in:
themancalledjakob 2024-03-16 16:25:24 +01:00
parent fa1d70e6a5
commit a868e34fa1

View file

@ -5,6 +5,7 @@
#include <fstream> #include <fstream>
#include <iomanip> #include <iomanip>
#include <string> #include <string>
#include <mutex>
#include <thread> #include <thread>
#include "ofMain.h" #include "ofMain.h"
@ -12,34 +13,34 @@
namespace ofxProfiler { namespace ofxProfiler {
using FloatingPointMicroseconds = chrono::duration <double, micro>; using FloatingPointMicroseconds = std::chrono::duration <double, std::micro>;
struct ProfileResult { struct ProfileResult {
string Name; std::string Name;
FloatingPointMicroseconds Start; FloatingPointMicroseconds Start;
chrono::microseconds ElapsedTime; std::chrono::microseconds ElapsedTime;
thread::id ThreadID; std::thread::id ThreadID;
}; };
struct InstrumentationSession { struct InstrumentationSession {
string Name; std::string Name;
}; };
class Instrumentor { class Instrumentor {
private: private:
mutex m_Mutex; std::mutex m_Mutex;
InstrumentationSession * m_CurrentSession; InstrumentationSession * m_CurrentSession;
ofstream m_OutputStream; std::ofstream m_OutputStream;
public: public:
Instrumentor() Instrumentor()
: m_CurrentSession(nullptr){ : m_CurrentSession(nullptr){
} }
void BeginSession(const string & name = "I do not want to name my profiling sessions.", void BeginSession(const std::string & name = "I do not want to name my profiling sessions.",
const string & filepath = "results.json", const std::string & filepath = "results.json",
const bool continueSession = false){ const bool continueSession = false){
lock_guard <mutex> lock(m_Mutex); std::lock_guard <std::mutex> lock(m_Mutex);
if(!m_CurrentSession || !continueSession){ if(!m_CurrentSession || !continueSession){
// if there is no session, or it should not be continued // if there is no session, or it should not be continued
if(m_CurrentSession){ if(m_CurrentSession){
@ -62,17 +63,17 @@ class Instrumentor {
} }
void EndSession(){ void EndSession(){
lock_guard <mutex> lock(m_Mutex); std::lock_guard <std::mutex> lock(m_Mutex);
InternalEndSession(); InternalEndSession();
} }
void WriteProfile(const ProfileResult & result){ void WriteProfile(const ProfileResult & result){
stringstream json; std::stringstream json;
string name = result.Name; std::string name = result.Name;
replace(name.begin(), name.end(), '"', '\''); replace(name.begin(), name.end(), '"', '\'');
json << setprecision(3) << fixed; json << std::setprecision(3) << std::fixed;
json << ",{"; json << ",{";
json << "\"cat\":\"function\","; json << "\"cat\":\"function\",";
json << "\"dur\":" << (result.ElapsedTime.count()) << ','; json << "\"dur\":" << (result.ElapsedTime.count()) << ',';
@ -83,7 +84,7 @@ class Instrumentor {
json << "\"ts\":" << result.Start.count(); json << "\"ts\":" << result.Start.count();
json << "}"; json << "}";
lock_guard <mutex> lock(m_Mutex); std::lock_guard <std::mutex> lock(m_Mutex);
if(m_CurrentSession){ if(m_CurrentSession){
m_OutputStream << json.str(); m_OutputStream << json.str();
m_OutputStream.flush(); m_OutputStream.flush();
@ -123,7 +124,7 @@ class InstrumentationTimer {
public: public:
InstrumentationTimer(const char * name) InstrumentationTimer(const char * name)
: m_Name(name), m_Stopped(false){ : m_Name(name), m_Stopped(false){
m_StartTimepoint = chrono::steady_clock::now(); m_StartTimepoint = std::chrono::steady_clock::now();
} }
~InstrumentationTimer(){ ~InstrumentationTimer(){
@ -133,17 +134,17 @@ class InstrumentationTimer {
} }
void Stop(){ void Stop(){
auto endTimepoint = chrono::steady_clock::now(); auto endTimepoint = std::chrono::steady_clock::now();
auto highResStart = FloatingPointMicroseconds{m_StartTimepoint.time_since_epoch()}; auto highResStart = FloatingPointMicroseconds{m_StartTimepoint.time_since_epoch()};
auto elapsedTime = chrono::time_point_cast <chrono::microseconds>(endTimepoint).time_since_epoch() - chrono::time_point_cast <chrono::microseconds>(m_StartTimepoint).time_since_epoch(); auto elapsedTime = std::chrono::time_point_cast <std::chrono::microseconds>(endTimepoint).time_since_epoch() - std::chrono::time_point_cast <std::chrono::microseconds>(m_StartTimepoint).time_since_epoch();
Instrumentor::Get().WriteProfile({m_Name, highResStart, elapsedTime, this_thread::get_id()}); Instrumentor::Get().WriteProfile({m_Name, highResStart, elapsedTime, std::this_thread::get_id()});
m_Stopped = true; m_Stopped = true;
} }
private: private:
const char * m_Name; const char * m_Name;
chrono::time_point <chrono::steady_clock> m_StartTimepoint; std::chrono::time_point <std::chrono::steady_clock> m_StartTimepoint;
bool m_Stopped; bool m_Stopped;
}; };
} }