AnaToolHandle.icc 31 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <AsgTools/MessageCheck.h>
#include <assert.h>
#include <cstdlib>

#ifndef ROOTCORE
#include "GaudiKernel/IJobOptionsSvc.h"
#endif


namespace asg
{
  namespace detail
  {
#ifdef ROOTCORE
    StatusCode makeToolRootCore (const std::string& type,
				 const std::string& name,
				 AsgTool*& tool);
#else
    StatusCode copyPropertiesInCatalogue( const std::string& fromTool, const std::string& toTool );
    StatusCode hasPropertiesInCatalogue( const std::string& toolName );
    StatusCode addPropertyToCatalogue( const std::string& toolName, const std::string& property, const std::string& val);
    StatusCode removePropertyFromCatalogue( const std::string& toolName, const std::string& property);
    StatusCode toolExists( const std::string& fullName, interfaceType_t*& tool );
    // StatusCode factoryExists( const std::string& type );
#endif



29
30
31
32
33
34
35
36
37
38
    /// \brief do a checked cast from one ToolHandle interface to
    /// another
    /// \par Guarantee
    ///   basic
    /// \par Failures
    ///   interface not supported for tool
    template<typename T1,typename T2> StatusCode
    toolHandleCast (ToolHandle<T1>& to, ToolHandle<T2>& from)
    {
      using namespace msgUserCode;
39
40

#ifdef ROOTCORE
41
42
43
44
45
46
      to = ToolHandle<T1> (dynamic_cast<T1*>(&*from));
      if (!from.empty() && &*to == nullptr)
      {
	ANA_MSG_ERROR ("failed to cast from type " << typeid(T2).name() << " to  " << typeid(T1).name());
	return StatusCode::FAILURE;
      }
47
#else
48
49
50
      to = ToolHandle<T1> (from.typeAndName(), from.parent());
      if (!from.empty())
        ANA_CHECK (to.retrieve());
51
#endif
52
53
54
55
56
      return StatusCode::SUCCESS;
    }



57

58
59
60
61

    /// \brief the information needed to share a tool

    class AnaToolShare
62
    {
63
64
65
      //
      // public interface
      //
66

67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
      /// \brief standard constructor
      /// \par Guarantee
      ///   strong
      /// \par Failures
      ///   out of memory II
    public:
      AnaToolShare (const ToolHandle<interfaceType_t>& val_th,
		    AnaToolCleanup val_cleanup);


      /// \brief the tool we contain
      /// \par Guarantee
      ///   no-fail
      /// \post result != nullptr
    public:
      ToolHandle<interfaceType_t>& th ();



      //
      // private interface
      //

      /// \brief the value of \ref tool
    private:
      ToolHandle<interfaceType_t> m_th;

      /// \brief resources to release shen we are releasing the tool
    private:
      AnaToolCleanup m_cleanup;
    };





    /// \brief a service for sharing tools of a given type

    class AnaToolShareList
    {
      //
      // public interface
      //

      /// \brief the singleton we are using
      /// \par Guarantee
      ///   no-fail
    public:
      static AnaToolShareList& singleton () noexcept;


      /// \brief get the share for the given name, or nullptr if no
      /// share has been defined
      /// \par Guarantee
      ///   strong
      /// \par Failures
      ///   out of memory I
    public:
      std::shared_ptr<AnaToolShare>
      getShare (const std::string& name) const;


      /// \brief set the share for the given name
      /// \return the share to use, this is either the share passed in
      /// or the already set share
      /// \par Guarantee
      ///   strong
      /// \par Failures
      ///   out of memory II
    private:
      std::shared_ptr<AnaToolShare>
      setShare (const std::string& name,
		std::unique_ptr<AnaToolShare> val_share);


      /// \brief make or get a share for the given name
      /// \return the share to use, either freshly created or
      /// retrieved from the store
      /// \par Guarantee
      ///   strong
      /// \par Failures
      ///   tool creation failures\n
      ///   out of memory II
    public:
      StatusCode
      makeShare (const std::string& name,
		 const AnaToolConfig& config,
		 std::shared_ptr<AnaToolShare>& result);



      //
      // private interface
      //

