Skip to content
Snippets Groups Projects
Commit b96aa9a9 authored by Gabriel Studer's avatar Gabriel Studer
Browse files

added a template based converter between vectors and python lists

parent c89a8ba6
Branches
Tags
No related merge requests found
......@@ -37,6 +37,7 @@ boost_filesystem_helper.hh
set(OST_EXPORT_HELPERS
generic_property_def.hh
pair_to_tuple_conv.hh
vec_to_list_conv.hh
)
module(NAME base SOURCES ${OST_BASE_SOURCES}
HEADERS ${OST_EXPORT_HELPERS} IN_DIR export_helper
......
//------------------------------------------------------------------------------
// This file is part of the OpenStructure project <www.openstructure.org>
//
// Copyright (C) 2008-2011 by the OpenStructure authors
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3.0 of the License, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//------------------------------------------------------------------------------
#ifndef VEC_LIST_CONV_HH
#define VEC_LIST_CONV_HH
#include <boost/python.hpp>
#include <vector>
namespace ost{
/// \brief helper to convert between python list and std::vector
///
/// Usage:
/// #include vec_to_list_conv.hh
/// std::vector<Real> fancy_vector();
/// boost::python::list fancy_list = VecToList<Real>(fancy_vector);
template<typename T>
boost::python::list VecToList(std::vector<T>& vec){
boost::python::list l;
for(typename std::vector<T>::iterator it=vec.begin();it!=vec.end();++it){
l.append(*it);
}
return l;
}
template<typename T>
std::vector<T> ListToVec(boost::python::list& l){
std::vector<T> vec;
for (int i = 0; i < boost::python::len(l); ++i){
vec.push_back(boost::python::extract<T>(l[i]));
}
return vec;
}
}
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment