Python bindings - ADTs

All methods of an ADT are reflected, and can be called as though they were member functions on the abstract type.

An ADT brings the responsibility of destroying objects onto the Python programmer!

With the following C++


// C++ code

////////////// X.h ////////////////
namespace ns
{
    $adt+ X
    {
        int32 Getx() const;
        void Setx(int32 x);
        void Destroy();
    };

    $function+ X* CreateX();
}

////////////// X.cpp ////////////////
namespace ns
{
    $adt+ implement X
    {
        X() : x_(0) {}
        
        int32 Getx() const { return x_; }
        void Setx(int32 x) { x_ = x; }
        void Destroy() { delete this; }

        int32 x_;
    };
    
    $function+ X* CreateX()
    {
        return new X();
    }
}

the following Python


# python code

x = ns.CreateX()
print 'dir(x) = ' + `dir(x)`
x.Setx(10)
print 'x.Getx() = ' + `x.Getx()`
                
# Typically an ADT will have a Close() or Destroy() method, or something similar
# This must be called to avoid a memory leak
x.Destroy()

produces the following output


dir(x) = ['Destroy', 'Getx', 'Setx']
x.Getx() = 10