// 
// File:          PetscLA_Factory_Impl.cc
// Symbol:        PetscLA.Factory-v0.0.3
// Symbol Type:   class
// Babel Version: 0.10.2
// Description:   Server-side implementation for PetscLA.Factory
// 
// WARNING: Automatically generated; only changes within splicers preserved
// 
// babel-version = 0.10.2
// 
#include "PetscLA_Factory_Impl.hh"

// DO-NOT-DELETE splicer.begin(PetscLA.Factory._includes)
#include <iostream>
#include "petsc.h"
#include "petscvec.h"
#include "petscmat.h"
#include "tao.h"

// DO-NOT-DELETE splicer.end(PetscLA.Factory._includes)

// user-defined constructor.
void PetscLA::Factory_impl::_ctor() {
  // DO-NOT-DELETE splicer.begin(PetscLA.Factory._ctor)
  // add construction details here
  // DO-NOT-DELETE splicer.end(PetscLA.Factory._ctor)
}

// user-defined destructor.
void PetscLA::Factory_impl::_dtor() {
  // DO-NOT-DELETE splicer.begin(PetscLA.Factory._dtor)
  // add destruction details here
  // DO-NOT-DELETE splicer.end(PetscLA.Factory._dtor)
}

// static class initializer.
void PetscLA::Factory_impl::_load() {
  // DO-NOT-DELETE splicer.begin(PetscLA.Factory._load)
  // Insert-Code-Here {PetscLA.Factory._load} (class initialization)
  // DO-NOT-DELETE splicer.end(PetscLA.Factory._load)
}

// user-defined static methods: (none)

// user-defined non-static methods:
/**
 * Starts up a component presence in the calling framework.
 * @param Svc the component instance's handle on the framework world.
 * Contracts concerning Svc and setServices:
 * 
 * The component interaction with the CCA framework
 * and Ports begins on the call to setServices by the framework.
 * 
 * This function is called exactly once for each instance created
 * by the framework.
 * 
 * The argument Svc will never be nil/null.
 * 
 * Those uses ports which are automatically connected by the framework
 * (so-called service-ports) may be obtained via getPort during
 * setServices.
 */
void
PetscLA::Factory_impl::setServices (
  /* in */ ::gov::cca::Services services ) 
throw ( 
  ::gov::cca::CCAException
){
  // DO-NOT-DELETE splicer.begin(PetscLA.Factory.setServices)
  if (services._is_nil()) {
    return;
  }
  gov::cca::Port port = this->self;
  if (port._is_nil()) {
    std::cerr << "unable to cast self as port (PetscLA::Factory_impl) in PetscLA::Factory_impl::setServices" << std::endl;
  }

  this->myServices = services;
  try {
    this->myServices.addProvidesPort(port,std::string("LAFactory"), std::string("PetscLA.Factory"),0);
  }
  catch (gov::cca::CCAException e) {
    std::cerr << "Error adding providesPort in LAFactory: " << e.getNote() << std::endl;
  }
  // DO-NOT-DELETE splicer.end(PetscLA.Factory.setServices)
}

/**
 * Method:  initialize[]
 */
int32_t
PetscLA::Factory_impl::initialize ()
throw () 

{
  // DO-NOT-DELETE splicer.begin(PetscLA.Factory.initialize)
  return 0;
  // DO-NOT-DELETE splicer.end(PetscLA.Factory.initialize)
}

/**
 * Method:  finalize[]
 */
int32_t
PetscLA::Factory_impl::finalize ()
throw () 

{
  // DO-NOT-DELETE splicer.begin(PetscLA.Factory.finalize)
  return 0;
  // DO-NOT-DELETE splicer.end(PetscLA.Factory.finalize)
}

/**
 * createVec returns a pointer to a TaoVec 
 */
void*
PetscLA::Factory_impl::createVec (
  /* in */ int32_t n ) 
throw () 
{
  // DO-NOT-DELETE splicer.begin(PetscLA.Factory.createVec)
  Vec v;
  TaoVecPetsc *tvp;
  int info;
  info = VecCreateSeq(PETSC_COMM_SELF, n, &v);
  if (info) std::cerr << "Unable to create Petsc Vector" << std::endl;
  info = TaoWrapPetscVec(v,&tvp); 
  return (void*)tvp;
  // DO-NOT-DELETE splicer.end(PetscLA.Factory.createVec)
}

/**
 * createMat returns a pointer to a TaoMat
 */
void*
PetscLA::Factory_impl::createMat (
  /* in */ int32_t m,
  /* in */ int32_t n ) 
throw () 
{
  // DO-NOT-DELETE splicer.begin(PetscLA.Factory.createMat)
  Mat mat;
  TaoMatPetsc *tmp;
  int info;
  info = MatCreateSeqDense(PETSC_COMM_SELF, m, n, PETSC_NULL, &mat);
  if (info) std::cerr << "Unable to create Petsc Matrix" << std::endl;
  info = TaoWrapPetscMat(mat,&tmp);
  return (void*)tmp;
  // DO-NOT-DELETE splicer.end(PetscLA.Factory.createMat)
}


// DO-NOT-DELETE splicer.begin(PetscLA.Factory._misc)
// Put miscellaneous code here
// DO-NOT-DELETE splicer.end(PetscLA.Factory._misc)

