[C++-sig] question on object section of bpl tutorial
David Abrahams
dave at boost-consulting.com
Sat Dec 21 02:44:36 CET 2002
Mark Russell <mrussell8081 at pacbell.net> writes:
> // buffstr.cpp
>
> #include "disablewarnings.h"
> #include <boost/python.hpp>
> #include <string>
>
> using namespace boost::python;
> //keeps msvc happy
> #define STR boost::python::str
I prefer to eschew macros where possible, especially in tutorial examples.
> #define STR_BASE boost::python::detail::str_base
Ooh, you shouldn't touch this. You shouldn't touch _anything_ in
namespace boost::python::detail! That's why it's not documented.
> class BuffStr : public STR_BASE {
> public:
> BuffStr(STR word) : STR_BASE(word) { count(word); }
> int required;
> std::string repr_() {
> std::string rtnstr = extract<std::string>(*this);
> return "'" + rtnstr + "'";
> }
> std::string str_() {
> std::string req = boost::lexical_cast<std::string>(required);
> std::string rtnstr = extract<std::string>(*this);
> return "string: " + rtnstr + " --> required buf space: " + req;
> }
> private:
> void count(STR word) {
> required = 4 * extract<int>(word.attr("__len__")()) + 16;
> }
> };
What are you trying to accomplish by deriving from str_base?
Why not just store a str data member?
> list f(list x, BuffStr y) {
> if(y.required <= 56)
> x.append(y);
> else
> x.append("too big");
> return x;
> }
>
> BOOST_PYTHON_MODULE(buffstr) {
> class_<BuffStr>("BuffStr", init<STR>())
> .def_readonly("required", &BuffStr::required)
> .def("__repr__", &BuffStr::repr_)
> .def("__str__", &BuffStr::str_);
>
> def("f", f);
> }
Hmm, I'm not convinced this one is ready for prime-time. If it's
supposed to be a replacement for the original example, I'd really like
to see more of the variety of object operations exploited.
-Dave
--
David Abrahams
dave at boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution
More information about the Cplusplus-sig
mailing list