Commit 6980697a authored by Davis King's avatar Davis King

Just moved code into dlib namespace.

parent 43e5f42e
...@@ -10,69 +10,73 @@ ...@@ -10,69 +10,73 @@
namespace py = pybind11; namespace py = pybind11;
template <typename T> namespace dlib
std::vector<T> python_list_to_vector ( {
template <typename T>
std::vector<T> python_list_to_vector (
const py::list& obj const py::list& obj
) )
/*! /*!
ensures ensures
- converts a python object into a std::vector<T> and returns it. - converts a python object into a std::vector<T> and returns it.
!*/ !*/
{ {
std::vector<T> vect(len(obj)); std::vector<T> vect(len(obj));
for (unsigned long i = 0; i < vect.size(); ++i) for (unsigned long i = 0; i < vect.size(); ++i)
{ {
vect[i] = obj[i].cast<T>(); vect[i] = obj[i].cast<T>();
} }
return vect; return vect;
} }
template <typename T> template <typename T>
py::list vector_to_python_list ( py::list vector_to_python_list (
const std::vector<T>& vect const std::vector<T>& vect
) )
/*! /*!
ensures ensures
- converts a std::vector<T> into a python list object. - converts a std::vector<T> into a python list object.
!*/ !*/
{ {
py::list obj; py::list obj;
for (unsigned long i = 0; i < vect.size(); ++i) for (unsigned long i = 0; i < vect.size(); ++i)
obj.append(vect[i]); obj.append(vect[i]);
return obj; return obj;
} }
template <typename T> template <typename T>
void extend_vector_with_python_list ( void extend_vector_with_python_list (
std::vector<T> &v, std::vector<T> &v,
const py::list &l const py::list &l
) )
/*! /*!
ensures ensures
- appends items from a python list to the end of std::vector<T>. - appends items from a python list to the end of std::vector<T>.
!*/ !*/
{ {
for (const auto &item : l) for (const auto &item : l)
v.push_back(item.cast<T>()); v.push_back(item.cast<T>());
} }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
template <typename T> template <typename T>
std::shared_ptr<T> load_object_from_file ( std::shared_ptr<T> load_object_from_file (
const std::string& filename const std::string& filename
) )
/*! /*!
ensures ensures
- deserializes an object of type T from the given file and returns it. - deserializes an object of type T from the given file and returns it.
!*/ !*/
{ {
std::ifstream fin(filename.c_str(), std::ios::binary); std::ifstream fin(filename.c_str(), std::ios::binary);
if (!fin) if (!fin)
throw dlib::error("Unable to open " + filename); throw dlib::error("Unable to open " + filename);
auto obj = std::make_shared<T>(); auto obj = std::make_shared<T>();
deserialize(*obj, fin); deserialize(*obj, fin);
return obj; return obj;
}
} }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
......
...@@ -8,9 +8,14 @@ ...@@ -8,9 +8,14 @@
#include <sstream> #include <sstream>
#include <dlib/vectorstream.h> #include <dlib/vectorstream.h>
template<typename T> namespace py = pybind11;
py::tuple getstate(const T& item)
namespace dlib
{ {
template<typename T>
py::tuple getstate(const T& item)
{
using namespace dlib; using namespace dlib;
std::vector<char> buf; std::vector<char> buf;
buf.reserve(5000); buf.reserve(5000);
...@@ -18,11 +23,11 @@ py::tuple getstate(const T& item) ...@@ -18,11 +23,11 @@ py::tuple getstate(const T& item)
serialize(item, sout); serialize(item, sout);
return py::make_tuple(py::handle( return py::make_tuple(py::handle(
PyBytes_FromStringAndSize(buf.size()?&buf[0]:0, buf.size()))); PyBytes_FromStringAndSize(buf.size()?&buf[0]:0, buf.size())));
} }
template<typename T> template<typename T>
T setstate(py::tuple state) T setstate(py::tuple state)
{ {
using namespace dlib; using namespace dlib;
if (len(state) != 1) if (len(state) != 1)
{ {
...@@ -60,6 +65,8 @@ T setstate(py::tuple state) ...@@ -60,6 +65,8 @@ T setstate(py::tuple state)
} }
return item; return item;
}
} }
#endif // DLIB_SERIALIZE_PiCKLE_Hh_ #endif // DLIB_SERIALIZE_PiCKLE_Hh_
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment