yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f65d756bf
master
1// slang-artifact-representation-impl.cpp 2#include "slang-artifact-representation-impl.h" 3 4#include "../core/slang-array-view.h" 5#include "../core/slang-castable.h" 6#include "../core/slang-file-system.h" 7#include "../core/slang-io.h" 8#include "../core/slang-type-text-util.h" 9#include "slang-artifact-util.h" 10 11namespace Slang 12{ 13 14/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ExtFileArtifactRepresentation !!!!!!!!!!!!!!!!!!!!!!!!!!! */ 15 16void * ExtFileArtifactRepresentation ::getInterface (const Guid & guid ) 17{ 18if (guid == ISlangUnknown ::getTypeGuid ()|| guid == ICastable ::getTypeGuid ()|| 19guid == IArtifactRepresentation ::getTypeGuid ()|| 20guid == IPathArtifactRepresentation ::getTypeGuid ()|| 21guid == IExtFileArtifactRepresentation ::getTypeGuid ()) 22 { 23return static_cast < IExtFileArtifactRepresentation *> (this ); 24 } 25return nullptr ; 26} 27 28void * ExtFileArtifactRepresentation ::getObject (const Guid & guid ) 29{ 30SLANG_UNUSED (guid ); 31return nullptr ; 32} 33 34void * ExtFileArtifactRepresentation ::castAs (const Guid & guid ) 35{ 36if (auto intf = getInterface (guid )) 37 { 38return intf ; 39 } 40return getObject (guid ); 41} 42 43SlangResult ExtFileArtifactRepresentation ::createRepresentation ( 44const Guid & typeGuid , 45ICastable ** outCastable ) 46{ 47// We can convert into a blob only, and only if we have a path 48// If it's referenced by a name only, it's a file that *can't* be loaded as a blob in general. 49if (typeGuid != ISlangBlob ::getTypeGuid ()) 50 { 51return SLANG_E_NOT_AVAILABLE ; 52 } 53 54ComPtr < ISlangBlob > blob ; 55SLANG_RETURN_ON_FAIL (m_fileSystem -> loadFile (m_path .getBuffer (),blob .writeRef ())); 56 57* outCastable = CastableUtil ::getCastable (blob ).detach (); 58return SLANG_OK ; 59} 60 61bool ExtFileArtifactRepresentation ::exists () 62{ 63SlangPathType pathType ; 64const auto res = m_fileSystem -> getPathType (m_path .getBuffer (),& pathType ); 65// It exists if it is a file 66return SLANG_SUCCEEDED (res )&& pathType == getPathType (); 67} 68 69const char * ExtFileArtifactRepresentation ::getUniqueIdentity () 70{ 71if (m_uniqueIdentity .getLength ()== 0 ) 72 { 73ComPtr < ISlangBlob > uniqueIdentityBlob ; 74if (SLANG_FAILED (m_fileSystem -> getFileUniqueIdentity ( 75m_path .getBuffer (), 76uniqueIdentityBlob .writeRef ()))) 77 { 78return nullptr ; 79 } 80m_uniqueIdentity = StringUtil ::getString (uniqueIdentityBlob ); 81 } 82 83return m_uniqueIdentity .getLength () ?m_uniqueIdentity .getBuffer () :nullptr ; 84} 85 86/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! SourceBlobWithPathArtifactRepresentation 87* !!!!!!!!!!!!!!!!!!!!!!!!!!! */ 88 89void * SourceBlobWithPathInfoArtifactRepresentation ::getInterface (const Guid & guid ) 90{ 91if (guid == ISlangUnknown ::getTypeGuid ()|| guid == ICastable ::getTypeGuid ()|| 92guid == IArtifactRepresentation ::getTypeGuid ()|| 93guid == IPathArtifactRepresentation ::getTypeGuid ()) 94 { 95return static_cast < IPathArtifactRepresentation *> (this ); 96 } 97return nullptr ; 98} 99 100void * SourceBlobWithPathInfoArtifactRepresentation ::getObject (const Guid & guid ) 101{ 102SLANG_UNUSED (guid ); 103return nullptr ; 104} 105 106void * SourceBlobWithPathInfoArtifactRepresentation ::castAs (const Guid & guid ) 107{ 108if (auto intf = getInterface (guid )) 109 { 110return intf ; 111 } 112return getObject (guid ); 113} 114 115SlangResult SourceBlobWithPathInfoArtifactRepresentation ::createRepresentation ( 116const Guid & typeGuid , 117ICastable ** outCastable ) 118{ 119// We can convert into a blob only. 120if (typeGuid != ISlangBlob ::getTypeGuid ()) 121 { 122return SLANG_E_NOT_AVAILABLE ; 123 } 124 125if (!m_blob ) 126 { 127return SLANG_E_NOT_AVAILABLE ; 128 } 129 130* outCastable = CastableUtil ::getCastable (m_blob ).detach (); 131return SLANG_OK ; 132} 133 134/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! FileArtifactRepresentation !!!!!!!!!!!!!!!!!!!!!!!!!!! */ 135 136void * OSFileArtifactRepresentation ::getInterface (const Guid & guid ) 137{ 138if (guid == ISlangUnknown ::getTypeGuid ()|| guid == ICastable ::getTypeGuid ()|| 139guid == IArtifactRepresentation ::getTypeGuid ()|| 140guid == IPathArtifactRepresentation ::getTypeGuid ()|| 141guid == IOSFileArtifactRepresentation ::getTypeGuid ()) 142 { 143return static_cast < IOSFileArtifactRepresentation *> (this ); 144 } 145return nullptr ; 146} 147 148void * OSFileArtifactRepresentation ::getObject (const Guid & guid ) 149{ 150SLANG_UNUSED (guid ); 151return nullptr ; 152} 153 154/* static */ ISlangMutableFileSystem * OSFileArtifactRepresentation ::_getFileSystem () 155{ 156return OSFileSystem ::getMutableSingleton (); 157} 158 159void * OSFileArtifactRepresentation ::castAs (const Guid & guid ) 160{ 161if (auto intf = getInterface (guid )) 162 { 163return intf ; 164 } 165return getObject (guid ); 166} 167 168SlangResult OSFileArtifactRepresentation ::createRepresentation ( 169const Guid & typeGuid , 170ICastable ** outCastable ) 171{ 172// We can convert into a blob only, and only if we have a path 173// If it's referenced by a name only, it's a file that *can't* be loaded as a blob in general. 174if (typeGuid != ISlangBlob ::getTypeGuid ()|| m_kind == Kind ::NameOnly ) 175 { 176return SLANG_E_NOT_AVAILABLE ; 177 } 178 179ComPtr < ISlangBlob > blob ; 180 181auto fileSystem = _getFileSystem (); 182SLANG_RETURN_ON_FAIL (fileSystem -> loadFile (m_path .getBuffer (),blob .writeRef ())); 183 184* outCastable = CastableUtil ::getCastable (blob ).detach (); 185return SLANG_OK ; 186} 187 188bool OSFileArtifactRepresentation ::exists () 189{ 190// TODO(JS): 191// If it's a name only it's hard to know what exists should do. It can't *check* because it 192// relies on the 'system' doing the actual location. We could ask the IArtifactUtil, and that 193// could change the behavior. For now we just assume it does. 194if (m_kind == Kind ::NameOnly ) 195 { 196return true; 197 } 198 199auto fileSystem = _getFileSystem (); 200 201SlangPathType pathType ; 202const auto res = fileSystem -> getPathType (m_path .getBuffer (),& pathType ); 203 204// It exists if it is a file 205return SLANG_SUCCEEDED (res )&& pathType == SLANG_PATH_TYPE_FILE ; 206} 207 208const char * OSFileArtifactRepresentation ::getUniqueIdentity () 209{ 210if (m_uniqueIdentity .getLength ()== 0 ) 211 { 212auto fileSystem = _getFileSystem (); 213 214ComPtr < ISlangBlob > uniqueIdentityBlob ; 215if (SLANG_FAILED (fileSystem -> getFileUniqueIdentity ( 216m_path .getBuffer (), 217uniqueIdentityBlob .writeRef ()))) 218 { 219return nullptr ; 220 } 221m_uniqueIdentity = StringUtil ::getString (uniqueIdentityBlob ); 222 } 223 224return m_uniqueIdentity .getLength () ?m_uniqueIdentity .getBuffer () :nullptr ; 225} 226 227void OSFileArtifactRepresentation ::disown () 228{ 229if (_isOwned ()) 230 { 231m_kind = Kind ::Reference ; 232 } 233} 234 235OSFileArtifactRepresentation ::~OSFileArtifactRepresentation () 236{ 237if (_isOwned ()) 238 { 239auto fileSystem = _getFileSystem (); 240fileSystem -> remove (m_path .getBuffer ()); 241 } 242} 243 244/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! PostEmitMetadataArtifactRepresentation !!!!!!!!!!!!!!!!!!!!!!!!!!! 245*/ 246 247void * ObjectArtifactRepresentation ::castAs (const Guid & guid ) 248{ 249 250if (auto ptr = getInterface (guid )) 251 { 252return ptr ; 253 } 254return getObject (guid ); 255} 256 257void * ObjectArtifactRepresentation ::getInterface (const Guid & guid ) 258{ 259if (guid == ISlangUnknown ::getTypeGuid ()|| guid == ICastable ::getTypeGuid ()|| 260guid == IArtifactRepresentation ::getTypeGuid ()) 261 { 262return static_cast < IArtifactRepresentation *> (this ); 263 } 264return nullptr ; 265} 266 267void * ObjectArtifactRepresentation ::getObject (const Guid & guid ) 268{ 269if (guid == getTypeGuid ()) 270 { 271return this ; 272 } 273 274// If matches the guid saved in the object, we return that 275if (m_object && m_typeGuid == guid ) 276 { 277return m_object ; 278 } 279 280return nullptr ; 281} 282 283}// namespace Slang