From: Baptiste L. <bl...@us...> - 2004-08-18 19:06:13
|
Update of /cvsroot/cpptool/CppParser/src/pycppparser In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17684 Modified Files: node.cpp Log Message: * added support for pickling Index: node.cpp =================================================================== RCS file: /cvsroot/cpptool/CppParser/src/pycppparser/node.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** node.cpp 7 Aug 2004 15:52:24 -0000 1.1 --- node.cpp 18 Aug 2004 19:05:59 -0000 1.2 *************** *** 1,4 **** --- 1,5 ---- // Boost Includes ============================================================== #include <boost/python.hpp> + #include <boost/python/detail/api_placeholder.hpp> #include <boost/cstdint.hpp> *************** *** 12,15 **** --- 13,79 ---- namespace py { + struct Node_pickle_suite : boost::python::pickle_suite + { + + static boost::python::tuple + getinitargs( const Parser::Node &node ) + { + return boost::python::make_tuple( node.name().str() ); + } + + static boost::python::tuple + getstate( const Parser::Node &node ) + { + const Parser::Token &token = node.token(); + tuple tokenState = make_tuple( token.type().hash(), + token.text().str(), + token.location().startPos_, + token.location().endPos_, + token.location().bufferId_ ); + + list childrenState; + Parser::NodeEnumerator enumChildren( node.enumChildren() ); + while ( enumChildren.hasNext() ) + childrenState.append( enumChildren.nextPtr() ); + + return make_tuple( tokenState, + childrenState ); + } + + static + void + setstate( Parser::Node &node, + boost::python::tuple state ) + { + if ( len(state) != 2 ) + { + PyErr_SetObject(PyExc_ValueError, + str("expected 2-item tuple in call to __setstate__").ptr() ); + throw_error_already_set(); + } + + tuple tokenState = extract<tuple>( state[0] ); + if ( len(tokenState) != 5 ) + { + PyErr_SetObject(PyExc_ValueError, + str("expected 5-item tuple for token state in call to __setstate__").ptr() ); + throw_error_already_set(); + } + std::string tokenText = extract<std::string>( tokenState[1] ); + Parser::Token token( tokenText, + Parser::TokenType( extract<unsigned long>( tokenState[0] ) ), + Parser::TokenLocation( extract<unsigned long>( tokenState[2] ), + extract<unsigned long>( tokenState[3] ), + extract<unsigned long>( tokenState[4] ) ) ); + node.setToken( token ); + + list childrenState = extract<list>( state[1] ); + int childrenCount = len( childrenState ); + for ( int index = 0; index < childrenCount; ++index ) + node.appendChild( extract<Parser::NodePtr>( childrenState[index] ) ); + } + }; + + BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(Node_treeStr_overloads, treeStr, 0, 2) *************** *** 47,51 **** void Export_cppparser_Node() { ! class_< Parser::Node, Parser::NodePtr, boost::noncopyable >("Node", no_init) .def( "enumChildren", &Parser::Node::enumChildren ) .def( "previousSibling", &py::Node_previousSibling ) --- 111,117 ---- void Export_cppparser_Node() { ! class_< Parser::Node, Parser::NodePtr, boost::noncopyable >("Node", ! init<std::string>() ) ! .def_pickle( py::Node_pickle_suite() ) .def( "enumChildren", &Parser::Node::enumChildren ) .def( "previousSibling", &py::Node_previousSibling ) |