Skip to content
This repository has been archived by the owner on Sep 5, 2020. It is now read-only.

Commit

Permalink
(#18) Moved compress from lambda to named func
Browse files Browse the repository at this point in the history
`compress` was implemented inlined as a lambda in the module
initialization.

Since it wasn't a "trivial" implementation, I think it's better for it
to be a "real" function.
  • Loading branch information
leandor committed Nov 5, 2016
1 parent 2dfbb81 commit 7022b9a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
36 changes: 18 additions & 18 deletions src/papi/lz4/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,33 @@

static test_initializer modules([](py::module &m) {

using namespace gsl;
using namespace lz4;

m.def_submodule("lz4")
.def("VERSION_NUMBER", VERSION_NUMBER)
.def("VERSION", [](){
return py::make_tuple(VERSION_MAJOR(), VERSION_MINOR(), VERSION_RELEASE());
})
return py::make_tuple(VERSION_MAJOR(), VERSION_MINOR(), VERSION_RELEASE());
})
.def("__version__", [](){
return py::str(VERSION_STRING());
})
.def("compress", [](const py::bytes& buffer){

std::string source{buffer};
auto size = lz4::api::compressBound(source.length());
return py::str(VERSION_STRING());
})
.def("compress", compress)
;
});

py::bytes lz4::compress(const py::bytes& buffer)
{
std::string source{buffer};
auto size = api::compressBound(source.length());

std::vector<char> dest(size);

auto res = lz4::api::compress_default(source, dest);
std::vector<char> dest(size);

if (res == 0) {
throw std::exception("compression failed");
}
auto res = api::compress_default(source, dest);

if (res == 0) {
throw std::exception("compression failed");
}

return py::bytes(dest.data(), res);
})
;
});
return py::bytes(dest.data(), res);
};
6 changes: 6 additions & 0 deletions src/papi/lz4/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@

#include "api.h"

namespace py = pybind11;
using namespace pybind11::literals;

namespace lz4 {


class context {
context();
~context();
};


py::bytes compress(const py::bytes& buffer);
};
#endif // __CINT_LZ4_MODULE__

0 comments on commit 7022b9a

Please sign in to comment.