Boost logo

Boost :

From: Pearu Peterson (boost_at_[hidden])
Date: 2001-04-18 17:38:42


On Wed, 18 Apr 2001 rwgk_at_[hidden] wrote:

> If your classes have no virtual functions I would say the
> answer is yes.
> If you have virtual functions I do not know the answer.
> Could you reduce your code to a minimal example that
> shows all the features you need? I.e., a minimal version
> of your basic class, and two examples of a derived class.
> That would help in deciding what is feasable and what is not.
> Ralf

Here follows a reduced case (5 files + comments):

/* File ginac_py.hpp */
#ifndef GINAC_PY_H
#define GINAC_PY_H
#include <ginac/ginac.h>
#include <Python.h>
#include <boost/python/class_builder.hpp>
#include <boost/python/cross_module.hpp>
namespace py = boost::python;
#endif

/* File basic_py.hpp */
#ifndef BASIC_PY_H
#define BASIC_PY_H
#include "ginac_py.hpp"
namespace basic_py {
  class basic : public GiNaC::basic {
    PyObject * self;
  public:
    basic(PyObject * self_): GiNaC::basic(), self(self_) {}
    ~basic() {}
  };
}
#endif

/* File basic_py.cpp */
#include "basic_py.hpp"
BOOST_PYTHON_MODULE_INIT(_basic)
{
  try
    {
      py::module_builder this_module("_basic");
      py::class_builder<basic_py::basic> basic_w_class(this_module, "_basic_w");
      py::class_builder<GiNaC::basic, basic_py::basic> basic_class(this_module, "basic");
      py::export_converters_noncopyable(basic_class);

      basic_class.declare_base(basic_w_class);

      basic_class.def(py::constructor<>());
    }
  catch(...)
    {
      py::handle_exception();
    }
}

/* File symbol_py.hpp */
#ifndef SYMBOL_PY_H
#define SYMBOL_PY_H
#include "ginac_py.hpp"
#include "basic_py.hpp"
namespace symbol_py {
  class symbol : public GiNaC::symbol {
    PyObject * self;
  public:
    symbol(PyObject * self_): GiNaC::symbol(), self(self_) {}
    ~symbol() {}
  };
}
#endif

/* File symbol_py.cpp */
#include "symbol_py.hpp"
BOOST_PYTHON_MODULE_INIT(symbol)
{
  try
    {
      py::module_builder this_module("symbol");
      py::import_converters< basic_py::basic > class_basic_converters("_basic", "basic");
      py::class_builder<symbol_py::symbol> symbol_w_class(this_module, "_symbol_w");
      py::class_builder<GiNaC::symbol, symbol_py::symbol> symbol_class(this_module, "symbol");

      symbol_class.declare_base(symbol_w_class);
      //symbol_class.declare_base(basic_class);

      symbol_class.def(py::constructor<>());
    }
  catch(...)
    {
      py::handle_exception();
    }
}

/* Comments */
The purpose of basic_py::basic class is to define a number of methods that
symbol_py::symbol, numeric_py::numeric, ... classes inherit.
The key question seems to be: How to declare a base class
that is defined in another module?:
        symbol_class.declare_base(basic_class);

If class_basic_converters object could provide a method
get_extension_class() then my problem would be solved, right?:

  symbol_class.declare_base(class_basic_converters.get_extension_class());

Pearu


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk