The Gaudi Framework  v36r9 (fd2bdac3)
DataObjectHandleBase Class Reference

DataObjectHandleBase GaudiKernel/DataObjectHandleBase.h. More...

#include </builds/gaudi/Gaudi/GaudiKernel/include/GaudiKernel/DataObjectHandleBase.h>

Inheritance diagram for DataObjectHandleBase:
Collaboration diagram for DataObjectHandleBase:

Public Member Functions

 DataObjectHandleBase (DataObjID k, Gaudi::DataHandle::Mode a, IDataHandleHolder *owner)
 
 DataObjectHandleBase (std::string k, Gaudi::DataHandle::Mode a, IDataHandleHolder *owner)
 
 ~DataObjectHandleBase () override
 
 DataObjectHandleBase (const DataObjectHandleBase &)=delete
 
 DataObjectHandleBase (DataObjectHandleBase &&)
 
DataObjectHandleBaseoperator= (const DataObjectHandleBase &)
 
template<class OWNER , class K , typename = std::enable_if_t<std::is_base_of_v<IProperty, OWNER>>>
 DataObjectHandleBase (OWNER *owner, Gaudi::DataHandle::Mode m, std::string name, K key={}, std::string doc="")
 Autodeclaring constructor with property name, mode, key and documentation. More...
 
bool isOptional () const
 Check if the data object declared is optional for the algorithm. More...
 
void setOptional (bool optional=true)
 
bool isValid () const
 
- Public Member Functions inherited from Gaudi::DataHandle
 DataHandle (DataObjID k, Mode a=Reader, IDataHandleHolder *owner=nullptr)
 
 DataHandle (DataObjID k, bool isCond, Mode a=Reader, IDataHandleHolder *owner=nullptr)
 
virtual ~DataHandle ()=default
 
virtual void setOwner (IDataHandleHolder *o)
 
virtual IDataHandleHolderowner () const
 
virtual Mode mode () const
 
virtual void setKey (DataObjID key) const
 
virtual void updateKey (std::string key) const
 
virtual const std::stringobjKey () const
 
virtual const DataObjIDfullKey () const
 
virtual void reset (bool)
 
virtual std::string pythonRepr () const
 
virtual bool isCondition () const
 

Protected Member Functions

bool init () override
 
DataObjectfetch () const
 

Protected Attributes

SmartIF< IDataProviderSvcm_EDS
 
SmartIF< IMessageSvcm_MS
 
bool m_init = false
 
bool m_optional = false
 
