Source

mana-core-athenaservices / src / UserDataSvc.cxx

The default branch has multiple heads

#include "boost/any.hpp"

#include "UserDataSvc.h" 
#include "DataModel/UserDataStore.h"
#include "AthenaKernel/errorcheck.h"
#include "Navigation/AthenaBarCodeBase.h"
#include "GaudiKernel/IIncidentSvc.h"
#include "StoreGate/StoreGateSvc.h" 

UserDataSvc::UserDataSvc(const std::string& name, ISvcLocator* pSvcLocator ) : 
  AthService(name,pSvcLocator),
  m_storeGateSvc("StoreGateSvc",name),
  m_userData(0),
  m_cUserData(0),
  m_eventABC(IAthenaBarCode::UNDEFINEDBARCODE){

  declareProperty("StoreGateSvc",m_storeGateSvc);
  declareProperty("Key",m_key="UserDataStore","StoreGate key of UserDataStore object");
  declareProperty("CreateStoreAtBegin",m_createAtBegin=false,"Create UserDataStore object at begin of each event");
}


StatusCode UserDataSvc::initialize() {

  if (m_storeGateSvc.retrieve().isFailure()) {
    msg(MSG::ERROR) << "Failed to retrieve StoreGateSvc" << endreq;
    return StatusCode::FAILURE;
  }


  IIncidentSvc* incSvc = 0;
  if (service("IncidentSvc",incSvc).isFailure()) {
    msg(MSG::ERROR) << "Unable to get the IncidentSvc" << endreq;
    return StatusCode::FAILURE;
  }
  incSvc->addListener( this, IncidentType::BeginEvent  );
  ATH_MSG_DEBUG("Added incidentLister for BeginEvent" );

  //}
  //else
  //ATH_MSG_DEBUG("Don't create UserDataStore at BeginEvent incident");

  return StatusCode::SUCCESS;
}


void UserDataSvc::handle( const Incident& inc ) {
  ATH_MSG_DEBUG ("Incident handle. Incident type " << inc.type() << " from " << inc.source());
  m_userData=NULL;
  m_cUserData=NULL;
  if (m_createAtBegin) createUserDataStore(m_key);
  return;
}


StatusCode UserDataSvc::finalize() {
  return StatusCode::SUCCESS;
}


UserDataSvc::~UserDataSvc() {

}


void UserDataSvc::createUserDataStore(const std::string& sgkey) {
   m_userData=new UserDataStore;
   StatusCode sc=m_storeGateSvc->record(m_userData,sgkey);
   if (sc.isFailure()) {
     msg(MSG::ERROR) << "Failed to record UserDataStore with key " << sgkey << endreq;
     delete m_userData;
     m_cUserData=NULL;
     m_userData=NULL;
     return;
   }
   m_cUserData=m_userData; //Store also as const pointer
   ATH_MSG_DEBUG("Successfully created UserDataStore Object with key " << m_key);
   AthenaBarCodeBase newEventABC; //Generate new AthenaBarCode
   m_userData->setEventABC(newEventABC.getAthenaBarCode());
   m_eventABC.setAthenaBarCode(newEventABC.getAthenaBarCode());
   ATH_MSG_DEBUG("Event BarCode: " << newEventABC.getAthenaBarCode()); 
   return;
}


void UserDataSvc::getUserDataStore() {
  if (!m_userData) {
    if (m_storeGateSvc->contains<UserDataStore>(m_key)) {
      StatusCode sc=m_storeGateSvc->retrieve(m_userData,m_key);
      if (sc.isFailure()) {
	msg(MSG::ERROR) << "Failed to retrieve non-const UsterDataStore!" << endreq;
	m_userData=NULL;
	m_cUserData=NULL;
	return;
      }
      m_cUserData=m_userData;
      m_eventABC.setAthenaBarCode(m_cUserData->getEventABC());
      ATH_MSG_DEBUG("Succesfully retrieved UserDataObject with key " << m_key);
    }// end if contains
    else {
      ATH_MSG_DEBUG("Going to create UserDataStore object.");
      createUserDataStore(m_key);
    }
  }//end if doesn't exits already
  return;
}

void UserDataSvc::getUserDataStore_cst() {
  if (!m_cUserData) {
    if (m_storeGateSvc->contains<UserDataStore>(m_key)) {
      StatusCode sc=m_storeGateSvc->retrieve(m_cUserData,m_key);
      if (sc.isFailure()) {
	msg(MSG::ERROR) << "Failed to retrieve const UserDataStore object with default key " << m_key << " from StoreGate." << endreq;
	return;
      }
      m_eventABC.setAthenaBarCode(m_cUserData->getEventABC());
    }
    else {//No object in SG
      ATH_MSG_WARNING("No UserDataStore object with key " << m_key << " in StoreGate");
    }
    return;
  }//end if doesn't exits already
  return;
}

StatusCode UserDataSvc::recordAny(const IAthenaBarCode& abc,const std::string& label, const boost::any& value) {
  ATH_MSG_DEBUG("Trying to record UserData with label " << label);
  getUserDataStore();
  if (!m_userData) return StatusCode::FAILURE;
  return m_userData->recordAny(abc,label,value);
}


  
StatusCode UserDataSvc::retrieveAny(const IAthenaBarCode& abc,const std::string& label, const boost::any*& value)  {
  ATH_MSG_DEBUG("Trying to retrieve UserData with label " << label);
  getUserDataStore_cst();
  if (!m_cUserData) return StatusCode::FAILURE;
  return m_cUserData->retrieveAny(abc,label,value);
}


