From 9f8de0144d13a954552f8d7e818d140e7963f9de Mon Sep 17 00:00:00 2001 From: Jean-Paul Chaput Date: Fri, 25 Feb 2011 09:14:54 +0000 Subject: [PATCH] Forgot this one. --- .../configuration/BoostPythonStlWrappers.h | 193 ++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 vlsisapd/src/configuration/src/vlsisapd/configuration/BoostPythonStlWrappers.h diff --git a/vlsisapd/src/configuration/src/vlsisapd/configuration/BoostPythonStlWrappers.h b/vlsisapd/src/configuration/src/vlsisapd/configuration/BoostPythonStlWrappers.h new file mode 100644 index 00000000..dc677710 --- /dev/null +++ b/vlsisapd/src/configuration/src/vlsisapd/configuration/BoostPythonStlWrappers.h @@ -0,0 +1,193 @@ + +// -*- C++ -*- +// +// This file is part of the VSLSI Stand-Alone Software. +// Copyright (c) UPMC/LIP6 2011-2011, All Rights Reserved +// +// =================================================================== +// +// $Id$ +// +// +-----------------------------------------------------------------+ +// | C O R I O L I S | +// | C o n f i g u r a t i o n D a t a - B a s e | +// | | +// | Author : Jean-Paul CHAPUT | +// | E-mail : Jean-Paul.Chaput@lip6.fr | +// | =============================================================== | +// | C++ Header : | +// | "./vlsisapd/configuration/BoostPythonStlWrapper.h" | +// +-----------------------------------------------------------------+ + + +#include +#include + + +namespace Cfg { + + +// Enumerated type to_python converter. + template + struct EnumToInt { + static PyObject* convert ( EnumType& type ) + { return boost::python::incref ( boost::python::object((int)type).ptr() ); } + }; + + +// STL vector<> wrapper for boost::python. + template + class VectorWrapper { + public: + static boost::python::scope wrap ( const std::string& ); + }; + + + template + boost::python::scope VectorWrapper::wrap ( const std::string& className ) + { + return boost::python::class_< Vector >(className.c_str()) + .def("size" ,&Vector::size ) + .def("empty" ,&Vector::empty) + .def("__iter__", boost::python::iterator()) + ; + } + + +// STL pair<> wrapper for boost::python. + template + class PairWrapper { + public: + typedef typename Pair::first_type Key; + typedef typename Pair::second_type Value; + public: + static boost::python::scope wrap ( const std::string& ); + static boost::python::scope wrap ( const std::string& + , const std::string& firstAlias + , const std::string& secondAlias + ); + }; + + + template + boost::python::scope PairWrapper::wrap ( const std::string& className ) + { + return boost::python::class_< Pair >(className.c_str(),boost::python::init()) + .def_readonly ("first" , &Pair::first ) + .def_readwrite("second", &Pair::second) + ; + } + + + template + boost::python::scope PairWrapper::wrap ( const std::string& className + , const std::string& firstAlias + , const std::string& secondAlias + ) + { + return boost::python::class_< Pair >(className.c_str(),boost::python::init()) + .def_readonly ("first" , &Pair::first ) + .def_readwrite("second" , &Pair::second) + .def_readonly (firstAlias .c_str(), &Pair::first ) + .def_readwrite(secondAlias.c_str(), &Pair::second) + ; + } + + +// STL map<> wrapper for boost::python. + template + class MapWrapper { + public: + typedef typename Map::key_type Key; + typedef typename Map::mapped_type Value; + public: + static Value& get ( Map&, const Key& ); + static void set ( Map&, const Key&, const Value& ); + static void del ( Map&, const Key& ); + static bool in ( Map&, const Key& ); + static boost::python::list keys ( Map& ); + static boost::python::list values ( Map& ); + static boost::python::list items ( Map& ); + static boost::python::scope wrap ( const std::string& ); + }; + + + inline void KeyError () { PyErr_SetString(PyExc_KeyError, "Key not found"); } + + + template + typename MapWrapper::Value& MapWrapper::get ( Map& m, const typename MapWrapper::Key& k ) + { + static Value notFound; + if( m.find(k) != m.end() ) return m[k]; + KeyError (); + return notFound; + } + + + template + void MapWrapper::set ( Map& m, const MapWrapper::Key& k, const typename MapWrapper::Value& v ) + { m[k] = v; } + + + template + void MapWrapper::del ( Map& m, const typename MapWrapper::Key& k) + { + if( m.find(k) != m.end() ) m.erase(k); + else KeyError (); + } + + + template + bool MapWrapper::in ( Map& m, const typename MapWrapper::Key& k ) + { return m.find(k) != m.end(); } + + + template + boost::python::list MapWrapper::keys ( Map& m ) + { + boost::python::list l; + for(typename Map::iterator it = m.begin() ; it != m.end() ; ++it ) + l.append ( it->first ); + return l; + } + + + template + boost::python::list MapWrapper::values ( Map& m) + { + boost::python::list l; + for( typename Map::iterator it=m.begin(); it != m.end() ; ++it ) + l.append ( it->second ); + return l; + } + + + template + boost::python::list MapWrapper::items ( Map& m ) { + boost::python::list l; + for( typename Map::iterator it=m.begin(); it!=m.end(); ++it ) + l.append( make_tuple(it->first,it->second) ); + return l; + } + + + template + boost::python::scope MapWrapper::wrap ( const std::string& className ) + { + return boost::python::class_< Map >(className.c_str()) + .def("__len__" , &Map::size ) + .def("clear" , &Map::clear) + .def("__getitem__" , &get , boost::python::return_value_policy()) + .def("__setitem__" , &set , boost::python::return_value_policy()) + .def("__delitem__" , &del ) + .def("__contains__" , &in ) + .def("has_key" , &in ) + .def("keys" , &keys ) + .def("values" , &values ) + .def("items" , &items ) + ; + } + + +} // End of Cfg namespace.