util.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* ScummVM Tools
  2. *
  3. * ScummVM Tools is the legal property of its developers, whose
  4. * names are too numerous to list here. Please refer to the
  5. * COPYRIGHT file distributed with this source distribution.
  6. *
  7. * Additionally this file is based on the ScummVM source code.
  8. * Copyright information for the ScummVM source code is
  9. * available in the COPYRIGHT file of the ScummVM source
  10. * distribution.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version 2
  15. * of the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  25. */
  26. #ifndef COMMON_UTIL_H
  27. #define COMMON_UTIL_H
  28. #include "common/scummsys.h"
  29. #include <streambuf>
  30. #include <ostream>
  31. #ifdef MIN
  32. #undef MIN
  33. #endif
  34. #ifdef MAX
  35. #undef MAX
  36. #endif
  37. // Define an ostream which doesn't output anything to avoid clutter
  38. // Source: http://groups.google.com/group/comp.lang.c++/msg/4a81a74500f9f4d3?hl=en
  39. template<class cT, class traits = std::char_traits<cT> >
  40. class basic_nullbuf : public std::basic_streambuf < cT, traits > {
  41. typename traits::int_type overflow(typename traits::int_type c)
  42. {
  43. return traits::not_eof(c);
  44. }
  45. };
  46. template<class cT, class traits = std::char_traits<cT> >
  47. class basic_onullstream : public std::basic_ostream < cT, traits > {
  48. public:
  49. basic_onullstream() :
  50. std::basic_ios<cT, traits>(&m_sbuf),
  51. std::basic_ostream<cT, traits>(&m_sbuf)
  52. {
  53. this->init(&m_sbuf);
  54. }
  55. private:
  56. basic_nullbuf<cT, traits> m_sbuf;
  57. };
  58. typedef basic_onullstream<char> onullstream;
  59. template<typename T> inline T ABS (T x) { return (x>=0) ? x : -x; }
  60. template<typename T> inline T MIN (T a, T b) { return (a<b) ? a : b; }
  61. template<typename T> inline T MAX (T a, T b) { return (a>b) ? a : b; }
  62. template<typename T> inline T CLIP (T v, T amin, T amax)
  63. { if (v < amin) return amin; else if (v > amax) return amax; else return v; }
  64. /**
  65. * Template method which swaps the vaulues of its two parameters.
  66. */
  67. template<typename T> inline void SWAP(T &a, T &b) { T tmp = a; a = b; b = tmp; }
  68. /**
  69. * Macro which determines the number of entries in a fixed size array.
  70. */
  71. #define ARRAYSIZE(x) ((int)(sizeof(x) / sizeof(x[0])))
  72. #endif