The Gaudi Framework  master (37c0b60a)
AnyDataWrapper.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #pragma once
12 #include <GaudiKernel/DataObject.h>
13 #include <cstddef>
14 #include <iterator>
15 #include <optional>
16 #include <typeindex>
17 #include <utility>
18 
19 namespace details {
20  using std::size;
21 
22  template <typename T, typename... Args>
23  constexpr auto size( const T&, Args&&... ) noexcept {
24  static_assert( sizeof...( Args ) == 0, "No extra args please" );
25  return std::nullopt;
26  }
27 } // namespace details
28 
29 // ugly hack to circumvent the usage of std::any
31  virtual std::optional<std::size_t> size() const = 0;
32 
33  class Ptr {
34  void const* m_ptr = nullptr;
35  std::type_index m_type = typeid( void );
36 
37  public:
38  template <typename T>
39  Ptr( T const* t ) : m_ptr{ t }, m_type{ typeid( T ) } {}
40 
41  operator void const*() const { return m_ptr; }
42  std::type_index type() const { return m_type; }
43 
44  template <typename T>
45  T const* get() const {
46  if ( std::is_void_v<T> || m_type == std::type_index( typeid( T ) ) ) return static_cast<T const*>( m_ptr );
47  struct bad_AnyDataWrapper_Ptr_cast : std::bad_cast {};
48  throw bad_AnyDataWrapper_Ptr_cast{};
49  }
50  };
51  virtual Ptr payload() const = 0;
52 };
53 
54 template <typename T>
56 protected:
57  T m_data;
58 
59 public:
60  AnyDataWrapper( T&& data ) : m_data{ std::move( data ) } {}
62  AnyDataWrapper( AnyDataWrapper const& ) = delete;
65 
66  const T& getData() const { return m_data; }
67  T& getData() { return m_data; }
68 
69  std::optional<std::size_t> size() const override {
71  return size( getData() );
72  }
73 
74  Ptr payload() const override { return &m_data; }
75 };
76 
77 template <typename ViewType, typename OwnedType>
79  OwnedType m_owned;
80 
81 public:
82  AnyDataWithViewWrapper( OwnedType&& data ) : AnyDataWrapper<ViewType>{ {} }, m_owned{ std::move( data ) } {
83  AnyDataWrapper<ViewType>::m_data = ViewType{ std::as_const( m_owned ) };
84  }
89 };
AnyDataWrapperBase::Ptr::Ptr
Ptr(T const *t)
Definition: AnyDataWrapper.h:39
AnyDataWithViewWrapper::m_owned
OwnedType m_owned
Definition: AnyDataWrapper.h:79
details::size
constexpr auto size(const T &, Args &&...) noexcept
Definition: AnyDataWrapper.h:23
std::move
T move(T... args)
std::type_index
AnyDataWrapper::operator=
AnyDataWrapper & operator=(AnyDataWrapper &&)=delete
AnyDataWrapper::getData
const T & getData() const
Definition: AnyDataWrapper.h:66
AnyDataWithViewWrapper::AnyDataWithViewWrapper
AnyDataWithViewWrapper(AnyDataWithViewWrapper &&)=delete
AnyDataWrapperBase
Definition: AnyDataWrapper.h:30
AnyDataWrapperBase::Ptr
Definition: AnyDataWrapper.h:33
std::bad_cast
STL class.
AnyDataWithViewWrapper::operator=
AnyDataWithViewWrapper & operator=(AnyDataWithViewWrapper &&)=delete
bug_34121.t
t
Definition: bug_34121.py:31
AnyDataWrapper::AnyDataWrapper
AnyDataWrapper(AnyDataWrapper &&)=delete
AnyDataWrapper::m_data
T m_data
Definition: AnyDataWrapper.h:57
details
Definition: AnyDataWrapper.h:19
AnyDataWrapperBase::Ptr::get
T const * get() const
Definition: AnyDataWrapper.h:45
AnyDataWrapper::size
std::optional< std::size_t > size() const override
Definition: AnyDataWrapper.h:69
AnyDataWrapper
Definition: AnyDataWrapper.h:55
AnyDataWrapper::operator=
AnyDataWrapper & operator=(AnyDataWrapper const &)=delete
AnyDataWrapperBase::payload
virtual Ptr payload() const =0
AnyDataWithViewWrapper
Definition: AnyDataWrapper.h:78
AnyDataWrapper::AnyDataWrapper
AnyDataWrapper(AnyDataWrapper const &)=delete
AnyDataWrapperBase::Ptr::type
std::type_index type() const
Definition: AnyDataWrapper.h:42
DataObject.h
AnyDataWithViewWrapper::AnyDataWithViewWrapper
AnyDataWithViewWrapper(AnyDataWithViewWrapper const &)=delete
AnyDataWrapper::getData
T & getData()
Definition: AnyDataWrapper.h:67
DataObject
Definition: DataObject.h:36
AnyDataWrapper::payload
Ptr payload() const override
Definition: AnyDataWrapper.h:74
AnyDataWithViewWrapper::operator=
AnyDataWithViewWrapper & operator=(AnyDataWithViewWrapper const &)=delete
AnyDataWithViewWrapper::AnyDataWithViewWrapper
AnyDataWithViewWrapper(OwnedType &&data)
Definition: AnyDataWrapper.h:82
AnyDataWrapper::AnyDataWrapper
AnyDataWrapper(T &&data)
Definition: AnyDataWrapper.h:60
AnyDataWrapperBase::size
virtual std::optional< std::size_t > size() const =0
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81