1 #ifndef __UTILITIES_HPP__     2 #define __UTILITIES_HPP__     5 #include "system_detection.inl"     7 #if _MSC_VER > 0 && _MSC_VER < 1201     8 #pragma warning( disable: 4786 )    11 #pragma warning( disable : 4996 )    42     #if defined(_MSC_VER) || defined(WIN32)    43         #pragma warning( disable: 4251 )    44         #pragma warning( disable: 4275 )    45         #ifdef  WINUX_DLL_EXPORTS    46             #define WINUX_DLL  __declspec(dllexport)    48             #define WINUX_DLL  __declspec(dllimport)    51         #define WINUX_API __stdcall    61 #define WINUX_FUNC_DECL(ret) WINUX_DLL ret WINUX_API    62 #define WINUX_FUNC_IMPL(ret) ret WINUX_API    65     #define countof(arr) ( sizeof(arr) / sizeof(arr[0]) )    70         #define TEXT(__x) L##__x    78 #define DISABLE_OBJECT_COPY( clsname ) private:\    79 clsname & operator = ( clsname const & ) = delete;\    80 clsname( clsname const & ) = delete;    83 #define CBufferToAnsiString( buf, size ) winux::AnsiString( (char const *)(buf), (size_t)(size) )    87 #define IF_PTR(ptr) if ( (ptr) != NULL ) (ptr)    89 #define ASSIGN_PTR(ptr) if ( (ptr) != NULL ) (*(ptr))    96 #define DEFINE_ATTR_MEMBER( ty, name, memname ) \    98     ty const & get##name() const { return this->##memname; }\    99     void set##name( ty const & v ) { this->##memname = v; }\   104 #define DEFINE_ATTR_MEMBER_READONLY( ty, name, memname ) \   106     ty const & get##name() const { return this->##memname; }\   111 #define DEFINE_ATTR( ty, name, getcode, setcode ) \   113     ty get##name() const { getcode; }\   114     void set##name( ty const & _VAL_ ) { setcode; }   117 #define DEFINE_ATTR_READONLY( ty, name, getcode ) \   119     ty get##name() const { getcode; }   123 #define GCC_VERSION_GREAT_THAN(Major,Minor,Patchlevel) \   124 ( __GNUC__ > Major || ( __GNUC__ == Major && ( __GNUC_MINOR__ > Minor || ( __GNUC_MINOR__ == Minor && __GNUC_PATCHLEVEL__ > Patchlevel ) ) ) )   139 typedef wchar_t wchar;
   140 typedef unsigned __int64 
uint64;
   142 typedef __int64 
int64;
   164 typedef std::basic_string<tchar> 
String;
   184 template < 
typename _MAP, 
typename _KEY >
   185 inline bool isset( _MAP 
const & m, _KEY 
const & k )
   187     return m.find(k) != m.end();
   191 template < 
typename _Ty >
   192 std::vector<_Ty> 
ToArray( _Ty * arr, uint count )
   194     return std::vector<_Ty>( arr, arr + count );
   197 template < 
typename _Ty, u
int _N >
   200     return std::vector<_Ty>( arr, arr + _N );
   206 template < 
typename _Fx, 
typename... _ArgType >
   209     fn( std::forward<_ArgType>(arg)... );
   214 template < u
int64 n > 
struct Bin0   220     enum : uint64 { 
val = 0 };
   224 #define BinVal(x) winux::Bin0<0##x>::val   227 template < 
typename _Ty >
   236     operator ParamTypeRef () { 
return _r; }
   240 template < 
typename _Ty >
   247 template < 
size_t... _Index >
   251 template < 
size_t _Num, 
typename _IdxSeq = IndexSequence<> >
   254 template < 
size_t _Num, 
size_t... _Index >
   257 template < 
size_t... _Index >
   264 #include "func_traits.inl"   267 #include "func_runable.inl"   270 #include "func_invoker.inl"   273 template < 
typename _PfnType, _PfnType pfn >
   276     template < 
typename... _ArgType >
   277     static typename FuncTraits<_PfnType>::ReturnType 
func( _ArgType&& ...arg )
   279         return (*pfn)( std::forward<_ArgType>(arg)... );
   284 template < 
typename _KTy, 
typename _VTy >
   287     std::map< _KTy, _VTy > * _m;
   296     operator std::map< _KTy, _VTy > & () { 
return *_m; }
   300 template < 
typename _Ty >
   303     std::vector<_Ty> * _a;
   311     operator std::vector<_Ty> & () { 
return *_a; }
   315 template < 
typename _KTy, 
typename _VTy >
   321 template < 
