comparison data/fileio/MatrixFile.h @ 148:1a42221a1522

* Reorganising code base. This revision will not compile.
author Chris Cannam
date Mon, 31 Jul 2006 11:49:58 +0000
parents
children 3e0f1f7bec85
comparison
equal deleted inserted replaced
147:3a13b0d4934e 148:1a42221a1522
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2006 Chris Cannam.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
14 */
15
16 #ifndef _MATRIX_FILE_CACHE_H_
17 #define _MATRIX_FILE_CACHE_H_
18
19 #include "base/ResizeableBitset.h"
20
21 #include "FileReadThread.h"
22
23 #include <sys/types.h>
24 #include <QString>
25 #include <QMutex>
26 #include <map>
27
28 class MatrixFile : public QObject
29 {
30 Q_OBJECT
31
32 public:
33 enum Mode { ReadOnly, ReadWrite };
34
35 /**
36 * Construct a MatrixFile object reading from and/or writing to
37 * the matrix file with the given base name in the application's
38 * temporary directory.
39 *
40 * If mode is ReadOnly, the file must exist and be readable.
41 *
42 * If mode is ReadWrite and the file does not exist, it will be
43 * created. If mode is ReadWrite and the file does exist, the
44 * existing file will be used and the mode will be reset to
45 * ReadOnly. Call getMode() to check whether this has occurred
46 * after construction.
47 *
48 * cellSize specifies the size in bytes of the object type stored
49 * in the matrix. For example, use cellSize = sizeof(float) for a
50 * matrix of floats. The MatrixFile object doesn't care about the
51 * objects themselves, it just deals with raw data of a given size.
52 *
53 * If eagerCache is true, blocks from the file will be cached for
54 * read. If eagerCache is false, only columns that have been set
55 * by calling setColumnAt on this MatrixFile (i.e. columns for
56 * which haveSetColumnAt returns true) will be cached.
57 */
58 MatrixFile(QString fileBase, Mode mode, size_t cellSize, bool eagerCache);
59 virtual ~MatrixFile();
60
61 Mode getMode() const { return m_mode; }
62
63 size_t getWidth() const { return m_width; }
64 size_t getHeight() const { return m_height; }
65 size_t getCellSize() const { return m_cellSize; }
66
67 void resize(size_t width, size_t height);
68 void reset();
69
70 bool haveSetColumnAt(size_t x) const { return m_columnBitset->get(x); }
71 void getColumnAt(size_t x, void *data);
72 void setColumnAt(size_t x, const void *data);
73
74 void suspend();
75
76 protected:
77 int m_fd;
78 Mode m_mode;
79 int m_flags;
80 mode_t m_fmode;
81 size_t m_cellSize;
82 size_t m_width;
83 size_t m_height;
84 size_t m_headerSize;
85 QString m_fileName;
86 size_t m_defaultCacheWidth;
87 size_t m_prevX;
88
89 struct Cache {
90 size_t x;
91 size_t width;
92 char *data;
93 };
94
95 Cache m_cache;
96 bool m_eagerCache;
97
98 bool getFromCache(size_t x, size_t ystart, size_t ycount, void *data);
99 void primeCache(size_t x, bool left);
100
101 void resume();
102
103 bool seekTo(size_t x, size_t y);
104
105 static FileReadThread *m_readThread;
106 int m_requestToken;
107
108 size_t m_requestingX;
109 size_t m_requestingWidth;
110 char *m_spareData;
111
112 static std::map<QString, int> m_refcount;
113 static QMutex m_refcountMutex;
114 QMutex m_fdMutex;
115 QMutex m_cacheMutex;
116
117 typedef std::map<QString, ResizeableBitset *> ResizeableBitsetMap;
118 static ResizeableBitsetMap m_columnBitsets;
119 static QMutex m_columnBitsetWriteMutex;
120 ResizeableBitset *m_columnBitset;
121 };
122
123 #endif
124