From 58621115e029100b7b829b3058d893e43caf3d1e Mon Sep 17 00:00:00 2001 From: falsycat Date: Mon, 7 Oct 2019 00:00:00 +0000 Subject: [PATCH] [add] Added ContextBuilderInterface and Replaced Context from struct to class. --- sjplayer/src/sjplayer/Context.d | 38 +++++++++++++------ .../src/sjplayer/ContextBuilderInterface.d | 16 ++++++++ .../src/sjplayer/ScheduledControllerFactory.d | 29 ++++---------- sjplayer/src/sjplayer/package.d | 2 +- sjplayer/standalone/main.d | 1 + 5 files changed, 53 insertions(+), 33 deletions(-) create mode 100644 sjplayer/src/sjplayer/ContextBuilderInterface.d diff --git a/sjplayer/src/sjplayer/Context.d b/sjplayer/src/sjplayer/Context.d index 4c03068..86b5f51 100644 --- a/sjplayer/src/sjplayer/Context.d +++ b/sjplayer/src/sjplayer/Context.d @@ -2,23 +2,23 @@ module sjplayer.Context; import std.algorithm, + std.array, std.typecons; import sjscript; -import sjplayer.ElementInterface, +import sjplayer.ContextBuilderInterface, + sjplayer.ElementInterface, sjplayer.ProgramSet, sjplayer.ScheduledControllerInterface, sjplayer.VarStoreInterface; /// -struct Context { +class Context { public: - @disable this(); - @disable this(this); - /// this(ParametersBlock[] params, ProgramSet programs) { + auto builder = new Builder; auto varstore = new BlackHole!VarStoreInterface; import sjplayer.CircleElementScheduledController; @@ -29,12 +29,13 @@ struct Context { ), ); foreach (factory; factories) { - auto result = factory[1]. - Create(params.filter!(x => x.name == factory[0])); - elements_ ~= result.elements; - drawers_ ~= result.drawer; - controllers_ ~= result.controllers; + factory[1]. + Create(params.filter!(x => x.name == factory[0]), builder); } + + elements_ = builder.elements[]; + drawers_ = builder.drawers[]; + controllers_ = builder.controllers[]; } /// ~this() { @@ -47,7 +48,6 @@ struct Context { ElementInterface.DamageCalculationResult CalculateDamage() const { assert(false); // TODO: } - /// void DrawElements() { drawers_.each!(x => x.Draw()); @@ -58,6 +58,22 @@ struct Context { } private: + class Builder : ContextBuilderInterface { + public: + override void AddElement(ElementInterface element) { + elements ~= element; + } + override void AddElementDrawer(ElementDrawerInterface drawer) { + drawers ~= drawer; + } + override void AddScheduledController(ScheduledControllerInterface controller) { + controllers ~= controller; + } + Appender!(ElementInterface[]) elements; + Appender!(ElementDrawerInterface[]) drawers; + Appender!(ScheduledControllerInterface[]) controllers; + } + ElementInterface[] elements_; ElementDrawerInterface[] drawers_; diff --git a/sjplayer/src/sjplayer/ContextBuilderInterface.d b/sjplayer/src/sjplayer/ContextBuilderInterface.d new file mode 100644 index 0000000..ec15b46 --- /dev/null +++ b/sjplayer/src/sjplayer/ContextBuilderInterface.d @@ -0,0 +1,16 @@ +/// License: MIT +module sjplayer.ContextBuilderInterface; + +import sjplayer.ElementInterface, + sjplayer.ScheduledControllerInterface; + +/// +interface ContextBuilderInterface { + public: + /// + void AddElement(ElementInterface element); + /// + void AddElementDrawer(ElementDrawerInterface drawer); + /// + void AddScheduledController(ScheduledControllerInterface controller); +} diff --git a/sjplayer/src/sjplayer/ScheduledControllerFactory.d b/sjplayer/src/sjplayer/ScheduledControllerFactory.d index 5b31cba..ceaa067 100644 --- a/sjplayer/src/sjplayer/ScheduledControllerFactory.d +++ b/sjplayer/src/sjplayer/ScheduledControllerFactory.d @@ -10,23 +10,13 @@ import std.algorithm, import sjscript; -import sjplayer.ElementInterface, +import sjplayer.ContextBuilderInterface, + sjplayer.ElementInterface, sjplayer.ProgramSet, sjplayer.ScheduledControllerInterface, sjplayer.VarStoreInterface, sjplayer.util.Period; -/// -struct ScheduledControllerCreationResult { - /// - ElementInterface[] elements; - /// - ScheduledControllerInterface[] controllers; - - /// - ElementDrawerInterface drawer; -} - /// struct ElementScheduledControllerFactory(ScheduledController, ElementDrawer) if (is(ScheduledController : ScheduledControllerInterface) && @@ -54,22 +44,19 @@ struct ElementScheduledControllerFactory(ScheduledController, ElementDrawer) } /// - ScheduledControllerCreationResult Create(R)(R params) + void Create(R)(R params, ContextBuilderInterface builder) if (isInputRange!R && is(ElementType!R == ParametersBlock)) { auto parallelized = params.ParallelizeParams(); - - auto elements = appender!(Element[]); - auto controllers = appender!(ScheduledController[]); + auto elements = appender!(Element[]); foreach (ref serial; parallelized) { auto element = new Element; elements ~= element; - controllers ~= new ScheduledController(element, varstore_, serial); + builder.AddElement(element); + builder.AddScheduledController( + new ScheduledController(element, varstore_, serial)); } - return ScheduledControllerCreationResult( - elements[] .map!(x => cast(ElementInterface) x).array, - controllers[].map!(x => cast(ScheduledControllerInterface) x).array, - new ElementDrawer(program_, elements[])); + builder.AddElementDrawer(new ElementDrawer(program_, elements[])); } private: diff --git a/sjplayer/src/sjplayer/package.d b/sjplayer/src/sjplayer/package.d index de2b085..9993ce7 100644 --- a/sjplayer/src/sjplayer/package.d +++ b/sjplayer/src/sjplayer/package.d @@ -15,5 +15,5 @@ Context CreateContextFromText(string src, ProgramSet programs) { /// Context CreateContextFromScriptAst( ParametersBlock[] params, ProgramSet programs) { - return Context(params, programs); + return new Context(params, programs); } diff --git a/sjplayer/standalone/main.d b/sjplayer/standalone/main.d index 1b10d14..d95b1b2 100644 --- a/sjplayer/standalone/main.d +++ b/sjplayer/standalone/main.d @@ -26,6 +26,7 @@ int main(string[] args) { scope(exit) programs.destroy(); auto context = script_file.readText.CreateContextFromText(programs); + scope(exit) context.destroy(); while (true) { sfEvent e;