Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added CompositeVariantWithCallback #200

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions src/composition.cc
Original file line number Diff line number Diff line change
Expand Up @@ -944,14 +944,15 @@ bool CompositePayloadRec(uint32_t depth, AssetResolutionResolver &resolver,
}

bool CompositeVariantRec(uint32_t depth, PrimSpec &primspec /* [inout] */,
std::string *warn, std::string *err) {
std::string *warn, std::string *err,
VariantSelectionCallback variant_selection_callback, void *userdata) {
if (depth > (1024 * 1024)) {
PUSH_ERROR_AND_RETURN("Too deep.");
}

// Traverse children first.
for (auto &child : primspec.children()) {
if (!CompositeVariantRec(depth + 1, child, warn, err)) {
if (!CompositeVariantRec(depth + 1, child, warn, err, variant_selection_callback, userdata)) {
return false;
}
}
Expand All @@ -960,6 +961,12 @@ bool CompositeVariantRec(uint32_t depth, PrimSpec &primspec /* [inout] */,
std::map<std::string, std::string>
variant_selection; // empty = use variant settings in PrimSpec.

if (variant_selection_callback) {
if (!variant_selection_callback(primspec, &variant_selection, warn, err, userdata)) {
return false;
}
}

if (!VariantSelectPrimSpec(dst, primspec, variant_selection, warn, err)) {
return false;
}
Expand Down Expand Up @@ -1097,7 +1104,28 @@ bool CompositeVariant(const Layer &in_layer, Layer *composited_layer,
Layer dst = in_layer; // deep copy

for (auto &item : dst.primspecs()) {
if (!CompositeVariantRec(/* depth */ 0, item.second, warn, err)) {
if (!CompositeVariantRec(/* depth */ 0, item.second, warn, err, nullptr, nullptr)) {
PUSH_ERROR_AND_RETURN("Composite `variantSet` failed.");
}
}

(*composited_layer) = dst;

DCOUT("Composite `variantSet` ok.");
return true;
}

bool CompositeVariantWithCallback(const Layer &in_layer, Layer *composited_layer,
std::string *warn, std::string *err,
VariantSelectionCallback variant_selection_callback, void *userdata) {
if (!composited_layer) {
return false;
}

Layer dst = in_layer; // deep copy

for (auto &item : dst.primspecs()) {
if (!CompositeVariantRec(/* depth */ 0, item.second, warn, err, variant_selection_callback, userdata)) {
PUSH_ERROR_AND_RETURN("Composite `variantSet` failed.");
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/composition.hh
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,21 @@ bool CompositeVariant(
const Layer &layer,
Layer *composited_layer, std::string *warn, std::string *err);

///
/// Resolve `variantSet` for each PrimSpec, and return composited(flattened) Layer
/// to `composited_layer` in `layer`.
/// Use a callback to select the specific variant (empty for default)
/// To externally specify variants to select, Use `ApplyVariantSelector`.
///
typedef bool (*VariantSelectionCallback)(
const PrimSpec &primspec, std::map<std::string, std::string>* variant_selection,
std::string *warn, std::string *err,
void *userdata);

bool CompositeVariantWithCallback(
const Layer &layer,
Layer *composited_layer, std::string *warn, std::string *err, VariantSelectionCallback variant_selection_callback, void *userdata);

///
/// Resolve `specializes` for each PrimSpec, and return composited(flattened) Layer
/// to `composited_layer` in `layer`.
Expand Down
Loading