Skip to content

Commit

Permalink
Add ResultPointer.
Browse files Browse the repository at this point in the history
  • Loading branch information
kring committed Oct 29, 2024
1 parent 49f1d63 commit d43edce
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Cesium3DTilesSelection/src/TileContentLoadInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ TileContentLoadInfo::TileContentLoadInfo(
const std::shared_ptr<IPrepareRendererResources>&
pPrepareRendererResources_,
const std::shared_ptr<spdlog::logger>& pLogger_,
const CesiumUtility::IntrusivePointer<TilesetSharedAssetSystem>
const CesiumUtility::IntrusivePointer<TilesetSharedAssetSystem>&
pSharedAssetSystem_,
const TilesetContentOptions& contentOptions_,
const Tile& tile)
Expand Down
2 changes: 1 addition & 1 deletion Cesium3DTilesSelection/src/TileContentLoadInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct TileContentLoadInfo {
const std::shared_ptr<IPrepareRendererResources>&
pPrepareRendererResources,
const std::shared_ptr<spdlog::logger>& pLogger,
const CesiumUtility::IntrusivePointer<TilesetSharedAssetSystem>
const CesiumUtility::IntrusivePointer<TilesetSharedAssetSystem>&
pSharedAssetSystem,
const TilesetContentOptions& contentOptions,
const Tile& tile);
Expand Down
30 changes: 12 additions & 18 deletions CesiumAsync/include/CesiumAsync/SharedAssetDepot.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ class CESIUMASYNC_API SharedAssetDepot
*/
int64_t staleAssetSizeLimit = 16 * 1024 * 1024;

using FactorySignature = CesiumAsync::Future<
CesiumUtility::Result<CesiumUtility::IntrusivePointer<TAssetType>>>(
const AsyncSystem& asyncSystem,
const std::shared_ptr<IAssetAccessor>& pAssetAccessor,
const TAssetKey& key);
using FactorySignature =
CesiumAsync::Future<CesiumUtility::ResultPointer<TAssetType>>(
const AsyncSystem& asyncSystem,
const std::shared_ptr<IAssetAccessor>& pAssetAccessor,
const TAssetKey& key);

SharedAssetDepot(std::function<FactorySignature> factory)
: _factory(std::move(factory)) {}
Expand All @@ -78,9 +78,7 @@ class CESIUMASYNC_API SharedAssetDepot
* @param assetKey The key uniquely identifying the asset to get or create.
* @return A shared future that resolves when the asset is ready or fails.
*/
SharedFuture<
CesiumUtility::Result<CesiumUtility::IntrusivePointer<TAssetType>>>
getOrCreate(
SharedFuture<CesiumUtility::ResultPointer<TAssetType>> getOrCreate(
const AsyncSystem& asyncSystem,
const std::shared_ptr<IAssetAccessor>& pAssetAccessor,
const TAssetKey& assetKey) {
Expand Down Expand Up @@ -154,9 +152,8 @@ class CESIUMASYNC_API SharedAssetDepot
return pEntry->toResult();
});

SharedFuture<
CesiumUtility::Result<CesiumUtility::IntrusivePointer<TAssetType>>>
sharedFuture = std::move(future).share();
SharedFuture<CesiumUtility::ResultPointer<TAssetType>> sharedFuture =
std::move(future).share();

pEntry->maybePendingAsset = sharedFuture;

Expand Down Expand Up @@ -315,8 +312,7 @@ class CESIUMASYNC_API SharedAssetDepot
* future that will resolve when the asset load is complete. This field will
* be empty if the asset finished loading, including if it failed to load.
*/
std::optional<SharedFuture<
CesiumUtility::Result<CesiumUtility::IntrusivePointer<TAssetType>>>>
std::optional<SharedFuture<CesiumUtility::ResultPointer<TAssetType>>>
maybePendingAsset;

/**
Expand All @@ -340,15 +336,13 @@ class CESIUMASYNC_API SharedAssetDepot
*/
CesiumUtility::DoublyLinkedListPointers<AssetEntry> deletionListPointers;

