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