Nuria Framework - Core
Core module of the NuriaProject Framework
 All Classes Functions Typedefs Enumerations Enumerator Groups
temporarybufferdevice.hpp
1 /* Copyright (c) 2014, The Nuria Project
2  * This software is provided 'as-is', without any express or implied
3  * warranty. In no event will the authors be held liable for any damages
4  * arising from the use of this software.
5  * Permission is granted to anyone to use this software for any purpose,
6  * including commercial applications, and to alter it and redistribute it
7  * freely, subject to the following restrictions:
8  * 1. The origin of this software must not be misrepresented; you must not
9  * claim that you wrote the original software. If you use this software
10  * in a product, an acknowledgment in the product documentation would be
11  * appreciated but is not required.
12  * 2. Altered source versions must be plainly marked as such, and must not be
13  * misrepresented as being the original software.
14  * 3. This notice may not be removed or altered from any source
15  * distribution.
16  */
17 
18 #ifndef NURIA_TEMPORARYBUFFERDEVICE_HPP
19 #define NURIA_TEMPORARYBUFFERDEVICE_HPP
20 
21 #include "essentials.hpp"
22 #include <QIODevice>
23 
24 namespace Nuria {
25 
26 class TemporaryBufferDevicePrivate;
27 
49 class NURIA_CORE_EXPORT TemporaryBufferDevice : public QIODevice {
50  Q_OBJECT
51 public:
52  enum {
56  DefaultMemorySize = 4096 * 1024 // 4MiB
57  };
58 
60  enum StorageMode {
61 
63  NoDevice = 0,
64 
67 
69  TemporaryFile
70  };
71 
73  explicit TemporaryBufferDevice (int maximumMemorySize, QObject *parent = 0);
74 
76  explicit TemporaryBufferDevice (QObject *parent = 0);
77 
79  ~TemporaryBufferDevice () override;
80 
85  int maximumMemorySize () const;
86 
92  void setMaximumMemorySize (int maximumSize);
93 
95  StorageMode storageMode () const;
96 
101  QIODevice *internalDevice () const;
102 
108  QIODevice *stealInternalDevice ();
109 
113  void discard ();
114 
115  //
116  bool isSequential () const;
117  bool open (OpenMode mode);
118 
122  void close ();
123  qint64 pos () const;
124  qint64 size () const;
125  bool seek (qint64 pos);
126  bool atEnd () const;
127  bool reset ();
128  qint64 bytesAvailable () const;
129  qint64 bytesToWrite () const;
130  bool canReadLine () const;
131  bool waitForReadyRead (int msecs);
132  bool waitForBytesWritten (int msecs);
133 
134 signals:
135 
139  void storageModeChanged (Nuria::TemporaryBufferDevice::StorageMode storageMode);
140 
141 protected:
142  qint64 readData(char *data, qint64 maxlen);
143  qint64 readLineData(char *data, qint64 maxlen);
144  qint64 writeData(const char *data, qint64 len);
145 
146 private:
147  void decideStrategy (int newSize);
148  void moveMemoryBufferToFileDevice (QIODevice *memory, QIODevice *file);
149  QIODevice *openMemoryBuffer ();
150  QIODevice *openFileBuffer ();
151 
152  TemporaryBufferDevicePrivate *d_ptr;
153 
154 };
155 
156 }
157 
159 
160 #endif // NURIA_TEMPORARYBUFFERDEVICE_HPP
In-memory buffer device with fallback to a temporary file.
Definition: temporarybufferdevice.hpp:49
Definition: temporarybufferdevice.hpp:66
StorageMode
Definition: temporarybufferdevice.hpp:60