CesiumUtility::Result<CesiumUtility::IntrusivePointer<TAssetType>>
toResult() const {
return CesiumUtility::Result<CesiumUtility::IntrusivePointer<TAssetType>>(
CesiumUtility::ResultPointer<TAssetType> toResult() const {
return CesiumUtility::ResultPointer<TAssetType>(
pAsset.get(),
errorsAndWarnings);
}

SharedFuture<
CesiumUtility::Result<CesiumUtility::IntrusivePointer<TAssetType>>>
SharedFuture<CesiumUtility::ResultPointer<TAssetType>>
toFuture(const AsyncSystem& asyncSystem) const {
if (this->maybePendingAsset) {
return *this->maybePendingAsset;
Expand Down
7 changes: 3 additions & 4 deletions CesiumGltfReader/src/GltfReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ void CesiumGltfReader::GltfReader::postprocessGltf(
const std::shared_ptr<IAssetAccessor>& pAssetAccessor,
const std::string& uri,
const std::vector<IAssetAccessor::THeader>& headers)
-> SharedFuture<Result<IntrusivePointer<ImageAsset>>> {
-> SharedFuture<ResultPointer<ImageAsset>> {
NetworkImageAssetDescriptor assetKey{
uri,
headers,
Expand All @@ -554,12 +554,11 @@ void CesiumGltfReader::GltfReader::postprocessGltf(
}
};

SharedFuture<Result<IntrusivePointer<ImageAsset>>> future =
SharedFuture<ResultPointer<ImageAsset>> future =
getAsset(asyncSystem, pAssetAccessor, uri, tHeaders);

resolvedBuffers.push_back(future.thenInWorkerThread(
[pImage = &image](
const Result<IntrusivePointer<ImageAsset>>& loadedImage) {
[pImage = &image](const ResultPointer<ImageAsset>& loadedImage) {
std::string imageUri = *pImage->uri;
pImage->uri = std::nullopt;

Expand Down
2 changes: 1 addition & 1 deletion CesiumGltfReader/src/GltfSharedAssetSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ CesiumUtility::IntrusivePointer<GltfSharedAssetSystem> createDefault() {
[](const AsyncSystem& asyncSystem,
const std::shared_ptr<IAssetAccessor>& pAssetAccessor,
const NetworkImageAssetDescriptor& key)
-> Future<Result<IntrusivePointer<ImageAsset>>> {
-> Future<ResultPointer<ImageAsset>> {
return key.load(asyncSystem, pAssetAccessor);
}));

Expand Down
6 changes: 3 additions & 3 deletions CesiumGltfReader/src/NetworkImageAssetDescriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ bool NetworkImageAssetDescriptor::operator==(
this->ktx2TranscodeTargets.UASTC_RGBA;
}

Future<Result<IntrusivePointer<ImageAsset>>> NetworkImageAssetDescriptor::load(
Future<ResultPointer<ImageAsset>> NetworkImageAssetDescriptor::load(
const AsyncSystem& asyncSystem,
const std::shared_ptr<IAssetAccessor>& pAssetAccessor) const {
return this->loadBytesFromNetwork(asyncSystem, pAssetAccessor)
.thenInWorkerThread([ktx2TranscodeTargets = this->ktx2TranscodeTargets](
Result<std::vector<std::byte>>&& result) {
if (!result.value) {
return Result<IntrusivePointer<ImageAsset>>(result.errors);
return ResultPointer<ImageAsset>(result.errors);
}

ImageReaderResult imageResult =
Expand All @@ -48,7 +48,7 @@ Future<Result<IntrusivePointer<ImageAsset>>> NetworkImageAssetDescriptor::load(
result.errors.merge(
ErrorList{imageResult.errors, imageResult.warnings});

return Result<IntrusivePointer<ImageAsset>>(
return ResultPointer<ImageAsset>(
imageResult.pImage,
std::move(result.errors));
});
Expand Down
8 changes: 8 additions & 0 deletions CesiumUtility/include/CesiumUtility/Result.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,12 @@ template <typename T> struct Result<CesiumUtility::IntrusivePointer<T>> {
ErrorList errors;
};

/**
* @brief A convenient shortcut for
* `CesiumUtility::Result<CesiumUtility::IntrusivePointer<T>>`.
*
* @tparam T The type of object that the IntrusivePointer points to.
*/
template <typename T> using ResultPointer = Result<IntrusivePointer<T>>;

} // namespace CesiumUtility

0 comments on commit d43edce

Please sign in to comment.