Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This file is part of w3c - wwwcam.
- // w3c is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- // w3c is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- // You should have received a copy of the GNU General Public License
- // along with w3c. If not, see <http://www.gnu.org/licenses/>.
- //
- // (C) Copyright 2015 by Sebastian Büttner <sebastian.buettner@iem.thm.de>
- #ifndef HELPER_BINARY_READER_H
- #define HELPER_BINARY_READER_H
- #include <cstring>
- #include <type_traits>
- namespace w3c {
- class binary_reader {
- public:
- binary_reader(const char *data);
- template<typename T>
- binary_reader& operator>>(T &value)
- {
- value = read<T>();
- return *this;
- }
- template<typename T>
- T read()
- {
- static_assert(std::is_arithmetic<T>::value || std::is_pod<T>::value,
- "Only arithmetic types and pod can be read");
- T value;
- std::memcpy((void*)&value, m_ptr, sizeof(T));
- m_ptr += sizeof(T);
- return value;
- }
- void read(char *ptr, size_t size)
- {
- std::memcpy(ptr, m_ptr, size);
- m_ptr += size;
- }
- private:
- const char *m_ptr;
- };
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement