-
Notifications
You must be signed in to change notification settings - Fork 9
accept comma separated rx_dbitlist #1463
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
base: developer
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1014,15 +1014,16 @@ std::string Caller::counters(int action) { | |
| if (args.empty()) { | ||
| WrongNumberOfParameters(1); | ||
| } | ||
| if (std::any_of(args.cbegin(), args.cend(), [](std::string s) { | ||
| return (StringTo<int>(s) < 0 || StringTo<int>(s) > 2); | ||
| //convert args to string and then to a vector of ints | ||
| auto counters = StringTo<std::vector<int>>( ToString(args)); | ||
| if (std::any_of(counters.cbegin(), counters.cend(), [](int val) { | ||
| return (val < 0 || val > 2); | ||
| })) { | ||
| throw RuntimeError("Invalid counter indices list. Example: 0 1 2"); | ||
| } | ||
| // convert vector to counter enable mask | ||
| uint32_t mask = 0; | ||
| for (size_t i = 0; i < args.size(); ++i) { | ||
| int val = StringTo<int>(args[i]); | ||
| for (auto val : counters) { | ||
| // already enabled earlier | ||
| if (mask & (1 << val)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add the end use: os << ToString(counters) << '\n'; instead of args. Returns what you actually set. |
||
| std::ostringstream oss; | ||
|
|
@@ -1226,11 +1227,7 @@ std::string Caller::rx_dbitlist(int action) { | |
| } | ||
| // 'none' option already covered as t is empty by default | ||
| else if (args[0] != "none") { | ||
| unsigned int ntrim = args.size(); | ||
| t.resize(ntrim); | ||
| for (unsigned int i = 0; i < ntrim; ++i) { | ||
| t[i] = StringTo<int>(args[i]); | ||
| } | ||
| t = StringTo<std::vector<int>>(ToString(args)); | ||
| } | ||
| det->setRxDbitList(t, std::vector<int>{det_id}); | ||
| os << ToString(args) << '\n'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also here ToString(t) |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| // SPDX-License-Identifier: LGPL-3.0-or-other | ||
| // Copyright (C) 2021 Contributors to the SLS Detector Package | ||
| #pragma once | ||
|
|
||
| /** | ||
| * \file ToString.h | ||
| * | ||
|
|
@@ -353,7 +352,9 @@ T StringTo(const std::string &f, const std::string &unit) { | |
| } | ||
| } | ||
|
|
||
| template <typename T> T StringTo(const std::string &t) { | ||
| template <typename T, | ||
| std::enable_if_t<!is_container<T>::value, int> = 0> | ||
| T StringTo(const std::string &t) { | ||
| std::string tmp{t}; | ||
| auto unit = RemoveUnit(tmp); | ||
| return StringTo<T>(tmp, unit); | ||
|
|
@@ -398,6 +399,7 @@ ToString(const T &obj) { | |
| return obj.str(); | ||
| } | ||
|
|
||
| /** Convert vector of strings to vector of type T */ | ||
| template <typename T> | ||
| std::vector<T> StringTo(const std::vector<std::string> &strings) { | ||
| std::vector<T> result; | ||
|
|
@@ -407,4 +409,36 @@ std::vector<T> StringTo(const std::vector<std::string> &strings) { | |
| return result; | ||
| } | ||
|
|
||
| /** Parse comma-separated string into vector type T (e.g., | ||
| * StringTo<std::vector<int>>()) */ | ||
| template <typename T, std::enable_if_t<is_container<T>::value && | ||
| !std::is_same_v<T, std::string>, | ||
| int> = 0> | ||
| T StringTo(const std::string &s) { | ||
| using ElementType = typename T::value_type; | ||
| T res; | ||
| std::istringstream ss(s); | ||
| std::string item; | ||
| while (std::getline(ss, item, ',')) { | ||
| item.erase(std::remove_if(item.begin(), item.end(), | ||
| [](char c) { | ||
| return c == '[' || c == ']' || c == '"' || | ||
| c == ' '; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we throw if start of string is [ and end is not closing ] ?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could but then we need to keep track of which symbols we found. I'm not sure it's needed but can be convinced. |
||
| }), | ||
| item.end()); | ||
|
|
||
| try { | ||
| if (item.empty()) | ||
| continue; | ||
| auto val = StringTo<ElementType>(item); | ||
| res.push_back(val); | ||
| } catch (const std::exception &e) { | ||
| throw RuntimeError("Could not convert '" + item + | ||
| "' to the container element type. " + | ||
| std::string(e.what())); | ||
| } | ||
| } | ||
| return res; | ||
| } | ||
|
|
||
| } // namespace sls | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update the help string in both commands - such that it includes comma seperated values and list