      /// \brief the shares we manage
    private:
      std::map<std::string,std::weak_ptr<AnaToolShare> > m_shared;
    };
166
167
168
169



#ifndef ROOTCORE
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
    /// \brief manage a single property in the job options service

    struct PropertyValueManager
    {
      PropertyValueManager (const std::string& val_toolName,
			    const std::string& val_name)
	: m_toolName (val_toolName), m_name (val_name)
      {
      }

      ~PropertyValueManager ()
      {
	if (m_cleanup)
	  detail::removePropertyFromCatalogue (m_toolName, m_name).ignore();
      }

      std::string m_toolName;
      std::string m_name;
      bool m_cleanup = false;
    };



    /// \brief a \ref AnaToolProperty containing a regular value for
    /// Athena

    class AnaToolPropertyValueAthena : public AnaToolProperty
    {
    public:
      template<typename Type>
      AnaToolPropertyValueAthena (const Type& val_value)
	: m_value (Gaudi::Utils::toString (val_value))
      {
      }

    public:
      virtual StatusCode
      applyPropertyAthena (const std::string& toolName,
			   const std::string& name,
			   AnaToolCleanup& cleanup)
	const override;

    private:
      std::string m_value;
    };
215
216
217
218
#endif



219
220


221
#ifdef ROOTCORE
222
223
    /// \brief a \ref AnaToolProperty containing a regular value in
    /// RootCore
224

225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
    template<typename Type>
    class AnaToolPropertyValueRootCore : public AnaToolProperty
    {
    public:
      AnaToolPropertyValueRootCore (const Type& val_value)
	: m_value (val_value)
      {}

    public:
      virtual StatusCode
      applyPropertyRootCore (AsgTool& tool, const std::string& name,
			     AnaToolCleanup& /*cleanup*/)
	const override
      {
	using namespace msgToolHandle;
	ANA_CHECK (tool.setProperty (name, m_value));
	return StatusCode::SUCCESS;
      }

    private:
      Type m_value;
    };
247
248
249
250
251
#endif




252
253
254
255
256
257
258
259
260
261
262
263

    /// \brief a \ref AnaToolProperty containing an \ref AnaToolConfig
    /// for configuring a private sub-tool

    template<typename Type>
    class AnaToolPropertyPrivateTool : public AnaToolProperty
    {
    public:
      AnaToolPropertyPrivateTool (const AnaToolConfig& val_config)
	: m_config (val_config)
      {}

264
#ifdef ROOTCORE
265
266
267
268
269
270
271
272
273
274
275
276
277
278
    public:
      virtual StatusCode
      applyPropertyRootCore (AsgTool& tool, const std::string& name,
			     AnaToolCleanup& cleanup)
	const override
      {
	using namespace msgToolHandle;
	ToolHandle<Type> th;
	AnaToolCleanup mycleanup;
	ANA_CHECK (m_config.makeTool (name, &tool, th, mycleanup));
	cleanup.addCleanup (std::move (mycleanup));
	ANA_CHECK (tool.setProperty (name, th));
	return StatusCode::SUCCESS;
      }
279
280
#endif

281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#ifndef ROOTCORE
    private:
      virtual StatusCode
      applyPropertyAthena (const std::string& toolName,
			   const std::string& name,
			   AnaToolCleanup& cleanup)
	const override
      {
	using namespace msgToolHandle;
	ANA_CHECK (m_config.applyPropertiesAthena (toolName + "." + name, cleanup));

	// this is so we clear out properties from catalogue when tool
	// is destroyed.  DISABLED: as we no longer release tools in Athena
	// std::shared_ptr<PropertyValueManager> manager
	//   (new PropertyValueManager (th.name(), name));
	// cleanup.addCleanup (manager);

	// for athena we hand our property to the joboptions svc
        ANA_CHECK (detail::addPropertyToCatalogue (toolName, name, m_config.type() + "/" + name));
	// manager->m_cleanup = true;
	return StatusCode::SUCCESS;
      }
303
304
#endif

305
306
307
    private:
      AnaToolConfig m_config;
    };
308
309
310
311
312
313





#ifndef ROOTCORE
314
315
316
317
318
319
320
321
    /// \brief a \ref AnaToolProperty copying a tool configuration
    /// from the job options service to a new place

