aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken6_generator/ApiExtractor/filecache.h
blob: eca46f7e9d011757b5ee7cba56e2aa7866d58b67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#ifndef FILECACHE_H
#define FILECACHE_H

#include <QtCore/qlist.h>
#include <QtCore/qstring.h>
#include <QtCore/qstringview.h>

#include <optional>

QT_FORWARD_DECLARE_CLASS(QRegularExpression)
QT_FORWARD_DECLARE_CLASS(QDebug)

// Queue-based cache for the contents of a number of recent files with a
// convenience API for retrieving lines and regexp-delimited snippets.
class FileCache
{
public:
    using Lines = QList<QStringView>;

    std::optional<QString> fileContents(const QString &name);
    std::optional<Lines> lines(const QString &name);
    std::optional<QString> fileSnippet(const QString &name,
                                       const QString &snippetName,
                                       const QRegularExpression &snippetPattern);

    const QString &errorString() const { return m_error; }

    void formatDebug(QDebug &debug) const;

private:
    struct FileCacheEntry
    {
        QString name;
        QString contents;
        Lines lines;
    };

    qsizetype ensureEntry(const QString &name);
    qsizetype indexOf(const QString &name) const;
    static void ensureLines(FileCacheEntry *entry);

    QList<FileCacheEntry> m_cache;
    QString m_error;
    int m_hits = 0;
    int m_misses = 0;
};

QDebug operator<<(QDebug debug, const FileCache &c);

#endif // FILECACHE_H