summaryrefslogtreecommitdiffstats
path: root/source/compiler-core/slang-slice-allocator.h
blob: 504fbe1b7528a123011c12e75b67a97e21ac748e (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// slang-slice-allocator.h
#ifndef SLANG_SLICE_ALLOCATOR_H
#define SLANG_SLICE_ALLOCATOR_H

// Has definition of CharSlice
#include "../core/slang-memory-arena.h"
#include "slang-artifact.h"
#include "slang-com-ptr.h"

namespace Slang
{


struct SliceAllocator;

struct SliceUtil
{
    /// Convert into a list of strings
    static List<String> toList(const Slice<TerminatedCharSlice>& in);

    /// Gets a 0 terminated string from a blob. If not possible returns nullptr
    static const char* getTerminated(ISlangBlob* blob, TerminatedCharSlice& outSlice);

    /// NOTE! the slice is only guarenteed to stay in scope whilst the blob does
    static TerminatedCharSlice toTerminatedCharSlice(SliceAllocator& allocator, ISlangBlob* blob);
    ///
    static TerminatedCharSlice toTerminatedCharSlice(StringBuilder& storage, ISlangBlob* blob);

    /// The slice will only be in scope whilst the string is
    static TerminatedCharSlice asTerminatedCharSlice(const String& in)
    {
        auto unowned = in.getUnownedSlice();
        return TerminatedCharSlice(unowned.begin(), unowned.getLength());
    }

    /// Get string as a char slice
    static CharSlice asCharSlice(const String& in)
    {
        auto unowned = in.getUnownedSlice();
        return CharSlice(unowned.begin(), unowned.getLength());
    }

    template<typename T>
    static Slice<T*> asSlice(const List<ComPtr<T>>& list)
    {
        return makeSlice((T* const*)list.getBuffer(), list.getCount());
    }

    /// Get a list as a slice
    template<typename T>
    static Slice<T> asSlice(const List<T>& list)
    {
        return Slice<T>(list.getBuffer(), list.getCount());
    }

    template<typename T>
    static List<ComPtr<T>> toComPtrList(const Slice<T*>& in)
    {
        ISlangUnknown* check = (T*)nullptr;
        SLANG_UNUSED(check);
        List<ComPtr<T>> list;
        list.setCount(in.count);
        for (Index i = 0; i < in.count; ++i)
            list[i] = ComPtr<T>(in[i]);
        return list;
    }

private:
    /*
    A reason to wrap in a struct rather than have as free functions is doing so will lead to compile
    time errors with incorrect usage around temporaries.
    */

    /// We don't want to make a temporary list into a slice..
    template<typename T>
    static Slice<T> asSlice(const List<T>&& list) = delete;
    // We don't want temporaries to be 'asSliced' so disable
    static TerminatedCharSlice asTerminatedCharSlice(const String&& in) = delete;
    static CharSlice asCharSlice(const String&& in) = delete;
};

SLANG_FORCE_INLINE UnownedStringSlice asStringSlice(const CharSlice& slice)
{
    return UnownedStringSlice(slice.begin(), slice.end());
}

SLANG_FORCE_INLINE CharSlice asCharSlice(const UnownedStringSlice& slice)
{
    return CharSlice(slice.begin(), slice.getLength());
}

SLANG_FORCE_INLINE String asString(const CharSlice& slice)
{
    return String(slice.begin(), slice.end());
}

struct SliceAllocator
{
    TerminatedCharSlice allocate(const Slice<char>& slice);
    TerminatedCharSlice allocate(const UnownedStringSlice& slice);
    TerminatedCharSlice allocate(const String& in) { return allocate(in.getUnownedSlice()); }
    TerminatedCharSlice allocate(const char* in);
    TerminatedCharSlice allocate(const char* start, const char* end)
    {
        return allocate(UnownedStringSlice(start, end));
    }

    Slice<TerminatedCharSlice> allocate(const List<String>& in);

    /// Get the backing arena
    MemoryArena& getArena() { return m_arena; }

    void deallocateAll() { m_arena.deallocateAll(); }

    SliceAllocator()
        : m_arena(2097152)
    {
    }

protected:
    MemoryArena m_arena;
};

} // namespace Slang

#endif