Skip to content

Commit 0c0e089

Browse files
jmirabelnim65s
authored andcommitted
[Python] Add constructor of Flags from list or tuple.
1 parent c1c8513 commit 0c0e089

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

include/sot/core/flags.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected:
3737
public:
3838
Flags(const bool &b = false);
3939
Flags(const char *flags);
40-
Flags(std::vector<bool> &&flags);
40+
Flags(const std::vector<bool> &flags);
4141

4242
void add(const bool &b);
4343

src/python-module.cc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,16 @@ BOOST_PYTHON_MODULE(wrap)
5050
using dgs::Flags;
5151
bp::class_<Flags>("Flags", bp::init<>())
5252
.def(bp::init<const char*>())
53-
//TODO .def(bp::init<std::vector<bool>&& >())
53+
.def("__init__", bp::make_constructor(+[](bp::list bools) {
54+
std::vector<bool> flags (bp::len(bools));
55+
for (std::size_t i = 0; i < flags.size(); ++i) flags[i] = bp::extract<bool>(bools[i]);
56+
return new Flags(flags);
57+
}))
58+
.def("__init__", bp::make_constructor(+[](bp::tuple bools) {
59+
std::vector<bool> flags (bp::len(bools));
60+
for (std::size_t i = 0; i < flags.size(); ++i) flags[i] = bp::extract<bool>(bools[i]);
61+
return new Flags(flags);
62+
}))
5463
.def("add", &Flags::add)
5564
.def("set", &Flags::set)
5665
.def("unset", &Flags::unset)

src/sot/flags.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Flags::Flags(const char *_flags)
4747
}
4848
}
4949

50-
Flags::Flags(std::vector<bool> &&_flags)
50+
Flags::Flags(const std::vector<bool> &_flags)
5151
: flags(_flags), outOfRangeFlag(false) {}
5252

5353
Flags::operator bool(void) const {

0 commit comments

Comments
 (0)