Source

mana-core-sgcomps / src / SGFolder.cxx

The default branch has multiple heads

#include <algorithm>
#include <cassert>
#include <functional>
#include <iostream>
#include <vector>

#include "GaudiKernel/IJobOptionsSvc.h"
#include "GaudiKernel/IMessageSvc.h"
#include "GaudiKernel/MsgStream.h"
#include "GaudiKernel/ISvcLocator.h"

#include "AthenaKernel/DefaultKey.h"
#include "AthenaKernel/IClassIDSvc.h"

#include "AthenaKernel/IClassIDSvc.h"
#include "CLIDSvc/tools/ClassID_traits.h"

#include "SGFolder.h"

/* #define SGFOLDER_DEBUG 1 */

using namespace SG;



Folder::Folder( const std::string& type, 
		const std::string& name,
		const IInterface* parent) : 
  AlgTool( type, name, parent ), m_pCLIDSvc("ClassIDSvc", name)
{
  declareProperty("ItemList", m_itemList,
		  " list of data objects identified by a class name (or clid)#key pairs. One can use '*' as key value to add all objects of a given type to the Folder ").ignore();
  m_itemList.declareUpdateHandler(&Folder::decodeItemList, this);
}


//-----------------------------------------------------------------------------
StatusCode Folder::queryInterface(const InterfaceID& riid, void** ppvIf) {
  if ( riid == SG::IFolder::interfaceID() ) {
    *ppvIf = (IFolder*)this;
    addRef();
    return StatusCode::SUCCESS;
  }
  return AlgTool::queryInterface( riid, ppvIf );
}

//-----------------------------------------------------------------------------
StatusCode Folder::initialize() {
  return m_pCLIDSvc.retrieve();
}

//-----------------------------------------------------------------------------
void Folder::decodeItemList(Property&) {
  std::for_each(m_itemList.value().begin(), m_itemList.value().end(),  
		std::bind1st(std::mem_fun(&Folder::decodeItem), this)); //HS!
}

void Folder::decodeItem(std::string item) {
  assert( !item.empty() );
  assert( m_pCLIDSvc );
#ifdef SGFOLDER_DEBUG
  //can't use MsgStream (log level still not defined)
  std::cout << "Folder::decodeItem("<< item<<") called" << std::endl;
#endif
  std::string::size_type sep(item.rfind('#'));
  std::string typeName (item.substr(0,sep));
  std::string skey;
  if (sep != std::string::npos) skey = item.substr(sep+1);

  //item contains a typename OR a CLID. Try the CLID hypothesis first 
  CLID clid(atoi(typeName.c_str()));  
  if ( !add(clid, skey).isSuccess() ) { 
    //lets see if it is a type name then
    add(typeName, skey).ignore();
  }
}

StatusCode 
Folder::add(const std::string& typeName, const std::string& skey) {
  CLID clid;
  StatusCode sc(m_pCLIDSvc->getIDOfTypeName(typeName, clid));
  if (sc.isSuccess()) sc=add(clid, skey);
  else {
    MsgStream log(msgSvc(), name());
    log << MSG::ERROR << "add: can not find type ["
	<< typeName << "] in clid db" << endreq;
  }
  return sc;
}

StatusCode
Folder::add(const CLID& clid, const std::string& skey) {
  StatusCode sc(StatusCode::FAILURE);
  if ( m_pCLIDSvc->isIDInUse(clid) ) {
    m_list.insert(FolderItem(clid, skey));
    sc = StatusCode::SUCCESS;
  } else if (0 != clid) {
    MsgStream log(msgSvc(), name());
    log << MSG::ERROR << "add: can not find clid "
	<< clid << " in clid db" << endreq;
  }
#ifdef SGFOLDER_DEBUG
    std::cout << "SG::Folder::add(" << clid << ",\"" << skey << "\") returns " 
	      << (sc.isSuccess() ? "SUCCESS" : "FAILURE") << std::endl;
#endif     
  return sc;
}