From fcdb4629c4c3dd2931eaa88b96b668d914c4519c Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Wed, 10 Aug 2022 11:37:26 -0400 Subject: Yet more refactoring around Artifact (#2352) * #include an absolute path didn't work - because paths were taken to always be relative. * WIP with hierarchical enums. * Some small fixes and improvements around artifact desc related types. * Improvements around hierarchical enum. * Fixes to get Artifact types refactor to be able to execute tests. * Attempt to better categorize PTX. * Work around for potentially unused function warning. * Typo fix. * Simplify Artifact header. * Small improvements around Artifact kind/payload/style. * Added IDestroyable/ICastable * Add IArtifactList. * First impl of IArtifactUtil. * Use the ICastable interface for IArtifactRepresentation. * Added IArtifactRepresentation & IArtifactAssociated. * Add SLANG_OVERRIDE to avoid gcc/clang warning. * Fix calling convention issue on win32. * Fix missing SLANG_OVERRIDE. * First attempt at file abstraction around Artifact. * Added creation of lock file. * Move functionality for determining file paths to the IArtifactUtil. Add casting to ICastable. * Added some casting/finding mechanisms. * Simplify IArtifact interface, and use Items for file reps. * Fix problem with libraries on DXIL. * Split out ArtifactRepresentation. * Move ArtifactDesc functionality to ArtifactDescUtil. ArtifactInfoUtil becomes ArtifactDescUtil. * Split implementations from the interfaces for Artifact. * Use TypeTextUtil for target name outputting. * Add artifact impls. * Add ICastableList * Added UnknownCastableAdapter * Make ISlangSharedLibrary derive from ICastable, and remain backwards compatible with slang-llvm. * Refactor Representation on Artifact. * Make our ISlangBlobs also derive from ICastable. Make ISlangBlob atomic ref counted. * Split out CastableList and related types, and placed in core. * Small fixes around IArtifact. Improve IArtifact docs. First impl of getChildren for IArtifact. * Documentation improvements for Artifact related types. * Fix typo. * Special case adding a ICastableList to a LazyCastableList. * Small simplification of LazyCastableList, by adding State member. * Removed the ILockFile interface because IFileArtifactRepresentation can be used. --- source/core/slang-lazy-castable-list.cpp | 163 +++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 source/core/slang-lazy-castable-list.cpp (limited to 'source/core/slang-lazy-castable-list.cpp') diff --git a/source/core/slang-lazy-castable-list.cpp b/source/core/slang-lazy-castable-list.cpp new file mode 100644 index 000000000..d9f0090d5 --- /dev/null +++ b/source/core/slang-lazy-castable-list.cpp @@ -0,0 +1,163 @@ +// slang-lazy-castable-list.cpp +#include "slang-lazy-castable-list.h" + +#include "slang-castable-list-impl.h" + +namespace Slang { + +void LazyCastableList::removeAt(Index index) +{ + SLANG_ASSERT(index >= 0 && index < getCount()); + + switch (m_state) + { + case State::None: break; + case State::One: + { + m_state = State::None; + m_castable.setNull(); + break; + } + case State::List: + { + static_cast(m_castable.get())->removeAt(index); + break; + } + } +} + +void LazyCastableList::clear() +{ + if (m_state == State::List) + { + auto list = static_cast(m_castable.get()); + list->clear(); + } + else + { + m_state = State::None; + m_castable.setNull(); + } +} + +void LazyCastableList::clearAndDeallocate() +{ + m_state = State::None; + m_castable.setNull(); +} + +Count LazyCastableList::getCount() const +{ + switch (m_state) + { + case State::None: return 0; + case State::One: return 1; + default: + case State::List: return static_cast(m_castable.get())->getCount(); + } +} + +void LazyCastableList::add(ICastable* castable) +{ + SLANG_ASSERT(castable); + if (m_state == State::None) + { + m_castable = castable; + m_state = State::One; + } + else + { + requireList()->add(castable); + } +} + +ICastableList* LazyCastableList::requireList() +{ + switch (m_state) + { + case State::None: + { + m_castable = new CastableList; + m_state = State::List; + break; + } + case State::One: + { + // Turn into a list + auto list = new CastableList; + list->add(m_castable); + m_castable = list; + m_state = State::List; + break; + } + default: break; + } + SLANG_ASSERT(m_state == State::List); + return static_cast(m_castable.get()); +} + +ICastableList* LazyCastableList::getList() +{ + return (m_state == State::None) ? nullptr : requireList(); +} + +void* LazyCastableList::find(const Guid& guid) +{ + for (auto castable : getView()) + { + if (auto ptr = castable->castAs(guid)) + { + return ptr; + } + } + return nullptr; +} + +ConstArrayView LazyCastableList::getView() const +{ + switch (m_state) + { + case State::None: return ConstArrayView(); + case State::One: return ConstArrayView((ICastable*const*)&m_castable, 1); + default: + case State::List: + { + auto list = static_cast(m_castable.get()); + return ConstArrayView(list->getBuffer(), list->getCount()); + } + } +} + +Index LazyCastableList::indexOf(ICastable* castable) const +{ + return getView().indexOf(castable); +} + +Index LazyCastableList::indexOfUnknown(ISlangUnknown* unk) const +{ + // Try as a ICastable first + { + ComPtr castable; + if (SLANG_SUCCEEDED(unk->queryInterface(ICastable::getTypeGuid(), (void**)castable.writeRef())) && castable) + { + return indexOf(castable); + } + } + + // It's not derived from ICastable, so can only be in list via an adapter + const auto view = getView(); + + const Count count = view.getCount(); + for (Index i = 0; i < count; ++i) + { + auto adapter = as(view[i]); + if (adapter && adapter->getContained() == unk) + { + return i; + } + } + + return -1; +} + +} // namespace Slang -- cgit v1.2.3