bool UserDataSvc::contains(const IAthenaBarCode& abc,const std::string& label) {
  ATH_MSG_DEBUG("Check existance of UserData with label " << label);
  getUserDataStore_cst();
  if (!m_cUserData) return false;
  return m_cUserData->contains(abc,label);
}
 
 

StatusCode UserDataSvc::recordAny(const std::string& label, const boost::any& value) {
  ATH_MSG_DEBUG("Trying to record Event UserData with label " << label);
  return recordAny(m_eventABC,label,value);
}

  
StatusCode UserDataSvc::retrieveAny(const std::string& label, const boost::any*& value) {
  ATH_MSG_DEBUG("Trying to retrieve Event UserData with label " << label);
  return retrieveAny(m_eventABC,label,value);
}


bool UserDataSvc::contains(const std::string& label) {
  ATH_MSG_DEBUG("Check existance of Event UserData with label " << label);
  return contains(m_eventABC,label);
}

StatusCode UserDataSvc::typeIDtoAny(const std::type_info &decoinfo, void* & deco, boost::any& out) {

  if (this->AnyFromType<uint32_t>(decoinfo,deco,out)) 
    return StatusCode::SUCCESS;
  else if (this->AnyFromType<int32_t>(decoinfo,deco,out))
    return StatusCode::SUCCESS;
  else if (this->AnyFromType<float>(decoinfo,deco,out))
    return StatusCode::SUCCESS;
  else if (this->AnyFromType<double>(decoinfo,deco,out))
    return StatusCode::SUCCESS;
  else if (this->AnyFromType<std::vector<uint32_t> >(decoinfo,deco,out)) 
    return StatusCode::SUCCESS;
  else if (this->AnyFromType<std::vector<int32_t> >(decoinfo,deco,out))
    return StatusCode::SUCCESS;
  else if (this->AnyFromType<std::vector<float> >(decoinfo,deco,out))
    return StatusCode::SUCCESS;
  else if (this->AnyFromType<std::vector<double> >(decoinfo,deco,out))
    return StatusCode::SUCCESS;
  else {
    msg(MSG::ERROR) << "Unsupported type " << decoinfo.name() << endreq;
    return StatusCode::FAILURE;
  }
}
  

int UserDataSvc::vdecorateElement(const IAthenaBarCode &abc, const std::string& label,
					 const std::type_info &decoinfo, void* & deco) {
  boost::any value;
  CHECK(this->typeIDtoAny(decoinfo,deco,value));
  if (this->recordAny(abc,label,value).isSuccess()) 
    return 0;
  else
    return -1;
}


int UserDataSvc::vgetElementDecoration(const IAthenaBarCode &abc, const std::string& label,
					      const std::type_info &decoinfo, void *&deco) {
  
  const boost::any* value=0;
  if (this->retrieveAny(abc,label,value).isFailure()) {
    return -1;
  }
  if (value->type()!=decoinfo) {
    msg(MSG::ERROR) << "Typeid mismatch! " <<  value->type().name() << " vs " << decoinfo.name() << endreq;
    return -1;
  }
 
  if (decoinfo==typeid(uint32_t)) {
    deco=(void*)boost::any_cast<uint32_t>(value);
  }
  else if (decoinfo==typeid(int32_t)) {
    deco=(void*)boost::any_cast<int32_t>(value);
  }
  else if (decoinfo==typeid(float)) {
    deco=(void*)boost::any_cast<float>(value);
  }
  else if (decoinfo==typeid(double)) {
    deco=(void*)boost::any_cast<double>(value);
  }
  else if (decoinfo==typeid(std::vector<uint32_t>)) {
    deco=(void*)boost::any_cast<std::vector<uint32_t> >(value);
  }
  else if (decoinfo==typeid(std::vector<int32_t>)) {
    deco=(void*)boost::any_cast<std::vector<int32_t> >(value);
  }
  else if (decoinfo==typeid(std::vector<float>)) {
    deco=(void*)boost::any_cast<std::vector<float> >(value);
  }
  else if (decoinfo==typeid(std::vector<double>)) {
    deco=(void*)boost::any_cast<std::vector<double> >(value);
  }
  else if (decoinfo==typeid(bool)) {
    deco=(void*)boost::any_cast<bool>(value);
  }
  else {
    msg(MSG::ERROR) << "Unsupported/unknown type " << decoinfo.name() << ", label=" << label << endreq;
    return -1;
  }

  return 0;
}


StatusCode
UserDataSvc::queryInterface(const InterfaceID& riid, void** ppvInterface) {
  if (IUserDataSvc::interfaceID().versionMatch(riid)) {
    *ppvInterface = (IUserDataSvc*) this;
  }
  else {
    // Interface is not directly available: try out a base class
    return AthService::queryInterface(riid, ppvInterface);
  }
  addRef();
  return StatusCode::SUCCESS;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.