ChimeraTK-ApplicationCore 04.06.00
Loading...
Searching...
No Matches
PyUserInputValidator.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
5
6#include "PyArrayAccessor.h"
7#include "PyScalarAccessor.h"
8
9#include <pybind11/functional.h>
10#include <pybind11/stl.h>
11
12namespace py = pybind11;
13
14namespace ChimeraTK {
15
16 /********************************************************************************************************************/
17
19 const std::string& errorMessage, std::function<bool(void)>& isValidFunction, py::args& args) {
20 auto* validator = _impl.addValidator(isValidFunction, errorMessage);
21 for(const auto& arg : args) {
22 if(py::type::of(arg).is(py::type::of<PyScalarAccessor>())) {
23 arg.cast<PyScalarAccessor&>().visit(
24 [&](auto& accessor) { _impl.registerAccessorWithValidator(accessor, validator); });
25 }
26 else if(py::type::of(arg).is(py::type::of<PyArrayAccessor>())) {
27 arg.cast<PyArrayAccessor&>().visit(
28 [&](auto& accessor) { _impl.registerAccessorWithValidator(accessor, validator); });
29 }
30 else {
31 throw ChimeraTK::logic_error("Invalid accessor type " + py::type::of(arg).attr("__name__").cast<std::string>());
32 }
33 }
34 }
35
36 /********************************************************************************************************************/
37
39 py::class_<PyUserInputValidator>(m, "UserInputValidator")
40 .def(py::init<>())
41 .def("add", &PyUserInputValidator::add)
42 .def("setErrorFunction", &PyUserInputValidator::setErrorFunction)
43 .def("validate", &PyUserInputValidator::validate)
44 .def("validateAll", &PyUserInputValidator::validateAll)
45 .def("setFallback",
46 py::overload_cast<PyScalarAccessor&, UserTypeVariantNoVoid>(&PyUserInputValidator::setFallback))
47 .def("setFallback",
48 py::overload_cast<PyArrayAccessor&, const UserTypeTemplateVariantNoVoid<Vector>&>(
50 }
51
52 /********************************************************************************************************************/
53
54 void PyUserInputValidator::setFallback(PyScalarAccessor& acc, UserTypeVariantNoVoid inputValue) {
55 acc.visit([&](auto& accessor) {
56 using Accessor = typename std::remove_reference_t<decltype(accessor)>;
57 using UserType = typename Accessor::value_type;
58 std::visit(
59 [&](auto value) { _impl.setFallback(accessor, ChimeraTK::userTypeToUserType<UserType>(value)); }, inputValue);
60 });
61 }
62
63 /********************************************************************************************************************/
64
66 PyArrayAccessor& acc, const UserTypeTemplateVariantNoVoid<Vector>& inputValue) {
67 acc.visit([&](auto& accessor) {
68 using Accessor = typename std::remove_reference_t<decltype(accessor)>;
69 using UserType = typename Accessor::value_type;
70 std::visit(
71 [&](auto vector) {
72 std::vector<UserType> converted(vector.size());
73 std::transform(vector.begin(), vector.end(), converted.begin(),
74 [](auto& value) { return userTypeToUserType<UserType>(value); });
75 _impl.setFallback(accessor, converted);
76 },
77 inputValue);
78 });
79 }
80 /********************************************************************************************************************/
81
82} // namespace ChimeraTK
Helper class acting as a ArrayAccessor with a variant UserType.
auto visit(CALLABLE fn) const
bool validate(ChimeraTK::TransferElementID &change)
void add(const std::string &errorMessage, std::function< bool(void)> &, py::args &args)
static void bind(py::module &mod)
void setErrorFunction(const std::function< void(const std::string &)> &errorFunction)
void setFallback(PyScalarAccessor &acc, UserTypeVariantNoVoid value)
InvalidityTracer application module.
module_ module
Validator * addValidator(const std::function< bool(void)> &isValidFunction, const std::string &errorMessage)
void registerAccessorWithValidator(Accessor< UserType > &accessor, Validator *validator)
void setFallback(Accessor< UserType > &accessor, UserType value)
Provide fallback value for the given accessor.