All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
DataObjectHandleBase.cpp
Go to the documentation of this file.
3 #include "GaudiKernel/AlgTool.h"
7 
8 #include <sstream>
9 #include <string>
10 #include <ostream>
11 #include <boost/tokenizer.hpp>
12 
13 
14 
16 
17 //---------------------------------------------------------------------------
19  Gaudi::DataHandle(other), m_EDS(std::move(other.m_EDS)),
20  m_MS(std::move(other.m_MS)), m_init(other.m_init),
21  m_optional(other.m_optional),
22  m_wasRead(other.m_wasRead), m_wasWritten(other.m_wasWritten),
23  m_searchDone(other.m_searchDone) {
24  m_owner->declare(*this);
25 }
26 
27 
28 //---------------------------------------------------------------------------
30  // avoid modification of searchDone in other while we are copying
32  //FIXME: operator= should not change our owner, only our 'value'
34  m_EDS = other.m_EDS;
35  m_MS = other.m_MS;
36  m_init = other.m_init;
37  m_optional = other.m_optional;
38  m_wasRead = other.m_wasRead;
39  m_wasWritten = other.m_wasWritten;
40  m_searchDone = other.m_searchDone;
41  return *this;
42 }
43 
44 //---------------------------------------------------------------------------
48  Gaudi::DataHandle(k,a,owner) {
49  m_owner->declare(*this);
50 }
51 
52 //---------------------------------------------------------------------------
53 
57  DataObjectHandleBase(DataObjID(k), a, owner)
58 {}
59 
60 //---------------------------------------------------------------------------
62  owner()->renounce(*this);
63 }
64 
65 //---------------------------------------------------------------------------
67  DataObject* p = nullptr;
68  if (LIKELY(m_searchDone)) { // fast path: searchDone, objKey is in its final state
70  return p;
71  }
72 
73  // slow path -- move into seperate function to improve code generation?
74 
75  // convenience towards users -- remind them to register
76  // but as m_MS is not yet set, we cannot use a MsgStream...
77  if (!m_init) {
78  std::cerr << ( owner() ? owner()->name() : "<UNKNOWN>:")
79  << "DataObjectHandle: uninitialized data handle" << std::endl;
80  }
81 
82  // as m_searchDone is not true yet, objKey may change... so in this
83  // branch we _first_ grab the mutex, to avoid objKey changing while we use it
84 
85  // take a lock to be sure we only execute this once at a time
87 
89  if (m_searchDone) { // another thread has done the search while we were blocked
90  // on the mutex. As a result, nothing left to do but return...
91  sc.ignore();
92  return p;
93  }
94 
95  if (!sc.isSuccess()) {
96  auto tokens = boost::tokenizer<boost::char_separator<char>>{objKey(),boost::char_separator<char>{":"}};
97  // let's try our alternatives (if any)
98  auto alt = std::find_if( tokens.begin(), tokens.end(),
99  [&](const std::string& n) {
100  return m_EDS->retrieveObject(n,p).isSuccess();
101  } );
102  if (alt!=tokens.end()) {
103  MsgStream log(m_MS,owner()->name() + ":DataObjectHandle");
104  log << MSG::DEBUG << ": could not find \"" << objKey()
105  << "\" -- using alternative source: \"" << *alt << "\" instead"
106  << endmsg;
107  // found something -- set it as default; this is not atomic, but
108  // at least in `fetch` there is no use of `objKey` that races with
109  // this assignment... (but there may be others!)
110  setKey(*alt);
111  }
112  }
113  m_searchDone = true;
114  return p;
115 }
116 
117 //---------------------------------------------------------------------------
118 
121  return objKey();
122 }
123 
124 //---------------------------------------------------------------------------
127  return "DataObjectHandleBase(\"" + toString() + "\")";
128 }
129 
130 //---------------------------------------------------------------------------
131 void
133  Gaudi::Parsers::parse(*this,s).ignore();
134 }
135 
136 //---------------------------------------------------------------------------
137 
138 bool
140 
141  assert(!m_init);
142 
143  if (!owner()) return false;
144 
145  setRead(false);
146  setWritten(false);
147 
148  Algorithm* algorithm = dynamic_cast<Algorithm*>( owner() );
149  if (algorithm) {
150  // Fetch the event Data Service from the algorithm
151  m_EDS = algorithm->evtSvc();
152  m_MS = algorithm->msgSvc();
153  } else {
154  AlgTool* tool = dynamic_cast<AlgTool*>( owner() );
155  if (tool) {
156  m_EDS = tool->evtSvc();
157  m_MS = tool->msgSvc();
158  } else {
159  throw GaudiException( "owner is neither AlgTool nor Algorithm" ,
160  "Invalid Cast", StatusCode::FAILURE) ;
161  }
162  }
163  m_init = true;
164  return true;
165 }
166 
167 //---------------------------------------------------------------------------
168 
169 bool
171 
172  return fullKey() != INVALID_DATAOBJID;
173 
174 }
175 
176 //---------------------------------------------------------------------------
177 
180 
181  str << d.fullKey() << " m: " << d.mode();
182  if (d.owner()) str << " o: " << d.owner()->name();
183  str << " opt: " << d.isOptional();
184  return str;
185 }
void setWritten(bool wasWritten=true)
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
Define general base for Gaudi exception.
virtual void declare(Gaudi::DataHandle &)=0
virtual const std::string & objKey() const
Definition: DataHandle.h:56
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:74
virtual Mode mode() const
Definition: DataHandle.h:51
T endl(T...args)
SmartIF< IDataProviderSvc > m_EDS
STL namespace.
StatusCode parse(GaudiUtils::HashMap< K, V > &result, const std::string &input)
Basic parser for the types of HashMap used in DODBasicMapper.
virtual void renounce(Gaudi::DataHandle &)=0
void setRead(bool wasRead=true)
std::string toString() const
virtual void setKey(const DataObjID &key) const
Definition: DataHandle.h:53
bool m_searchDone
Whether the search part of the fetch method (so dealing with alt names was already executed or not...
Provide serialization function (output only) for some common STL classes (vectors, lists, pairs, maps) plus GaudiUtils::Map and GaudiUtils::HashMap.
PropertyMgr & operator=(const PropertyMgr &)=delete
STL class.
DataObjectHandleBase & operator=(const DataObjectHandleBase &)
virtual const DataObjID & fullKey() const
Definition: DataHandle.h:57
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
IDataHandleHolder * m_owner
Definition: DataHandle.h:77
const DataObjID INVALID_DATAOBJID
void fromString(const std::string &s)
IDataProviderSvc * evtSvc() const
accessor to event service service
Definition: AlgTool.cpp:93
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:78
T find_if(T...args)
SmartIF< IDataProviderSvc > & evtSvc() const
shortcut for method eventSvc
Definition: Algorithm.h:276
#define LIKELY(x)
Definition: Kernel.h:125
DataObjectHandleBase GaudiKernel/DataObjectHandleBase.h.
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...
DataHandle(const DataObjID &k, Mode a=Reader, IDataHandleHolder *owner=nullptr)
Definition: DataHandle.h:37
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:48
string s
Definition: gaudirun.py:245
std::string pythonRepr() const override
SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
DataObject * fetch() const
void ignore() const
Definition: StatusCode.h:106
SmartIF< IMessageSvc > m_MS
bool isOptional() const
Check if the data object declared is optional for the algorithm.
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
friend std::ostream & operator<<(std::ostream &str, const DataObjectHandleBase &d)
Helper functions to set/get the application return code.
Definition: __init__.py:1
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
virtual const std::string & name() const =0
Retrieve the name of the instance.
virtual IDataHandleHolder * owner() const
Definition: DataHandle.h:49
DataObjectHandleBase(const DataObjID &k, Gaudi::DataHandle::Mode a, IDataHandleHolder *owner)
virtual StatusCode retrieveObject(IRegistry *pDirectory, const std::string &path, DataObject *&pObject)=0
Retrieve object identified by its directory entry.