File:  [CENS] / python / pyGiNaC / src / ginac.cpp
Revision 1.3: download - view: text, annotated - select for diffs - revision graph
Sat Mar 10 06:04:31 2001 UTC (16 years, 8 months ago) by pearu
Branches: MAIN
CVS tags: HEAD
Created automatic wrapper generator


#include <boost/python/class_builder.hpp>
#include <ginac/ginac.h>

namespace python = boost::python;

namespace { // Avoid cluttering the global namespace.

  std::string print_wrap (GiNaC::basic const & f) {
    std::ostrstream o;
    f.print(o);
    o << ends;
    return o.str();
  }

  std::string printraw_wrap (GiNaC::basic const & f) {
    std::ostrstream o;
    f.printraw(o);
    o << ends;
    return o.str();
  }
  std::string printtree_wrap (GiNaC::basic const & f,
			      unsigned const indent) {
    std::ostrstream o;
    f.printtree(o,indent);
    o << ends;
    return o.str();
  }
  std::string printcsrc_wrap (GiNaC::basic const & f,
			      unsigned const type) {
    std::ostrstream o;
    f.printcsrc(o,type);
    o << ends;
    return o.str();
  }
  std::string ex_print_wrap (GiNaC::ex const & f) {
    std::ostrstream o;
    f.print(o);
    o << ends;
    return o.str();
  }
  std::string ex_printraw_wrap (GiNaC::ex const & f) {
    std::ostrstream o;
    f.printraw(o);
    o << ends;
    return o.str();
  }
  std::string ex_printtree_wrap (GiNaC::ex const & f,
				 unsigned const indent) {
    std::ostrstream o;
    f.printtree(o,indent);
    o << ends;
    return o.str();
  }
}


extern "C"
DL_EXPORT(void)
initginac()
{
  try
  {
    // Create an object representing this extension module.
    python::module_builder this_module("ginac");

    // Create the Python type object for our extension class.
    python::class_builder<GiNaC::basic> basic_class(this_module, "basic");
    basic_class.def(python::constructor<>());
    basic_class.def(python::constructor<const GiNaC::basic &>());

    python::class_builder<GiNaC::ex> ex_class(this_module, "ex");
    ex_class.def(python::constructor<>());
    ex_class.def(python::constructor<const GiNaC::ex &>());

    ex_class.def(python::constructor<double const>());
    ex_class.def(python::constructor<long>()); // never used!!!!!

    // Derived classes from basic
    
    python::class_builder<GiNaC::symbol> symbol_class(this_module, "symbol");
    symbol_class.def(python::constructor<>());
    symbol_class.def(python::constructor<std::string>());
    symbol_class.def(python::constructor<const GiNaC::symbol &>());
    
    // Establish the inheritance relationship between Base and Derived
    symbol_class.declare_base(basic_class);


    // class basic methods
    basic_class.def(&print_wrap, "print_");
    basic_class.def(&printraw_wrap, "printraw");
    basic_class.def(&printtree_wrap, "printtree");
    basic_class.def(&printcsrc_wrap, "printcsrc");

#define EX_CLASS_DEF(com) ex_class.def(&GiNaC::ex::com, #com)
#define BASIC_CLASS_DEF(com) basic_class.def(&GiNaC::basic::com, #com)
#define EXBASIC_CLASS_DEF(com) EX_CLASS_DEF(com); BASIC_CLASS_DEF(com)
    
    EXBASIC_CLASS_DEF(eval);
    EXBASIC_CLASS_DEF(nops);
    EXBASIC_CLASS_DEF(op);
    EXBASIC_CLASS_DEF(let_op);
    EXBASIC_CLASS_DEF(diff);
    
    this_module.def(GiNaC::ex_to_symbol, "ex_to_symbol");
    // class ex methods
    ex_class.def(&ex_print_wrap, "print_");
    ex_class.def(&ex_printraw_wrap, "printraw");
    ex_class.def(&ex_printtree_wrap, "printtree");
    ex_class.def(&GiNaC::ex::numer, "numer");
    ex_class.def(&GiNaC::ex::denom, "denom");
    ex_class.def(python::operators<(
		 python::op_add |
		 python::op_sub |
		 python::op_mul |
		 python::op_div |
		 python::op_mod
		 )>()
		 );
  }
  catch(...)
  {
    python::handle_exception(); // Deal with the exception for Python
  }
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>