typename _Ty >
   332 template < 
typename _TargetCls >
   345         if ( &other != 
this )
   347             *_data = *other._data;
   352 #ifndef MOVE_SEMANTICS_DISABLED   356         other._data = 
nullptr;
   361         if ( &other != 
this )
   365             other._data = 
nullptr;
   376             delete (_TargetCls *)_data;
   382     template < 
typename... _ArgType >
   386         _data = 
new _TargetCls( std::forward<_ArgType>(arg)... );
   389     _TargetCls * operator -> ()
   393     _TargetCls 
const * operator -> ()
 const   397     operator _TargetCls & ()
   401     operator _TargetCls 
const & () 
const   405     operator bool()
 const   407         return _data != 
nullptr;
   420     Error( 
int errType, AnsiString 
const & errStr ) 
throw() : _errType(errType), _errStr(errStr) { }
   423     virtual char const * 
what() 
const throw() { 
return _errStr.c_str(); }
   444     Buffer( 
void * buf, uint size, 
bool isPeek = 
false );
   448     Buffer( AnsiString 
const & data, 
bool isPeek = 
false );
   453 #ifndef MOVE_SEMANTICS_DISABLED   465     void setBuf( 
void * buf, uint size, uint capacity, 
bool isPeek );
   468     void setBuf( 
void * buf, uint size, 
bool isPeek ) { this->
setBuf( buf, size, size, isPeek ); }
   471     void alloc( uint capacity, 
bool setDataSize = 
true );
   476     void realloc( uint newCapacity );
   479     bool peekCopy( 
bool copyCapacity = 
false );
   487     template < 
typename _Ty >
   488     _Ty * 
getBuf()
 const { 
return reinterpret_cast<_Ty *
>(_buf); }
   497     void _setSize( uint dataSize ) { _dataSize = ( dataSize > _capacity ? _capacity : dataSize ); }
   503     operator bool()
 const { 
return _buf != NULL; }
   506     template < 
typename _ChTy >
   509         typedef typename std::basic_string<_ChTy>::value_type CharType;
   510         return std::basic_string<_ChTy>( (CharType*)_buf, _dataSize / 
sizeof(CharType) );
   513     AnsiString 
toAnsi()
 const { 
return this->toString<AnsiString::value_type>(); }
   515     UnicodeString 
toUnicode()
 const { 
return this->toString<UnicodeString::value_type>(); }
   518     static void * _Alloc( uint size );
   519     static void * _Realloc( 
void * p, uint newSize );
   520     static void _Free( 
void * p );
   541 #ifndef MOVE_SEMANTICS_DISABLED   553     void append( 
void const * data, uint size );
   556     void append( AnsiString 
const & data ) { this->
append( data.c_str(), (
uint)data.size() ); }
   562     void erase( uint start, uint count = (uint)-1 );
   582     MixedError( 
int errType, AnsiString 
const & errStr ) 
throw() : 
Error( errType, errStr ) { }
   591     #define MixedType_ENUM_ITEM(item) item,   592     #define MixedType_ENUM_ITEMSTRING(item) #item,   593     #define MixedType_ENUM_ITEMLIST(_)\   597         _(MT_SHORT) _(MT_USHORT)\   598         _(MT_INT) _(MT_UINT)\   599         _(MT_LONG) _(MT_ULONG)\   600         _(MT_INT64) _(MT_UINT64)\   601         _(MT_FLOAT) _(MT_DOUBLE)\   602         _(MT_ANSI) _(MT_UNICODE)\   607     MixedType_ENUM_ITEMLIST(MixedType_ENUM_ITEM)   611     static String 
const & TypeString( MixedType type );
   616         bool operator () ( 
Mixed const & v1, 
Mixed const & v2 ) 
const;
   643                 UnicodeString * _pWStr;
   649             MixedMixedMap * _pMap;
   659     Mixed( AnsiString 
const & str ); 
   660     Mixed( UnicodeString 
const & str ); 
   661     Mixed( 
char const * str, 
int len = -1 ); 
   662     Mixed( wchar 
const * str, 
int len = -1 ); 
   664     Mixed( 
bool boolVal );
   666     Mixed( 
short shVal );
   667     Mixed( ushort ushVal );
   671     Mixed( ulong ulVal );
   672     Mixed( 
float fltVal );
   673     Mixed( int64 i64Val );
   674     Mixed( uint64 ui64Val );
   675     Mixed( 
double dblVal );
   678     Mixed( 
void * binaryData, uint size, 
bool isPeek = 
false );
   684     template < 
