yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f65d756bf
master
1#include "slang-blob.h" 2 3namespace Slang 4{ 5 6/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! BlobBase !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ 7 8ISlangUnknown * BlobBase ::getInterface (const Guid & guid ) 9{ 10if (guid == ISlangUnknown ::getTypeGuid ()|| guid == ISlangBlob ::getTypeGuid ()) 11 { 12return static_cast < ISlangBlob *> (this ); 13 } 14if (guid == ICastable ::getTypeGuid ()) 15 { 16return static_cast < ICastable *> (this ); 17 } 18return nullptr ; 19} 20 21void * BlobBase ::getObject (const Guid & guid ) 22{ 23SLANG_UNUSED (guid ); 24return nullptr ; 25} 26 27void * BlobBase ::castAs (const SlangUUID & guid ) 28{ 29if (auto intf = getInterface (guid )) 30 { 31return intf ; 32 } 33return getObject (guid ); 34} 35 36/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! StringBlob !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ 37 38void StringBlob ::_setUniqueRep (StringRepresentation * uniqueRep ) 39{ 40SLANG_ASSERT (uniqueRep == nullptr || uniqueRep -> isUniquelyReferenced ()); 41 42m_uniqueRep = uniqueRep ; 43 44m_slice = uniqueRep ?UnownedTerminatedStringSlice (uniqueRep -> getData (),uniqueRep -> getLength ()) 45 :UnownedTerminatedStringSlice (); 46} 47 48/* static */ StringRepresentation * StringBlob ::_createUniqueCopy (StringRepresentation * rep ) 49{ 50if (rep ) 51 { 52// If the length is 0, we can just init as empty 53auto length = rep -> getLength (); 54if (length == 0 ) 55 { 56return nullptr ; 57 } 58else 59 { 60const UnownedStringSlice slice (rep -> getData (),length ); 61return StringRepresentation ::createWithReference (slice ); 62 } 63 } 64return nullptr ; 65} 66 67void StringBlob ::_setWithCopy (StringRepresentation * rep ) 68{ 69_setUniqueRep (_createUniqueCopy (rep )); 70} 71 72void StringBlob ::_setWithMove (StringRepresentation * rep ) 73{ 74if (rep && !rep -> isUniquelyReferenced ()) 75 { 76_setUniqueRep (_createUniqueCopy (rep )); 77// We need to release a ref as rep is passed in with the 'current' ref count 78rep -> releaseReference (); 79 } 80else 81 { 82_setUniqueRep (rep ); 83 } 84} 85 86/* static */ ComPtr < ISlangBlob > StringBlob ::create (const UnownedStringSlice & slice ) 87{ 88StringRepresentation * rep = nullptr ; 89if (slice .getLength ()) 90 { 91rep = StringRepresentation ::createWithReference (slice ); 92 } 93 94auto blob = new StringBlob ; 95 96// rep must be unique at this point 97blob -> _setUniqueRep (rep ); 98return ComPtr < ISlangBlob > (blob ); 99} 100 101/* static */ ComPtr < ISlangBlob > StringBlob ::create (const String & in ) 102{ 103auto blob = new StringBlob ; 104blob -> _setWithCopy (in .getStringRepresentation ()); 105return ComPtr < ISlangBlob > (blob ); 106} 107 108/* static */ ComPtr < ISlangBlob > StringBlob ::moveCreate (String & in ) 109{ 110auto blob = new StringBlob ; 111blob -> _setWithMove (in .detachStringRepresentation ()); 112return ComPtr < ISlangBlob > (blob ); 113} 114 115/* static */ ComPtr < ISlangBlob > StringBlob ::moveCreate (String && in ) 116{ 117auto blob = new StringBlob ; 118blob -> _setWithMove (in .detachStringRepresentation ()); 119return ComPtr < ISlangBlob > (blob ); 120} 121 122StringBlob ::~StringBlob () 123{ 124if (m_uniqueRep ) 125 { 126delete m_uniqueRep ; 127 } 128} 129void * StringBlob ::castAs (const SlangUUID & guid ) 130{ 131if (auto intf = getInterface (guid )) 132 { 133return intf ; 134 } 135return getObject (guid ); 136} 137 138void * StringBlob ::getObject (const Guid & guid ) 139{ 140// Can allow accessing the contained String 141if (guid == getTypeGuid ()) 142 { 143return this ; 144 } 145// Can always be accessed as terminated char* 146if (guid == SlangTerminatedChars ::getTypeGuid ()) 147 { 148return const_cast < char *> (m_slice .begin ()); 149 } 150return nullptr ; 151} 152 153/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! RawBlob !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ 154 155void * RawBlob ::castAs (const SlangUUID & guid ) 156{ 157if (auto intf = getInterface (guid )) 158 { 159return intf ; 160 } 161return getObject (guid ); 162} 163 164void * RawBlob ::getObject (const Guid & guid ) 165{ 166// If the data has 0 termination, we can return the pointer 167if (guid == SlangTerminatedChars ::getTypeGuid ()&& m_data .isTerminated ()) 168 { 169return (char * )m_data .getData (); 170 } 171return nullptr ; 172} 173 174/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ScopeBlob !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ 175 176void * ScopeBlob ::castAs (const SlangUUID & guid ) 177{ 178if (auto intf = getInterface (guid )) 179 { 180return intf ; 181 } 182if (auto obj = getObject (guid )) 183 { 184return obj ; 185 } 186 187// If the contained thing is castable, ask it 188if (m_castable ) 189 { 190return m_castable -> castAs (guid ); 191 } 192 193return nullptr ; 194} 195 196/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ListBlob !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ 197 198void * ListBlob ::castAs (const SlangUUID & guid ) 199{ 200if (auto intf = getInterface (guid )) 201 { 202return intf ; 203 } 204return getObject (guid ); 205} 206 207void * ListBlob ::getObject (const Guid & guid ) 208{ 209// If the data is terminated return the pointer 210if (guid == SlangTerminatedChars ::getTypeGuid ()) 211 { 212const auto count = m_data .getCount (); 213if (m_data .getCapacity ()> count ) 214 { 215auto buf = m_data .getBuffer (); 216if (buf [count ]== 0 ) 217 { 218return (char * )buf ; 219 } 220 } 221 } 222return nullptr ; 223} 224 225/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! StaticBlob !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ 226 227SlangResult StaticBlob ::queryInterface (SlangUUID const & guid ,void ** outObject ) 228{ 229if (auto intf = getInterface (guid )) 230 { 231* outObject = intf ; 232return SLANG_OK ; 233 } 234return SLANG_E_NO_INTERFACE ; 235} 236 237void * StaticBlob ::castAs (const SlangUUID & guid ) 238{ 239if (auto intf = getInterface (guid )) 240 { 241return intf ; 242 } 243return getObject (guid ); 244} 245 246ISlangUnknown * StaticBlob ::getInterface (const Guid & guid ) 247{ 248if (guid == ISlangUnknown ::getTypeGuid ()|| guid == ISlangBlob ::getTypeGuid ()) 249 { 250return static_cast < ISlangBlob *> (this ); 251 } 252if (guid == ICastable ::getTypeGuid ()) 253 { 254return static_cast < ICastable *> (this ); 255 } 256return nullptr ; 257} 258 259void * StaticBlob ::getObject (const Guid & guid ) 260{ 261SLANG_UNUSED (guid ); 262return nullptr ; 263} 264 265}// namespace Slang