refactor: use template asset structs for AddAsset api

This commit is contained in:
Jan 2024-04-23 00:06:28 +02:00
parent d0c7311dce
commit 931fe695e1
No known key found for this signature in database
GPG Key ID: 44B581F78FF5C57C

View File

@ -19,36 +19,53 @@ public:
_NODISCARD virtual AssetLoadingContext* GetAssetLoadingContext() const = 0; _NODISCARD virtual AssetLoadingContext* GetAssetLoadingContext() const = 0;
virtual XAssetInfoGeneric* AddAsset(std::unique_ptr<XAssetInfoGeneric> xAssetInfo) = 0; template<typename AssetType> XAssetInfo<typename AssetType::Type>* AddAsset(const std::string& assetName, typename AssetType::Type* asset)
XAssetInfoGeneric* AddAsset(const asset_type_t assetType, const std::string& assetName, void* asset)
{ {
return AddAsset(assetType, assetName, asset, std::vector<XAssetInfoGeneric*>(), std::vector<scr_string_t>()); static_assert(std::is_base_of_v<IAssetBase, AssetType>);
return static_cast<XAssetInfo<typename AssetType::Type>*>(AddAsset(std::make_unique<XAssetInfoGeneric>(
AssetType::EnumEntry, assetName, asset, std::vector<XAssetInfoGeneric*>(), std::vector<scr_string_t>(), std::vector<IndirectAssetReference>())));
} }
XAssetInfoGeneric* AddAsset(const asset_type_t assetType, const std::string& assetName, void* asset, std::vector<XAssetInfoGeneric*> dependencies) template<typename AssetType>
XAssetInfo<typename AssetType::Type>* AddAsset(const std::string& assetName, typename AssetType::Type* asset, std::vector<XAssetInfoGeneric*> dependencies)
{ {
return AddAsset(assetType, assetName, asset, std::move(dependencies), std::vector<scr_string_t>()); static_assert(std::is_base_of_v<IAssetBase, AssetType>);
return static_cast<XAssetInfo<typename AssetType::Type>*>(AddAsset(std::make_unique<XAssetInfoGeneric>(
AssetType::EnumEntry, assetName, asset, std::move(dependencies), std::vector<scr_string_t>(), std::vector<IndirectAssetReference>())));
} }
XAssetInfoGeneric* AddAsset(const asset_type_t assetType, template<typename AssetType>
const std::string& assetName, XAssetInfo<typename AssetType::Type>* AddAsset(const std::string& assetName,
void* asset, typename AssetType::Type* asset,
std::vector<XAssetInfoGeneric*> dependencies, std::vector<XAssetInfoGeneric*> dependencies,
std::vector<scr_string_t> usedScriptStrings) std::vector<scr_string_t> usedScriptStrings)
{ {
return AddAsset(assetType, assetName, asset, std::move(dependencies), std::move(usedScriptStrings), std::vector<IndirectAssetReference>()); static_assert(std::is_base_of_v<IAssetBase, AssetType>);
return static_cast<XAssetInfo<typename AssetType::Type>*>(AddAsset(std::make_unique<XAssetInfoGeneric>(
AssetType::EnumEntry, assetName, asset, std::move(dependencies), std::move(usedScriptStrings), std::vector<IndirectAssetReference>())));
} }
XAssetInfoGeneric* AddAsset(const asset_type_t assetType, template<typename AssetType>
const std::string& assetName, XAssetInfo<typename AssetType::Type>* AddAsset(const std::string& assetName,
void* asset, typename AssetType::Type* asset,
std::vector<XAssetInfoGeneric*> dependencies, std::vector<XAssetInfoGeneric*> dependencies,
std::vector<scr_string_t> usedScriptStrings, std::vector<scr_string_t> usedScriptStrings,
std::vector<IndirectAssetReference> indirectAssetReferences) std::vector<IndirectAssetReference> indirectAssetReferences)
{ {
return AddAsset(std::make_unique<XAssetInfoGeneric>( static_assert(std::is_base_of_v<IAssetBase, AssetType>);
assetType, assetName, asset, std::move(dependencies), std::move(usedScriptStrings), std::move(indirectAssetReferences)));
return static_cast<XAssetInfo<typename AssetType::Type>*>(AddAsset(std::make_unique<XAssetInfoGeneric>(
AssetType::EnumEntry, assetName, asset, std::move(dependencies), std::move(usedScriptStrings), std::move(indirectAssetReferences))));
}
template<typename AssetType> XAssetInfo<typename AssetType::Type>* AddAsset(std::unique_ptr<XAssetInfo<typename AssetType::Type>> xAssetInfo)
{
static_assert(std::is_base_of_v<IAssetBase, AssetType>);
return static_cast<XAssetInfo<typename AssetType::Type>*>(AddAsset(std::unique_ptr<XAssetInfoGeneric>(xAssetInfo.release())));
} }
template<typename AssetType> XAssetInfo<typename AssetType::Type>* LoadDependency(const std::string& assetName) template<typename AssetType> XAssetInfo<typename AssetType::Type>* LoadDependency(const std::string& assetName)
@ -66,6 +83,7 @@ public:
} }
protected: protected:
virtual XAssetInfoGeneric* AddAsset(std::unique_ptr<XAssetInfoGeneric> xAssetInfo) = 0;
virtual XAssetInfoGeneric* LoadDependency(asset_type_t assetType, const std::string& assetName) = 0; virtual XAssetInfoGeneric* LoadDependency(asset_type_t assetType, const std::string& assetName) = 0;
virtual IndirectAssetReference LoadIndirectAssetReference(asset_type_t assetType, const std::string& assetName) = 0; virtual IndirectAssetReference LoadIndirectAssetReference(asset_type_t assetType, const std::string& assetName) = 0;
}; };