    class AnaToolPropertyCopyTool : public AnaToolProperty
    {
    public:
      AnaToolPropertyCopyTool (const std::string& val_type,
                               const std::string& val_name);
322

323
324
325
326
327
328
    private:
      virtual StatusCode
      applyPropertyAthena (const std::string& toolName,
			   const std::string& name,
			   AnaToolCleanup& cleanup)
	const override;
329

330
331
332
      /// \brief the type of the tool to create
    private:
      std::string m_type;
333

334
335
336
337
      /// \brief the name of the source tool
    private:
      std::string m_name;
    };
338
339
340
341
342
343
#endif





344
345
346
347
348
349
350
351
352
353
354
355
    /// \brief a \ref AnaToolProperty containing an \ref AnaToolConfig
    /// for configuring a public sub-tool

    template<typename Type>
    class AnaToolPropertyPublicTool : public AnaToolProperty
    {
    public:
      AnaToolPropertyPublicTool (const std::string& val_name,
				 const AnaToolConfig& val_config)
	: m_name (val_name), m_config (val_config)
      {}

356
#ifdef ROOTCORE
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
    public:
      virtual StatusCode
      applyPropertyRootCore (AsgTool& tool, const std::string& name,
			     AnaToolCleanup& cleanup)
	const override
      {
	using namespace msgToolHandle;
	std::shared_ptr<AnaToolShare> share;
	ANA_CHECK (AnaToolShareList::singleton().makeShare
		     (m_name, m_config, share));
	cleanup.addCleanup (share);
	ToolHandle<Type> myth;
	ANA_CHECK (toolHandleCast (myth, share->th()));
	ANA_CHECK (tool.setProperty (name, myth));
	return StatusCode::SUCCESS;
      }
373
374
#endif

375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#ifndef ROOTCORE
    private:
      virtual StatusCode
      applyPropertyAthena (const std::string& toolName,
			   const std::string& name,
			   AnaToolCleanup& cleanup)
	const override
      {
	using namespace msgToolHandle;
	std::shared_ptr<AnaToolShare> share;
	ANA_CHECK (AnaToolShareList::singleton().makeShare
		     (m_name, m_config, share));
	cleanup.addCleanup (share);

	ToolHandle<Type> th;
	ANA_CHECK (toolHandleCast (th, share->th()));

	// this is so we clear out properties from catalogue when tool
	// is destroyed.  DISABLED: we no longer clear out tools after
	// we are done with them
	// std::shared_ptr<PropertyValueManager> manager
	//   (new PropertyValueManager (th.name(), name));
	// cleanup.addCleanup (manager);

	// for athena we hand our property to the joboptions svc
	ANA_CHECK (detail::addPropertyToCatalogue (toolName, name, th.typeAndName()));
        // manager->m_cleanup = true;
	return StatusCode::SUCCESS;
      }
404
405
#endif

406
407
408
409
410
    private:
      std::string m_name;
      AnaToolConfig m_config;
    };

411
412
413
414




415
416
417
#ifdef ROOTCORE
    template<typename Type> void AnaToolConfig ::
    registerNew ()
418
    {
419
420
421
      m_factory = [] (AsgTool*& tool, const std::string& name) -> StatusCode {
	tool = new Type (name); return StatusCode::SUCCESS;};
    }
422
#endif
423
424
425
426
427
428
429
430
431
432



#ifdef ROOTCORE
    template<typename Type> StatusCode AnaToolConfig ::
    setProperty (const std::string& val_name, const Type& val_value)
    {
      std::shared_ptr<detail::AnaToolProperty> anaProperty
	(new detail::AnaToolPropertyValueRootCore<Type> (val_value));
      addProperty (val_name, anaProperty);
433
434
      return StatusCode::SUCCESS;
    }
435
436
437
438
439
440
441
442
443
444
445
#else
    template<typename Type> StatusCode AnaToolConfig ::
    setProperty (const std::string& val_name, const Type& val_value)
    {
      using namespace msgToolHandle;
      std::shared_ptr<detail::AnaToolProperty> anaProperty
	(new detail::AnaToolPropertyValueAthena (val_value));
      addProperty (val_name, anaProperty);
      return StatusCode::SUCCESS;
    }
#endif
446
447
448



449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
    template<typename Type> StatusCode AnaToolConfig ::
    setProperty (const std::string& val_name,
		 const AnaToolHandle<Type>& val_value)
    {
      using namespace msgToolHandle;

      // once initialized the AnaToolHandle is not that different from
      // a regular ToolHandle and we just use it as such
      if (val_value.isInitialized())
	return setProperty (val_name, val_value.getHandle());

      switch (val_value.mode())
      {
      case AnaToolHandleMode::EMPTY:
	return setProperty (val_name, ToolHandle<Type> ());
      case AnaToolHandleMode::CREATE_PRIVATE:
	{
	  std::shared_ptr<AnaToolPropertyPrivateTool<Type> > anaProperty
	    (new AnaToolPropertyPrivateTool<Type> (val_value.config()));
	  addProperty (val_name, anaProperty);
	}
	return StatusCode::SUCCESS;
      case AnaToolHandleMode::CREATE_SHARED:
      case AnaToolHandleMode::RETRIEVE_SHARED:
	{
	  std::shared_ptr<AnaToolPropertyPublicTool<Type> > anaProperty
	    (new AnaToolPropertyPublicTool<Type> (val_value.name(), val_value.config()));
	  addProperty (val_name, anaProperty);
	}
	return StatusCode::SUCCESS;
      case AnaToolHandleMode::USER:
	return setProperty (val_name, val_value.getHandle());
      }
      return StatusCode::FAILURE; //compiler dummy
    }



#ifndef ROOTCORE
    template<typename Type> StatusCode AnaToolConfig ::
    setProperty (const std::string& val_name,
		 const ToolHandle<Type>& val_value)
    {
      using namespace msgToolHandle;

      //strip the parent name
      if (val_value.empty())
        return setProperty (val_name, std::string ());
      else if (val_value.isPublic())
      {
        return setProperty (val_name,
                            val_value.type() + "/" + val_value.name());
      } else
      {
        std::shared_ptr<AnaToolPropertyCopyTool> shared
          (new AnaToolPropertyCopyTool
           (val_value.type(), val_value.parentName() + "." + val_value.name()));
        addProperty (val_name, shared);
        return StatusCode::SUCCESS;
      }
    }