std::atomic< bool > m_searchDone = false
 Whether the search part of the fetch method (so dealing with alt names was already executed or not. More...
 
std::mutex m_searchMutex
 A Mutex protecting the calls to the search part of the fetch method, so that we are sure that we only call it once. More...
 
- Protected Attributes inherited from Gaudi::DataHandle
DataObjID m_key = { "NONE" }
 The key of the object behind this DataHandle Although it may look strange to have it mutable, this can actually change in case the object had alternative names, and it should not be visible to the end user, for which the Handle is still the same. More...
 
IDataHandleHolderm_owner = nullptr
 

Friends

std::ostreamoperator<< (std::ostream &str, const DataObjectHandleBase &d)
 

Additional Inherited Members

- Public Types inherited from Gaudi::DataHandle
enum  Mode { Reader = 1 << 2, Writer = 1 << 4 }
 
using PropertyType = DataHandleProperty
 
- Static Protected Attributes inherited from Gaudi::DataHandle
static const std::string default_type = "unknown_t"
 

Detailed Description

DataObjectHandleBase GaudiKernel/DataObjectHandleBase.h.

Base class for handles to Data Objects in the Event Store, to simplify access via Properties.

Author
Charles Leggett
Date
2015-09-01

Definition at line 35 of file DataObjectHandleBase.h.

Constructor & Destructor Documentation

◆ DataObjectHandleBase() [1/5]

DataObjectHandleBase::DataObjectHandleBase ( DataObjID  k,
Gaudi::DataHandle::Mode  a,
IDataHandleHolder owner 
)

Definition at line 51 of file DataObjectHandleBase.cpp.

52  : Gaudi::DataHandle( std::move( k ), a, owner ) {
53  m_owner->declare( *this );
54 }

◆ DataObjectHandleBase() [2/5]

DataObjectHandleBase::DataObjectHandleBase ( std::string  k,
Gaudi::DataHandle::Mode  a,
IDataHandleHolder owner 
)

Definition at line 58 of file DataObjectHandleBase.cpp.

59  : DataObjectHandleBase( DataObjID{ std::move( k ) }, a, owner ) {}

◆ ~DataObjectHandleBase()

DataObjectHandleBase::~DataObjectHandleBase ( )
override

Definition at line 62 of file DataObjectHandleBase.cpp.

62 { owner()->renounce( *this ); }

◆ DataObjectHandleBase() [3/5]

DataObjectHandleBase::DataObjectHandleBase ( const DataObjectHandleBase )
delete

◆ DataObjectHandleBase() [4/5]

DataObjectHandleBase::DataObjectHandleBase ( DataObjectHandleBase &&  other)

Definition at line 26 of file DataObjectHandleBase.cpp.

27  : Gaudi::DataHandle( other )
28  , m_EDS( std::move( other.m_EDS ) )
29  , m_MS( std::move( other.m_MS ) )
30  , m_init( other.m_init )
31  , m_optional( other.m_optional )
32  , m_searchDone( other.m_searchDone.load() ) {
33  m_owner->declare( *this );
34 }

◆ DataObjectHandleBase() [5/5]

template<class OWNER , class K , typename = std::enable_if_t<std::is_base_of_v<IProperty, OWNER>>>
DataObjectHandleBase::DataObjectHandleBase ( OWNER *  owner,
Gaudi::DataHandle::Mode  m,
std::string  name,
key = {},
std::string  doc = "" 
)
inline

Autodeclaring constructor with property name, mode, key and documentation.

Note
the use std::enable_if is required to avoid ambiguities

Definition at line 49 of file DataObjectHandleBase.h.

49  {},
50  std::string doc = "" )
52  auto p = owner->declareProperty( std::move( name ), *this, std::move( doc ) );
53  p->template setOwnerType<OWNER>();
54  }

Member Function Documentation

◆ fetch()

DataObject * DataObjectHandleBase::fetch ( ) const
protected

Definition at line 65 of file DataObjectHandleBase.cpp.

65  {
66  DataObject* p = nullptr;
67  if ( m_searchDone ) { // fast path: searchDone, objKey is in its final state
68  m_EDS->retrieveObject( objKey(), p ).ignore();
69  return p;
70  }
71 
72  // slow path -- move into seperate function to improve code generation?
73 
74  // as m_searchDone is not true yet, objKey may change... so in this
75  // branch we _first_ grab the mutex, to avoid objKey changing while we use it
76 
77  // take a lock to be sure we only execute this one at a time
78  auto guard = std::scoped_lock{ m_searchMutex };
79 
80  // convenience towards users -- remind them to register
81  // but as m_MS is not yet set, we cannot use a MsgStream...
82  if ( !m_init ) {
83  std::cerr << ( owner() ? owner()->name() : "<UNKNOWN>:" ) << "DataObjectHandle: uninitialized data handle"
84  << std::endl;
85  }
86 
87  // note: if no seperator is found, this returns a range of one with the input as the single token
88  // so if the search was done while we were waiting on the mutex, this (also) does the right thing
89  auto tokens = boost::tokenizer<boost::char_separator<char>>{ objKey(), boost::char_separator<char>{ ":" } };
90  // let's try our alternatives (if any)
91  auto alt = std::find_if( tokens.begin(), tokens.end(),
92  [&]( const std::string& n ) { return m_EDS->retrieveObject( n, p ).isSuccess(); } );
93  if ( alt != tokens.end() && *alt != objKey() ) {
94  MsgStream log( m_MS, owner()->name() + ":DataObjectHandle" );
95  log << MSG::DEBUG << ": could not find \"" << objKey() << "\" -- using alternative source: \"" << *alt
96  << "\" instead" << endmsg;
97  // found something -- set it as default; this is not atomic, but
98  // at least in `fetch` there is no use of `objKey` that races with
99  // this assignment... (but there may be others!)
100  setKey( *alt ); // if there was one token, this is an unnecessary write... but who cares...
101  }
102 
103  bool expected = false; // but could be true (already)...
104  m_searchDone.compare_exchange_strong( expected, true ); // if not yet true, set it to true...
105  return p;
106 }

