diff --git a/dune/xt/common/parameter.hh b/dune/xt/common/parameter.hh index 4554f7c721d96216a3a241635481ead321760e5c..70a57c5278e9e5e41fbbfee57f0ad682738fdf13 100644 --- a/dune/xt/common/parameter.hh +++ b/dune/xt/common/parameter.hh @@ -59,6 +59,12 @@ public: return keys_; } + void clear() + { + keys_.clear(); + dict_.clear(); + } + bool empty() const { return dict_.size() == 0; diff --git a/python/dune/xt/common/parameter.hh b/python/dune/xt/common/parameter.hh new file mode 100644 index 0000000000000000000000000000000000000000..675f10dfae5920832a00630d6347732c41666e3a --- /dev/null +++ b/python/dune/xt/common/parameter.hh @@ -0,0 +1,82 @@ +// This file is part of the dune-xt project: +// https://github.com/dune-community/dune-xt +// Copyright 2009-2018 dune-xt developers and contributors. All rights reserved. +// License: Dual licensed as BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause) +// or GPL-2.0+ (http://opensource.org/licenses/gpl-license) +// with "runtime exception" (http://www.dune-project.org/license.html) +// Authors: +// Felix Schindler (2020) + +#ifndef PYTHON_DUNE_XT_COMMON_PARAMETER_HH +#define PYTHON_DUNE_XT_COMMON_PARAMETER_HH + +#include <dune/pybindxi/pybind11.h> +#include <dune/pybindxi/cast.h> + +#include <dune/xt/common/parameter.hh> + + +NAMESPACE_BEGIN(pybind11) +NAMESPACE_BEGIN(detail) + + +template <typename Type, typename Value> +struct simple_dict_caster +{ + using Key = std::string; + using key_conv = make_caster<Key>; + using value_conv = make_caster<Value>; + + bool load(handle src, bool convert) + { + if (!isinstance<dict>(src)) + return false; + auto d = reinterpret_borrow<dict>(src); + value.clear(); + for (auto it : d) { + key_conv kconv; + value_conv vconv; + if (!kconv.load(it.first.ptr(), convert) || !vconv.load(it.second.ptr(), convert)) + return false; + value.set(cast_op<Key&&>(std::move(kconv)), cast_op<Value&&>(std::move(vconv))); + } + return true; + } // ... load(...) + + static handle cast(const Type& src, return_value_policy policy, handle parent) + { + dict d; + return_value_policy policy_key = policy; + return_value_policy policy_value = policy; + if (!std::is_lvalue_reference<Type>::value) { + policy_key = return_value_policy_override<Key>::policy(policy_key); + policy_value = return_value_policy_override<Value>::policy(policy_value); + } + for (auto&& key : src.keys()) { + auto key_obj = reinterpret_steal<object>(key_conv::cast(key, policy_key, parent)); + auto value_obj = reinterpret_steal<object>(value_conv::cast(src.get(key), policy_value, parent)); + if (!key_obj || !value_obj) + return handle(); + d[key_obj] = value_obj; + } + return d.release(); + } + + PYBIND11_TYPE_CASTER(Type, _("Dict[") + key_conv::name + _(", ") + value_conv::name + _("]")); +}; // struct simple_dict_caster + + +template <> +struct type_caster<Dune::XT::Common::ParameterType> : public simple_dict_caster<Dune::XT::Common::ParameterType, size_t> +{}; + +template <> +struct type_caster<Dune::XT::Common::Parameter> + : public simple_dict_caster<Dune::XT::Common::Parameter, std::vector<double>> +{}; + + +NAMESPACE_END(detail) +NAMESPACE_END(pybind11) + +#endif // PYTHON_DUNE_XT_COMMON_PARAMETER_HH