yum-mirror/slang

Making it easier to work with shaders

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

Ellie Hermaszewskaformatf65d756bf

master
3.6 KiB126 linesraw
1// slang-slice-allocator.h
2#ifndef SLANG_SLICE_ALLOCATOR_H
3#define SLANG_SLICE_ALLOCATOR_H
4
5// Has definition of CharSlice
6#include "../core/slang-memory-arena.h"
7#include "slang-artifact.h"
8#include "slang-com-ptr.h"
9
10namespace Slang
11{
12
13
14struct SliceAllocator;
15
16struct SliceUtil
17{
18    /// Convert into a list of strings
19    static List<String> toList(const Slice<TerminatedCharSlice>& in);
20
21    /// Gets a 0 terminated string from a blob. If not possible returns nullptr
22    static const char* getTerminated(ISlangBlob* blob, TerminatedCharSlice& outSlice);
23
24    /// NOTE! the slice is only guarenteed to stay in scope whilst the blob does
25    static TerminatedCharSlice toTerminatedCharSlice(SliceAllocator& allocator, ISlangBlob* blob);
26    ///
27    static TerminatedCharSlice toTerminatedCharSlice(StringBuilder& storage, ISlangBlob* blob);
28
29    /// The slice will only be in scope whilst the string is
30    static TerminatedCharSlice asTerminatedCharSlice(const String& in)
31    {
32        auto unowned = in.getUnownedSlice();
33        return TerminatedCharSlice(unowned.begin(), unowned.getLength());
34    }
35
36    /// Get string as a char slice
37    static CharSlice asCharSlice(const String& in)
38    {
39        auto unowned = in.getUnownedSlice();
40        return CharSlice(unowned.begin(), unowned.getLength());
41    }
42
43    template<typename T>
44    static Slice<T*> asSlice(const List<ComPtr<T>>& list)
45    {
46        return makeSlice((T* const*)list.getBuffer(), list.getCount());
47    }
48
49    /// Get a list as a slice
50    template<typename T>
51    static Slice<T> asSlice(const List<T>& list)
52    {
53        return Slice<T>(list.getBuffer(), list.getCount());
54    }
55
56    template<typename T>
57    static List<ComPtr<T>> toComPtrList(const Slice<T*>& in)
58    {
59        ISlangUnknown* check = (T*)nullptr;
60        SLANG_UNUSED(check);
61        List<ComPtr<T>> list;
62        list.setCount(in.count);
63        for (Index i = 0; i < in.count; ++i)
64            list[i] = ComPtr<T>(in[i]);
65        return list;
66    }
67
68private:
69    /*
70    A reason to wrap in a struct rather than have as free functions is doing so will lead to compile
71    time errors with incorrect usage around temporaries.
72    */
73
74    /// We don't want to make a temporary list into a slice..
75    template<typename T>
76    static Slice<T> asSlice(const List<T>&& list) = delete;
77    // We don't want temporaries to be 'asSliced' so disable
78    static TerminatedCharSlice asTerminatedCharSlice(const String&& in) = delete;
79    static CharSlice asCharSlice(const String&& in) = delete;
80};
81
82SLANG_FORCE_INLINE UnownedStringSlice asStringSlice(const CharSlice& slice)
83{
84    return UnownedStringSlice(slice.begin(), slice.end());
85}
86
87SLANG_FORCE_INLINE CharSlice asCharSlice(const UnownedStringSlice& slice)
88{
89    return CharSlice(slice.begin(), slice.getLength());
90}
91
92SLANG_FORCE_INLINE String asString(const CharSlice& slice)
93{
94    return String(slice.begin(), slice.end());
95}
96
97struct SliceAllocator
98{
99    TerminatedCharSlice allocate(const Slice<char>& slice);
100    TerminatedCharSlice allocate(const UnownedStringSlice& slice);
101    TerminatedCharSlice allocate(const String& in) { return allocate(in.getUnownedSlice()); }
102    TerminatedCharSlice allocate(const char* in);
103    TerminatedCharSlice allocate(const char* start, const char* end)
104    {
105        return allocate(UnownedStringSlice(start, end));
106    }
107
108    Slice<TerminatedCharSlice> allocate(const List<String>& in);
109
110    /// Get the backing arena
111    MemoryArena& getArena() { return m_arena; }
112
113    void deallocateAll() { m_arena.deallocateAll(); }
114
115    SliceAllocator()
116        : m_arena(2097152)
117    {
118    }
119
120protected:
121    MemoryArena m_arena;
122};
123
124} // namespace Slang
125
126#endif