mercredi 6 janvier 2021

How to access templatized static variable of a class in multiple places in c++

I am working on a functional simulator project in c++. My aim is to store last 50 debug log statements in a rolling buffer and display them whenever there is a error in the simulator. This will help be debug faster. Else I will have to note down the cycle when there is a error and re-run the simulator with logs enabled which is time consuming and slow.

In file abc.h Loging structure stores debug log statements, simulation cycle at which the log was generated etc

template <typename T, typename... U>
struct Logging {
    ......
}

The following templatized class in abc.h holds a static member which is the rolling window holding last 50 log debug statements

template <typename T> 
class LogGlobal {
   public:    
       static std::deque<T> last_50_logs; //rolling window of last 50 log statements

};
template <typename T> std::deque<T> LogGlobal<T>::last_50_logs = {};

Inside debug_log function in abc.h I push some objects to the static variable like shown below:

template <typename T, typename... U>
void debug_log(T&& t, U&&... u) const
{
    //other code to ensure that this deque holds last 50 log statements only is not written here for simplicity
    LogGlobal<Logging<T, U...>>::last_50_logs.push_back(<Logging<T, U...>{...}); 
}

Inside abc.cpp file, we have a error handing function which is called whenever there is a error in the simulator.

void error_handler()
{
    //I want to access the last_50_logs static variable here but I don't have information about the template T and U here. How do I solve this?
} 

In the error_handler function I do not have access to what is the type of the template that is used to create LogGlobal class. Is there a way or workaround to know these details?

One way to solve this problem is whenever I push to last_50_logs, I can store log statements as std::string instead of template T and U inside Logging structure. But this is really really slow as string operations are expensive.

Aucun commentaire:

Enregistrer un commentaire