Clone of Bael'Zharon's Respite @ https://github.com/boardwalk/bzr

BinReader.h 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Bael'Zharon's Respite
  3. * Copyright (C) 2014 Daniel Skorupski
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #ifndef BZR_BINREADER_H
  19. #define BZR_BINREADER_H
  20. #include "Noncopyable.h"
  21. #include <type_traits>
  22. class BinReader : Noncopyable
  23. {
  24. public:
  25. BinReader(const void* data, size_t size);
  26. const uint8_t* readRaw(size_t size);
  27. uint8_t readByte();
  28. uint16_t readShort();
  29. uint32_t readInt();
  30. uint64_t readLong();
  31. float readFloat();
  32. double readDouble();
  33. uint16_t readPackedShort();
  34. uint32_t readPackedInt();
  35. string readString();
  36. template<class T>
  37. const typename enable_if<is_trivial<T>::value, T>::type* readPointer()
  38. {
  39. return reinterpret_cast<const T*>(readRaw(sizeof(T)));
  40. }
  41. void align();
  42. void dump(size_t maxLen = SIZE_MAX) const;
  43. size_t position() const;
  44. size_t remaining() const;
  45. private:
  46. const void* data_;
  47. const size_t size_;
  48. size_t position_;
  49. };
  50. #endif