typename _Ty >
   685     Mixed( std::vector<_Ty> 
const & arr )
   688         this->_type = MT_ARRAY;
   691         for ( i = 0; i < arr.size(); ++i )
   693             this->_pArr->at(i) = arr[i];
   697 #if defined(__GNUC__) || _MSC_VER > 1200   698     template < 
typename _Ty, u
int n >
   699     Mixed( _Ty (&arr)[n] )
   702         this->_type = MT_ARRAY;
   705         for ( i = 0; i < n; ++i )
   707             this->_pArr->at(i) = arr[i];
   713     template < 
typename _KTy, 
typename _VTy, 
typename _Pr, 
typename _Alloc >
   714     Mixed( std::map< _KTy, _VTy, _Pr, _Alloc > 
const & m )
   717         this->assign< _KTy, _VTy, _Pr, _Alloc >(m);
   720 #if defined(__GNUC__) || _MSC_VER > 1200   721     template < 
typename _KTy, 
typename _VTy, u
int count >
   722     Mixed( std::pair< _KTy, _VTy > (&pairs)[count] )
   725         this->assign< _KTy, _VTy, count >(pairs);
   728     template < 
typename _KTy, 
typename _VTy, u
int count >
   729     Mixed( _KTy (&keys)[count], _VTy (&vals)[count] )
   732         this->assign< _KTy, _VTy, count >( keys, vals );
   744 #ifndef MOVE_SEMANTICS_DISABLED   751     void assign( 
Buffer && buf );
   762     MixedType type()
 const { 
return this->_type; }
   764     String 
const & typeString()
 const { 
return TypeString(this->_type); }
   767     #include "mixed_ref_specified_type.inl"   773     operator bool() 
const;
   774     operator byte() 
const;
   775     operator short() 
const;
   777     operator int() 
const;
   778     operator uint() 
const;
   779     operator long() 
const;
   780     operator ulong() 
const;
   781     operator float() 
const;
   782     operator int64() 
const;
   784     operator double() 
const;
   786     AnsiString toAnsi()
 const { 
return this->
operator AnsiString(); }
   789     bool toBool()
 const { 
return this->
operator bool(); }
   791     short toShort()
 const { 
return this->
operator short(); }
   793     int toInt()
 const { 
return this->
operator int(); }
   795     long toLong()
 const { 
return this->
operator long(); }
   797     float toFloat()
 const { 
return this->
operator float(); }
   800     double toDouble()
 const { 
return this->
operator double(); }
   803     bool operator == ( 
Mixed const & other ) 
const;
   804     bool operator < ( 
Mixed const & other ) 
const;
   805     bool operator != ( 
Mixed const & other )
 const { 
return !this->operator == (other); }
   806     bool operator > ( 
Mixed const & other )
 const { 
return !this->operator <= (other); }
   807     bool operator >= ( 
Mixed const & other )
 const { 
return !this->operator < (other); }
   808     bool operator <= ( 
Mixed const & other )
 const { 
return this->operator < (other) || this->operator == (other); }
   811     bool isNull()
 const { 
return this->_type == MT_NULL; }
   812     bool isArray()
 const { 
return this->_type == MT_ARRAY; }
   813     bool isCollection()
 const { 
return this->_type == MT_COLLECTION; }
   814     bool isContainer()
 const { 
return this->_type == MT_ARRAY || this->_type == MT_COLLECTION; }
   815     bool isBinary()
 const { 
return this->_type == MT_BINARY; }
   816     bool isNumeric()
 const { 
return this->_type > MT_NULL && this->_type < MT_ANSI; }
   817     bool isInteger()
 const { 
return this->isNumeric() && this->_type != MT_FLOAT && this->_type != MT_DOUBLE; }
   818     bool isAnsi()
 const { 
return this->_type == MT_ANSI; }
   819     bool isUnicode()
 const { 
return this->_type == MT_UNICODE; }
   820     bool isString()
 const { 
return this->_type == MT_ANSI || this->_type == MT_UNICODE; }
   824     Mixed & createString();
   826     Mixed & createUnicode();
   828     Mixed & createArray( uint count = 0 );
   830     Mixed & createCollection();
   832     Mixed & createBuffer( uint size = 0 );
   837     template < 
typename _Ty >
   838     int getArray( std::vector<_Ty> * arr )
 const   841         MixedArray::const_iterator it;
   842         for ( it = this->_pArr->begin(); it != this->_pArr->end(); ++it )
   844         return (
int)arr->size();
   848     template < 
