Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-11 08:32:06

0001 /***********************************************************************************\
0002 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
0003 *                                                                                   *
0004 * This software is distributed under the terms of the Apache version 2 licence,     *
0005 * copied verbatim in the file "LICENSE".                                            *
0006 *                                                                                   *
0007 * In applying this licence, CERN does not waive the privileges and immunities       *
0008 * granted to it by virtue of its status as an Intergovernmental Organization        *
0009 * or submit itself to any jurisdiction.                                             *
0010 \***********************************************************************************/
0011 #pragma once
0012 #include <GaudiKernel/DataObject.h>
0013 #include <cstddef>
0014 #include <iterator>
0015 #include <optional>
0016 #include <typeindex>
0017 #include <utility>
0018 
0019 namespace details {
0020   using std::size;
0021 
0022   template <typename T, typename... Args>
0023   constexpr auto size( const T&, Args&&... ) noexcept {
0024     static_assert( sizeof...( Args ) == 0, "No extra args please" );
0025     return std::nullopt;
0026   }
0027 } // namespace details
0028 
0029 // ugly hack to circumvent the usage of std::any
0030 struct GAUDI_API AnyDataWrapperBase : DataObject {
0031   virtual std::optional<std::size_t> size() const = 0;
0032 
0033   class Ptr {
0034     void const*     m_ptr  = nullptr;
0035     std::type_index m_type = typeid( void );
0036 
0037   public:
0038     template <typename T>
0039     Ptr( T const* t ) : m_ptr{ t }, m_type{ typeid( T ) } {}
0040 
0041     operator void const*() const { return m_ptr; }
0042     std::type_index type() const { return m_type; }
0043 
0044     template <typename T>
0045     T const* get() const {
0046       if ( std::is_void_v<T> || m_type == std::type_index( typeid( T ) ) ) return static_cast<T const*>( m_ptr );
0047       struct bad_AnyDataWrapper_Ptr_cast : std::bad_cast {};
0048       throw bad_AnyDataWrapper_Ptr_cast{};
0049     }
0050   };
0051   virtual Ptr payload() const = 0;
0052 };
0053 
0054 template <typename T>
0055 class GAUDI_API AnyDataWrapper : public AnyDataWrapperBase {
0056 protected:
0057   T m_data;
0058 
0059 public:
0060   AnyDataWrapper( T&& data ) : m_data{ std::move( data ) } {};
0061   AnyDataWrapper( AnyDataWrapper&& )                 = delete;
0062   AnyDataWrapper( AnyDataWrapper const& )            = delete;
0063   AnyDataWrapper& operator=( AnyDataWrapper&& )      = delete;
0064   AnyDataWrapper& operator=( AnyDataWrapper const& ) = delete;
0065 
0066   const T& getData() const { return m_data; }
0067   T&       getData() { return m_data; }
0068 
0069   std::optional<std::size_t> size() const override {
0070     using ::details::size;
0071     return size( getData() );
0072   }
0073 
0074   Ptr payload() const override { return &m_data; }
0075 };
0076 
0077 template <typename ViewType, typename OwnedType>
0078 class GAUDI_API AnyDataWithViewWrapper : public AnyDataWrapper<ViewType> {
0079   OwnedType m_owned;
0080 
0081 public:
0082   AnyDataWithViewWrapper( OwnedType&& data ) : AnyDataWrapper<ViewType>{ {} }, m_owned{ std::move( data ) } {
0083     AnyDataWrapper<ViewType>::m_data = ViewType{ std::as_const( m_owned ) };
0084   }
0085   AnyDataWithViewWrapper( AnyDataWithViewWrapper&& )                 = delete;
0086   AnyDataWithViewWrapper( AnyDataWithViewWrapper const& )            = delete;
0087   AnyDataWithViewWrapper& operator=( AnyDataWithViewWrapper&& )      = delete;
0088   AnyDataWithViewWrapper& operator=( AnyDataWithViewWrapper const& ) = delete;
0089 };