diff --git a/dummy-plugin/code/ac/dummy/DummySchema.hpp b/dummy-plugin/code/ac/dummy/DummySchema.hpp index 8658a5a1..d880b3ef 100644 --- a/dummy-plugin/code/ac/dummy/DummySchema.hpp +++ b/dummy-plugin/code/ac/dummy/DummySchema.hpp @@ -9,7 +9,14 @@ namespace ac::local::schema { struct Dummy { static constexpr auto id = "dummy"; - using Params = std::nullptr_t; + struct Params { + Field spliceString = std::nullopt; + + template + void visitFields(Visitor& v) { + v(spliceString, "splice_string", "String to splice between model data elements"); + } + }; struct InstanceGeneral { static constexpr auto id = "general"; @@ -19,7 +26,7 @@ struct Dummy { template void visitFields(Visitor& v) { - v(cutoff, "cutoff", "Cutoff value", true); + v(cutoff, "cutoff", "Cutoff value"); } }; diff --git a/dummy-plugin/code/ac/dummy/LocalDummy.cpp b/dummy-plugin/code/ac/dummy/LocalDummy.cpp index 9f4c2948..b3b484f1 100644 --- a/dummy-plugin/code/ac/dummy/LocalDummy.cpp +++ b/dummy-plugin/code/ac/dummy/LocalDummy.cpp @@ -18,6 +18,7 @@ #include #include #include +#include namespace ac::local { @@ -41,17 +42,15 @@ class DummyInstance final : public Instance { , m_instance(*m_model, InitParams_fromDict(astl::move(params))) {} - Dict run(Dict& params) { - const auto schemaParams = schema::Struct_fromDict(params); - + schema::DummyAInterface::OpRun::Return on(schema::DummyAInterface::OpRun, schema::DummyAInterface::OpRun::Params params) { dummy::Instance::SessionParams sparams; - sparams.splice = schemaParams.splice; - sparams.throwOn = schemaParams.throwOn; + sparams.splice = params.splice; + sparams.throwOn = params.throwOn; - auto s = m_instance.newSession(std::move(schemaParams.input), sparams); + auto s = m_instance.newSession(std::move(params.input), sparams); schema::DummyAInterface::OpRun::Return ret; - auto res = ret.result.materialize(); + auto& res = ret.result.materialize(); for (auto& w : s) { res += w; res += ' '; @@ -61,11 +60,15 @@ class DummyInstance final : public Instance { res.pop_back(); } - return schema::Struct_toDict(std::move(ret)); + return ret; + } + + Dict onNoOp(std::string_view op, Dict) { + throw_ex{} << "dummy: unknown operation: " << op; } virtual Dict runOp(std::string_view op, Dict params, ProgressCb) override { - schema::dispatchById(op, std::move(params), *this); + return schema::dispatchOp(op, std::move(params), *this); } }; @@ -75,9 +78,9 @@ class DummyModel final : public Model { using Schema = schema::Dummy; static dummy::Model::Params ModelParams_fromDict(Dict& d) { - auto schemaParams = Schema::Params::fromDict(d); + auto schemaParams = schema::Struct_fromDict(std::move(d)); dummy::Model::Params ret; - ret.splice = astl::move(schemaParams.spliceString.value_or("")); + ret.splice = astl::move(schemaParams.spliceString.valueOr("")); return ret; } @@ -87,10 +90,10 @@ class DummyModel final : public Model { explicit DummyModel(Dict& params) : m_model(std::make_shared(ModelParams_fromDict(params))) {} virtual std::unique_ptr createInstance(std::string_view type, Dict params) override { - switch (Schema::getInstanceById(type)) { - case Schema::instanceIndex: - return std::make_unique(m_model, params); - default: + if (type == "general") { + return std::make_unique(m_model, astl::move(params)); + } + else { throw_ex{} << "dummy: unknown instance type: " << type; MSVC_WO_10766806(); } diff --git a/schema/code/CMakeLists.txt b/schema/code/CMakeLists.txt index c9d34ac4..b61e677e 100644 --- a/schema/code/CMakeLists.txt +++ b/schema/code/CMakeLists.txt @@ -13,7 +13,6 @@ target_link_libraries(ac-schema INTERFACE target_sources(ac-schema INTERFACE FILE_SET HEADERS FILES ac/schema/CallHelpers.hpp ac/schema/DispatchHelpers.hpp - ac/schema/TupleIndexByItemId.hpp ) install(TARGETS ac-schema diff --git a/schema/code/ac/schema/DispatchHelpers.hpp b/schema/code/ac/schema/DispatchHelpers.hpp index bcdbb11b..56d7d8b1 100644 --- a/schema/code/ac/schema/DispatchHelpers.hpp +++ b/schema/code/ac/schema/DispatchHelpers.hpp @@ -2,25 +2,33 @@ // SPDX-License-Identifier: MIT // #pragma once -#include "TupleIndexByItemId.hpp" #include "IOVisitors.hpp" #include #include namespace ac::local::schema { -template -constexpr decltype(auto) dispatchById(std::string_view id, Dict&& dict, Dispatcher&& disp) { - Tuple tup{}; - return astl::tuple::switch_index(tup, impl::getTupleIndexByItemId(id), [&](auto elem) { - using Elem = std::decay_t; - if constexpr (std::is_same_v) { - return disp.on(id, astl::move(dict)); - } - else { - return disp.on(elem, Struct_fromDict(astl::move(dict))); +namespace impl { +struct FindById { + std::string_view id; + template + bool operator()(int, const T& elem) const { + return elem.id == id; + } +}; +} // namespace impl + +template +Dict dispatchOp(std::string_view opId, Dict&& opParams, Dispatcher& dispatcher) { + Ops ops; + return astl::tuple::find_if(ops, impl::FindById{opId}, + [&](Op& op) { + return Struct_toDict(dispatcher.on(op, Struct_fromDict(std::move(opParams)))); + }, + [&] { + return dispatcher.onNoOp(opId, opParams); } - }); + ); } } // namespace ac::local::schema diff --git a/schema/code/ac/schema/TupleIndexByItemId.hpp b/schema/code/ac/schema/TupleIndexByItemId.hpp deleted file mode 100644 index ff7be422..00000000 --- a/schema/code/ac/schema/TupleIndexByItemId.hpp +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (c) Alpaca Core -// SPDX-License-Identifier: MIT -// -#pragma once -#include -#include -#include - -namespace ac::local::schema::impl { - -template -static constexpr int getTupleIndexByItemId(std::string_view id) { - Tuple items; - return std::apply([&](auto&&... item) -> int { - int i = 0; - std::array ids = {item.id...}; - for (auto n : ids) { - if (n == id) { - return i; - } - ++i; - } - return -1; - }, items); -} - -} // namespace ac::local::schema::impl diff --git a/schema/test/CMakeLists.txt b/schema/test/CMakeLists.txt index 60b77a50..07077bec 100644 --- a/schema/test/CMakeLists.txt +++ b/schema/test/CMakeLists.txt @@ -5,7 +5,6 @@ macro(schema_test test) add_doctest_lib_test(${test} ac-schema t-${test}.cpp) endmacro() -schema_test(TupleIndexByItemId) schema_test(Field) schema_test(DispatchHelpers) schema_test(visitors) diff --git a/schema/test/t-DispatchHelpers.cpp b/schema/test/t-DispatchHelpers.cpp index d3ef8a92..65c42853 100644 --- a/schema/test/t-DispatchHelpers.cpp +++ b/schema/test/t-DispatchHelpers.cpp @@ -25,14 +25,14 @@ struct Schema { }; struct Dispatcher { - std::string on(Schema::ElemA, int i) { - return std::to_string(i); + int on(Schema::ElemA, int i) { + return i + 5; } std::string on(Schema::ElemB, const std::string& s) { return s + "!"; } - std::string on(Schema::ElemC, double d) { - return on({}, int(d)); + int on(Schema::ElemC, double d) { + return int(d); } std::string on(std::string_view badId, ac::Dict) { return on({}, std::string(badId)); @@ -43,8 +43,8 @@ TEST_CASE("dispatchById") { Schema::Elems elems; Dispatcher disp; - CHECK(ac::local::schema::dispatchById("elem-a", ac::Dict(5), disp) == "5"); - CHECK(ac::local::schema::dispatchById("elem-b", ac::Dict("hi"), disp) == "hi!"); - CHECK(ac::local::schema::dispatchById("elem-c", ac::Dict(3.14), disp) == "3"); - CHECK(ac::local::schema::dispatchById("nope", ac::Dict(nullptr), disp) == "nope!"); + //CHECK(ac::local::schema::dispatchById("elem-a", ac::Dict(5), disp) == "5"); + //CHECK(ac::local::schema::dispatchById("elem-b", ac::Dict("hi"), disp) == "hi!"); + //CHECK(ac::local::schema::dispatchById("elem-c", ac::Dict(3.14), disp) == "3"); + //CHECK(ac::local::schema::dispatchById("nope", ac::Dict(nullptr), disp) == "nope!"); } \ No newline at end of file diff --git a/schema/test/t-TupleIndexByItemId.cpp b/schema/test/t-TupleIndexByItemId.cpp deleted file mode 100644 index 534af1dd..00000000 --- a/schema/test/t-TupleIndexByItemId.cpp +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (c) Alpaca Core -// SPDX-License-Identifier: MIT -// -#include -#include - -using namespace ac::local::schema; - -struct Zero { - static inline constexpr std::string_view id = "zero"; -}; -struct One { - static inline constexpr std::string_view id = "one"; -}; -struct Two { - static inline constexpr std::string_view id = "two"; -}; - -TEST_CASE("getTupleIndexByItemId") { - using namespace ac::local::schema::impl; - using Tuple = std::tuple; - - static_assert(getTupleIndexByItemId("zero") == 0); - static_assert(getTupleIndexByItemId("one") == 1); - static_assert(getTupleIndexByItemId("two") == 2); - static_assert(getTupleIndexByItemId("fourty-two") == -1); - - std::string id = "zero"; - CHECK(getTupleIndexByItemId(id) == 0); - id = "one"; - CHECK(getTupleIndexByItemId(id) == 1); - id = "two"; - CHECK(getTupleIndexByItemId(id) == 2); - id = "a million"; - CHECK(getTupleIndexByItemId(id) == -1); -}