ATLAS Offline Software
Loading...
Searching...
No Matches
AthToolSupport/AsgDataHandles/AsgDataHandles/WriteHandle.h
Go to the documentation of this file.
1// This file's extension implies that it's C, but it's really -*- C++ -*-.
2
3/*
4 Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
5*/
6
13
14
15#ifndef ASG_DATA_HANDLES_WRITE_HANDLE_H
16#define ASG_DATA_HANDLES_WRITE_HANDLE_H
17
18#ifndef XAOD_STANDALONE
20#else
21
24#include <string>
25#include <memory> /*unique_ptr*/
26
27
28namespace SG {
29
30
43template <class T>
44class WriteHandle
45 : public SG::VarHandleBase
46{
47public:
48 typedef T* pointer_type; // FIXME: better handling of
49 typedef const T* const_pointer_type; // qualified T type ?
50 typedef T& reference_type;
51 typedef const T& const_reference_type;
52
53
54 //************************************************************************
55 // Constructors, etc.
56 //
57
58
59 // /**
60 // * @brief Default constructor.
61 // *
62 // * The handle will not be usable until a non-blank key is assigned.
63 // */
64 // WriteHandle();
65
66
71 explicit WriteHandle(const std::string& sgkey);
72
73
81 explicit WriteHandle (const WriteHandleKey<T>& key);
82
83
95 explicit WriteHandle (const WriteHandleKey<T>& key, const EventContext& ctx);
96
97
101 WriteHandle( const WriteHandle& rhs );
102
103
107 WriteHandle( WriteHandle&& rhs );
108
109
113 WriteHandle& operator=( const WriteHandle& rhs );
114
115
120
121
127 ~WriteHandle();
128
129
130 //************************************************************************
131 // Deference. These all return only the cached pointer.
132 //
133
134
135 // /**
136 // * @brief Dereference the pointer.
137 // * Returns the cached pointer. Throws ExcNullWriteHandle if null.
138 // */
139 // pointer_type operator->();
140
141
147
148
149 // /**
150 // * @brief Dereference the pointer.
151 // * Returns the cached pointer.
152 // */
153 // const_pointer_type cptr() const;
154
155
156 // /**
157 // * @brief Dereference the pointer.
158 // * Returns the cached pointer.
159 // */
160 // pointer_type ptr();
161
162
163 // /**
164 // * @brief Return the cached pointer directly; no lookup.
165 // */
166 // pointer_type cachedPtr();
167
168
172 bool isValid();
173
174
175 //************************************************************************
176 // Record.
177 //
178
179
184 StatusCode record (std::unique_ptr<T> data);
185
186
187 // /**
188 // * @brief Record a non-const object to the store.
189 // * @param data The object to record.
190 // */
191 // StatusCode recordNonConst (std::unique_ptr<T> data);
192
193
199 template <class AUXSTORE>
200 StatusCode record (std::unique_ptr<T> data,
201 std::unique_ptr<AUXSTORE> store);
202
203
204 // /**
205 // * @brief Record a non-const object and its auxiliary store to the store.
206 // * @param data The object to record.
207 // * @param auxstore Auxiliary store object.
208 // */
209 // template <class AUXSTORE>
210 // StatusCode recordNonConst (std::unique_ptr<T> data,
211 // std::unique_ptr<AUXSTORE> store);
212
213
214 // /**
215 // * @brief Record a const shared DataObject to the store.
216 // * @param data The object to record.
217 // *
218 // * The event store takes shared ownership of the object.
219 // */
220 // StatusCode record (SG::DataObjectSharedPtr<T> data);
221
222
223 // /**
224 // * @brief Record a non-const shared DataObject to the store.
225 // * @param data The object to record.
226 // *
227 // * The event store takes shared ownership of the object.
228 // */
229 // StatusCode recordNonConst (SG::DataObjectSharedPtr<T> data);
230
231
246 const_pointer_type put (std::unique_ptr<T> data/*,
247 bool returnExisting = false*/) const;
248
249
250 // /**
251 // * @brief Record an object to the store.
252 // * @param data The object to record.
253 // * @param returnExisting Allow an existing object?
254 // *
255 // * Unlike record(), this does not change the handle object.
256 // * That means that one will not be able to get the object back
257 // * by dereferencing the handle.
258 // * Returns the object placed in the store, or nullptr if there
259 // * was an error.
260 // * If there was already an object in the store with the given key,
261 // * then return null, unless @c returnExisting is true, in which case
262 // * return success. In either case, @c data is destroyed.
263 // */
264 // const_pointer_type put (std::unique_ptr<const T> data,
265 // bool returnExisting = false) const;
266
267
268 // /**
269 // * @brief Record an object to the store.
270 // * @param data The object to record.
271 // * @param returnExisting Allow an existing object?
272 // *
273 // * Unlike record(), this does not change the handle object.
274 // * That means that one will not be able to get the object back
275 // * by dereferencing the handle.
276 // * Returns the object placed in the store, or nullptr if there
277 // * was an error.
278 // * If there was already an object in the store with the given key,
279 // * then return null, unless @c returnExisting is true, in which case
280 // * return success. In either case, @c data is destroyed.
281 // */
282 // const_pointer_type put (std::unique_ptr<const ConstDataVector<T> > data,
283 // bool returnExisting = false) const;
284
285
286 // /**
287 // * @brief Record an object to the store.
288 // * @param ctx The event context to use.
289 // * @param data The object to record.
290 // * @param returnExisting Allow an existing object?
291 // *
292 // * Unlike record(), this does not change the handle object.
293 // * That means that one will not be able to get the object back
294 // * by dereferencing the handle.
295 // * Returns the object placed in the store, or nullptr if there
296 // * was an error.
297 // * If there was already an object in the store with the given key,
298 // * then return null, unless @c returnExisting is true, in which case
299 // * return success. In either case, @c data is destroyed.
300 // */
301 // const_pointer_type put (const EventContext& ctx,
302 // std::unique_ptr<const ConstDataVector<T> > data,
303 // bool returnExisting = false) const;
304
305
306 // /**
307 // * @brief Record an object to the store.
308 // * @param ctx The event context to use.
309 // * @param data The object to record.
310 // * @param returnExisting Allow an existing object?
311 // *
312 // * Unlike record(), this does not change the handle object.
313 // * That means that one will not be able to get the object back
314 // * by dereferencing the handle.
315 // * Returns the object placed in the store, or nullptr if there
316 // * was an error.
317 // * If there was already an object in the store with the given key,
318 // * then return null, unless @c returnExisting is true, in which case
319 // * return success. In either case, @c data is destroyed.
320 // */
321 // const_pointer_type put (const EventContext& ctx,
322 // std::unique_ptr<T> data,
323 // bool returnExisting = false) const;
324
325
326 // /**
327 // * @brief Record an object to the store.
328 // * @param ctx The event context to use.
329 // * @param data The object to record.
330 // * @param returnExisting Allow an existing object?
331 // *
332 // * Unlike record(), this does not change the handle object.
333 // * That means that one will not be able to get the object back
334 // * by dereferencing the handle.
335 // * Returns the object placed in the store, or nullptr if there
336 // * was an error.
337 // * If there was already an object in the store with the given key,
338 // * then return null, unless @c returnExisting is true, in which case
339 // * return success. In either case, @c data is destroyed.
340 // */
341 // const_pointer_type put (const EventContext& ctx,
342 // std::unique_ptr<const T> data,
343 // bool returnExisting = false) const;
344
345
346 // /**
347 // * @brief Record an object to the store.
348 // * @param data The object to record.
349 // *
350 // * Unlike record(), this does not change the handle object.
351 // * That means that one will not be able to get the object back
352 // * by dereferencing the handle.
353 // * Returns the object placed in the store, or nullptr if there
354 // * was an error.
355 // *
356 // * The event store takes shared ownership of the object.
357 // */
358 // const_pointer_type put (SG::DataObjectSharedPtr<T> data) const;
359
360
361 // /**
362 // * @brief Record an object to the store.
363 // * @param ctx The event context to use.
364 // * @param data The object to record.
365 // *
366 // * Unlike record(), this does not change the handle object.
367 // * That means that one will not be able to get the object back
368 // * by dereferencing the handle.
369 // * Returns the object placed in the store, or nullptr if there
370 // * was an error.
371 // *
372 // * The event store takes shared ownership of the object.
373 // */
374 // const_pointer_type put (const EventContext& ctx,
375 // SG::DataObjectSharedPtr<T> data) const;
376
377
378 // /**
379 // * @brief Record an object and its auxiliary store to the store.
380 // * @param data The object to record.
381 // * @param auxstore Auxiliary store object.
382 // *
383 // * Unlike record(), this does not change the handle object.
384 // * That means that one will not be able to get the object back
385 // * by dereferencing the handle.
386 // * Returns the object placed in the store, or nullptr if there
387 // * was an error.
388 // * If there was already an object in the store with the given key,
389 // * then return null, and the objects passed in are destroyed.
390 // */
391 // template <class AUXSTORE>
392 // const_pointer_type
393 // put (std::unique_ptr<T> data,
394 // std::unique_ptr<AUXSTORE> auxstore) const;
395
396
397 // /**
398 // * @brief Record an object and its auxiliary store to the store.
399 // * @param data The object to record.
400 // * @param auxstore Auxiliary store object.
401 // *
402 // * Unlike record(), this does not change the handle object.
403 // * That means that one will not be able to get the object back
404 // * by dereferencing the handle.
405 // * Returns the object placed in the store, or nullptr if there
406 // * was an error.
407 // * If there was already an object in the store with the given key,
408 // * then return null, and the objects passed in are destroyed.
409 // *
410 // * Unlike the version taking unique_ptr<T>, this does not alter the
411 // * store pointer of @c data.
412 // */
413 // template <class AUXSTORE>
414 // const_pointer_type
415 // put (std::unique_ptr<const T> data,
416 // std::unique_ptr<const AUXSTORE> auxstore) const;
417
418
419 // /**
420 // * @brief Record an object and its auxiliary store to the store.
421 // * @param ctx The event context to use.
422 // * @param data The object to record.
423 // * @param auxstore Auxiliary store object.
424 // *
425 // * Unlike record(), this does not change the handle object.
426 // * That means that one will not be able to get the object back
427 // * by dereferencing the handle.
428 // * Returns the object placed in the store, or nullptr if there
429 // * was an error.
430 // * If there was already an object in the store with the given key,
431 // * then return null, and the objects passed in are destroyed.
432 // */
433 // template <class AUXSTORE>
434 // const_pointer_type
435 // put (const EventContext& ctx,
436 // std::unique_ptr<T> data,
437 // std::unique_ptr<AUXSTORE> auxstore) const;
438
439
440 // /**
441 // * @brief Record an object and its auxiliary store to the store.
442 // * @param ctx The event context to use.
443 // * @param data The object to record.
444 // * @param auxstore Auxiliary store object.
445 // *
446 // * Unlike record(), this does not change the handle object.
447 // * That means that one will not be able to get the object back
448 // * by dereferencing the handle.
449 // * Returns the object placed in the store, or nullptr if there
450 // * was an error.
451 // * If there was already an object in the store with the given key,
452 // * then return null, and the objects passed in are destroyed.
453 // *
454 // * Unlike the version taking unique_ptr<T>, this does not alter the
455 // * store pointer of @c data.
456 // */
457 // template <class AUXSTORE>
458 // const_pointer_type
459 // put (const EventContext& ctx,
460 // std::unique_ptr<const T> data,
461 // std::unique_ptr<const AUXSTORE> auxstore) const;
462
463
470 WriteHandle& operator=( std::unique_ptr<T> data );
471
472
473 // /**
474 // * @brief Make an alias.
475 // * @param key Alternate key by which the referenced object should be known.
476 // *
477 // * The current handle should be valid and referencing an object
478 // * (i.e., @c record should have been called on it).
479 // *
480 // * The object will also be known by the name given in @c key.
481 // */
482 // StatusCode alias (const WriteHandleKey<T>& key);
483
484
485 // /**
486 // * @brief Make an explicit link.
487 // * @param key Alternate clid by which the referenced object
488 // * should be known. The SG key must match the key of the
489 // * current handle.
490 // *
491 // * You should generally not be using this!
492 // *
493 // * The current handle should be valid and referencing an object
494 // * (i.e., @c record should have been called on it).
495 // *
496 // * This makes a symlink: the object will be retrievable
497 // * as a different type.
498 // *
499 // * Note that if @c T and @c @U are related via @c SG_BASE and/or
500 // * @c DATAVECTOR_BASE, then you shouldn't need to explicitly make a symlink;
501 // * that should happen automatically.
502 // *
503 // * If a @c U* is not convertable to a @c T* via C++ rules, then you likely
504 // * will be, at best, relying on undefined behavior. You will probably
505 // * get warnings from the undefined behavior sanitizer when if you try
506 // * to dereference the @c U*.
507 // *
508 // * This usage is here mainly to assist in migrating some existing
509 // * patterns to MT. You should think several times before using
510 // * in new code.
511 // */
512 // template <class U>
513 // StatusCode symLink (const WriteHandleKey<U>& key);
514
515
516private:
517 // /**
518 // * @brief Return the cached pointer directly.
519 // *
520 // * If it is null, throw ExcNullWriteHandle.
521 // */
522 // pointer_type checkedCachedPtr();
523
524
525 // /**
526 // * @brief Helper for record.
527 // * @param data The object to record.
528 // * @param isConst If true, record the object as const.
529 // * @param returnExisting Allow an existing object.
530 // */
531 // template <class U>
532 // StatusCode doRecord (U data,
533 // bool isConst,
534 // bool returnExisting);
535
536
537 // /**
538 // * @brief Helper for put.
539 // * @param ctx The event context, or nullptr to use the current context.
540 // * @param data The object to record.
541 // * @param returnExisting Allow an existing object.
542 // * @param[out] store The store being used.
543 // *
544 // * Unlike record(), this does not change the handle object.
545 // * That means that one will not be able to get the object back
546 // * by dereferencing the handle.
547 // * Returns the object placed in the store, or nullptr if there
548 // * was an error.
549 // * If there was already an object in the store with the given key,
550 // * then return null, unless @c returnExisting is true, in which case
551 // * return success. In either case, @c data is destroyed.
552 // */
553 // template <class U>
554 // const_pointer_type doPut (const EventContext* ctx,
555 // U data,
556 // bool returnExisting,
557 // IProxyDict* & store) const;
558
559
560 // /**
561 // * @brief Helper for recording an object and its auxiliary store to the store.
562 // * @param ctx The event context, or nullptr to use the current context.
563 // * @param data The object to record.
564 // * @param auxstore Auxiliary store object.
565 // *
566 // * Unlike record(), this does not change the handle object.
567 // * That means that one will not be able to get the object back
568 // * by dereferencing the handle.
569 // * Returns the object placed in the store, or nullptr if there
570 // * was an error.
571 // * If there was already an object in the store with the given key,
572 // * then return null, and the objects passed in are destroyed.
573 // */
574 // template <class AUXSTORE>
575 // typename WriteHandle<T>::const_pointer_type
576 // doPut (const EventContext* ctx,
577 // std::unique_ptr<T> data,
578 // std::unique_ptr<AUXSTORE> auxstore) const;
579
580
581 // /**
582 // * @brief Helper for recording an object and its auxiliary store to the store.
583 // * @param ctx The event context, or nullptr to use the current context.
584 // * @param data The object to record.
585 // * @param auxstore Auxiliary store object.
586 // *
587 // * Unlike record(), this does not change the handle object.
588 // * That means that one will not be able to get the object back
589 // * by dereferencing the handle.
590 // * Returns the object placed in the store, or nullptr if there
591 // * was an error.
592 // * If there was already an object in the store with the given key,
593 // * then return null, and the objects passed in are destroyed.
594 // */
595 // template <class AUXSTORE>
596 // typename WriteHandle<T>::const_pointer_type
597 // doPut (const EventContext* ctx,
598 // std::unique_ptr<const T> data,
599 // std::unique_ptr<const AUXSTORE> auxstore) const;
600
601
602 // /**
603 // * @brief Record an object and its auxiliary store to the store.
604 // * @param data The object to record.
605 // * @param auxstore Auxiliary store object.
606 // * @param isConst If true, record the objects as const.
607 // */
608 // template <class AUXSTORE>
609 // StatusCode
610 // record (std::unique_ptr<T> data,
611 // std::unique_ptr<AUXSTORE> auxstore,
612 // bool isConst);
613
614
615private:
616 // /// If non-null, then we need to lock the associated aux store object
617 // /// when we're deleted.
618 // ///
619 // /// This is set when we record an object along with the associated aux const
620 // /// with the const flag set (the default). Recall that for a const record,
621 // /// we want to support the semantics that you can get a non-const pointer
622 // /// back from the handle as long as it exists, to finish initialization
623 // /// of the object. For an aux store, though, just getting back a non-const
624 // /// pointer is not sufficient, since the store will have been locked
625 // /// at the time of the record, preventing changes to the store.
626 // ///
627 // /// So if we're meant to record a const aux store object, we don't actually
628 // /// set it const on the record, but instead set this and do the
629 // /// setConst in the destructor.
630 // SG::DataProxy* m_lockAuxPending = nullptr;
631
633 T *m_ptr {nullptr};
634};
635
636
644template <class T>
646
647
659template <class T>
661 const EventContext& ctx);
662
663
664} /* namespace SG */
665
666
668
669
670// #ifndef NO_LEGACY_HANDLES
671// namespace SG {
672// template <class T>
673// using WVar = WriteHandle<T>;
674// }
675// #endif
676
677#endif
678
679#endif
Base class for VarHandle classes.
Property holding a SG store/key/clid from which a WriteHandle is made.
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11
Handle class for recording to StoreGate.
virtual const std::string & key() const =0
void * m_ptr
The object to which we are bound.
std::string store() const
Return the name of the store holding the object we are proxying.
Property holding a SG store/key/clid from which a WriteHandle is made.
WriteHandle()
Default constructor.
WriteHandle & operator=(const WriteHandle &rhs)
Assignment operator.
~WriteHandle()
Destructor.
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
const_pointer_type put(std::unique_ptr< T > data, bool returnExisting=false) const
Record an object to the store.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
reference_type operator*()
Dereference the pointer.
::StatusCode StatusCode
StatusCode definition for legacy code.
Forward declaration.
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
unsigned long long T