annotate data/fileio/MatrixFile.h @ 694:2c4aca969c24 toggle

Option added in the preference menu to be able to start the application in minimal mode
author mathieub <mathieu.barthet@eecs.qmul.ac.uk>
date Wed, 20 Jul 2011 16:30:07 +0100
parents 60482f13e627
children 59e7fe1b1003
rev   line source
Chris@148 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@148 2
Chris@148 3 /*
Chris@148 4 Sonic Visualiser
Chris@148 5 An audio file viewer and annotation editor.
Chris@148 6 Centre for Digital Music, Queen Mary, University of London.
Chris@537 7 This file copyright 2006-2009 Chris Cannam and QMUL.
Chris@148 8
Chris@148 9 This program is free software; you can redistribute it and/or
Chris@148 10 modify it under the terms of the GNU General Public License as
Chris@148 11 published by the Free Software Foundation; either version 2 of the
Chris@148 12 License, or (at your option) any later version. See the file
Chris@148 13 COPYING included with this distribution for more information.
Chris@148 14 */
Chris@148 15
Chris@148 16 #ifndef _MATRIX_FILE_CACHE_H_
Chris@148 17 #define _MATRIX_FILE_CACHE_H_
Chris@148 18
Chris@148 19 #include "base/ResizeableBitset.h"
Chris@148 20
Chris@148 21 #include "FileReadThread.h"
Chris@148 22
Chris@148 23 #include <sys/types.h>
Chris@148 24 #include <QString>
Chris@148 25 #include <QMutex>
Chris@148 26 #include <map>
Chris@148 27
Chris@148 28 class MatrixFile : public QObject
Chris@148 29 {
Chris@148 30 Q_OBJECT
Chris@148 31
Chris@148 32 public:
Chris@537 33 enum Mode { ReadOnly, WriteOnly };
Chris@148 34
Chris@148 35 /**
Chris@148 36 * Construct a MatrixFile object reading from and/or writing to
Chris@148 37 * the matrix file with the given base name in the application's
Chris@148 38 * temporary directory.
Chris@148 39 *
Chris@148 40 * If mode is ReadOnly, the file must exist and be readable.
Chris@148 41 *
Chris@537 42 * If mode is WriteOnly, the file must not exist.
Chris@148 43 *
Chris@148 44 * cellSize specifies the size in bytes of the object type stored
Chris@148 45 * in the matrix. For example, use cellSize = sizeof(float) for a
Chris@148 46 * matrix of floats. The MatrixFile object doesn't care about the
Chris@148 47 * objects themselves, it just deals with raw data of a given size.
Chris@148 48 *
Chris@537 49 * width and height specify the dimensions of the file. These
Chris@537 50 * cannot be changed after construction.
Chris@537 51 *
Chris@537 52 * MatrixFiles are reference counted by name. When the last
Chris@537 53 * MatrixFile with a given name is destroyed, the file is removed.
Chris@537 54 * These are temporary files; the normal usage is to have one
Chris@537 55 * MatrixFile of WriteOnly type creating the file and then
Chris@537 56 * persisting until all readers are complete.
Chris@537 57 *
Chris@537 58 * MatrixFile has no built-in cache and is not thread-safe. Use a
Chris@537 59 * separate MatrixFile in each thread.
Chris@148 60 */
Chris@537 61 MatrixFile(QString fileBase, Mode mode, size_t cellSize,
Chris@537 62 size_t width, size_t height);
Chris@148 63 virtual ~MatrixFile();
Chris@148 64
Chris@148 65 Mode getMode() const { return m_mode; }
Chris@148 66
Chris@148 67 size_t getWidth() const { return m_width; }
Chris@148 68 size_t getHeight() const { return m_height; }
Chris@148 69 size_t getCellSize() const { return m_cellSize; }
Chris@550 70
Chris@550 71 /**
Chris@550 72 * If this is set true on a write-mode MatrixFile, then the file
Chris@550 73 * will close() itself when all columns have been written.
Chris@550 74 */
Chris@550 75 void setAutoClose(bool a) { m_autoClose = a; }
Chris@550 76
Chris@537 77 void close(); // does not decrement ref count; that happens in dtor
Chris@148 78
Chris@537 79 bool haveSetColumnAt(size_t x) const;
Chris@455 80 void getColumnAt(size_t x, void *data); // may throw FileReadFailed
Chris@148 81 void setColumnAt(size_t x, const void *data);
Chris@148 82
Chris@148 83 protected:
Chris@148 84 int m_fd;
Chris@148 85 Mode m_mode;
Chris@148 86 int m_flags;
Chris@148 87 mode_t m_fmode;
Chris@148 88 size_t m_cellSize;
Chris@148 89 size_t m_width;
Chris@148 90 size_t m_height;
Chris@148 91 size_t m_headerSize;
Chris@148 92 QString m_fileName;
Chris@148 93
Chris@550 94 ResizeableBitset *m_setColumns; // only in writer
Chris@550 95 bool m_autoClose;
Chris@550 96
Chris@554 97 // In reader: if this is >= 0, we can read that column directly
Chris@554 98 // without seeking (and we know that the column exists)
Chris@554 99 mutable int m_readyToReadColumn;
Chris@554 100
Chris@148 101 static std::map<QString, int> m_refcount;
Chris@537 102 static QMutex m_createMutex;
Chris@148 103
Chris@537 104 void initialise();
Chris@554 105 bool seekTo(size_t x) const;
Chris@148 106 };
Chris@148 107
Chris@148 108 #endif
Chris@148 109