diff --git a/modules/base/src/CMakeLists.txt b/modules/base/src/CMakeLists.txt index e6691bc1de9af1dccafbaebad7b6b399b0fa81e4..796052d610b50a5335eb2a803600c315bb324c97 100644 --- a/modules/base/src/CMakeLists.txt +++ b/modules/base/src/CMakeLists.txt @@ -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 diff --git a/modules/base/src/export_helper/vec_to_list_conv.hh b/modules/base/src/export_helper/vec_to_list_conv.hh new file mode 100644 index 0000000000000000000000000000000000000000..470d82dd48945c7867514122ef5dbc233fad3849 --- /dev/null +++ b/modules/base/src/export_helper/vec_to_list_conv.hh @@ -0,0 +1,57 @@ +//------------------------------------------------------------------------------ +// 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