From fc879e25fd4b45ec3520b4bd0cbfb4bec4ad1590 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Mon, 20 Apr 2020 14:00:17 -0700 Subject: libstage: introduce 'adopt' flag to stage_conf_t When set, this turns stage_new() into a stage_replace() against the supplied parent. So if you have an existing stage_t and you wish to turn it into something else directly via stage_new(), simply pass it in as the parent with the adopt flag set. Previously it was rather contorted to do this kind of thing, and kind of impossible really. This way avoids the allocation of the new stage_t while preserving the usability of all existing components build around the stage_new/stage_conf_t API. This is kind of experimental and I haven't tested it yet, just a wart I tripped over in the "sars" Blender 2020 project and wanted to do something quick about before it falls off my radar as I'm busy with unrelated things. --- src/stage.c | 5 +++++ src/stage.h | 1 + 2 files changed, 6 insertions(+) diff --git a/src/stage.c b/src/stage.c index 13519b9..8a540a8 100644 --- a/src/stage.c +++ b/src/stage.c @@ -88,14 +88,19 @@ static void _stage_free(stage_t *stage) /* returns a new stage, attached at the specified layer under parent if supplied */ /* layer has no effect when parent == NULL */ +/* if adopt is set, parent must be set and is instead replaced internally and returned */ stage_t * stage_new(const stage_conf_t *conf, const stage_ops_t *ops, void *object) { stage_t *stage; assert(conf); assert(conf->parent || !conf->layer); + assert(conf->parent || !conf->adopt); assert(conf->layer < STAGE_LAYERS_MAX); + if (conf->adopt) + return stage_replace(conf->parent, conf->name, ops, object); + stage = _stage_new(conf->name, ops, object); if (!stage) return NULL; diff --git a/src/stage.h b/src/stage.h index ec0a72b..785ddf5 100644 --- a/src/stage.h +++ b/src/stage.h @@ -40,6 +40,7 @@ typedef struct stage_conf_t { unsigned active:1; unsigned locked:1; unsigned dirty:1; + unsigned adopt:1; } stage_conf_t; stage_t * stage_new(const stage_conf_t *conf, const stage_ops_t *ops, void *object); -- cgit v1.2.3