Overte C++ Documentation
ExtendedIODevice.h
1 //
2 // ExtendedIODevice.h
3 // libraries/networking/src
4 //
5 // Created by Stephen Birarda on 8/7/18.
6 // Copyright 2018 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 
12 #ifndef hifi_ExtendedIODevice_h
13 #define hifi_ExtendedIODevice_h
14 
15 #include <QtCore/QIODevice>
16 
17 class ExtendedIODevice : public QIODevice {
18 public:
19  ExtendedIODevice(QObject* parent = nullptr) : QIODevice(parent) {};
20 
21  template<typename T> qint64 peekPrimitive(T* data);
22  template<typename T> qint64 readPrimitive(T* data);
23  template<typename T> qint64 writePrimitive(const T& data);
24 };
25 
26 template<typename T> qint64 ExtendedIODevice::peekPrimitive(T* data) {
27  return peek(reinterpret_cast<char*>(data), sizeof(T));
28 }
29 
30 template<typename T> qint64 ExtendedIODevice::readPrimitive(T* data) {
31  return read(reinterpret_cast<char*>(data), sizeof(T));
32 }
33 
34 template<typename T> qint64 ExtendedIODevice::writePrimitive(const T& data) {
35  static_assert(!std::is_pointer<T>::value, "T must not be a pointer");
36  return write(reinterpret_cast<const char*>(&data), sizeof(T));
37 }
38 
39 #endif // hifi_ExtendedIODevice_h