yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie Hermaszewskaformatf65d756bf

master
2.7 KiB78 linesraw
1#ifndef SLANG_CORE_IMPLICIT_DIRECTORY_COLLECTOR_H
2#define SLANG_CORE_IMPLICIT_DIRECTORY_COLLECTOR_H
3
4#include "slang-basic.h"
5#include "slang-string-slice-index-map.h"
6
7namespace Slang
8{
9
10/* This class helps to find the contents and/or existence of an implicit directory.This finds the
11contents of a directory.
12
13This is achieved by using a path prefix that any contained path must at least match. If the
14remainder of the path contains a folder
15 - detectable because it's not a leaf and so contains a delimiter - that directory is added. As a
16sub folder may contain many files, and the directory itself may also be defined, it is necessary to
17dedup. The deduping is handled by the StringSliceIndexMap. */
18class ImplicitDirectoryCollector
19{
20public:
21    enum class State
22    {
23        None,            ///< Neither the directory or content have been found
24        DirectoryExists, ///< The directory exists
25        HasContent,      ///< If it has content, the directory must exist
26    };
27
28    /// Get the current state
29    State getState() const
30    {
31        return (m_map.getCount() > 0) ? State::HasContent
32                                      : (m_directoryExists ? State::DirectoryExists : State::None);
33    }
34    /// True if collector at least has the specified state
35    bool hasState(State state) { return Index(getState()) >= Index(state); }
36
37    /// Set that it exists
38    void setDirectoryExists(bool directoryExists) { m_directoryExists = directoryExists; }
39    /// Get if it exists (implicitly or explicitly)
40    bool getDirectoryExists() const { return m_directoryExists || m_map.getCount() > 0; }
41
42    /// True if the path matches the prefix
43    bool hasPrefix(const UnownedStringSlice& path) const
44    {
45        return path.startsWith(m_prefix.getUnownedSlice());
46    }
47
48    /// True if the directory has content
49    bool hasContent() const { return m_map.getCount() > 0; }
50
51    /// Gets the remainder or path after the prefix
52    UnownedStringSlice getRemainder(const UnownedStringSlice& path) const
53    {
54        SLANG_ASSERT(hasPrefix(path));
55        return UnownedStringSlice(path.begin() + m_prefix.getLength(), path.end());
56    }
57
58    /// Add a remaining path
59    void addRemainingPath(SlangPathType pathType, const UnownedStringSlice& inPathRemainder);
60    /// Add a path
61    void addPath(SlangPathType pathType, const UnownedStringSlice& canonicalPath);
62    /// Enumerate the contents
63    SlangResult enumerate(FileSystemContentsCallBack callback, void* userData);
64
65    /// Ctor
66    ImplicitDirectoryCollector(const String& canonicalPath, bool directoryExists = false);
67
68    static bool isRootPath(const UnownedStringSlice& path);
69
70protected:
71    StringSliceIndexMap m_map;
72    String m_prefix;
73    bool m_directoryExists;
74};
75
76} // namespace Slang
77
78#endif