    template<typename Type> StatusCode AnaToolConfig ::
    setProperty (const std::string& val_name,
		 const ToolHandleArray<Type>& val_value)
    {
      using namespace msgToolHandle;

      std::vector<std::string> tools;
      // loop over toolhandles in array, strip off any parent naming
      // and set property with the result
      for (auto& toolHandle : val_value)
      {
	if (toolHandle.empty())
	{
	  ANA_MSG_ERROR ("trying to initialize ToolHandleArray property " << val_name << " with empty handle");
	  return StatusCode::FAILURE;
	}
529

530
531
532
533
534
535
536
	//strip the parent name
	std::string tool_name = toolHandle.name();
	tools.push_back (toolHandle.type() + "/" + tool_name);
      }

      return setProperty (val_name, tools);
    }
537
538
#endif

539
540
541
542
543
544
545
546



    template<typename ToolType> StatusCode AnaToolConfig ::
    makeTool (const std::string& name,
              parentType_t *parent,
	      ToolHandle<ToolType>& th,
	      AnaToolCleanup& cleanup) const
547
    {
548
549
550
551
552
553
554
555
      using namespace msgUserCode;
      ToolHandle<interfaceType_t> baseTH;
      AnaToolCleanup baseCleanup;
      ANA_CHECK (makeBaseTool (name, parent, baseTH, baseCleanup));
      ANA_CHECK (toolHandleCast (th, baseTH));
      cleanup.swap (baseCleanup);
#ifndef ROOTCORE
      baseTH->release ();
556
557
558
#endif
      return StatusCode::SUCCESS;
    }
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
  }



  template <typename T>
  std::ostream& operator << (std::ostream& str, const AnaToolHandle<T>& obj)
  {
    return str << obj.getHandle();
  }



