MarlinMT  0.1.0
MemLayout.h
Go to the documentation of this file.
1 #pragma once
2 
3 // -- std header
4 #include <memory>
5 #include <utility>
6 #include <vector>
7 
8 namespace marlinmt {
9  namespace book {
10 
14  class MemLayout {
15  public:
21  template < typename T >
22  [[nodiscard]] std::shared_ptr< const T > at( std::size_t idx ) const {
23  return std::static_pointer_cast< const T >( impAt( idx ) ) ;
24  }
25 
31  template < typename T >
32  std::shared_ptr< T > at( std::size_t idx ) {
33  return std::static_pointer_cast< T >( impAt( idx ) ) ;
34  }
35 
40  template < typename T >
41  std::shared_ptr< const T > merged() {
42  return std::static_pointer_cast< const T >( impMerged() ) ;
43  }
44 
45  MemLayout() = default ;
46  MemLayout( const MemLayout & ) = default ;
47  MemLayout &operator=( const MemLayout & ) = default ;
48  MemLayout( MemLayout && ) = default ;
49  MemLayout &operator=( MemLayout && ) = default ;
50  virtual ~MemLayout() = default ;
51 
52  protected:
54  [[nodiscard]] virtual std::shared_ptr< void >
55  impAt( std::size_t idx ) const = 0 ;
57  [[nodiscard]] virtual std::shared_ptr< void > impMerged() = 0 ;
58  } ;
59 
65  template < typename T,
66  void ( *MERGE )( const std::shared_ptr< T > & /* dst */,
67  const std::shared_ptr< T > & /* src */
68  ),
69  typename... Args_t >
70  class SharedMemLayout : public MemLayout {
71 
72  public:
78  explicit SharedMemLayout( std::size_t num_instances, Args_t... args )
79  : _objects{num_instances, nullptr},
80  _ctor_p{
81  std::make_unique< typename decltype( _ctor_p )::element_type >(
82  args... )} {
83  for ( std::shared_ptr< T > &pObj : _objects ) {
84  pObj = std::make_shared< T >( args... ) ;
85  }
86  }
87 
88  SharedMemLayout( const SharedMemLayout & ) = default ;
89  SharedMemLayout &operator=( const SharedMemLayout & ) = default ;
90  SharedMemLayout( SharedMemLayout && ) noexcept = default ;
91  SharedMemLayout &operator=( SharedMemLayout && ) noexcept = default ;
92  ~SharedMemLayout() override final = default ;
93 
94  private:
96  [[nodiscard]] std::shared_ptr< void >
97  impAt( std::size_t idx ) const override final {
98  return _objects[idx] ;
99  }
100 
101  [[nodiscard]] std::shared_ptr< void > impMerged() override final {
102  _mergedObj
103  = std::make_shared< T >( std::make_from_tuple< T >( *_ctor_p ) ) ;
104  for ( std::shared_ptr< T > &pObj : _objects ) {
105  if ( pObj ) {
106  ( *MERGE )( _mergedObj, pObj ) ;
107  }
108  }
109  return _mergedObj ;
110  }
111 
112  std::vector< std::shared_ptr< T > >
114  std::shared_ptr< T > _mergedObj{nullptr} ;
115  std::unique_ptr<
116  std::tuple< const typename std::remove_reference< Args_t >::type... > >
118  } ;
119 
124  template < typename T, typename... Args_t >
125  class SingleMemLayout : public MemLayout {
126  public:
127  explicit SingleMemLayout( Args_t... args )
128  : _object{std::make_shared< T >( args... )} {}
129 
130  SingleMemLayout( const SingleMemLayout & ) = default ;
131  SingleMemLayout &operator=( const SingleMemLayout & ) = default ;
132  SingleMemLayout( SingleMemLayout && ) noexcept = default ;
133  SingleMemLayout &operator=( SingleMemLayout && ) noexcept = default ;
134  ~SingleMemLayout() override final = default ;
135 
136  private:
137  [[nodiscard]] std::shared_ptr< void >
138  impAt( std::size_t /*idx*/ ) const override final {
139  return _object ;
140  }
141 
143  [[nodiscard]] std::shared_ptr< void > impMerged() override final {
144  return _object ;
145  }
146 
147  std::shared_ptr< T > _object{nullptr} ;
148  } ;
149 
150  } // end namespace book
151 } // end namespace marlinmt
virtual std::shared_ptr< void > impAt(std::size_t idx) const =0
implementation from at
MemLayout base class to store booked objects in MarlinMT.
Definition: MemLayout.h:14
MemLayout for mutable object instances.
Definition: MemLayout.h:70
virtual ~MemLayout()=default
std::shared_ptr< const T > merged()
Get Completed Resource.
Definition: MemLayout.h:41
MemLayout & operator=(const MemLayout &)=default
std::vector< std::shared_ptr< T > > _objects
Definition: MemLayout.h:113
std::shared_ptr< void > impAt(std::size_t) const override final
implementation from at
Definition: MemLayout.h:138
std::unique_ptr< std::tuple< const typename std::remove_reference< Args_t >::type... > > _ctor_p
Definition: MemLayout.h:117
std::shared_ptr< void > impAt(std::size_t idx) const override final
Get Resource for Instance. Lazy operation.
Definition: MemLayout.h:97
std::shared_ptr< const T > at(std::size_t idx) const
Get Resource for an instance.
Definition: MemLayout.h:22
std::shared_ptr< void > impMerged() override final
Definition: MemLayout.h:143
std::shared_ptr< void > impMerged() override final
implementation from merged
Definition: MemLayout.h:101
SingleMemLayout(Args_t... args)
Definition: MemLayout.h:127
virtual std::shared_ptr< void > impMerged()=0
implementation from merged
MemLayout for Single object instance.
Definition: MemLayout.h:125
std::shared_ptr< T > at(std::size_t idx)
Get Resource for an instance.
Definition: MemLayout.h:32
SharedMemLayout(std::size_t num_instances, Args_t... args)
Constructor.
Definition: MemLayout.h:78