Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dune-xt
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ag-ohlberger
dune-community
dune-xt
Commits
f3f82df9
Commit
f3f82df9
authored
8 years ago
by
Dr. Felix Tobias Schindler
Browse files
Options
Downloads
Patches
Plain Diff
[configuration] reduce bindings to type converters
parent
5e7046d2
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
dune/xt/common/bindings.cc
+0
-2
0 additions, 2 deletions
dune/xt/common/bindings.cc
dune/xt/common/configuration.pbh
+10
-83
10 additions, 83 deletions
dune/xt/common/configuration.pbh
with
10 additions
and
85 deletions
dune/xt/common/bindings.cc
+
0
−
2
View file @
f3f82df9
...
@@ -42,8 +42,6 @@ PYBIND11_PLUGIN(common)
...
@@ -42,8 +42,6 @@ PYBIND11_PLUGIN(common)
},
},
"args"
_a
=
std
::
vector
<
std
::
string
>
());
"args"
_a
=
std
::
vector
<
std
::
string
>
());
Dune
::
XT
::
Common
::
bind_Configuration
(
m
);
return
m
.
ptr
();
return
m
.
ptr
();
}
}
...
...
This diff is collapsed.
Click to expand it.
dune/xt/common/configuration.pbh
+
10
−
83
View file @
f3f82df9
...
@@ -12,82 +12,10 @@
...
@@ -12,82 +12,10 @@
#if HAVE_DUNE_PYBINDXI
#if HAVE_DUNE_PYBINDXI
#include <dune/pybindxi/pybind11.h>
#include <dune/pybindxi/pybind11.h>
#include <dune/pybindxi/operators.h>
#include <dune/pybindxi/cast.h>
#include <dune/pybindxi/stl.h>
#include "configuration.hh"
#include "configuration.hh"
namespace Dune {
namespace XT {
namespace Common {
pybind11::class_<Configuration> bind_Configuration(pybind11::module& m)
{
namespace py = pybind11;
using namespace pybind11::literals;
py::class_<Configuration> c(m, "Configuration", "Configuration");
c.def(py::init<>());
c.def(py::init<std::string, ssize_t>());
c.def(py::init<std::string, std::string>());
c.def(py::init<std::string, double>());
c.def("__repr__", [](const Configuration& cfg) { return "Configuration: " + cfg.report_string(); });
c.def("has_key", &Configuration::has_key);
c.def("has_sub", &Configuration::has_sub);
c.def("sub", &Configuration::sub, "sub_id"_a);
c.def("get_int",
[](const Configuration& self, const std::string& key) { return self.template get<ssize_t>(key); },
"key"_a);
c.def("get_str",
[](const Configuration& self, const std::string& key) { return self.template get<std::string>(key); },
"key"_a);
c.def("get_float",
[](const Configuration& self, const std::string& key) { return self.template get<double>(key); },
"key"_a);
c.def("set",
(void (Configuration::*)(std::string, const ssize_t&, const bool)) & Configuration::set,
"key"_a,
"value"_a,
"overwrite"_a = false);
c.def("set",
(void (Configuration::*)(std::string, const std::string&, const bool)) & Configuration::set,
"key"_a,
"value"_a,
"overwrite"_a = false);
c.def("set",
(void (Configuration::*)(std::string, const double&, const bool)) & Configuration::set,
"key"_a,
"value"_a,
"overwrite"_a = false);
c.def("add",
(Configuration & (Configuration::*)(const Configuration&, const std::string, const bool)) & Configuration::add,
"other"_a,
"sub_id"_a,
"overwrite"_a);
c.def("empty", &Configuration::empty);
c.def("flatten", &Configuration::flatten);
c.def(py::self + py::self);
c.def(py::self += py::self);
c.def("__setitem__", [](Configuration& self, const std::string& key, ssize_t value) { self.set(key, value, true); });
c.def("__setitem__",
[](Configuration& self, const std::string& key, const std::string& value) { self.set(key, value, true); });
c.def("__setitem__",
[](Configuration& self, const std::string& key, const double& value) { self.set(key, value, true); });
c.def("__getitem__", [](const Configuration& self, const std::string& key) { return self[key]; });
return c;
} // bind_Configuration
} // namespace Common
} // namespace XT
} // namespace Dune
NAMESPACE_BEGIN(pybind11)
NAMESPACE_BEGIN(pybind11)
NAMESPACE_BEGIN(detail)
NAMESPACE_BEGIN(detail)
...
@@ -95,35 +23,34 @@ NAMESPACE_BEGIN(detail)
...
@@ -95,35 +23,34 @@ NAMESPACE_BEGIN(detail)
template <>
template <>
struct type_caster<Dune::XT::Common::Configuration>
struct type_caster<Dune::XT::Common::Configuration>
{
{
typedef Dune::XT::Common::Configuration
T
ype;
typedef Dune::XT::Common::Configuration
t
ype;
typedef std::string S;
typedef std::string S;
using type = Type;
//
using type = Type;
using key_conv = make_caster<S>;
using key_conv = make_caster<S>;
using value_conv = make_caster<S>;
using value_conv = make_caster<S>;
bool load(handle src, bool convert)
bool load(handle src, bool convert)
{
{
dict d(src, true);
if (!isinstance<dict>(src))
if (!d.check())
return false;
return false;
auto d = reinterpret_borrow<dict>(src);
key_conv kconv;
key_conv kconv;
value_conv vconv;
value_conv vconv;
value =
T
ype();
value =
t
ype();
for (auto it : d) {
for (auto it : d) {
if (!kconv.load(it.first.ptr(), convert) || !vconv.load(it.second.ptr(), convert))
if (!kconv.load(it.first.ptr(), convert) || !vconv.load(it.second.ptr(), convert))
return false;
return false;
value[
(S)
kconv] =
(S)
vconv;
value[
cast_op<S>(
kconv
)
] =
cast_op<S>(
vconv
)
;
}
}
return true;
return true;
} // ... load(...)
} // ... load(...)
static handle cast(const type& src, return_value_policy policy, handle parent)
static handle cast(const type& src, return_value_policy policy, handle parent)
{
{
auto flat_map = src.flatten();
dict d;
dict d;
for (auto const& kv :
flat_map
) {
for (auto const& kv :
src.flatten()
) {
object
key = object(key_conv::cast(kv.first, policy, parent)
, false
);
auto
key =
reinterpret_steal<
object
>
(key_conv::cast(kv.first, policy, parent));
object
value = object(value_conv::cast(kv.second, policy, parent)
, false
);
auto
value =
reinterpret_steal<
object
>
(value_conv::cast(kv.second, policy, parent));
if (!key || !value)
if (!key || !value)
return handle();
return handle();
d[key] = value;
d[key] = value;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment