📄 warsvrprotocolmsgs.h
字号:
/** */#ifndef WAR_SVR_PROTOCOL_MSGS_H#define WAR_SVR_PROTOCOL_MSGS_H/* SYSTEM INCLUDES */#ifndef WAR_VECTOR_INCLUDED# define WAR_VECTOR_INCLUDED# include <vector>#endif#ifndef WAR_OSTREAM_INCLUDED# define WAR_OSTREAM_INCLUDED# include <ostream>#endif/* PROJECT INCLUDES */#ifndef WAR_TYPES_H# include "WarTypes.h"#endif#ifndef WAR_EXCEPTION_H# include "WarException.h"#endif#ifndef WAR_LOG_H# include "WarLog.h"#endif/* LOCAL INCLUDES *//* FORWARD REFERENCES */#ifdef __cplusplusextern "C" {#endif/****************** BEGIN OLD STYLE C spesific ********//****************** END OLD STYLE C spesific **********/#ifdef __cplusplus }#endif/****************** BEGIN C++ spesific ****************/#ifdef __cplusplustemplate <class msgenumT>class WarSvrProtocolMsgs{public: typedef msgenumT enum_t; typedef std::vector<std::string> strings_t; // LIFECYCLE /** * Default constructor. */ WarSvrProtocolMsgs(void) : mStrMsg(mArraySize) { } WarSvrProtocolMsgs(const WarSvrProtocolMsgs& from) : mStrMsg(from.mStrMsg) { } // OPERATORS war_ccstr_t operator [] (int index) const throw(WarException) { if ((index < 0) || (index >= (int)mArraySize)) WarThrow(WarError(WAR_ERR_OUT_OF_RANGE), NULL); return mStrMsg[index].empty() ? msDefMsg[index] : mStrMsg[index].c_str(); } const WarSvrProtocolMsgs& operator = (const WarSvrProtocolMsgs& from) { mStrMsg = from.mStrMsg; return *this; } // OPERATIONS // ACCESS enum_t GetIndex(war_ccstr_t MacroName) const { // No need to optimize for speed... for(int i = 0; i < mArraySize; i++) { if (!strcmp(MacroName, msMacroName[i])) return (enum_t)i; } return mArraySize; } void Load(std::istream& in) { char buf[1024 * 4]; int line = 0; while(!in.bad()) { buf[0] = 0; in.getline(buf, sizeof(buf)); ++line; if (in.eof()) break; if (!*buf || (*buf == '#')) continue; char *p = strchr(buf, ' '); if (!p) continue; *p++ = 0; msgenumT index = GetIndex(buf); if (index != WarSvrProtocolMsgs::SM_INVALID) mStrMsg[GetIndex(buf)] = p; else { WarLog err_log(WARLOG_ERROR, "operator >> (std::istream& in, WarSvrProtocolMsgs&"); err_log << "Error in input stream at line " << line << ". Tag '" << buf << "' does not exist" << war_endl; } } } void Save(std::ostream& out) { for(int i = 0; i < msgenumT::INVALID; i++) out << msMacroName[i] << " " << operator [](i) << endl; } // INQUIRY protected: strings_t mStrMsg; static const war_ccstr_t *msDefMsg; static const war_ccstr_t *msMacroName; static const size_t mArraySize;private:};/* INLINE METHODS *//* EXTERNAL REFERENCES */template <class msgenumT>std::ostream& operator << (std::ostream& out, const WarSvrProtocolMsgs<msgenumT>& from){ from.Save(out); return out;}template <class msgenumT>std::istream& operator >> (std::istream& in, WarSvrProtocolMsgs<msgenumT>& from){ from.Load(in); return in;}#endif /* __cplusplus *//****************** END C++ spesific ******************/#endif /* WAR_SVR_PROTOCOL_MSGS_H_ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -