Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-01-03 10:09:54

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 "GaudiKernel/DataObject.h"
0025 #include "podio/CollectionBase.h"
0026 
0027 // forward declaration
0028 namespace k4FWCore {
0029 template <typename T>
0030 class DataHandle;
0031 }
0032 
0033 class GAUDI_API DataWrapperBase : public DataObject {
0034 public:
0035   // ugly hack to circumvent the usage of boost::any yet
0036   // DataSvc would need a templated register method
0037   virtual podio::CollectionBase* collectionBase() = 0;
0038   virtual void resetData() = 0;
0039 };
0040 
0041 template <class T>
0042 class GAUDI_API DataWrapper : public DataWrapperBase {
0043 public:
0044   template <class T2>
0045   friend class k4FWCore::DataHandle;
0046 
0047 public:
0048   DataWrapper() : m_data(nullptr) {}
0049   DataWrapper(T&& coll) {
0050     m_data = new T(std::move(coll));
0051     is_owner = true;
0052   }
0053   DataWrapper(std::unique_ptr<T> uptr) : m_data(uptr.get()) {
0054     uptr.release();
0055     is_owner = false;
0056   }
0057   ~DataWrapper() override;
0058 
0059   const T* getData() const { return m_data; }
0060   void setData(const T* data) { m_data = data; }
0061   void resetData() override { m_data = nullptr; }
0062 
0063   operator const T&() const& { return *m_data; }
0064 
0065 private:
0066   /// try to cast to collectionBase; may return nullptr;
0067   podio::CollectionBase* collectionBase() override;
0068 
0069 private:
0070   const T* m_data;
0071   bool is_owner{true};
0072 };
0073 
0074 template <class T>
0075 DataWrapper<T>::~DataWrapper() {
0076   if (is_owner) {
0077     delete m_data;
0078   }
0079 }
0080 
0081 template <class T>
0082 podio::CollectionBase* DataWrapper<T>::collectionBase() {
0083   if constexpr (std::is_base_of<podio::CollectionBase, T>::value) {
0084     return const_cast<T*>(m_data);
0085   }
0086   return nullptr;
0087 }
0088 
0089 #endif