Skip to content

Commit b436bf4

Browse files
committed
Move glob() and walk() generator versions to standalone functions.
1 parent 867061d commit b436bf4

File tree

5 files changed

+298
-253
lines changed

5 files changed

+298
-253
lines changed

include/uibase/ifiletree.h

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2929
#include <mutex>
3030
#include <stdexcept>
3131
#include <vector>
32+
#include <version>
3233

3334
#include <QDateTime>
3435
#include <QFlags>
@@ -112,14 +113,6 @@ struct QDLLEXPORT UnsupportedOperationException : public Exception
112113
using Exception::Exception;
113114
};
114115

115-
/**
116-
* @brief Exception thrown when an invalid glob pattern is specified.
117-
*/
118-
struct QDLLEXPORT InvalidGlobPatternException : public Exception
119-
{
120-
using Exception::Exception;
121-
};
122-
123116
/**
124117
* @brief Represent an entry in a file tree, either a file or a directory. This class
125118
* inherited by IFileTree so that operations on entry are the same for a file or
@@ -631,20 +624,6 @@ class QDLLEXPORT IFileTree : public virtual FileTreeEntry
631624

632625
};
633626

634-
enum class GlobPatternType
635-
{
636-
/**
637-
* @brief Glob mode, similar to python pathlib.Path.glob function
638-
*/
639-
GLOB,
640-
641-
/**
642-
* @brief Regex mode, each part of the pattern (between / or \) is considered a
643-
* regex, except for ** which is still considered as glob.
644-
*/
645-
REGEX
646-
};
647-
648627
/**
649628
* @brief Walk this tree, calling the given function for each entry in it.
650629
*
@@ -662,27 +641,6 @@ class QDLLEXPORT IFileTree : public virtual FileTreeEntry
662641
callback,
663642
QString sep = "\\") const;
664643

665-
/**
666-
* @brief Walk this tree, returning entries.
667-
*
668-
* During the walk, parent tree are guaranteed to be visited before their childrens.
669-
* The current tree is not included in the return generator.
670-
*
671-
* @return a generator over the entries.
672-
*/
673-
std::generator<std::shared_ptr<const FileTreeEntry>> walk() const;
674-
675-
/**
676-
* @brief Glob entries matching the given pattern in this tree.
677-
*
678-
* @param pattern Glob pattern to match, using the same syntax as QRegularExpression.
679-
* @param patternType Type of the pattern.
680-
*
681-
* @return a generator over the entries matching the given pattern.
682-
*/
683-
std::generator<std::shared_ptr<const FileTreeEntry>>
684-
glob(QString pattern, GlobPatternType patternType = GlobPatternType::GLOB) const;
685-
686644
public: // Utility functions:
687645
/**
688646
* @brief Create a new orphan empty tree.
@@ -1161,4 +1119,12 @@ class QDLLEXPORT IFileTree : public virtual FileTreeEntry
11611119

11621120
} // namespace MOBase
11631121

1122+
// __has_cpp_attribute(__cpp_lib_generator) does not seem to work, maybe some conflict
1123+
// with Qt?
1124+
#ifdef __cpp_lib_generator
1125+
1126+
#include "ifiletree_utils.h"
1127+
1128+
#endif
1129+
11641130
#endif

include/uibase/ifiletree_utils.h

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Mod Organizer shared UI functionality
3+
4+
Copyright (C) 2020 MO2 Team. All rights reserved.
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 3 of the License, or (at your option) any later version.
10+
11+
This library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
#ifndef UIBASE_IFILETREE_UTILS_H
22+
#define UIBASE_IFILETREE_UTILS_H
23+
24+
#include <generator>
25+
26+
#include <QString>
27+
28+
#include "dllimport.h"
29+
#include "exceptions.h"
30+
#include "ifiletree.h"
31+
32+
namespace MOBase
33+
{
34+
35+
/**
36+
* @brief Exception thrown when an invalid glob pattern is specified.
37+
*/
38+
struct QDLLEXPORT InvalidGlobPatternException : public Exception
39+
{
40+
using Exception::Exception;
41+
};
42+
43+
enum class GlobPatternType
44+
{
45+
/**
46+
* @brief Glob mode, similar to python pathlib.Path.glob function
47+
*/
48+
GLOB,
49+
50+
/**
51+
* @brief Regex mode, each part of the pattern (between / or \) is considered a
52+
* regex, except for ** which is still considered as glob.
53+
*/
54+
REGEX
55+
};
56+
57+
/**
58+
* @brief Walk this tree, returning entries.
59+
*
60+
* During the walk, parent tree are guaranteed to be visited before their childrens.
61+
* The current tree is not included in the return generator.
62+
*
63+
* @return a generator over the entries.
64+
*/
65+
QDLLEXPORT std::generator<std::shared_ptr<const FileTreeEntry>>
66+
walk(std::shared_ptr<const IFileTree> fileTree);
67+
68+
/**
69+
* @brief Glob entries matching the given pattern in this tree.
70+
*
71+
* @param pattern Glob pattern to match, using the same syntax as QRegularExpression.
72+
* @param patternType Type of the pattern.
73+
*
74+
* @return a generator over the entries matching the given pattern.
75+
*/
76+
QDLLEXPORT std::generator<std::shared_ptr<const FileTreeEntry>>
77+
glob(std::shared_ptr<const IFileTree> fileTree, QString pattern,
78+
GlobPatternType patternType = GlobPatternType::GLOB);
79+
80+
} // namespace MOBase
81+
82+
#endif

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ set(root_headers
3535
)
3636
set(interface_headers
3737
../include/uibase/ifiletree.h
38+
../include/uibase/ifiletree_utils.h
3839
../include/uibase/iinstallationmanager.h
3940
../include/uibase/imodinterface.h
4041
../include/uibase/imodlist.h

0 commit comments

Comments
 (0)