◆ init()

bool DataObjectHandleBase::init ( )
overrideprotectedvirtual

Reimplemented from Gaudi::DataHandle.

Definition at line 110 of file DataObjectHandleBase.cpp.

110  {
111  if ( m_init ) return true; // initialization already done
112 
113  if ( !owner() ) return false;
114 
115  Gaudi::Algorithm* algorithm = dynamic_cast<Gaudi::Algorithm*>( owner() );
116  if ( algorithm ) {
117  // Fetch the event Data Service from the algorithm
118  m_EDS = algorithm->evtSvc();
119  m_MS = algorithm->msgSvc();
120  } else {
121  AlgTool* tool = dynamic_cast<AlgTool*>( owner() );
122  if ( tool ) {
123  m_EDS = tool->evtSvc();
124  m_MS = tool->msgSvc();
125  } else {
126  throw GaudiException( "owner is neither AlgTool nor Gaudi::Algorithm", "Invalid Cast", StatusCode::FAILURE );
127  }
128  }
129  m_init = true;
130  return true;
131 }

◆ isOptional()

bool DataObjectHandleBase::isOptional ( ) const
inline

Check if the data object declared is optional for the algorithm.

Definition at line 59 of file DataObjectHandleBase.h.

59 { return m_optional; }

◆ isValid()

bool DataObjectHandleBase::isValid ( ) const

Definition at line 135 of file DataObjectHandleBase.cpp.

135 { return fullKey() != INVALID_DATAOBJID; }

◆ operator=()

DataObjectHandleBase & DataObjectHandleBase::operator= ( const DataObjectHandleBase other)

Definition at line 37 of file DataObjectHandleBase.cpp.

37  {
38  // avoid modification of searchDone in other while we are copying
39  auto guard = std::scoped_lock{ other.m_searchMutex };
40  // FIXME: operator= should not change our owner, only our 'value'
41  Gaudi::DataHandle::operator=( other );
42  m_EDS = other.m_EDS;
43  m_MS = other.m_MS;
44  m_init = other.m_init;
45  m_optional = other.m_optional;
46  m_searchDone = other.m_searchDone.load();
47  return *this;
48 }

◆ setOptional()

void DataObjectHandleBase::setOptional ( bool  optional = true)
inline

Definition at line 60 of file DataObjectHandleBase.h.

60 { m_optional = optional; }

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream str,
const DataObjectHandleBase d 
)
friend

Definition at line 139 of file DataObjectHandleBase.cpp.

139  {
140 
141  str << d.fullKey() << " m: " << d.mode();
142  if ( d.owner() ) str << " o: " << d.owner()->name();
143  str << " opt: " << d.isOptional();
144  return str;
145 }

Member Data Documentation

◆ m_EDS

SmartIF<IDataProviderSvc> DataObjectHandleBase::m_EDS
protected

Definition at line 70 of file DataObjectHandleBase.h.

◆ m_init

bool DataObjectHandleBase::m_init = false
protected

Definition at line 73 of file DataObjectHandleBase.h.

◆ m_MS

SmartIF<IMessageSvc> DataObjectHandleBase::m_MS
protected

Definition at line 71 of file DataObjectHandleBase.h.

◆ m_optional

bool DataObjectHandleBase::m_optional = false
protected

Definition at line 74 of file DataObjectHandleBase.h.

◆ m_searchDone

std::atomic<bool> DataObjectHandleBase::m_searchDone = false
mutableprotected

