Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:13:26

0001 /*
0002  * Copyright (c) 2014-2024 Key4hep-Project.
0003  *
0004  * This file is part of Key4hep.
0005  * See https://key4hep.github.io/key4hep-doc/ for further info.
0006  *
0007  * Licensed under the Apache License, Version 2.0 (the "License");
0008  * you may not use this file except in compliance with the License.
0009  * You may obtain a copy of the License at
0010  *
0011  *     http://www.apache.org/licenses/LICENSE-2.0
0012  *
0013  * Unless required by applicable law or agreed to in writing, software
0014  * distributed under the License is distributed on an "AS IS" BASIS,
0015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0016  * See the License for the specific language governing permissions and
0017  * limitations under the License.
0018  */
0019 #ifndef K4FWCORE_DATAWRAPPER_H
0020 #define K4FWCORE_DATAWRAPPER_H
0021 
0022 #include <type_traits>
0023 
0024 // Include files
0025 #include "GaudiKernel/DataObject.h"
0026 #include "podio/CollectionBase.h"
0027 
0028 // forward declaration
0029 template <typename T> class DataHandle;
0030 
0031 class GAUDI_API DataWrapperBase : public DataObject {
0032 public:
0033   // ugly hack to circumvent the usage of boost::any yet
0034   // DataSvc would need a templated register method
0035   virtual podio::CollectionBase* collectionBase() = 0;
0036   virtual ~DataWrapperBase(){};
0037   virtual void resetData() = 0;
0038 };
0039 
0040 template <class T> class GAUDI_API DataWrapper : public DataWrapperBase {
0041 public:
0042   template <class T2> friend class DataHandle;
0043 
0044 public:
0045   DataWrapper() : m_data(nullptr){};
0046   DataWrapper(T&& coll) {
0047     m_data   = new T(std::move(coll));
0048     is_owner = true;
0049   }
0050   DataWrapper(std::unique_ptr<T> uptr) : m_data(uptr.get()) {
0051     uptr.release();
0052     is_owner = false;
0053   };
0054   virtual ~DataWrapper();
0055 
0056   const T*     getData() const { return m_data; }
0057   void         setData(const T* data) { m_data = data; }
0058   virtual void resetData() { m_data = nullptr; }
0059 
0060   operator const T&() const& { return *m_data; }
0061 
0062 private:
0063   /// try to cast to collectionBase; may return nullptr;
0064   virtual podio::CollectionBase* collectionBase();
0065 
0066 private:
0067   const T* m_data;
0068   bool     is_owner{true};
0069 };
0070 
0071 template <class T> DataWrapper<T>::~DataWrapper() {
0072   if (is_owner && !m_data)
0073     delete m_data;
0074 }
0075 
0076 template <class T> podio::CollectionBase* DataWrapper<T>::collectionBase() {
0077   if constexpr (std::is_base_of<podio::CollectionBase, T>::value) {
0078     return const_cast<T*>(m_data);
0079   }
0080   return nullptr;
0081 }
0082 
0083 #endif