typename _KTy >
   849     int getKeys( std::vector<_KTy> * keys )
 const   852         MixedArray::const_iterator it;
   853         for ( it = this->_pArr->begin(); it != this->_pArr->end(); ++it )
   854             keys->push_back(*it);
   859     template < 
typename _KTy, 
typename _VTy >
   860     int getMap( std::map< _KTy, _VTy > * m )
 const   863         MixedMixedMap::const_iterator it;
   864         for ( it = this->_pMap->begin(); it != this->_pMap->end(); ++it )
   865             (*m)[(_KTy)it->first] = (_VTy)it->second;
   870     bool isEmpty()
 const { 
return this->getCount() == 0; }
   875     int getCount() 
const;
   880     Mixed const & operator [] ( 
Mixed const & k ) 
const;
   882     template < 
typename _ChTy >
   883     Mixed & operator [] ( _ChTy 
const * k ) { 
return this->operator[]( 
Mixed(k) ); }
   885     template < 
typename _ChTy >
   886     Mixed const & operator [] ( _ChTy 
const * k )
 const { 
return this->operator[]( 
Mixed(k) ); }
   889     template < 
typename _Ty >
   890     _Ty 
get( 
Mixed const & k ) 
const { 
return (_Ty)this->operator [] (k); }
   893     MixedMixedMap::value_type & getPair( 
int i );
   895     MixedMixedMap::value_type 
const & getPair( 
int i ) 
const;
   903         operator Mixed & () { 
return *_mx; }
   920         operator Mixed & () { 
return *_mx; }
   929     int add( 
Mixed const & v );
   932     int addUnique( 
Mixed const & v );
   935     void del( 
Mixed const & k );
   940     bool has( 
Mixed const & ek ) 
const;
   949     void alloc( uint size );
   952     bool peekCopy( 
bool copyCapacity = 
false );
   958     void * getBuf() 
const;
   961     void assign( 
char const * str, 
int len = -1 );
   962     void assign( wchar 
const * str, 
int len = -1 );
   963     void assign( 
bool boolVal );
   964     void assign( byte btVal );
   965     void assign( 
short shVal );
   966     void assign( ushort ushVal );
   967     void assign( 
int iVal );
   968     void assign( uint uiVal );
   969     void assign( 
long lVal );
   970     void assign( ulong ulVal );
   971     void assign( 
float fltVal );
   972     void assign( int64 i64Val );
   973     void assign( uint64 ui64Val );
   974     void assign( 
double dblVal );
   976     void assign( 
Buffer const & buf );
   978     void assign( 
void * binaryData, uint size, 
bool isPeek = 
false );
   980     void assign( 
Mixed * arr, uint count );
   982     template < 
typename _Ty >
   983     void assign( std::vector<_Ty> 
const & arr )
   986         this->_type = MT_ARRAY;
   989         for ( i = 0; i < arr.size(); ++i )
   991             this->_pArr->at(i) = arr[i];
   995 #if defined(__GNUC__) || _MSC_VER > 1200   996     template < 
typename _Ty, u
int n >
   997     void assign( _Ty (&arr)[n] )
  1000         this->_type = MT_ARRAY;
  1003         for ( i = 0; i < n; ++i )
  1005             this->_pArr->at(i) = arr[i];
  1011     template < 
typename _KTy, 
typename _VTy, 
typename _Pr, 
typename _Alloc >
  1012     void assign( std::map< _KTy, _VTy, _Pr, _Alloc > 
const & m )
  1015         this->_type = MT_COLLECTION;
  1017         this->_pMap = 
new MixedMixedMap();
  1018         typename std::map< _KTy, _VTy, _Pr, _Alloc >::const_iterator it;
  1019         for ( it = m.begin(); it != m.end(); ++it )
  1021             this->_pArr->push_back(it->first);
  1022             (*this->_pMap)[it->first] = it->second;
  1027 #if defined(__GNUC__) || _MSC_VER > 1200  1029     template < 
typename _KTy, 
typename _VTy, u
int count >
  1030     void assign( std::pair< _KTy, _VTy > (&pairs)[count] )
  1033         this->_type = MT_COLLECTION;
  1035         this->_pMap = 
new MixedMixedMap();
  1037         for ( i = 0; i < count; ++i )
  1039             this->_addUniqueKey(pairs[i].first);
  1040             (*this->_pMap)[pairs[i].first] = pairs[i].second;
  1045     template < 
typename _KTy, 
typename _VTy, u
int count >
  1046     void assign( _KTy (&keys)[count], _VTy (&vals)[count] )
  1049         this->_type = MT_COLLECTION;
  1051         this->_pMap = 
