GEOSX
StackBuffer.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020, 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 
85  LVARRAY_HOST_DEVICE inline
86  void reallocate( std::ptrdiff_t const size, std::ptrdiff_t const newCapacity )
87  {
89  LVARRAY_ERROR_IF_GT( newCapacity, LENGTH );
90  }
95  LVARRAY_HOST_DEVICE inline constexpr
96  void free()
97  {}
98 
102  LVARRAY_HOST_DEVICE inline constexpr
103  std::ptrdiff_t capacity() const
104  { return LENGTH; }
105 
109  LVARRAY_HOST_DEVICE inline constexpr
110  T * data() const
111  { return const_cast< T * >( m_data ); }
112 
119  template< typename INDEX_TYPE >
120  LVARRAY_HOST_DEVICE inline constexpr
121  T & operator[]( INDEX_TYPE const i ) const
122  { return const_cast< T * >( m_data )[ i ]; }
123 
124 private:
125 
127  T m_data[ LENGTH ];
128 };
129 
130 } // namespace LvArray
#define LVARRAY_UNUSED_VARIABLE(X)
Mark X as an unused variable, used to silence compiler warnings.
Definition: Macros.hpp:51
constexpr StackBuffer(StackBuffer const &src, std::ptrdiff_t)
Sized copy constructor, creates a deep copy.
Definition: StackBuffer.hpp:64
constexpr std::ptrdiff_t capacity() const
This class implements the default behavior for the Buffer methods related to execution space...
constexpr T * data() const
#define LVARRAY_ERROR_IF_GT(lhs, rhs)
Raise a hard error if one value compares greater than the other.
Definition: Macros.hpp:252
constexpr T & operator[](INDEX_TYPE const i) const
Contains functions for manipulating buffers.
static constexpr bool hasShallowCopy
Signifies that the StackBuffer&#39;s copy semantics are deep.
Definition: StackBuffer.hpp:49
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
constexpr StackBuffer(bool=true)
Constructor for creating an empty/uninitialized buffer.
Definition: StackBuffer.hpp:56
constexpr void free()
Free the data in the buffer but does not destroy any values.
Definition: StackBuffer.hpp:96
Contains a bunch of macro definitions.
void reallocate(std::ptrdiff_t const size, std::ptrdiff_t const newCapacity)
Notionally this method reallocates the buffer, but since the StackBuffer is sized at compile time all...
Definition: StackBuffer.hpp:86
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:389