-
Notifications
You must be signed in to change notification settings - Fork 250
feat: adding JsonKindDispatcher + Surface conversion #5195
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
Merged
kodiakhq
merged 18 commits into
acts-project:main
from
ssdetlab:surface-json-converter-fixes
Apr 8, 2026
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
51b7992
adding kind dispatcher and refactoring surface json conversion
2f4cbef
Adding perigee surface
23cb7f0
cleanup
15fba09
Merge branch 'main' into surface-json-converter-fixes
ssdetlab 48cbfa6
split overload for boundless surfaces
83c2110
lint, docs
86993e8
moving templates to cpp, header cleanup, forwarding reference
6e2c918
add config setter
cf75c2b
lint, docs
0a81bca
docs
433a4f6
docs
01a9538
trying to template the args for forwarding
ad71713
sonar cloud contains
8bce0d6
Merge branch 'main' into surface-json-converter-fixes
ssdetlab c8868d3
Merge branch 'main' into surface-json-converter-fixes
ssdetlab 850ec5c
Merge branch 'main' into surface-json-converter-fixes
ssdetlab bd0dfd3
delete default constructor
d80912b
Merge branch 'main' into surface-json-converter-fixes
kodiakhq[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
136 changes: 136 additions & 0 deletions
136
Plugins/Json/include/ActsPlugins/Json/JsonKindDispatcher.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| // This file is part of the ACTS project. | ||
| // | ||
| // Copyright (C) 2016 CERN for the benefit of the ACTS project | ||
| // | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| // file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstddef> | ||
| #include <functional> | ||
| #include <stdexcept> | ||
| #include <string> | ||
| #include <string_view> | ||
| #include <type_traits> | ||
| #include <unordered_map> | ||
| #include <utility> | ||
|
|
||
| #include <nlohmann/json.hpp> | ||
|
|
||
| namespace Acts { | ||
|
|
||
| /// JSON-specific dispatcher that routes decoder functions based on a string | ||
| /// kind tag found in the encoded payload. | ||
| template <typename return_t, typename... args_t> | ||
| class JsonKindDispatcher { | ||
| public: | ||
| /// Type of the dispatcher specialization | ||
| using self_type = JsonKindDispatcher<return_t, args_t...>; | ||
| /// Function signature type | ||
| using decoder_signature = return_t(const nlohmann::json&, args_t...); | ||
| /// Decoder callable type | ||
| using decoder_type = std::function<decoder_signature>; | ||
| /// Decoder callable pointer type | ||
| using decoder_pointer_type = return_t (*)(args_t...); | ||
|
|
||
| /// Explicit constructor of the dispatcher | ||
| /// | ||
| /// @param kindKey the key containing the type kind in json | ||
| /// @param context context string for error signaling | ||
| explicit JsonKindDispatcher(std::string kindKey = "kind", | ||
| std::string context = "JSON payload") | ||
| : m_kindKey(std::move(kindKey)), m_context(std::move(context)) { | ||
| if (m_kindKey.empty()) { | ||
| throw std::invalid_argument( | ||
| "JsonKindDispatcher kind key must be non-empty"); | ||
| } | ||
| if (m_context.empty()) { | ||
| m_context = "JSON payload"; | ||
| } | ||
| } | ||
|
|
||
| /// Register a kind and the corresponding decoder | ||
| /// | ||
| /// @param kind kind to register | ||
| /// @param decoder corresponding decoder | ||
| /// | ||
| /// @return reference to this dispatcher instance | ||
| self_type& registerKind(std::string kind, decoder_type decoder) { | ||
| if (kind.empty()) { | ||
| throw std::invalid_argument("JsonKindDispatcher kind must be non-empty"); | ||
| } | ||
| if (!decoder) { | ||
| throw std::invalid_argument("JsonKindDispatcher decoder must be valid"); | ||
| } | ||
| auto [_, inserted] = | ||
| m_decoders.emplace(std::move(kind), std::move(decoder)); | ||
| if (!inserted) { | ||
| throw std::invalid_argument( | ||
| "JsonKindDispatcher duplicate kind registration"); | ||
| } | ||
| return *this; | ||
| } | ||
|
|
||
| /// Decode the registered kind from a json file | ||
| /// | ||
| /// @param encoded json file to decode | ||
| /// @param args forwarding reference to the decoder arguments | ||
| /// | ||
| /// @return the object constructed from the json encoding | ||
| template <typename... func_args_t> | ||
| return_t operator()(const nlohmann::json& encoded, | ||
| func_args_t&&... args) const | ||
| requires std::invocable<decoder_pointer_type, func_args_t...> | ||
| { | ||
| if (!encoded.contains(m_kindKey)) { | ||
| throw std::invalid_argument("Missing '" + m_kindKey + "' in " + | ||
| m_context); | ||
| } | ||
|
|
||
| const auto& kindValue = encoded.at(m_kindKey); | ||
| if (!kindValue.is_string()) { | ||
| throw std::invalid_argument("Invalid '" + m_kindKey + "' type in " + | ||
| m_context); | ||
| } | ||
|
|
||
| const auto kind = kindValue.template get<std::string>(); | ||
| auto decoder = m_decoders.find(kind); | ||
| if (decoder == m_decoders.end()) { | ||
| throw std::invalid_argument("Unsupported " + m_context + | ||
| " kind: " + kind); | ||
| } | ||
|
|
||
| if constexpr (std::is_void_v<return_t>) { | ||
| decoder->second(encoded, std::forward<func_args_t>(args)...); | ||
| return; | ||
| } else { | ||
| return decoder->second(encoded, std::forward<func_args_t>(args)...); | ||
| } | ||
| } | ||
|
|
||
| /// Check if a certain kind is registered | ||
| /// | ||
| /// @param kind the kind to check for registration | ||
| /// | ||
| /// @return boolean showing registration status | ||
| bool hasKind(std::string_view kind) const { | ||
| return m_decoders.contains(std::string{kind}); | ||
| } | ||
|
|
||
| /// Clear the registered decoders list | ||
| void clear() { m_decoders.clear(); } | ||
|
|
||
| /// Get the number of registered decoders | ||
| /// | ||
| /// @return number of registered decoders | ||
| std::size_t size() const { return m_decoders.size(); } | ||
|
|
||
| private: | ||
| std::string m_kindKey; | ||
| std::string m_context; | ||
| std::unordered_map<std::string, decoder_type> m_decoders; | ||
| }; | ||
|
|
||
| } // namespace Acts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.