diff --git a/oi/CodeGen.cpp b/oi/CodeGen.cpp index 84fb9b4..a905784 100644 --- a/oi/CodeGen.cpp +++ b/oi/CodeGen.cpp @@ -892,21 +892,33 @@ void genContainerTypeHandler(std::unordered_set& used, containerWithTypes = "OICaptureKeys<" + containerWithTypes + ">"; } - code += "template +struct TypeHandler> { + using container_type = std::variant; +)"; + } else { + code += "template & used, code += " static constexpr bool captureKeys = false;\n"; } - code += " using container_type = "; - code += containerWithTypes; - code += ";\n"; - code += " using type = "; if (processors.empty()) { code += "types::st::Unit"; @@ -934,11 +942,11 @@ void genContainerTypeHandler(std::unordered_set& used, } code += ";\n"; + code += c.codegen.scopedExtra; + code += " static types::st::Unit getSizeType(\n"; code += " Ctx& ctx,\n"; - code += " const "; - code += containerWithTypes; - code += "& container,\n"; + code += " const container_type& container,\n"; code += " typename TypeHandler& used, code += "},\n"; } code += " };\n"; - code += "};\n\n"; } diff --git a/oi/ContainerInfo.cpp b/oi/ContainerInfo.cpp index 4cb20b8..389004a 100644 --- a/oi/ContainerInfo.cpp +++ b/oi/ContainerInfo.cpp @@ -297,6 +297,10 @@ ContainerInfo::ContainerInfo(const fs::path& path) { codegenToml["extra"].value()) { codegen.extra = std::move(*str); } + if (std::optional str = + codegenToml["scoped_extra"].value()) { + codegen.scopedExtra = std::move(*str); + } if (toml::array* arr = codegenToml["processor"].as_array()) { codegen.processors.reserve(arr->size()); diff --git a/oi/ContainerInfo.h b/oi/ContainerInfo.h index 9733601..bc0f806 100644 --- a/oi/ContainerInfo.h +++ b/oi/ContainerInfo.h @@ -38,6 +38,7 @@ struct ContainerInfo { std::string func; std::string traversalFunc = ""; std::string extra = ""; + std::string scopedExtra = ""; std::vector processors{}; }; diff --git a/test/integration/std_variant.toml b/test/integration/std_variant.toml index 38ad343..518e3e5 100644 --- a/test/integration/std_variant.toml +++ b/test/integration/std_variant.toml @@ -9,7 +9,6 @@ definitions = ''' [cases] [cases.char_int64_1] - oil_skip = "std::variant is not implemented for treebuilder v2" # https://github.com/facebookexperimental/object-introspection/issues/298 param_types = ["const std::variant&"] setup = "return 'a';" expect_json = '''[{ @@ -23,7 +22,6 @@ definitions = ''' {"typeName":"int8_t", "staticSize":1, "exclusiveSize":1, "dynamicSize":0} ]}]''' [cases.char_int64_2] - oil_skip = "std::variant is not implemented for treebuilder v2" # https://github.com/facebookexperimental/object-introspection/issues/298 param_types = ["const std::variant&"] setup = "return 1234;" expect_json = '''[{ @@ -38,7 +36,6 @@ definitions = ''' ]}]''' [cases.vector_int_1] - oil_skip = "std::variant is not implemented for treebuilder v2" # https://github.com/facebookexperimental/object-introspection/issues/298 param_types = ["const std::variant, int>&"] setup = "return std::vector{1,2,3};" expect_json = '''[{ @@ -60,7 +57,6 @@ definitions = ''' } ]}]''' [cases.vector_int_2] - oil_skip = "std::variant is not implemented for treebuilder v2" # https://github.com/facebookexperimental/object-introspection/issues/298 param_types = ["const std::variant, int>&"] setup = "return 123;" expect_json = '''[{ @@ -78,7 +74,6 @@ definitions = ''' ]}]''' [cases.optional] - oil_skip = "std::variant is not implemented for treebuilder v2" # https://github.com/facebookexperimental/object-introspection/issues/298 # This test case ensures that the alignment of std::variant is set # correctly, as otherwise the size of the std::optional would be wrong param_types = ["const std::optional>&"] @@ -105,7 +100,6 @@ definitions = ''' ]}]''' [cases.empty] - oil_skip = "std::variant is not implemented for treebuilder v2" # https://github.com/facebookexperimental/object-introspection/issues/298 # https://en.cppreference.com/w/cpp/utility/variant/valueless_by_exception param_types = ["const std::variant&"] setup = ''' @@ -133,7 +127,6 @@ definitions = ''' # 0xff can be a valid index if there are at least 256 parameters, and that # the invalid index value is raised to 0xffff. [cases.256_params_256] - oil_skip = "std::variant is not implemented for treebuilder v2" # https://github.com/facebookexperimental/object-introspection/issues/298 param_types = ["const std::variant&"] setup = "return 'a';" expect_json = '''[{ @@ -147,7 +140,6 @@ definitions = ''' {"typeName":"int8_t", "staticSize":1, "exclusiveSize":1, "dynamicSize":0} ]}]''' [cases.256_params_empty] - oil_skip = "std::variant is not implemented for treebuilder v2" # https://github.com/facebookexperimental/object-introspection/issues/298 param_types = ["const std::variant&"] setup = ''' std::variant var{'a'}; diff --git a/types/README.md b/types/README.md index e2b70fd..30acf0b 100644 --- a/types/README.md +++ b/types/README.md @@ -55,6 +55,11 @@ This document describes the format of the container definition files contained i `static types::st::Unit getSizeType(const T& container, ST returnArg)` where `ST` defines the combined static type of each supplied processor. +- `scoped_extra` + + Extra C++ code to be included in the generated `TypeHandler` for this + container. + - `extra` Any extra C++ code to be included directly. This code is not automatically diff --git a/types/std_variant.toml b/types/std_variant.toml index c7b9dcc..fc7559d 100644 --- a/types/std_variant.toml +++ b/types/std_variant.toml @@ -36,5 +36,54 @@ void getSizeType(const %1% &container, size_t& returnArg) } """ -# TODO: Add tbv2 definitions. The removed intermediate handler is a good -# template for this, find it in the git logs. +scoped_extra = """ +template +static types::st::Unit +getSizeTypeRecursive( + Ctx& ctx, + const typename container_type& container, + typename TypeHandler::type returnArg +) { + if constexpr (I < sizeof...(Types)) { + if (I == container.index()) { + return returnArg.template delegate([&ctx, &container](auto ret) { + return OIInternal::getSizeType(ctx, std::get(container), ret); + }); + } else { + return getSizeTypeRecursive(ctx, container, returnArg); + } + } else { + return returnArg.template delegate(std::identity()); + } +} +""" + +handler_header = """ +template +struct TypeHandler> +""" + +traversal_func = """ +return getSizeTypeRecursive(ctx, container, returnArg); +""" + +[[codegen.processor]] +type = "types::st::Sum::type..., types::st::Unit>" +func = """ +static constexpr std::array children{ + make_field("*")..., +}; + +auto sum = std::get(d.val); + +el.container_stats = result::Element::ContainerStats { + .capacity = 1, + .length = sum.index == sizeof...(Types) ? 0 : 1, +}; + +if (el.container_stats->length == 0) + return; + +el.exclusive_size -= children[sum.index].static_size; +stack_ins(children[sum.index]); +"""