new MixedMixedMap();
  1053         for ( i = 0; i < count; ++i )
  1055             this->_addUniqueKey(keys[i]);
  1056             (*this->_pMap)[keys[i]] = vals[i];
  1061     String json() 
const;
  1062     String myJson() 
const;
  1063     Mixed & json( String 
const & jsonStr );
  1067     static bool ParseBool( AnsiString 
const & str, 
bool * boolVal );
  1068     static bool ParseBool( UnicodeString 
const & str, 
bool * boolVal );
  1070     static bool ParseULong( AnsiString 
const & str, ulong * ulVal );
  1071     static bool ParseULong( UnicodeString 
const & str, ulong * ulVal );
  1073     static bool ParseDouble( AnsiString 
const & str, 
double * dblVal );
  1074     static bool ParseDouble( UnicodeString 
const & str, 
double * dblVal );
  1076     static bool ParseUInt64( AnsiString 
const & str, uint64 * ui64Val );
  1077     static bool ParseUInt64( UnicodeString 
const & str, uint64 * ui64Val );
  1080     static Mixed & ParseJson( AnsiString 
const & str, 
Mixed * val );
  1085     void _addUniqueKey( 
Mixed const & k )
  1087         if ( this->_pMap->find(k) == this->_pMap->end() )
  1088             this->_pArr->push_back(k);
  1101 #endif // __UTILITIES_HPP__ 
void * getBuf() const 
暴露缓冲区指针 
 
AnsiString toAnsi() const 
转换到AnsiString 
 
std::map< String, Mixed > StringMixedMap
 
MapAssigner< _KTy, _VTy > Assign(std::map< _KTy, _VTy > *m)
给容器赋值 
 
void create(_ArgType &&...arg)
必须在使用者类的构造函数里第一个调用 
 
std::basic_string< char > AnsiString
 
int VoidReturnInt(_Fx fn, _ArgType &&...arg)
调用一个返回void的函数或函数对象,返回一个数字 
 
MembersWrapper(MembersWrapper &&other)
 
void setBuf(void *buf, uint size, bool isPeek)
设置缓冲区,当isPeek为false时拷贝数据缓冲区 
 
std::pair< String, Mixed > StringMixedPair
 
std::basic_string< _ChTy > toString() const 
转换到字符串 
 
bool ValueIsInArray(StringArray const &arr, String const &val, bool caseInsensitive=false)
判断一个字符串值是否在一个字符串数组里,默认大小写敏感 
 
std::map< String, String > StringStringMap
 
void destroy()
必须在使用者类的析构函数里最后一个调用 
 
std::ostream & operator<<(std::ostream &o, ConsoleAttrT< _VarType > const &tr)
 
std::vector< String > StringArray
 
uint getCapacity() const 
获取容量大小 
 
int Random(int n1, int n2)
随机数,随机产生n1~n2的数字. 包括n1,n2本身 
 
uint getSize() const 
获取数据大小 
 
std::vector< Mixed > MixedArray
 
缓冲区,表示内存中一块2进制数据(利用malloc/realloc进行内存分配) 
 
std::map< Mixed, Mixed, MixedLess > MixedMixedMap
 
void _setSize(uint dataSize)
设置数据大小,不能超过容量大小(不建议外部调用) 
 
MixedMixedMap::value_type MixedMixedPair
 
std::basic_string< char16 > UnicodeString16
 
bool isset(_MAP const &m, _KEY const &k)
检测map中是否有该键的值 
 
MapAssigner(std::map< _KTy, _VTy > *m)
 
MixedError(int errType, AnsiString const &errStr)
 
#define WINUX_FUNC_DECL(ret)
 
void append(AnsiString const &data)
添加数据 
 
virtual char const * what() const 
 
MapAssigner & operator()(_KTy const &k, _VTy const &v)
 
std::vector< _Ty > ToArray(_Ty *arr, uint count)
将C数组转换成vector 
 
void append(Buffer const &data)
添加数据 
 
UnicodeString toUnicode() const 
转换到UnicodeString 
 
Error(int errType, AnsiString const &errStr)
 
virtual int getErrType() const 
 
static FuncTraits< _PfnType >::ReturnType func(_ArgType &&...arg)
 
unsigned long long ulonglong
 
unsigned long long uint64
 
RefParam< _Ty > Ref(_Ty &r)
向模板参数传递引用型参数 
 
std::pair< String, String > StringStringPair
 
std::basic_string< char32 > UnicodeString32
 
std::basic_string< tchar > String
 
std::basic_string< wchar > UnicodeString
 
ArrayAssigner(std::vector< _Ty > *a)