  template<class T> void AnaToolHandle<T> ::
  testInvariant () const
  {
#define CHECK_INVARIANT(x)			\
    if (!(x)) { std::cerr << __FILE__ << ":" << __LINE__ << ": invariant violated: " << #x << std::endl; std::abort(); }

    // General requirements
    CHECK_INVARIANT (&*this != nullptr);
    CHECK_INVARIANT (m_handleUser != nullptr);
    // CHECK_INVARIANT (m_parentPtr == nullptr || m_parentPtr->name() == m_parentName);
    // #ifndef ROOTCORE
    // CHECK_INVARIANT (m_parentPtr == m_handleUser->parent());
    // #endif
584

585
#undef CHECK_INVARIANT
586
587
588
589
  }



590
591
  template<class T> AnaToolHandle<T> ::
  AnaToolHandle (const std::string& val_name, parentType_t *val_parent)
592
    : m_parentPtr (val_parent),
593
594
      m_handleUser (new ToolHandle<T> (val_name, val_parent)),
      m_allowEmpty (val_name.empty())
595
  {
596
    setTypeAndName (val_name);
597
598
599
600

#ifndef NDEBUG
    this->testInvariant ();
#endif
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
  }



  template<class T> AnaToolHandle<T> ::
  AnaToolHandle (AnaToolHandle<T>&& that)
    : AnaToolHandle ()
  {
    swap (that);
  }



  template<class T> AnaToolHandle<T> ::
  AnaToolHandle (const AnaToolHandle<T>& that)
    : m_config (that.m_config),
      m_name (that.m_name),
      m_parentPtr (that.m_parentPtr),
      m_handleUser (new ToolHandle<T> (*that.m_handleUser)),
      m_originalTypeAndName (that.m_originalTypeAndName),
      m_isInitialized (that.m_isInitialized.load()),
      m_allowEmpty (that.m_allowEmpty)
  {
    if (m_isInitialized.load())
625
    {
626
627
628
      m_toolPtr = that.m_toolPtr;
      m_mode = that.m_mode;
      m_cleanup = that.m_cleanup;
629
630
    }

631
632
633
634
#ifndef ROOTCORE
    if (!m_handleUser->empty())
      (*m_handleUser)->release();
#endif
635
636

#ifndef NDEBUG
637
    that.testInvariant ();
638
639
    testInvariant ();
#endif
640
  }
641
642
643



644
645
646
647
648
649
  template<class T> AnaToolHandle<T>& AnaToolHandle<T> ::
  operator = (const AnaToolHandle<T>& that)
  {
    // no invariant used
    AnaToolHandle<T> (that).swap (*this);
    return *this;
650
651
652
653
  }



654
655
  template<class T> void AnaToolHandle<T> ::
  swap (AnaToolHandle<T>& that) noexcept
656
657
  {
#ifndef NDEBUG
658
659
    testInvariant ();
    that.testInvariant ();
660
661
#endif

662
663
664
665
    m_cleanup.swap (that.m_cleanup);
    m_config.swap (that.m_config);
    m_name.swap (that.m_name);
    std::swap (m_parentPtr, that.m_parentPtr);
666
    {
667
668
669
670
      ToolHandle<T> tmp = *m_handleUser;
#ifndef ROOTCORE
      if (!tmp.empty())
        tmp->release();
671
#endif
672
673
      *m_handleUser = *that.m_handleUser;
      *that.m_handleUser = tmp;
674
    }
675
    m_originalTypeAndName.swap (that.m_originalTypeAndName);
676
    {
677
678
679
      const auto tmp = m_isInitialized.load();
      m_isInitialized = that.m_isInitialized.load();
      that.m_isInitialized = tmp;
680
    }
681
682
683
    std::swap (m_toolPtr, that.m_toolPtr);
    std::swap (m_mode, that.m_mode);
    std::swap (m_allowEmpty, that.m_allowEmpty);
684
685
686

#ifndef NDEBUG
    testInvariant ();
687
    that.testInvariant ();
688
#endif
689
  }
690
691


692

693
694
695
696
697
698
  template<class T> AnaToolHandle<T>& AnaToolHandle<T> ::
  operator = (AnaToolHandle<T>&& that)
  {
    // no invariant used
    swap (that);
    return *this;
699
700
701
702
  }



703
704
  template<class T> AnaToolHandle<T> ::
  ~AnaToolHandle () noexcept
705
706
707
708
709
  {
    using namespace msgToolHandle;
#ifndef NDEBUG
    this->testInvariant ();
#endif
710
  }
711
712


713
714
715
716
  template<class T> bool
  AnaToolHandle<T> ::
  empty () const
  {
717
#ifndef NDEBUG
718
     this->testInvariant ();
719
#endif
720
721
722
723
724
725
726
727
728
729
    switch (getMode())
    {
    case detail::AnaToolHandleMode::EMPTY:
      return true;
    case detail::AnaToolHandleMode::CREATE_PRIVATE:
    case detail::AnaToolHandleMode::CREATE_SHARED:
    case detail::AnaToolHandleMode::RETRIEVE_SHARED:
      return false;
    case detail::AnaToolHandleMode::USER:
      return m_handleUser->empty();
730
    }
731
732
    return false; //compiler dummy
  }
733

734

735

736
737
738
739
740
741
  template<class T> inline bool AnaToolHandle<T> ::
  isPublic () const noexcept
  {
#ifndef NDEBUG
     this->testInvariant ();
#endif
742
     return m_parentPtr == nullptr;
743
744
  }

745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763


   template<class T> inline const std::string& AnaToolHandle<T> ::
   type () const noexcept
   {
#ifndef NDEBUG
     this->testInvariant ();
#endif
     return m_config.type();
   }



   template<class T> inline const std::string&
   AnaToolHandle<T> ::
   name () const noexcept
   {
#ifndef NDEBUG
     this->testInvariant ();
764
#endif
765
766
767
     return m_name;
   }

768
769
770
771


  template <class T> template <class T2>
  StatusCode AnaToolHandle<T> ::
772
  setProperty (const std::string& property, const T2& value)
773
774
775
  {
    using namespace msgToolHandle;

776
777
778
#ifndef NDEBUG
    this->testInvariant ();
    if (isInitialized())
779
    {
780
781
      ANA_MSG_FATAL ("AnaToolHandle has already been initialized: " << *this);
      std::abort();
782
783
    }
#endif
784
785

    return m_config.setProperty (property, value);
786
787
788
789
790
791
792
793
794
795
  }



  template<class T> StatusCode
  AnaToolHandle<T> ::
  initialize ()
  {
    using namespace msgToolHandle;

796
797
798
    // ensure we don't initialize twice concurrently
    std::lock_guard<std::recursive_mutex> lock (m_initializeMutex);

799
800
#ifndef NDEBUG
    this->testInvariant ();
801
    if (isInitialized())
802
    {
803
804
      ANA_MSG_FATAL ("AnaToolHandle has already been initialized: " << *this);
      std::abort();
805
    }
806
#endif
807

808
809
    std::shared_ptr<detail::AnaToolShare> sharedTool;
    const detail::AnaToolHandleMode mode = getMode (sharedTool);
810

811
812
    ToolHandle<T> th;
    detail::AnaToolCleanup cleanup;
813

814
    switch (mode)
815
    {
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
    case detail::AnaToolHandleMode::EMPTY:
      break;
    case detail::AnaToolHandleMode::CREATE_PRIVATE:
      ANA_CHECK (m_config.makeTool (m_name, m_parentPtr, th, cleanup));
      break;
    case detail::AnaToolHandleMode::CREATE_SHARED:
      ANA_CHECK (detail::AnaToolShareList::singleton().makeShare (m_name, m_config, sharedTool));
      // no break
    case detail::AnaToolHandleMode::RETRIEVE_SHARED:
      assert (sharedTool != nullptr);
      ANA_CHECK (detail::toolHandleCast (th, sharedTool->th()));
#ifndef ROOTCORE
      if (!th.empty())
        th->release ();
#endif
      cleanup.addCleanup (sharedTool);
      break;
    case detail::AnaToolHandleMode::USER:
      th = *m_handleUser;
      if (th.empty() && !m_allowEmpty)
      {
	ANA_MSG_ERROR ("user configured an empty handle for a non-empty AnaToolHandle: " << *this);
	return StatusCode::FAILURE;
      }
      break;
841
842
    }

843
844
    T *toolPtr = nullptr;
    ANA_CHECK (makeToolRetrieve (toolPtr, th));
845

846
847
848
849
    *m_handleUser = th;
#ifndef ROOTCORE
    if (!th.empty())
      th->release ();
850
#endif
851
852
853
854
    m_mode = mode;
    m_cleanup.swap (cleanup);
    m_toolPtr = toolPtr;
    m_isInitialized = true;
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880

#ifndef NDEBUG
    this->testInvariant ();
#endif
    return StatusCode::SUCCESS;
  }



  template<class T> StatusCode
  AnaToolHandle<T> ::
  retrieve ()
  {
    // no invariant used
    return initialize ();
  }


  template<class T> bool
  AnaToolHandle<T> ::
  isConfigurable () const
  {
    using namespace msgToolHandle;
#ifndef NDEBUG
    this->testInvariant ();
#endif
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
    switch (getMode())
    {
    case detail::AnaToolHandleMode::EMPTY:
    case detail::AnaToolHandleMode::CREATE_PRIVATE:
    case detail::AnaToolHandleMode::CREATE_SHARED:
      return true;
    case detail::AnaToolHandleMode::RETRIEVE_SHARED:
    case detail::AnaToolHandleMode::USER:
      return false;
    }
    return true; //compiler dummy
  }



  template<class T>
#ifndef NDEBUG
  inline
899
#endif
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
  bool AnaToolHandle<T> ::
  isInitialized () const noexcept
  {
#ifndef NDEBUG
    this->testInvariant ();
#endif
    return m_isInitialized;
  }



  template<class T> inline T *
  AnaToolHandle<T> ::
  operator -> ()
  {
    // no invariant used
    return get ();
917
918
919
920
  }



921
  template<class T> inline const T *
922
  AnaToolHandle<T> ::
923
  operator -> () const
924
  {
925
926
    // no invariant used
    return get ();
927
928
929
930
  }



931
  template<class T> inline T&
932
  AnaToolHandle<T> ::
933
  operator * ()
934
935
  {
    // no invariant used
936
    return *get ();
937
938
939
940
  }



941
  template<class T> inline const T&
942
  AnaToolHandle<T> ::
943
  operator * () const
944
945
946
947
948
949
950
951
952
  {
    // no invariant used
    return *get ();
  }



  template<class T> inline T*
  AnaToolHandle<T> ::
953
  get ()
954
955
956
957
958
959
  {
    using namespace msgToolHandle;

#ifndef NDEBUG
    this->testInvariant ();
#endif
960
961
962
963
964
965
966
967

    if (m_isInitialized.load())
      return m_toolPtr;

    std::lock_guard<std::recursive_mutex> lock (m_initializeMutex);
    if (!m_isInitialized)
      ANA_CHECK_THROW (initialize());
    assert (m_isInitialized);
968
969
970
971
972
    return m_toolPtr;
  }



973
974
975
976
977
978
979
980
981
  template<class T> inline const T*
  AnaToolHandle<T> ::
  get () const
  {
    return const_cast<AnaToolHandle<T>*>(this)->get();
  }



982
983
984
985
986
987
988
989
  template <class T> template<typename T2> void
  AnaToolHandle<T> ::
  declarePropertyFor (T2 *tool, const std::string& name,
		      const std::string& description)
  {
    using namespace msgToolHandle;
#ifndef NDEBUG
    this->testInvariant ();
990
    if (isInitialized())
991
992
993
994
    {
      ANA_MSG_FATAL ("can't declare tool handle " << *this << " as property " << name << " after tool has been instantiated");
      std::abort ();
    }
995
#endif
996

997
    if (m_parentPtr != nullptr && m_parentPtr != tool)
998
    {
999
      ANA_MSG_FATAL ("can't declare tool handle " << *this << " as property for tool " << tool->name() << " as it has a different parent tool " << m_parentPtr->name());
1000
      std::abort ();
For faster browsing, not all history is shown. View entire blame