stdutil.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #ifndef STDUTIL_H
  2. #define STDUTIL_H
  3. #pragma warning(disable:4786)
  4. #include "../config/config.h"
  5. #include <string>
  6. #include <iostream>
  7. #ifdef MEMDEBUG
  8. void * _cdecl operator new( size_t size );
  9. void * _cdecl operator new[]( size_t size );
  10. void * _cdecl operator new( size_t size,const char *file,int line );
  11. void * _cdecl operator new[]( size_t size,const char *file,int line );
  12. void _cdecl operator delete( void *q );
  13. void _cdecl operator delete[]( void *q );
  14. void _cdecl operator delete( void *q,const char *file,int line );
  15. void _cdecl operator delete[]( void *q,const char *file,int line );
  16. #define d_new new( __FILE__,__LINE__ )
  17. #else
  18. #define d_new new
  19. #endif
  20. void trackmem( bool enable );
  21. void checkmem( std::ostream &out );
  22. //some stuff that should be in std libs
  23. int atoi( const std::string &s );
  24. double atof( const std::string &s );
  25. std::string itoa( int n );
  26. std::string ftoa( float n );
  27. std::string tolower( const std::string &s );
  28. std::string toupper( const std::string &s );
  29. std::string fullfilename( const std::string &t );
  30. std::string filenamepath( const std::string &t );
  31. std::string filenamefile( const std::string &t );
  32. //lazy version of auto_ptr
  33. template<class T>
  34. class a_ptr{
  35. public:
  36. a_ptr(T *t=0):t(t){}
  37. ~a_ptr(){delete t;}
  38. a_ptr &operator=(T *t){this->t=t;return *this;}
  39. T &operator*()const{return *t;}
  40. T *operator->()const{return t;}
  41. operator T&()const{return *t;}
  42. operator T*()const{return t;}
  43. T *release(){ T *tt=t;t=0;return tt; }
  44. private:
  45. T *t;
  46. };
  47. //Speed-up for SLOW sstream
  48. class qstreambuf : public std::streambuf{
  49. public:
  50. qstreambuf();
  51. ~qstreambuf();
  52. int size(); //bytes unread
  53. char *data(); //start of bytes unread
  54. private:
  55. char *buf;
  56. int_type underflow();
  57. int_type overflow( int_type c );
  58. };
  59. template<class T>
  60. class pool{
  61. T *free;
  62. enum{ N=512 };
  63. public:
  64. typedef size_t size_type;
  65. typedef ptrdiff_t difference_type;
  66. typedef T *pointer;
  67. typedef const T *const_pointer;
  68. typedef T &reference;
  69. typedef const T &const_reference;
  70. typedef T value_type;
  71. pointer address( reference q )const{ return &q; }
  72. const_pointer address( const_reference q )const{ return &q; }
  73. pool():free(0){}
  74. pointer allocate( size_type n,const void *){
  75. clog<<"Allocating "<<n<<endl;
  76. if( n>1 ) return d_new T[n];
  77. if( !free ){
  78. free=(T*)d_new char[sizeof(T)*N];
  79. for( int k=0;k<N-1;++k ) *(T**)(free+k)=free+k+1;
  80. *(T**)(free+N-1)=0;
  81. }
  82. T *t=free;
  83. free=*(T**)t;
  84. return t;
  85. }
  86. void deallocate( pointer q,size_type n ){
  87. clog<<"Deallocating "<<n<<endl;
  88. while( n-->0 ){
  89. *(T**)q=free;
  90. *(T**)free=q;
  91. ++q;
  92. }
  93. }
  94. void construct( pointer p,const T &q ){ new(p)T(q); }
  95. void destroy( pointer p ){ p->~T(); }
  96. };
  97. #endif