The Gaudi Framework  master (ff829712)
Loading...
Searching...
No Matches
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
13#include <cstddef>
14#include <iterator>
15#include <optional>
16#include <typeindex>
17#include <utility>
18
19namespace 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
54template <typename T>
56protected:
58
59public:
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 {
70 using ::details::size;
71 return size( getData() );
72 }
73
74 Ptr payload() const override { return &m_data; }
75};
76
77template <typename ViewType, typename OwnedType>
79 OwnedType m_owned;
80
81public:
82 AnyDataWithViewWrapper( OwnedType&& data ) : AnyDataWrapper<ViewType>{ {} }, m_owned{ std::move( data ) } {
83 AnyDataWrapper<ViewType>::m_data = ViewType{ std::as_const( m_owned ) };
84 }
89};
#define GAUDI_API
Definition Kernel.h:49
AnyDataWithViewWrapper & operator=(AnyDataWithViewWrapper const &)=delete
AnyDataWithViewWrapper(AnyDataWithViewWrapper const &)=delete
AnyDataWithViewWrapper & operator=(AnyDataWithViewWrapper &&)=delete
AnyDataWithViewWrapper(OwnedType &&data)
AnyDataWithViewWrapper(AnyDataWithViewWrapper &&)=delete
std::type_index type() const
T const * get() const
AnyDataWrapper(T &&data)
AnyDataWrapper(AnyDataWrapper const &)=delete
AnyDataWrapper & operator=(AnyDataWrapper &&)=delete
Ptr payload() const override
const T & getData() const
std::optional< std::size_t > size() const override
AnyDataWrapper & operator=(AnyDataWrapper const &)=delete
AnyDataWrapper(AnyDataWrapper &&)=delete
DataObject()
Standard Constructor.
constexpr auto size(const T &, Args &&...) noexcept
STL namespace.
virtual std::optional< std::size_t > size() const =0
virtual Ptr payload() const =0