Whether the search part of the fetch method (so dealing with alt names was already executed or not.

On subsequent calls (when this is true), it will be skipped.

Definition at line 81 of file DataObjectHandleBase.h.

◆ m_searchMutex

std::mutex DataObjectHandleBase::m_searchMutex
mutableprotected

A Mutex protecting the calls to the search part of the fetch method, so that we are sure that we only call it once.

Definition at line 87 of file DataObjectHandleBase.h.


The documentation for this class was generated from the following files:
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:25
std::string
STL class.
Gaudi.Configuration.log
log
Definition: Configuration.py:28
std::move
T move(T... args)
DataObjectHandleBase::m_EDS
SmartIF< IDataProviderSvc > m_EDS
Definition: DataObjectHandleBase.h:70
DataObjectHandleBase::m_MS
SmartIF< IMessageSvc > m_MS
Definition: DataObjectHandleBase.h:71
std::find_if
T find_if(T... args)
GaudiException
Definition: GaudiException.h:31
DataObjectHandleBase::m_optional
bool m_optional
Definition: DataObjectHandleBase.h:74
Gaudi::DataHandle::setKey
virtual void setKey(DataObjID key) const
Definition: DataHandle.h:57
Gaudi::DataHandle
Definition: DataHandle.h:38
INamedInterface::name
virtual const std::string & name() const =0
Retrieve the name of the instance.
std::atomic::compare_exchange_strong
T compare_exchange_strong(T... args)
DataObjectHandleBase::isOptional
bool isOptional() const
Check if the data object declared is optional for the algorithm.
Definition: DataObjectHandleBase.h:59
IDataHandleHolder::declare
virtual void declare(Gaudi::DataHandle &)=0
bug_34121.tool
tool
Definition: bug_34121.py:17
TimingHistograms.name
name
Definition: TimingHistograms.py:25
Gaudi::DataHandle::owner
virtual IDataHandleHolder * owner() const
Definition: DataHandle.h:53
DataObjectHandleBase::m_init
bool m_init
Definition: DataObjectHandleBase.h:73
std::atomic::load
T load(T... args)
Gaudi::Units::m
constexpr double m
Definition: SystemOfUnits.h:108
std::cerr
Gaudi::Algorithm
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:90
IDataHandleHolder::renounce
virtual void renounce(Gaudi::DataHandle &)=0
Gaudi::DataHandle::objKey
virtual const std::string & objKey() const
Definition: DataHandle.h:60
Gaudi::DataHandle::m_owner
IDataHandleHolder * m_owner
Definition: DataHandle.h:81
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:203
MsgStream
Definition: MsgStream.h:34
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:235
DataObjID
Definition: DataObjID.h:47
std::endl
T endl(T... args)
Gaudi::Algorithm::evtSvc
SmartIF< IDataProviderSvc > & evtSvc() const
shortcut for method eventSvc
Definition: Algorithm.h:248
AlgTool
Definition: AlgTool.h:62
Gaudi::DataHandle::mode
virtual Mode mode() const
Definition: DataHandle.h:55
DataObjectHandleBase::m_searchDone
std::atomic< bool > m_searchDone
Whether the search part of the fetch method (so dealing with alt names was already executed or not.
Definition: DataObjectHandleBase.h:81
DataObject
Definition: DataObject.h:40
DataObjectHandleBase::DataObjectHandleBase
DataObjectHandleBase(DataObjID k, Gaudi::DataHandle::Mode a, IDataHandleHolder *owner)
Definition: DataObjectHandleBase.cpp:51
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
INVALID_DATAOBJID
const DataObjID INVALID_DATAOBJID
Definition: DataObjectHandleBase.cpp:23
DataObjectHandleBase::m_searchMutex
std::mutex m_searchMutex
A Mutex protecting the calls to the search part of the fetch method, so that we are sure that we only...
Definition: DataObjectHandleBase.h:87
Gaudi::DataHandle::fullKey
virtual const DataObjID & fullKey() const
Definition: DataHandle.h:61
ProduceConsume.key
key
Definition: ProduceConsume.py:81