(C/C++)基于SharpUI控件库的插件式框架开发--第三篇框架基础服务库
一个框架基础的东西,一般也是操作的最基础的类,比如char、int、bool等,有时出现内存泄露的问题导致错误的抛出,但是C++开发有的时候就算是抛出异常,那也是靠经验来积累才能非常快速准确的找出错误所在,这就需要在框架中需要添加日志管理的接口,日志管理的好处就是开发者自身在找异常时提供参考,另一个就是如果用户操作时出现问题,也可将日志反馈,帮助快速解决问题;总之了为了更好的扩展完善我的框架,我详细列一下这个基础服务库(XPCore)包含内容:
- 虽说sharpui控件库内封闭好string类,但是不够满足需求,我就新定义了xstring类,这个字条串类中,涉及常用的所有操作,比如:查找、去空、大小写转换、取指定的字符串、连接、操作符运算、编码转换、与其他基础类间的转换、删除、替换等等;头文件如下:
1 //xstring 常用编码格式 2 enum XPCORE_API XEncodings 3 { 4 //默认编码 5 encoding_gb2312=0, 6 //wchar_t 7 encoding_wchart=1, 8 //Unicode 9 encoding_unicode=2, 10 //Unicode 的一种表达形式 11 encoding_utf8=3 12 13 }; 14 15 //自定义字符串类 xstring的最大长度 16 #define _xstring_max_Length_ 4096 17 //自定义字符串类 默认编码为GB2312,支持与Unicode wchar_t Utf-8转换 18 class XPCORE_API xstring:implements XPCore::Object 19 { 20 public: 21 xstring(); 22 xstring(const char* value); 23 xstring(const wchar_t* value); 24 xstring(const xstring &other); 25 ~xstring(); 26 27 const char* c_str() const; 28 char* c_str(); 29 30 const wchar_t* w_str() const; 31 wchar_t* w_str(); 32 33 int Lendth() const; 34 bool Empty() const; 35 36 XEncodings Encoding() const; 37 void SetEncoding(int encoding); 38 39 bool StartsWith(const xstring& value) const; 40 bool EndsWith(const xstring& value) const; 41 bool Contains(const xstring& value,int &startIndex) const; 42 bool StartsWith(const char value) const; 43 bool EndsWith(const char value) const; 44 bool Contains(const char value,int &startIndex) const; 45 bool Equals(const xstring& value) const; 46 47 static xstring FromInt(int value); 48 static xstring FromFloat(float value,int numofDigits=10); 49 static xstring FromBool(bool value); 50 51 int ToInt() const; 52 float ToFloat() const; 53 bool ToBool() const; 54 55 int IndexOf(char value, int startIndex) const; 56 int IndexOf(const xstring& value) const; 57 int IndexOf(const xstring& value, int startIndex) const; 58 int IndexOf(char value, int startIndex, int count) const; 59 int IndexOf(const xstring& value, int startIndex, int count) const; 60 61 bool Remove(int startIndex); 62 bool Remove(int startIndex, int count); 63 bool Replace(char oldChar, char newChar); 64 bool Replace(const xstring& oldValue, const xstring& newValue); 65 66 xstring Insert(int startIndex, const xstring& insertValue); 67 68 xstring Substring(int startIndex) const; 69 xstring Substring(int startIndex, int length) const; 70 xstring Substring(int startIndex,int count, char sz) const; 71 72 xstring ToLower(); 73 xstring ToString(); 74 xstring ToUpper(); 75 xstring Trim(); 76 xstring TrimLeft(); 77 xstring TrimRight(); 78 79 bool IsDigital() const; 80 bool IsBool() const; 81 bool IsCharacter() const; 82 83 void Clear(); 84 85 char& operator[](int index); 86 const char& operator[](int index) const; 87 88 xstring operator=(const xstring &other); 89 xstring operator=(const char* val); 90 xstring operator=(const wchar_t* val); 91 xstring operator+(const xstring &other); 92 void operator+=(const xstring &other); 93 bool operator==(const xstring &other); 94 bool operator>(const xstring &other); 95 bool operator<(const xstring &other); 96 ///////////////////////////////////////////////// 97 98 static xstring FromUtf8(const xstring &other); 99 static xstring FromUnicode(const xstring &other); 100 static xstring FromGB2312(const xstring &other); 101 102 xstring ToUtf8(); 103 xstring ToUnicode(); 104 xstring ToGB2312(); 105 protected: 106 char* _ptr; 107 int _encoding; 108 };
xstring xstring::ToUtf8() { char* _out=new char[_xstring_max_Length_]; code_convert("GB2312","UTF-8",_ptr,strlen(_ptr),_out,_xstring_max_Length_); xstring _temStr=_out; _temStr.SetEncoding(XEncodings::encoding_utf8); return _temStr; }