Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-14 08:56:25

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 void                   resetData()      = 0;
0037 };
0038 
0039 template <class T> class GAUDI_API DataWrapper : public DataWrapperBase {
0040 public:
0041   template <class T2> friend class DataHandle;
0042 
0043 public:
0044   DataWrapper() : m_data(nullptr) {}
0045   DataWrapper(T&& coll) {
0046     m_data   = new T(std::move(coll));
0047     is_owner = true;
0048   }
0049   DataWrapper(std::unique_ptr<T> uptr) : m_data(uptr.get()) {
0050     uptr.release();
0051     is_owner = false;
0052   }
0053   virtual ~DataWrapper();
0054 
0055   const T* getData() const { return m_data; }
0056   void     setData(const T* data) { m_data = data; }
0057   void     resetData() override { m_data = nullptr; }
0058 
0059   operator const T&() const& { return *m_data; }
0060 
0061 private:
0062   /// try to cast to collectionBase; may return nullptr;
0063   podio::CollectionBase* collectionBase() override;
0064 
0065 private:
0066   const T* m_data;
0067   bool     is_owner{true};
0068 };
0069 
0070 template <class T> DataWrapper<T>::~DataWrapper() {
0071   if (is_owner) {
0072     delete m_data;
0073   }
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