NuriaProject Framework  0.1
The NuriaProject Framework
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules
temporarybufferdevice.hpp
1 /* Copyright (c) 2014-2015, The Nuria Project
2  * The NuriaProject Framework is free software: you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public License as
4  * published by the Free Software Foundation, either version 3 of the License,
5  * or (at your option) any later version.
6  *
7  * The NuriaProject Framework is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public License
13  * along with The NuriaProject Framework.
14  * If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #ifndef NURIA_TEMPORARYBUFFERDEVICE_HPP
18 #define NURIA_TEMPORARYBUFFERDEVICE_HPP
19 
20 #include "essentials.hpp"
21 #include <QIODevice>
22 
23 namespace Nuria {
24 
25 class TemporaryBufferDevicePrivate;
26 
48 class NURIA_CORE_EXPORT TemporaryBufferDevice : public QIODevice {
49  Q_OBJECT
50 public:
51  enum {
55  DefaultMemorySize = 4096 * 1024 // 4MiB
56  };
57 
59  enum StorageMode {
60 
62  NoDevice = 0,
63 
66 
68  TemporaryFile
69  };
70 
72  explicit TemporaryBufferDevice (int maximumMemorySize, QObject *parent = 0);
73 
75  explicit TemporaryBufferDevice (QObject *parent = 0);
76 
78  ~TemporaryBufferDevice () override;
79 
84  int maximumMemorySize () const;
85 
91  void setMaximumMemorySize (int maximumSize);
92 
94  StorageMode storageMode () const;
95 
100  QIODevice *internalDevice () const;
101 
107  QIODevice *stealInternalDevice ();
108 
112  void discard ();
113 
114  //
115  bool isSequential () const;
116  bool open (OpenMode mode);
117 
121  void close ();
122  qint64 pos () const;
123  qint64 size () const;
124  bool seek (qint64 pos);
125  bool atEnd () const;
126  bool reset ();
127  qint64 bytesAvailable () const;
128  qint64 bytesToWrite () const;
129  bool canReadLine () const;
130  bool waitForReadyRead (int msecs);
131  bool waitForBytesWritten (int msecs);
132 
133 signals:
134 
138  void storageModeChanged (Nuria::TemporaryBufferDevice::StorageMode storageMode);
139 
140 protected:
141  qint64 readData(char *data, qint64 maxlen);
142  qint64 readLineData(char *data, qint64 maxlen);
143  qint64 writeData(const char *data, qint64 len);
144 
145 private:
146  void decideStrategy (int newSize);
147  void moveMemoryBufferToFileDevice (QIODevice *memory, QIODevice *file);
148  QIODevice *openMemoryBuffer ();
149  QIODevice *openFileBuffer ();
150 
151  TemporaryBufferDevicePrivate *d_ptr;
152 
153 };
154 
155 }
156 
158 
159 #endif // NURIA_TEMPORARYBUFFERDEVICE_HPP
Definition: temporarybufferdevice.hpp:65
StorageMode
Definition: temporarybufferdevice.hpp:59
Definition: abstractsessionmanager.hpp:24
In-memory buffer device with fallback to a temporary file.
Definition: temporarybufferdevice.hpp:48