LvArray
StackBuffer.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021, Lawrence Livermore National Security, LLC and LvArray contributors.
3  * All rights reserved.
4  * See the LICENSE file for details.
5  * SPDX-License-Identifier: (BSD-3-Clause)
6  */
7 
14 #pragma once
15 
16 // Source includes
17 #include "LvArrayConfig.hpp"
18 #include "Macros.hpp"
19 #include "bufferManipulation.hpp"
20 
21 // System includes
22 #include <stddef.h>
23 #include <type_traits>
24 
25 
26 namespace LvArray
27 {
28 
39 template< typename T, int LENGTH >
41 {
42 public:
43  static_assert( std::is_trivially_destructible< T >::value, "The StackBuffer can only hold trivially copyable and destructable types." );
44 
46  using value_type = T;
47 
49  constexpr static bool hasShallowCopy = false;
50 
55  LVARRAY_HOST_DEVICE inline constexpr
56  StackBuffer( bool=true )
57  {}
58 
63  LVARRAY_HOST_DEVICE inline constexpr
64  StackBuffer( StackBuffer const & src, std::ptrdiff_t ):
65  StackBuffer( src )
66  {}
67 
73  template< typename _T=T, typename=std::enable_if_t< std::is_const< _T >::value > >
74  LVARRAY_HOST_DEVICE inline constexpr
75  StackBuffer( StackBuffer< std::remove_const_t< T >, LENGTH > const & src ):
76  StackBuffer( reinterpret_cast< StackBuffer const & >( src ) )
77  {}
78 
86  LVARRAY_HOST_DEVICE inline
87  void reallocate( std::ptrdiff_t const size, MemorySpace const space, std::ptrdiff_t const newCapacity )
88  {
90  LVARRAY_UNUSED_VARIABLE( space );
91  LVARRAY_ERROR_IF_GT( newCapacity, LENGTH );
92  }
97  LVARRAY_HOST_DEVICE inline constexpr
98  void free()
99  {}
100 
104  LVARRAY_HOST_DEVICE inline constexpr
105  std::ptrdiff_t capacity() const
106  { return LENGTH; }
107 
111  LVARRAY_HOST_DEVICE inline constexpr
112  T * data() const
113  { return const_cast< T * >( m_data ); }
114 
121  template< typename INDEX_TYPE >
122  LVARRAY_HOST_DEVICE inline constexpr
123  T & operator[]( INDEX_TYPE const i ) const
124  { return const_cast< T * >( m_data )[ i ]; }
125 
126 private:
127 
129  T m_data[ LENGTH ];
130 };
131 
132 } // namespace LvArray
#define LVARRAY_UNUSED_VARIABLE(X)
Mark X as an unused variable, used to silence compiler warnings.
Definition: Macros.hpp:79
LVARRAY_HOST_DEVICE constexpr T * data() const
Definition: StackBuffer.hpp:112
LVARRAY_HOST_DEVICE constexpr T & operator[](INDEX_TYPE const i) const
Definition: StackBuffer.hpp:123
LVARRAY_HOST_DEVICE constexpr std::ptrdiff_t capacity() const
Definition: StackBuffer.hpp:105
This class implements the default behavior for the Buffer methods related to execution space...
Definition: bufferManipulation.hpp:78
T m_data[LENGTH]
The c-array containing the values.
Definition: StackBuffer.hpp:129
#define LVARRAY_ERROR_IF_GT(lhs, rhs)
Raise a hard error if one value compares greater than the other.
Definition: Macros.hpp:392
LVARRAY_HOST_DEVICE constexpr StackBuffer(bool=true)
Constructor for creating an empty/uninitialized buffer.
Definition: StackBuffer.hpp:56
Contains functions for manipulating buffers.
static constexpr bool hasShallowCopy
Signifies that the StackBuffer&#39;s copy semantics are deep.
Definition: StackBuffer.hpp:49
camp::resources::Platform MemorySpace
an alias for camp::resources::Platform.
Definition: bufferManipulation.hpp:31
LVARRAY_HOST_DEVICE constexpr StackBuffer(StackBuffer const &src, std::ptrdiff_t)
Sized copy constructor, creates a deep copy.
Definition: StackBuffer.hpp:64
LVARRAY_HOST_DEVICE constexpr StackBuffer(StackBuffer< std::remove_const_t< T >, LENGTH > const &src)
Create a copy of src with const T.
Definition: StackBuffer.hpp:75
The top level namespace.
Definition: Array.hpp:24
T value_type
Alias used in the bufferManipulation functions.
Definition: StackBuffer.hpp:46
LVARRAY_HOST_DEVICE void reallocate(std::ptrdiff_t const size, MemorySpace const space, std::ptrdiff_t const newCapacity)
Notionally this method reallocates the buffer, but since the StackBuffer is sized at compile time all...
Definition: StackBuffer.hpp:87
Contains a bunch of macro definitions.
LVARRAY_HOST_DEVICE constexpr void free()
Free the data in the buffer but does not destroy any values.
Definition: StackBuffer.hpp:98
This class implements the Buffer interface using a c-array.
Definition: StackBuffer.hpp:40
#define LVARRAY_HOST_DEVICE
Mark a function for both host and device usage.
Definition: Macros.hpp:600