File indexing completed on 2024-04-21 03:56:53

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2008 Christian Ehrlicher <ch.ehrlicher@gmx.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KMEMFILE_H
0009 #define KMEMFILE_H
0010 
0011 #include <QIODevice>
0012 #include <kservice_export.h>
0013 #include <memory>
0014 
0015 #ifndef QT_NO_SHAREDMEMORY
0016 
0017 /**
0018  * @internal
0019  * Simple QIODevice for QSharedMemory to keep ksycoca cache in memory only once
0020  * The first call to open() loads the file into a shm segment. Every
0021  * subsequent call only attaches to this segment. When the file content changed,
0022  * you have to execute KMemFile::fileContentsChanged() to update the internal
0023  * structures. The next call to open() creates a new shm segment. The old one
0024  * is automatically destroyed when the last process closed KMemFile.
0025  */
0026 
0027 class KMemFile : public QIODevice
0028 {
0029     Q_OBJECT
0030 public:
0031     /**
0032      * ctor
0033      *
0034      * @param filename the file to load into memory
0035      * @param parent our parent
0036      */
0037     explicit KMemFile(const QString &filename, QObject *parent = nullptr);
0038     /**
0039      * dtor
0040      */
0041     ~KMemFile() override;
0042     /**
0043      * closes the KMemFile
0044      *
0045      * @reimp
0046      */
0047     void close() override;
0048     /**
0049      * As KMemFile is a random access device, it returns false
0050      *
0051      * @reimp
0052      */
0053     bool isSequential() const override;
0054     /**
0055      * @reimp
0056      * @param mode only QIODevice::ReadOnly is accepted
0057      */
0058     bool open(OpenMode mode) override;
0059     /**
0060      * Sets the current read/write position to pos
0061      * @reimp
0062      * @param pos the new read/write position
0063      */
0064     bool seek(qint64 pos) override;
0065     /**
0066      * Returns the size of the file
0067      * @reimp
0068      */
0069     qint64 size() const override;
0070     /**
0071      * This static function updates the internal information about the file
0072      * loaded into shared memory. The next time the file is opened, the file is
0073      * reread from the file system.
0074      */
0075     static void fileContentsChanged(const QString &filename);
0076 
0077 protected:
0078     /** @reimp */
0079     qint64 readData(char *data, qint64 maxSize) override;
0080     /** @reimp */
0081     qint64 writeData(const char *data, qint64 maxSize) override;
0082 
0083 private:
0084     class Private;
0085     friend class Private;
0086     std::unique_ptr<Private> const d;
0087 };
0088 
0089 #endif // QT_NO_SHAREDMEMORY
0090 
0091 #endif // KMEMFILE_H