Skip to content

Commit

Permalink
Fix wrapping shared pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
lamyj committed Apr 19, 2018
1 parent c2cdc68 commit 328e6c0
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/odil/Reader.h
Expand Up @@ -85,7 +85,7 @@ class ODIL_API Reader
*/
Element read_element(
Tag const & tag=Tag(0xffff,0xffff),
std::shared_ptr<DataSet const> data_set={}) const;
std::shared_ptr<DataSet const> data_set=std::make_shared<DataSet>()) const;

/// @brief Return the meta-data header and data set stored in the stream.
static std::pair<std::shared_ptr<DataSet>, std::shared_ptr<DataSet>>
Expand Down
35 changes: 29 additions & 6 deletions wrappers/python/DataSet.cpp
Expand Up @@ -20,6 +20,30 @@
namespace
{

std::shared_ptr<odil::DataSet>
constructor()
{
return std::make_shared<odil::DataSet>();
}

std::shared_ptr<odil::DataSet>
constructor_string(std::string const & s)
{
return std::make_shared<odil::DataSet>(s);
}

bool equal(
std::shared_ptr<odil::DataSet> left, std::shared_ptr<odil::DataSet> right)
{
return *left == *right;
}

bool different(
std::shared_ptr<odil::DataSet> left, std::shared_ptr<odil::DataSet> right)
{
return !equal(left, right);
}

void add(
odil::DataSet & data_set, odil::Tag const & tag,
boost::python::object value_python=boost::python::object(),
Expand Down Expand Up @@ -191,10 +215,9 @@ void wrap_DataSet()
using namespace boost::python;
using namespace odil;


class_<DataSet>("DataSet")
.def(init<>())
.def(init<std::string>())
class_<DataSet, std::shared_ptr<DataSet>>("DataSet", no_init)
.def("__init__", make_constructor(constructor))
.def("__init__", make_constructor(constructor_string))
.def("add", add, add_overloads())
.def("remove", &DataSet::remove)
.def("has", &DataSet::has)
Expand Down Expand Up @@ -256,8 +279,8 @@ void wrap_DataSet()
.def("__iter__", range(&begin, &end))
.def("values", &values)
.def("items", &items)
.def(self == self)
.def(self != self)
.def("__eq__", equal)
.def("__ne__", different)
.def(
"__len__",
static_cast<std::size_t (DataSet::*)() const>(&DataSet::size))
Expand Down
13 changes: 11 additions & 2 deletions wrappers/python/Reader.cpp
Expand Up @@ -57,6 +57,14 @@ read_data_set(
return reader.read_data_set(wrap_halt_condition(halt_condition));
}

odil::Element
read_element(
odil::Reader const & reader,
odil::Tag const & tag, std::shared_ptr<odil::DataSet> data_set)
{
return reader.read_element(tag, data_set);
}

boost::python::tuple
read_file(
odil::wrappers::python::iostream & stream, bool keep_group_length,
Expand Down Expand Up @@ -90,8 +98,9 @@ void wrap_Reader()
.def("read_tag", &Reader::read_tag)
.def("read_length", &Reader::read_length)
.def(
"read_element", &Reader::read_element, (
arg("tag")=Tag(0xffff, 0xffff), arg("data_set")=DataSet()
"read_element", read_element, (
arg("tag")=Tag(0xffff, 0xffff),
arg("data_set")=std::make_shared<DataSet>()
))
.def(
"read_file", read_file, (
Expand Down
15 changes: 14 additions & 1 deletion wrappers/python/VRFinder.cpp
Expand Up @@ -12,12 +12,25 @@

#include "odil/VRFinder.h"

namespace
{

odil::VR VRFinder_call(
odil::VRFinder const & self,
odil::Tag const & tag, std::shared_ptr<odil::DataSet> data_set,
std::string const & transfer_syntax)
{
return self(tag, data_set, transfer_syntax);
}

}

void wrap_VRFinder()
{
using namespace boost::python;
using namespace odil;

class_<VRFinder>("VRFinder", init<>())
.def("__call__", &VRFinder::operator())
.def("__call__", VRFinder_call)
;
}
14 changes: 10 additions & 4 deletions wrappers/python/Writer.cpp
Expand Up @@ -56,9 +56,9 @@ create_Writer_2(
}

void write_file(
std::shared_ptr<odil::DataSet const> data_set,
std::shared_ptr<odil::DataSet> data_set,
odil::wrappers::python::iostream & stream,
std::shared_ptr<odil::DataSet const> meta_information,
std::shared_ptr<odil::DataSet> meta_information,
std::string const & transfer_syntax, int item_encoding,
bool use_group_length)
{
Expand All @@ -68,6 +68,12 @@ void write_file(
use_group_length);
}

void write_data_set(
odil::Writer const & writer, std::shared_ptr<odil::DataSet> data_set)
{
writer.write_data_set(data_set);
}

}

void wrap_Writer()
Expand All @@ -92,14 +98,14 @@ void wrap_Writer()
arg("stream"), arg("transfer_syntax"),
arg("item_encoding")=static_cast<int>(Writer::ItemEncoding::ExplicitLength),
arg("use_group_length")=false)))
.def("write_data_set", &Writer::write_data_set)
.def("write_data_set", write_data_set)
.def("write_tag", &Writer::write_tag)
.def("write_element", &Writer::write_element)
.def(
"write_file", write_file,
(
arg("data_set"), arg("stream"),
arg("meta_information")=DataSet(),
arg("meta_information")=std::make_shared<DataSet>(),
arg("transfer_syntax")=registry::ExplicitVRLittleEndian,
arg("item_encoding")=static_cast<int>(Writer::ItemEncoding::ExplicitLength),
arg("use_group_length")=false))
Expand Down
2 changes: 1 addition & 1 deletion wrappers/python/json_converter.cpp
Expand Up @@ -20,7 +20,7 @@
namespace
{

std::string as_json(std::shared_ptr<odil::DataSet const> data_set, bool pretty_print)
std::string as_json(std::shared_ptr<odil::DataSet> data_set, bool pretty_print)
{
auto const json = odil::as_json(data_set);

Expand Down
2 changes: 1 addition & 1 deletion wrappers/python/xml_converter.cpp
Expand Up @@ -18,7 +18,7 @@
namespace
{

std::string as_xml(std::shared_ptr<odil::DataSet const> data_set, bool pretty_print)
std::string as_xml(std::shared_ptr<odil::DataSet> data_set, bool pretty_print)
{
auto const xml = odil::as_xml(data_set);
std::ostringstream stream;
Expand Down

0 comments on commit 328e6c0

Please sign in to comment.