fastdo  0.5.12
utilities.hpp
浏览该文件的文档.
1 #ifndef __UTILITIES_HPP__
2 #define __UTILITIES_HPP__
3 
4 // 各平台条件编译宏检测
5 #include "system_detection.inl"
6 
7 #if _MSC_VER > 0 && _MSC_VER < 1201
8 #pragma warning( disable: 4786 )
9 #endif
10 #if _MSC_VER > 0
11 #pragma warning( disable : 4996 )
12 #endif
13 
14 #include <string>
15 #include <sstream>
16 #include <vector>
17 #include <map>
18 #include <tuple>
19 #include <queue>
20 #include <functional>
21 #include <algorithm>
22 #include <assert.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #if defined(OS_WIN)
27 #include <windows.h>
28 #endif
29 
31 namespace winux
32 {
33 // 一些宏定义 ----------------------------------------------------------------------------------
34 /* Dll相关宏定义:
35  WINUX_DLL_USE - 此宏开关表示有DLL参与,包括生成DLL或者导入DLL,不定义此宏和一般的使用源码没区别
36  WINUX_DLL_EXPORTS - 此宏开关表示是生成DLL(dllexport)还是导入DLL(dllimport),linux平台下忽略
37  WINUX_DLL - 标记函数、类、变量,用于标明其要从DLL导出还是导入,linux平台下忽略
38  WINUX_API - 标记函数调用约定,Win下Dll参与时为stdcall,否则为空白默认,linux平台下忽略
39  WINUX_FUNC_DECL - 标记函数声明
40  WINUX_FUNC_IMPL - 标记函数实现 */
41 #ifdef WINUX_DLL_USE
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)
47  #else
48  #define WINUX_DLL __declspec(dllimport)
49  #endif
50 
51  #define WINUX_API __stdcall
52  #else
53  #define WINUX_DLL
54  #define WINUX_API
55  #endif
56 #else
57  #define WINUX_DLL
58  #define WINUX_API
59 #endif
60 
61 #define WINUX_FUNC_DECL(ret) WINUX_DLL ret WINUX_API
62 #define WINUX_FUNC_IMPL(ret) ret WINUX_API
63 
64 #ifndef countof
65  #define countof(arr) ( sizeof(arr) / sizeof(arr[0]) )
66 #endif
67 
68 #ifndef TEXT
69  #ifdef UNICODE
70  #define TEXT(__x) L##__x
71  #else
72  #define TEXT(__x) __x
73  #endif
74 #endif
75 
76 // 禁止类的对象赋值/拷贝构造
77 // DISABLE_OBJECT_COPY
78 #define DISABLE_OBJECT_COPY( clsname ) private:\
79 clsname & operator = ( clsname const & ) = delete;\
80 clsname( clsname const & ) = delete;
81 
82 // C语言缓冲区转换为AnsiString二进制串
83 #define CBufferToAnsiString( buf, size ) winux::AnsiString( (char const *)(buf), (size_t)(size) )
84 
85 // 如果指针非NULL
86 // IF_PTR
87 #define IF_PTR(ptr) if ( (ptr) != NULL ) (ptr)
88 // ASSIGN_PTR
89 #define ASSIGN_PTR(ptr) if ( (ptr) != NULL ) (*(ptr))
90 
91 // 不使用一个变量
92 // UNUSED
93 #define UNUSED(s)
94 
95 // 给类添加一个属性,和相关的数据成员,自动添加get/set方法
96 #define DEFINE_ATTR_MEMBER( ty, name, memname ) \
97 public:\
98  ty const & get##name() const { return this->##memname; }\
99  void set##name( ty const & v ) { this->##memname = v; }\
100 private:\
101  ty memname;
102 
103 // 给类添加一个只读属性,和相关的数据成员,自动添加get方法
104 #define DEFINE_ATTR_MEMBER_READONLY( ty, name, memname ) \
105 public:\
106  ty const & get##name() const { return this->##memname; }\
107 private:\
108  ty memname;
109 
110 // 给类添加一个属性,自动添加get/set方法
111 #define DEFINE_ATTR( ty, name, getcode, setcode ) \
112 public:\
113  ty get##name() const { getcode; }\
114  void set##name( ty const & _VAL_ ) { setcode; }
115 
116 // 给类添加一个只读属性,自动添加get方法
117 #define DEFINE_ATTR_READONLY( ty, name, getcode ) \
118 public:\
119  ty get##name() const { getcode; }
120 
121 
122 // 判断GCC版本大于给定版本
123 #define GCC_VERSION_GREAT_THAN(Major,Minor,Patchlevel) \
124 ( __GNUC__ > Major || ( __GNUC__ == Major && ( __GNUC_MINOR__ > Minor || ( __GNUC_MINOR__ == Minor && __GNUC_PATCHLEVEL__ > Patchlevel ) ) ) )
125 
126 // 基本类型 -----------------------------------------------------------------------------------
127 typedef int int32;
128 typedef unsigned int uint, uint32;
129 typedef unsigned long ulong;
130 typedef short int16;
131 typedef unsigned short ushort, uint16;
132 typedef char int8;
133 typedef unsigned char uint8;
134 
135 typedef char16_t char16;
136 typedef char32_t char32;
137 
138 #if defined(CL_VC)
139 typedef wchar_t wchar;
140 typedef unsigned __int64 uint64;
141 typedef unsigned __int64 ulonglong;
142 typedef __int64 int64;
143 typedef __int64 longlong;
144 #else
145 typedef wchar_t wchar;
146 typedef unsigned long long uint64;
147 typedef unsigned long long ulonglong;
148 typedef long long int64;
149 typedef long long longlong;
150 #endif
151 
152 //#ifdef UNICODE
153 //typedef wchar tchar;
154 //#else
155 typedef char tchar;
156 //#endif
157 
158 #ifndef byte
159 typedef unsigned char byte;
160 #endif
161 
162 class Mixed;
163 // STL wrappers
164 typedef std::basic_string<tchar> String;
165 typedef std::basic_string<char> AnsiString;
166 typedef AnsiString LocalString;
167 typedef std::basic_string<wchar> UnicodeString;
168 typedef std::basic_string<char16> UnicodeString16;
169 typedef std::basic_string<char32> UnicodeString32;
170 
171 typedef std::vector<String> StringArray;
172 typedef std::map<String, String> StringStringMap;
173 typedef std::pair<String, String> StringStringPair;
174 
175 typedef std::vector<Mixed> MixedArray;
176 typedef std::map<String, Mixed> StringMixedMap;
177 typedef std::pair<String, Mixed> StringMixedPair;
178 //typedef std::map<Mixed, Mixed> MixedMixedMap;
179 //typedef std::pair<Mixed, Mixed> MixedMixedPair;
180 
181 // 模板元编程支持 ---------------------------------------------------------------------------
182 
184 template < typename _MAP, typename _KEY >
185 inline bool isset( _MAP const & m, _KEY const & k )
186 {
187  return m.find(k) != m.end();
188 }
189 
191 template < typename _Ty >
192 std::vector<_Ty> ToArray( _Ty * arr, uint count )
193 {
194  return std::vector<_Ty>( arr, arr + count );
195 }
196 
197 template < typename _Ty, uint _N >
198 std::vector<_Ty> ToArray( _Ty (&arr)[_N] )
199 {
200  return std::vector<_Ty>( arr, arr + _N );
201 }
202 
206 template < typename _Fx, typename... _ArgType >
207 int VoidReturnInt( _Fx fn, _ArgType&& ...arg )
208 {
209  fn( std::forward<_ArgType>(arg)... );
210  return 1;
211 }
212 
214 template < uint64 n > struct Bin0
215 {
216  enum : uint64 { val = ( Bin0< n / 8 >::val << 1 ) + n % 8 };
217 };
218 template <> struct Bin0<0>
219 {
220  enum : uint64 { val = 0 };
221 };
222 
223 // 二进制数 macro包装
224 #define BinVal(x) winux::Bin0<0##x>::val
225 
226 // 引用参数包装
227 template < typename _Ty >
228 class RefParam
229 {
230  _Ty & _r;
231 public:
232  typedef _Ty ParamType;
233  typedef _Ty & ParamTypeRef;
234 
235  explicit RefParam( ParamTypeRef r ) : _r(r) { }
236  operator ParamTypeRef () { return _r; }
237 };
238 
240 template < typename _Ty >
241 RefParam<_Ty> Ref( _Ty & r )
242 {
243  return RefParam<_Ty>(r);
244 }
245 
247 template < size_t... _Index >
248 struct IndexSequence { };
249 
250 // 构建一个IndexSequence< 0, 1, 2, ..., _Num - 1 >
251 template < size_t _Num, typename _IdxSeq = IndexSequence<> >
253 
254 template < size_t _Num, size_t... _Index >
255 struct MakeIndexSequence< _Num, IndexSequence<_Index...> > : MakeIndexSequence< _Num - 1, IndexSequence< _Index..., sizeof...(_Index) > > { };
256 
257 template < size_t... _Index >
258 struct MakeIndexSequence< 0, IndexSequence<_Index...> >
259 {
260  using Type = IndexSequence<_Index...>;
261 };
262 
264 #include "func_traits.inl"
265 
267 #include "func_runable.inl"
268 
270 #include "func_invoker.inl"
271 
273 template < typename _PfnType, _PfnType pfn >
275 {
276  template < typename... _ArgType >
277  static typename FuncTraits<_PfnType>::ReturnType func( _ArgType&& ...arg )
278  {
279  return (*pfn)( std::forward<_ArgType>(arg)... );
280  }
281 };
282 
284 template < typename _KTy, typename _VTy >
286 {
287  std::map< _KTy, _VTy > * _m;
288 public:
289  MapAssigner( std::map< _KTy, _VTy > * m ) : _m(m) { }
290  MapAssigner & operator()( _KTy const & k, _VTy const & v )
291  {
292  (*_m)[k] = v;
293  //_m->insert( std::pair< _KTy, _VTy >( k, v ) );
294  return *this;
295  }
296  operator std::map< _KTy, _VTy > & () { return *_m; }
297 };
298 
300 template < typename _Ty >
302 {
303  std::vector<_Ty> * _a;
304 public:
305  ArrayAssigner( std::vector<_Ty> * a ) : _a(a) { }
306  ArrayAssigner & operator () ( _Ty const & v )
307  {
308  _a->push_back(v);
309  return *this;
310  }
311  operator std::vector<_Ty> & () { return *_a; }
312 };
313 
315 template < typename _KTy, typename _VTy >
316 MapAssigner< _KTy, _VTy > Assign( std::map< _KTy, _VTy > * m )
317 {
318  return MapAssigner< _KTy, _VTy >(m);
319 }
321 template < typename _Ty >
322 ArrayAssigner<_Ty> Assign( std::vector<_Ty> * a )
323 {
324  return ArrayAssigner<_Ty>(a);
325 }
326 
332 template < typename _TargetCls >
334 {
335 private:
336  _TargetCls * _data;
337 
338  MembersWrapper( MembersWrapper const & other ) = delete;
339 public:
340  MembersWrapper() : _data(nullptr) { }
341 
343  MembersWrapper & operator = ( MembersWrapper const & other )
344  {
345  if ( &other != this )
346  {
347  *_data = *other._data;
348  }
349  return *this;
350  }
351 
352 #ifndef MOVE_SEMANTICS_DISABLED
354  {
355  _data = other._data;
356  other._data = nullptr;
357  }
359  MembersWrapper & operator = ( MembersWrapper && other )
360  {
361  if ( &other != this )
362  {
363  this->destroy();
364  _data = other._data;
365  other._data = nullptr;
366  }
367  return *this;
368  }
369 #endif
370 
372  void destroy()
373  {
374  if ( _data )
375  {
376  delete (_TargetCls *)_data;
377  _data = nullptr;
378  }
379  }
380 
382  template < typename... _ArgType >
383  void create( _ArgType&&... arg )
384  {
385  this->destroy();
386  _data = new _TargetCls( std::forward<_ArgType>(arg)... );
387  }
388 
389  _TargetCls * operator -> ()
390  {
391  return _data;
392  }
393  _TargetCls const * operator -> () const
394  {
395  return _data;
396  }
397  operator _TargetCls & ()
398  {
399  return *_data;
400  }
401  operator _TargetCls const & () const
402  {
403  return *_data;
404  }
405  operator bool() const
406  {
407  return _data != nullptr;
408  }
409 };
410 
411 // ----------------------------------------------------------------------------------------
413 class WINUX_DLL Error : public std::exception
414 {
415 private:
416  int _errType;
417  AnsiString _errStr;
418 public:
419  Error() throw() : _errType(0) { }
420  Error( int errType, AnsiString const & errStr ) throw() : _errType(errType), _errStr(errStr) { }
421  virtual ~Error() throw() { }
422  virtual int getErrType() const throw() { return _errType; }
423  virtual char const * what() const throw() { return _errStr.c_str(); }
424 };
425 
427 WINUX_FUNC_DECL(bool) ValueIsInArray( StringArray const & arr, String const & val, bool caseInsensitive = false );
428 
430 WINUX_FUNC_DECL(int) Random( int n1, int n2 );
431 
432 // -------------------------------------------------------------------------------
433 class GrowBuffer;
434 
437 {
438 public:
440  Buffer();
444  Buffer( void * buf, uint size, bool isPeek = false );
448  Buffer( AnsiString const & data, bool isPeek = false );
449  virtual ~Buffer();
450  Buffer( Buffer const & other );
451  Buffer & operator = ( Buffer const & other );
452 
453 #ifndef MOVE_SEMANTICS_DISABLED
454 
455  Buffer( Buffer && other );
457  Buffer & operator = ( Buffer && other );
459  Buffer( GrowBuffer && other );
461  Buffer & operator = ( GrowBuffer && other );
462 #endif
463 
465  void setBuf( void * buf, uint size, uint capacity, bool isPeek );
466 
468  void setBuf( void * buf, uint size, bool isPeek ) { this->setBuf( buf, size, size, isPeek ); }
469 
471  void alloc( uint capacity, bool setDataSize = true );
472 
476  void realloc( uint newCapacity );
477 
479  bool peekCopy( bool copyCapacity = false );
480 
482  void free();
483 
485  void * getBuf() const { return _buf; }
486 
487  template < typename _Ty >
488  _Ty * getBuf() const { return reinterpret_cast<_Ty *>(_buf); }
489 
490  winux::byte & operator [] ( int i ) { return reinterpret_cast<winux::byte *>(_buf)[i]; }
491  winux::byte const & operator [] ( int i ) const { return reinterpret_cast<winux::byte const *>(_buf)[i]; }
492 
494  uint getSize() const { return _dataSize; }
495 
497  void _setSize( uint dataSize ) { _dataSize = ( dataSize > _capacity ? _capacity : dataSize ); }
498 
500  uint getCapacity() const { return _capacity; }
501 
503  operator bool() const { return _buf != NULL; }
504 
506  template < typename _ChTy >
507  std::basic_string<_ChTy> toString() const
508  {
509  typedef typename std::basic_string<_ChTy>::value_type CharType;
510  return std::basic_string<_ChTy>( (CharType*)_buf, _dataSize / sizeof(CharType) );
511  }
513  AnsiString toAnsi() const { return this->toString<AnsiString::value_type>(); }
515  UnicodeString toUnicode() const { return this->toString<UnicodeString::value_type>(); }
516 
517 protected:
518  static void * _Alloc( uint size );
519  static void * _Realloc( void * p, uint newSize );
520  static void _Free( void * p );
521 
522  void * _buf;
523  uint _dataSize; // 数据的大小
524  uint _capacity; // 容量
525  bool _isPeek; // 是否为窥视模式
526 
527  friend class GrowBuffer;
528 };
529 
532 {
533 public:
535  explicit GrowBuffer( uint capacity = 0 );
536  GrowBuffer( GrowBuffer const & other );
537  GrowBuffer & operator = ( GrowBuffer const & other );
538  explicit GrowBuffer( Buffer const & other );
539  GrowBuffer & operator = ( Buffer const & other );
540 
541 #ifndef MOVE_SEMANTICS_DISABLED
542 
543  GrowBuffer( GrowBuffer && other );
545  GrowBuffer & operator = ( GrowBuffer && other );
547  GrowBuffer( Buffer && other );
549  GrowBuffer & operator = ( Buffer && other );
550 #endif
551 
553  void append( void const * data, uint size );
554 
556  void append( AnsiString const & data ) { this->append( data.c_str(), (uint)data.size() ); }
557 
559  void append( Buffer const & data ) { this->append( data.getBuf(), data.getSize() ); }
560 
562  void erase( uint start, uint count = (uint)-1 );
563 
564 protected:
565  friend class Buffer;
566 };
567 
568 // 混合体相关 ------------------------------------------------------------------------------
570 class WINUX_DLL MixedError : public Error
571 {
572 public:
573  enum
574  {
580  };
581 
582  MixedError( int errType, AnsiString const & errStr ) throw() : Error( errType, errStr ) { }
583 };
584 
587 {
588 public:
589  enum MixedType : ushort
590  {
591  #define MixedType_ENUM_ITEM(item) item,
592  #define MixedType_ENUM_ITEMSTRING(item) #item,
593  #define MixedType_ENUM_ITEMLIST(_)\
594  _(MT_NULL)\
595  _(MT_BOOLEAN)\
596  _(MT_BYTE)\
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)\
603  _(MT_ARRAY) \
604  _(MT_COLLECTION) \
605  _(MT_BINARY)
607  MixedType_ENUM_ITEMLIST(MixedType_ENUM_ITEM)
608  };
609 
611  static String const & TypeString( MixedType type );
613  class WINUX_DLL MixedLess
614  {
615  public:
616  bool operator () ( Mixed const & v1, Mixed const & v2 ) const;
617  };
619  typedef std::map< Mixed, Mixed, MixedLess > MixedMixedMap;
620  typedef MixedMixedMap::value_type MixedMixedPair;
622  MixedType _type;
623 
624  union
625  {
626  double _dblVal;
627  uint64 _ui64Val;
628  int64 _i64Val;
629  float _fltVal;
630  ulong _ulVal;
631  long _lVal;
632  uint _uiVal;
633  int _iVal;
634  ushort _ushVal;
635  short _shVal;
636  byte _btVal;
637  bool _boolVal;
638  struct // 字符串
639  {
640  union
641  {
642  AnsiString * _pStr;
643  UnicodeString * _pWStr;
644  };
645  };
646  struct // Array or Collection
647  {
648  MixedArray * _pArr;
649  MixedMixedMap * _pMap;
650  };
651  struct // 二进制数据
652  {
653  Buffer * _pBuf;
654  };
655  };
656 
657 public:
658  Mixed();
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 );
663 
664  Mixed( bool boolVal );
665  Mixed( byte btVal );
666  Mixed( short shVal );
667  Mixed( ushort ushVal );
668  Mixed( int iVal );
669  Mixed( uint uiVal );
670  Mixed( long lVal );
671  Mixed( ulong ulVal );
672  Mixed( float fltVal );
673  Mixed( int64 i64Val );
674  Mixed( uint64 ui64Val );
675  Mixed( double dblVal );
676 
677  Mixed( Buffer const & buf );
678  Mixed( void * binaryData, uint size, bool isPeek = false );
679 
680  // Array构造函数 -----------------------------------------------------------------------
682  Mixed( Mixed * arr, uint count );
683 
684  template < typename _Ty >
685  Mixed( std::vector<_Ty> const & arr )
686  {
687  _zeroInit();
688  this->_type = MT_ARRAY;
689  this->_pArr = new MixedArray( arr.size() );
690  uint i;
691  for ( i = 0; i < arr.size(); ++i )
692  {
693  this->_pArr->at(i) = arr[i];
694  }
695  }
696 
697 #if defined(__GNUC__) || _MSC_VER > 1200
698  template < typename _Ty, uint n >
699  Mixed( _Ty (&arr)[n] )
700  {
701  _zeroInit();
702  this->_type = MT_ARRAY;
703  this->_pArr = new MixedArray(n);
704  uint i;
705  for ( i = 0; i < n; ++i )
706  {
707  this->_pArr->at(i) = arr[i];
708  }
709  }
710 #endif
711  // Collection构造函数 ------------------------------------------------------------------
713  template < typename _KTy, typename _VTy, typename _Pr, typename _Alloc >
714  Mixed( std::map< _KTy, _VTy, _Pr, _Alloc > const & m )
715  {
716  _zeroInit();
717  this->assign< _KTy, _VTy, _Pr, _Alloc >(m);
718  }
719 
720 #if defined(__GNUC__) || _MSC_VER > 1200
721  template < typename _KTy, typename _VTy, uint count >
722  Mixed( std::pair< _KTy, _VTy > (&pairs)[count] )
723  {
724  _zeroInit();
725  this->assign< _KTy, _VTy, count >(pairs);
726  }
727 
728  template < typename _KTy, typename _VTy, uint count >
729  Mixed( _KTy (&keys)[count], _VTy (&vals)[count] )
730  {
731  _zeroInit();
732  this->assign< _KTy, _VTy, count >( keys, vals );
733  }
734 #endif
735 
737  ~Mixed();
738 
740  Mixed( Mixed const & other );
742  Mixed & operator = ( Mixed const & other );
743 
744 #ifndef MOVE_SEMANTICS_DISABLED
745 
746  Mixed( Mixed && other );
748  Mixed & operator = ( Mixed && other );
749 
750  Mixed( Buffer && buf );
751  void assign( Buffer && buf );
752 
753  Mixed( GrowBuffer && buf );
754  void assign( GrowBuffer && buf );
755 
756 #endif
757 
759  void free();
760 
762  MixedType type() const { return this->_type; }
764  String const & typeString() const { return TypeString(this->_type); }
765 
766  // 取得相关类型的引用 --------------------------------------------------------------------
767  #include "mixed_ref_specified_type.inl"
768 
769  // 类型转换 ----------------------------------------------------------------------------
770  operator AnsiString() const;
771  operator UnicodeString() const;
772  operator Buffer() const;
773  operator bool() const;
774  operator byte() const;
775  operator short() const;
776  operator ushort() 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;
783  operator uint64() const;
784  operator double() const;
786  AnsiString toAnsi() const { return this->operator AnsiString(); }
787  UnicodeString toUnicode() const { return this->operator UnicodeString(); }
788  Buffer toBuffer() const { return this->operator Buffer(); }
789  bool toBool() const { return this->operator bool(); }
790  byte toByte() const { return this->operator winux::byte(); }
791  short toShort() const { return this->operator short(); }
792  ushort toUShort() const { return this->operator winux::ushort(); }
793  int toInt() const { return this->operator int(); }
794  uint toUInt() const { return this->operator winux::uint(); }
795  long toLong() const { return this->operator long(); }
796  ulong toULong() const { return this->operator winux::ulong(); }
797  float toFloat() const { return this->operator float(); }
798  int64 toInt64() const { return this->operator winux::int64(); }
799  uint64 toUInt64() const { return this->operator winux::uint64(); }
800  double toDouble() const { return this->operator double(); }
801 
802  // 比较操作符 --------------------------------------------------------------------------
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); }
809 
810  // 判定特殊类型 -------------------------------------------------------------------------
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; }
821 
822  // 创建相关类型 -------------------------------------------------------------------------
824  Mixed & createString();
826  Mixed & createUnicode();
828  Mixed & createArray( uint count = 0 );
830  Mixed & createCollection();
832  Mixed & createBuffer( uint size = 0 );
833 
834  // Array/Collection有关的操作 ----------------------------------------------------------
835 
837  template < typename _Ty >
838  int getArray( std::vector<_Ty> * arr ) const
839  {
840  if ( !this->isArray() && !this->isCollection() ) throw MixedError( MixedError::meUnexpectedType, TypeString(this->_type) + " can't support getArray()" );
841  MixedArray::const_iterator it;
842  for ( it = this->_pArr->begin(); it != this->_pArr->end(); ++it )
843  arr->push_back(*it);
844  return (int)arr->size();
845  }
846 
848  template < typename _KTy >
849  int getKeys( std::vector<_KTy> * keys ) const
850  {
851  if ( !this->isCollection() ) throw MixedError( MixedError::meUnexpectedType, TypeString(this->_type) + " can't support getKeys()" );
852  MixedArray::const_iterator it;
853  for ( it = this->_pArr->begin(); it != this->_pArr->end(); ++it )
854  keys->push_back(*it);
855  return keys->size();
856  }
857 
859  template < typename _KTy, typename _VTy >
860  int getMap( std::map< _KTy, _VTy > * m ) const
861  {
862  if ( !this->isCollection() ) throw MixedError( MixedError::meUnexpectedType, TypeString(this->_type) + " can't support getMap()" );
863  MixedMixedMap::const_iterator it;
864  for ( it = this->_pMap->begin(); it != this->_pMap->end(); ++it )
865  (*m)[(_KTy)it->first] = (_VTy)it->second;
866  return m->size();
867  }
868 
870  bool isEmpty() const { return this->getCount() == 0; }
871 
875  int getCount() const;
876 
878  Mixed & operator [] ( Mixed const & k );
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) ); }
887 
889  template < typename _Ty >
890  _Ty get( Mixed const & k ) const { return (_Ty)this->operator [] (k); }
891 
893  MixedMixedMap::value_type & getPair( int i );
895  MixedMixedMap::value_type const & getPair( int i ) const;
898  {
899  Mixed * _mx;
900  public:
901  CollectionAssigner( Mixed * mx ) : _mx(mx) { }
902  CollectionAssigner & operator()( Mixed const & k, Mixed const & v );
903  operator Mixed & () { return *_mx; }
904  };
905 
909  CollectionAssigner addPair();
910 
912  Mixed & addPair( Mixed const & k, Mixed const & v );
915  {
916  Mixed * _mx;
917  public:
918  ArrayAssigner( Mixed * mx ) : _mx(mx) { }
919  ArrayAssigner & operator()( Mixed const & v );
920  operator Mixed & () { return *_mx; }
921  };
922 
926  ArrayAssigner add();
927 
929  int add( Mixed const & v );
930 
932  int addUnique( Mixed const & v );
933 
935  void del( Mixed const & k );
936 
940  bool has( Mixed const & ek ) const;
941 
945  Mixed & merge( Mixed const & v );
946 
947  // Buffer有关操作 --------------------------------------------------------------------------
949  void alloc( uint size );
950 
952  bool peekCopy( bool copyCapacity = false );
953 
955  int getSize() const;
956 
958  void * getBuf() const;
959 
960  // 赋值操作 -------------------------------------------------------------------------------
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 );
975 
976  void assign( Buffer const & buf );
978  void assign( void * binaryData, uint size, bool isPeek = false );
980  void assign( Mixed * arr, uint count );
981 
982  template < typename _Ty >
983  void assign( std::vector<_Ty> const & arr )
984  {
985  this->free();
986  this->_type = MT_ARRAY;
987  this->_pArr = new MixedArray( arr.size() );
988  uint i;
989  for ( i = 0; i < arr.size(); ++i )
990  {
991  this->_pArr->at(i) = arr[i];
992  }
993  }
994 
995 #if defined(__GNUC__) || _MSC_VER > 1200
996  template < typename _Ty, uint n >
997  void assign( _Ty (&arr)[n] )
998  {
999  this->free();
1000  this->_type = MT_ARRAY;
1001  this->_pArr = new MixedArray(n);
1002  uint i;
1003  for ( i = 0; i < n; ++i )
1004  {
1005  this->_pArr->at(i) = arr[i];
1006  }
1007  }
1008 #endif
1009 
1011  template < typename _KTy, typename _VTy, typename _Pr, typename _Alloc >
1012  void assign( std::map< _KTy, _VTy, _Pr, _Alloc > const & m )
1013  {
1014  this->free();
1015  this->_type = MT_COLLECTION;
1016  this->_pArr = new MixedArray(); // 存放keys
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 )
1020  {
1021  this->_pArr->push_back(it->first);
1022  (*this->_pMap)[it->first] = it->second;
1023  }
1024  //std::sort( this->_pArr->begin(), this->_pArr->end() );
1025  }
1026 
1027 #if defined(__GNUC__) || _MSC_VER > 1200
1028 
1029  template < typename _KTy, typename _VTy, uint count >
1030  void assign( std::pair< _KTy, _VTy > (&pairs)[count] )
1031  {
1032  this->free();
1033  this->_type = MT_COLLECTION;
1034  this->_pArr = new MixedArray(); // 存放keys
1035  this->_pMap = new MixedMixedMap();
1036  uint i;
1037  for ( i = 0; i < count; ++i )
1038  {
1039  this->_addUniqueKey(pairs[i].first);
1040  (*this->_pMap)[pairs[i].first] = pairs[i].second;
1041  }
1042  }
1043 
1045  template < typename _KTy, typename _VTy, uint count >
1046  void assign( _KTy (&keys)[count], _VTy (&vals)[count] )
1047  {
1048  this->free();
1049  this->_type = MT_COLLECTION;
1050  this->_pArr = new MixedArray(); // 存放keys
1051  this->_pMap = new MixedMixedMap();
1052  uint i;
1053  for ( i = 0; i < count; ++i )
1054  {
1055  this->_addUniqueKey(keys[i]);
1056  (*this->_pMap)[keys[i]] = vals[i];
1057  }
1058  }
1059 #endif
1060  // JSON相关操作 ------------------------------------------------------------------------
1061  String json() const;
1062  String myJson() const;
1063  Mixed & json( String const & jsonStr );
1064 
1065  // 类型解析功能 -------------------------------------------------------------------------
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 );
1078 
1080  static Mixed & ParseJson( AnsiString const & str, Mixed * val );
1081 
1082 private:
1083  void _zeroInit();
1084  // MT_COLLECTION,给数组加入一个唯一键名
1085  void _addUniqueKey( Mixed const & k )
1086  {
1087  if ( this->_pMap->find(k) == this->_pMap->end() )
1088  this->_pArr->push_back(k);
1089  }
1090 };
1091 
1093 WINUX_FUNC_DECL(std::ostream &) operator << ( std::ostream & o, Mixed const & m );
1094 WINUX_FUNC_DECL(std::wostream &) operator << ( std::wostream & o, Mixed const & m );
1095 
1096 //std::istream & operator >> ( std::istream & o, Mixed const & m );
1097 //std::wistream & operator >> ( std::wistream & o, Mixed const & m );
1098 
1099 } // namespace winux
1100 
1101 #endif // __UTILITIES_HPP__
函数特征
Definition: utilities.hpp:274
void * getBuf() const
暴露缓冲区指针
Definition: utilities.hpp:485
char32_t char32
Definition: utilities.hpp:136
AnsiString toAnsi() const
转换到AnsiString
Definition: utilities.hpp:513
值是Null因此无法操作
Definition: utilities.hpp:576
Array赋值器
Definition: utilities.hpp:301
std::map< String, Mixed > StringMixedMap
Definition: utilities.hpp:176
MapAssigner< _KTy, _VTy > Assign(std::map< _KTy, _VTy > *m)
给容器赋值
Definition: utilities.hpp:316
void create(_ArgType &&...arg)
必须在使用者类的构造函数里第一个调用
Definition: utilities.hpp:383
#define WINUX_DLL
Definition: utilities.hpp:57
wchar_t wchar
Definition: utilities.hpp:145
std::basic_string< char > AnsiString
Definition: utilities.hpp:165
int VoidReturnInt(_Fx fn, _ArgType &&...arg)
调用一个返回void的函数或函数对象,返回一个数字
Definition: utilities.hpp:207
MembersWrapper(MembersWrapper &&other)
Definition: utilities.hpp:353
void setBuf(void *buf, uint size, bool isPeek)
设置缓冲区,当isPeek为false时拷贝数据缓冲区
Definition: utilities.hpp:468
std::pair< String, Mixed > StringMixedPair
Definition: utilities.hpp:177
long long longlong
Definition: utilities.hpp:149
std::basic_string< _ChTy > toString() const
转换到字符串
Definition: utilities.hpp:507
bool ValueIsInArray(StringArray const &arr, String const &val, bool caseInsensitive=false)
判断一个字符串值是否在一个字符串数组里,默认大小写敏感
二进制数,编译时计算, 0开头(基于8进制)
Definition: utilities.hpp:214
std::map< String, String > StringStringMap
Definition: utilities.hpp:172
void destroy()
必须在使用者类的析构函数里最后一个调用
Definition: utilities.hpp:372
virtual ~Error()
Definition: utilities.hpp:421
std::ostream & operator<<(std::ostream &o, ConsoleAttrT< _VarType > const &tr)
Definition: console.hpp:134
std::vector< String > StringArray
Definition: utilities.hpp:171
char tchar
Definition: utilities.hpp:155
不能转换到某种类型
Definition: utilities.hpp:577
uint getCapacity() const
获取容量大小
Definition: utilities.hpp:500
_Ty * getBuf() const
Definition: utilities.hpp:488
AnsiString LocalString
Definition: utilities.hpp:166
int Random(int n1, int n2)
随机数,随机产生n1~n2的数字. 包括n1,n2本身
uint getSize() const
获取数据大小
Definition: utilities.hpp:494
std::vector< Mixed > MixedArray
Definition: utilities.hpp:175
缓冲区,表示内存中一块2进制数据(利用malloc/realloc进行内存分配)
Definition: utilities.hpp:436
std::map< Mixed, Mixed, MixedLess > MixedMixedMap
Definition: utilities.hpp:618
void _setSize(uint dataSize)
设置数据大小,不能超过容量大小(不建议外部调用)
Definition: utilities.hpp:497
MixedMixedMap::value_type MixedMixedPair
Definition: utilities.hpp:619
char int8
Definition: utilities.hpp:132
std::basic_string< char16 > UnicodeString16
Definition: utilities.hpp:168
bool isset(_MAP const &m, _KEY const &k)
检测map中是否有该键的值
Definition: utilities.hpp:185
unsigned int uint
Definition: utilities.hpp:128
MapAssigner(std::map< _KTy, _VTy > *m)
Definition: utilities.hpp:289
MixedError(int errType, AnsiString const &errStr)
Definition: utilities.hpp:582
意料外的类型,该类型不能执行这个操作
Definition: utilities.hpp:578
#define WINUX_FUNC_DECL(ret)
Definition: utilities.hpp:61
Tuple参数序列
Definition: utilities.hpp:248
unsigned char byte
Definition: utilities.hpp:159
void append(AnsiString const &data)
添加数据
Definition: utilities.hpp:556
virtual char const * what() const
Definition: utilities.hpp:423
高效的可增长缓冲区,1.33倍冗余量
Definition: utilities.hpp:531
MapAssigner & operator()(_KTy const &k, _VTy const &v)
Definition: utilities.hpp:290
char16_t char16
Definition: utilities.hpp:135
std::vector< _Ty > ToArray(_Ty *arr, uint count)
将C数组转换成vector
Definition: utilities.hpp:192
void append(Buffer const &data)
添加数据
Definition: utilities.hpp:559
UnicodeString toUnicode() const
转换到UnicodeString
Definition: utilities.hpp:515
int int32
Definition: utilities.hpp:127
Error(int errType, AnsiString const &errStr)
Definition: utilities.hpp:420
unsigned short ushort
Definition: utilities.hpp:131
virtual int getErrType() const
Definition: utilities.hpp:422
混合体,能表示多种类型的值
Definition: utilities.hpp:586
unsigned short uint16
Definition: utilities.hpp:131
RefParam(ParamTypeRef r)
Definition: utilities.hpp:235
static FuncTraits< _PfnType >::ReturnType func(_ArgType &&...arg)
Definition: utilities.hpp:277
错误类
Definition: utilities.hpp:413
unsigned long long ulonglong
Definition: utilities.hpp:147
unsigned long long uint64
Definition: utilities.hpp:146
RefParam< _Ty > Ref(_Ty &r)
向模板参数传递引用型参数
Definition: utilities.hpp:241
unsigned long ulong
Definition: utilities.hpp:129
std::pair< String, String > StringStringPair
Definition: utilities.hpp:173
unsigned int uint32
Definition: utilities.hpp:128
std::basic_string< char32 > UnicodeString32
Definition: utilities.hpp:169
unsigned char uint8
Definition: utilities.hpp:133
混合体错误
Definition: utilities.hpp:570
short int16
Definition: utilities.hpp:130
MAP赋值器
Definition: utilities.hpp:285
std::basic_string< tchar > String
Definition: utilities.hpp:162
std::basic_string< wchar > UnicodeString
Definition: utilities.hpp:167
跨平台基础功能库
Definition: archives.hpp:7
long long int64
Definition: utilities.hpp:148
ArrayAssigner(std::vector< _Ty > *a)
Definition: utilities.hpp:305