#include "Math_s.hh"

class MathImpl : public virtual POA_Math::Std, 
		 public virtual PortableServer::RefCountServantBase
{
 public:
    CORBA::Double add(double x, double y) {
	return x + y; 
    };
    CORBA::Double sub(double x, double y) {
	return x - y; 
    };
};

class MathBstImpl : public virtual POA_Math::Bst, 
		    public virtual PortableServer::RefCountServantBase
{
    Math::Std_ptr getit() {

      	try {

	    // Create Math object
	    PortableServer::ServantBase_var servant = new MathImpl();
	    cout << "Created std math object." << endl;

	    // Activate it on the default POA which is root POA
	    PortableServer::POA_var default_poa = _default_POA();
	    CORBA::Object_var ref = default_poa->servant_to_reference(servant);
	    Math::Std_var mstd = Math::Std::_narrow(ref);

	    // Print out the new std math object
	    cout << "Returning std math object ref. " << endl;

	    // Return the math object reference
	    return Math::Std::_duplicate(mstd);

	} catch(const CORBA::Exception& e) {
	    cerr << "_narrow caught exception: " << e << endl;
	} 

	return Math::Std::_nil();
    };
};
