[C++-sig] Re: pure virtual methods with pyste

Daniel Wallin dalwan01 at student.umu.se
Mon Jul 28 22:33:50 CEST 2003


At 21:42 2003-07-28, David Abrahams wrote:
>Daniel Wallin <dalwan01 at student.umu.se> writes:
>
> > At 20:54 2003-07-28, David Abrahams wrote:
> >>David Abrahams <dave at boost-consulting.com> writes:
> >>
> >> >   BOOST_PYTHON_MODULE(test)
> >> >   {
> >> >       class_<Abstract, AbstractWrap, boost::noncopyable>("Abstract")
> >> >           .def("a", &Abstract::a)
> >> >           .def("a", &AbstractWrap::default_a)
> >> >           ;
> >> >
> >> >       def("call", call);
> >> >   }
> >> >
> >> > I just wish I knew a way to detect that a function was pure-virtual so
> >> > we could have Boost.Python generate that default_a automatically.
> >>
> >>Perhaps this interface would be nice:
> >>
> >>   BOOST_PYTHON_MODULE(test)
> >>   {
> >>       class_<Abstract, AbstractWrap, boost::noncopyable>("Abstract")
> >>           .def("a", pure_virtual(&Abstract::a))
> >>           ;
> >>
> >>       def("call", call);
> >>   }
> >>
> >>??
> >
> > FWIW, we had a similar problem in luabind, where we needed to specify
> > if a function
> > was going to yield from a coroutine. We solved this by introducing a
> > placeholder
> > in the policy list, like this:
> >
> >     .def("a", &Abstract::a, yield)
> >
> > This solution could also apply to this problem
> >
> >     .def("a", &Abstract::a, pure_virtual)
>
>The deal here, though, is that I want to use the def_arg visitation
>trick to avoid expanding the def(...) interface (which is already way
>too big!)
>
>template <...>
>struct class_
>{
>     ...
>     // Generic visitation
>     template <class Derived>
>     self& def(def_arg<Derived> const& visitor)
>     {
>          static_cast<Derived const&>(visitor).visit(*this);
>          return *this;
>     }
>     ...
>};

Ok, that's pretty cool. Our way doesn't expand the interface either though,
since yield (or pure_virtual) is just a placeholder in the policy list. The 
implementation
just does a search for yield in the policy list. But it's kind of nice with
a non-intrusive approach too.

---
Daniel Wallin





More information about the Cplusplus-sig mailing list