|
Prev: designing your own characters in C++
Next: balancing
From: Andrew on 20 Nov 2007 05:01 Hello All, I have a debug header (see source code below), but I'm not sure if it's good style to have four template functions with the same code. Could anyone advice me how to optimize that code? Thank you! #ifndef DEBUG_HPP_ #define DEBUG_HPP_ #include <fstream> #define _DEBUG #ifdef _DEBUG const char* LOG_NAME = "d:\\log.txt"; template <typename T> inline void _DBG(T msg) { std::ofstream logfile(LOG_NAME, std::ios::app); logfile << msg << std::endl; } template <typename T1, typename T2> inline void _DBG(T1 msg1, T2 msg2) { std::ofstream logfile(LOG_NAME, std::ios::app); logfile << msg1 << " " << msg2 << std::endl; } template <typename T1, typename T2, typename T3> inline void _DBG(T1 msg1, T2 msg2, T3 msg3) { std::ofstream logfile(LOG_NAME, std::ios::app); logfile << msg1 << " " << msg2 << " " << msg3 << std::endl; } template <typename T1, typename T2, typename T3, typename T4> inline void _DBG(T1 msg1, T2 msg2, T3 msg3, T4 msg4) { std::ofstream logfile(LOG_NAME, std::ios::app); logfile << msg1 << " " << msg2 << " " << msg3 << " " << msg4 << std::endl; } #else template <typename T> inline void _DBG(T msg) { } template <typename T1, typename T2> inline void _DBG(T1 msg1, T2 msg2) { } template <typename T1, typename T2, typename T3> inline void _DBG(T1 msg1, T2 msg2, T3 msg3) { } template <typename T1, typename T2, typename T3, typename T4> inline void _DBG(T1 msg1, T2 msg2, T3 msg3, T4 msg4) { } #endif #endif /*DEBUG_HPP_*/
From: Francis Glassborow on 20 Nov 2007 08:10 Andrew wrote: > Hello All, > > I have a debug header (see source code below), but I'm not sure if > it's good style to have four template functions with the same code. > Could anyone advice me how to optimize that code? Thank you! > There is no problem with having a set of overloaded function templates however I am puzzled as to why you are overloading them when it would seem that they all concern error messages. Surely you use the same type for all such debug messages (either std::string or more likely for this kind of use a null terminated array of char). Could you provide a spec for each of the four cases then we can judge if it is reasonable to use templates and how best to design an overload set.
From: Andrew on 20 Nov 2007 08:38 On 20 ÎÏÑÂ, 16:10, Francis Glassborow <francis.glassbo...(a)btinternet.com> wrote: > Andrew wrote: > > Hello All, > > > I have a debug header (see source code below), but I'm not sure if > > it's good style to have four template functions with the same code. > > Could anyone advice me how to optimize that code? Thank you! > > There is no problem with having a set of overloaded function templates > however I am puzzled as to why you are overloading them when it would > seem that they all concern error messages. Surely you use the same type > for all such debug messages (either std::string or more likely for this > kind of use a null terminated array of char). Could you provide a spec > for each of the four cases then we can judge if it is reasonable to use > templates and how best to design an overload set. I have no particular specs in this case and parameters are not always the same type (see below) So, the first argument is a more likely a description and all subsequent arguments are types (int, string, bool, ...) ------------------ fragment 0 ------------------ bool is_opened //... int open(const char* _base_path) { _DBG("open:",_base_path, is_opened); // [const char*, const char*, bool] //... } ------------------ fragment 1 ------------------ int column_count = sqlite3_column_count(stmt); DBG("sqlite column count:",column_count); // [const char*, int] ------------------ fragment 2 ------------------ unsigned int ip_addr = ip_to_int(addr.c_str()); _DBG("ipaddr:",ip_addr); // [const char*, unsigned int] ------------------ fragment 3 ------------------ _DBG("select user"); ------------------ fragment 4 ------------------ typedef std::vector< std::string > sqlite_row; //... sqlite_row user_row; //... _DBG("rows:",user_row[0],user_row[1],user_row[3]);
|
Pages: 1 Prev: designing your own characters in C++ Next: balancing |