??? boost就有一個實現類型安全format的類,不過類比較龐大,而且也比較復雜,用起來也不是很習慣。
所以還是自己簡單實現一個了。
??? 先看看需求:
format_string.format("[%] = %") % a % strName
??? 其實就是希望后面的a和strName分別替代格式化字符串"[%] = %"中的兩個%號,相當于:
??? stringstream format_string;
??? format_string << "[" << a << "] = " << strName;
??? 當然,要做到通用,還是希望不單能輸出到stringstream,最好還是能輸出到std::ostream。
??? class format_stream
??? {
??? public:
??????? explicit format_stream(std::ostream & outS) : m_output(outS),m_lpszFormat(&g_nEndFlags)
??????? {
??????? }
??????? inline format_stream & format(const char * lpszFormat)
??????? {
??????????? flushFormat();
??????????? m_lpszFormat = lpszFormat;
??????????? return outputPrefix();
?????? }
???????
??????? template
??????? inline format_stream & arg(const typeArg1 & val) {
??????????????? getOutput() << val;
??????????????? return outputPrefix();
??????? }
?????? ~format_stream(void) {}
??? protected:
??????? inline std::ostream & getOutput(void)
??????? {
??????????????? return m_output;
??????? }
??????? void??? flushFormat(void)
??????? {
??????????????? if (*m_lpszFormat)
??????????????? {
??????????????????????? getOutput() << m_lpszFormat;
??????????????????????? m_lpszFormat = &g_nEndFlags;
??????????????? }??????
??????? }
??????? format_stream & outputPrefixLoop(void);
??????? format_stream & outputPrefix(void);
????
??????? static? char??? g_nEndFlags;
??????? std::ostream &? m_output;
??????? const char *??? m_lpszFormat;
??? };
???????
??? char format_stream::g_nEndFlags = char();
??? format_stream & format_stream::outputPrefix(void)
??? {
??????? char * lpPos = strchr(m_lpszFormat,'%');
??????? if (lpPos != NULL)
??????? {
??????????????? getOutput().write(m_lpszFormat, lpPos - m_lpszFormat);
??????????????? m_lpszFormat = lpPos + 1;
??????????????? if (*m_lpszFormat == '%' && *(m_lpszFormat + 1) != '%')
??????????????????????? return outputPrefixLoop();
??????? } // if (lpPos != NULL)
??????? else
??????????????? flushFormat();
??????? return *this;
??? }
??? format_stream & format_stream::outputPrefixLoop(void)??
??? {
??????? while (*m_lpszFormat == '%')
??????? {
??????????????? char * lpPos = strchr(m_lpszFormat + 1, '%');
??????????????? if (lpPos != NULL)
??????????????? {
??????????????????????? getOutput().write(m_lpszFormat, lpPos - m_lpszFormat);
??????????????????????? m_lpszFormat = lpPos + 1;
??????????????????????? if (*m_lpszFormat != '%' || *(m_lpszFormat + 1) == '%')
??????????????????????????????? break;
??????????????? } // if (lpPos != NULL)
??????????????? else
??????????????? {
??????????????????????? flushFormat();
??????????????????????? break;
??????????????? }
??????? } // while (*m_lpszFormat)
??????? return *this;
??? }
??? 1、規定以'%'作為占位符,表示后續的變量替換的位置。
???????
??? 2、兩個連續'%'即 '%%'表示一個真正的'%'。不過需要注意的是:一般想輸出百分數的時候,就是要寫%%%,
分析程序發現三個連續'%',則認為第一個是占位符,后兩個表示一個'%'。而發現四個'%'的時候,前兩個都會被認
為是占位符,最后兩個被認為是一個'%'。
??? 3、boost用%連接后面的多個變量,也有些類庫使用逗號。個人都不是很喜歡:'%'用得太多,程序看起來不好
看;很多書都再三聲明最好不要重載逗號運算符。所以還是使用函數比較穩妥,所以就用函數arg(a)的形式。如果真
的喜歡使用'%'或者逗號,只需要增加成員函數:
??????? template
??????????? inline format_stream & operator%(const typeArg1 & val) { return arg(val); }
??????? template
??????????? inline format_stream & operator,(const typeArg1 & val) { return arg(val); }
???????????????
??? 4、arg還可以繼續擴展,
??????? a、同一個占位符輸出多個變量,只需要增加多幾個成員函數:
??????? template
??????? inline format_stream & arg(const typeArg1 & val1,const typeArg2 & val2)
??????? {
??????????????? getOutput() << val << val2;
??????????????? return outputPrefix();
??????? }
??????? template
??????? inline format_stream & arg(const typeArg1 & val1,
??????????????????????????????? const typeArg2 & val2,
??????????????????????????????? const typeArg3 & val3)
??????? {
??????????????? getOutput() << val1 << val2 << val3;
??????????????? return outputPrefix();
??????? }
??????? 例如有時候想輸出一個范圍:
???????
??????? stream.format("range1:% range2:%").arg(lowerbound1,'-',upperbound1);
??????? stream.arg(lowerbound2,'~',upperbound2)
???????
??????? b、格式化輸出。printf那么多的格式化輸出參數寫在格式化字符串中,我老是會記錯,一旦寫錯
??????? 程序就容易出問題了(不單是顯示錯誤,還有可能會coredump),另外發現不同平臺格式還會有
??????? 些不一樣。還是寫在arg里面比較穩妥,而且程序也容易閱讀。
???????
??????? 為了和上面"同一個占位符輸出多個變量"的函數區分,還是另外取一個函數名:
???????
??????? enum??? INT_BASE
??????????????? {BASE10=std::ios::dec, BASE8=std::ios::oct, BASE16=std::ios::hex};
??????? enum??? CHAR_CASE
??????????????? {CHAR_UPCASE=0, CHAR_LOWCASE=1 };
??????? enum??? BASE_FLAG
??????????????? {HIDE_BASE=0, SHOW_BASE=1 };
??????? enum??? POS_FLAG
??????????????? {HIDE_POS=0, SHOW_POS=1 };
??????? enum??? FIX_FLAG
??????????????? { FIXED=0, SCIENTIFIC=1 };
??????? enum??? POINT_FLAG
??????????????? { HIDE_POINT=0, SHOW_POINT=1 };
??????? enum??? ADJUSTFIELD_FLAG
??????? {
??????????????? ADJUST_LEFT=std::ios::left,
??????????????? ADJUST_RIGHT=std::ios::right,
??????????????? ADJUST_INTERNAL=std::ios::internal
??????? };
??????? template
??????????????? format_stream & argWithFormat(typeInt nVal,
??????????????????????? INT_BASE nBase = BASE10,
??????????????????????? CHAR_CASE bUpcase = CHAR_UPCASE,
??????????????????????? POS_FLAG bShowPos = HIDE_POS,
??????????????????????? BASE_FLAG bShowBase = HIDE_BASE,
??????????????????????? ADJUSTFIELD_FLAG nAdjust= ADJUST_LEFT,
??????????????????????? int nWidth = -1,char chFill = ' ')
??????? {
??????????????? std::ios::fmtflags nFlags = getOutput().flags();
??????????????? getOutput().setf((std::ios::fmtflags)nBase, std::ios::basefield);
??????????????? if (bShowPos == SHOW_POS)
??????????????????????? getOutput().setf(std::ios::showpos);
??????????????? else
??????????????????????? getOutput().unsetf(std::ios::showpos);
??????????????? if (bUpcase == CHAR_UPCASE)
??????????????????????? getOutput().setf(std::ios::uppercase);
??????????????? else
??????????????????????? getOutput().unsetf(std::ios::uppercase);
??????????????? if (bShowBase == SHOW_BASE)
??????????????????????? getOutput().setf(std::ios::showbase);
??????????????? else
??????????????????????? getOutput().unsetf(std::ios::showbase);
??????????????? getOutput().setf((std::ios::fmtflags)nAdjust, std::ios::adjustfield);
??????????????? if (nWidth != -1)
??????????????????????? nWidth = getOutput().width(nWidth);
??????????????? chFill = getOutput().fill(chFill);
??????????????? getOutput() << static_cast(nVal);
??????????????? getOutput().flags(nFlags);
??????????????? if (nWidth != -1)
??????????????????????? getOutput().width(nWidth);
??????????????? getOutput().fill(chFill);
??????????????? return outputPrefix();
??????? }
??????? 還可以增加浮點數、字符串等等的格式處理。
???????????????
??? 5、現在輸入的格式字符串,在類里面只是使用const char * m_lpszFormat來保存,一旦傳入的
lpszFormat所指的資源已經被釋放,則會造成非法內存訪問。例如:
??? std::string???? getString(void) { ..... }
??? stream.format(getString().c_str());
??? stream.arg(...);
??? 如果要避免這種情況,應該在format_stream里面增加一個std::string成員,記錄字符串指針。
又或者應該寫成:
??? std::string strTemp = getString();
??? stream.format(strTemp.c_str());
??? stream.arg(...)
應該如何處理,還是看各人的習慣和需求了。
???????
??? 6、當輸入參數比占位符多的時候,則多余的參數都會輸出到格式字符串的后面。
??? 7、當輸入參數比占位符少的時候,輸出則停留在第一個多余的占位符的前面。
??? 8、對于字符串,可以使用stringstream來輔助:
??? stringstream str;
??? format_stream fmt(str);
??? fmt.format(".....").arg(...);
??? myFun(str.str());
??? 當然,經過適當的改造,可以使類更方便使用,這里就不再多說,各位高手自己發揮了。