Advertisement
bueddl

Untitled

May 8th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. // This file is part of w3c - wwwcam.
  2.  
  3. // w3c is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7.  
  8. // w3c is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12.  
  13. // You should have received a copy of the GNU General Public License
  14. // along with w3c. If not, see <http://www.gnu.org/licenses/>.
  15. //
  16. // (C) Copyright 2015 by Sebastian Büttner <sebastian.buettner@iem.thm.de>
  17.  
  18. #ifndef HELPER_BINARY_READER_H
  19. #define HELPER_BINARY_READER_H
  20.  
  21. #include <cstring>
  22. #include <type_traits>
  23.  
  24. namespace w3c {
  25.  
  26. class binary_reader {
  27. public:
  28.     binary_reader(const char *data);
  29.  
  30.     template<typename T>
  31.     binary_reader& operator>>(T &value)
  32.     {
  33.         value = read<T>();
  34.         return *this;
  35.     }
  36.  
  37.     template<typename T>
  38.     T read()
  39.     {
  40.     static_assert(std::is_arithmetic<T>::value || std::is_pod<T>::value,
  41.                   "Only arithmetic types and pod can be read");
  42.  
  43.         T value;
  44.         std::memcpy((void*)&value, m_ptr, sizeof(T));
  45.         m_ptr += sizeof(T);
  46.         return value;
  47.     }
  48.  
  49.     void read(char *ptr, size_t size)
  50.     {
  51.         std::memcpy(ptr, m_ptr, size);
  52.         m_ptr += size;
  53.     }
  54.  
  55. private:
  56.     const char *m_ptr;
  57. };
  58.  
  59. }
  60.  
  61. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement