-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathStackBuffer.hpp
More file actions
132 lines (112 loc) · 3.9 KB
/
Copy pathStackBuffer.hpp
File metadata and controls
132 lines (112 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Copyright (c) 2021, Lawrence Livermore National Security, LLC and LvArray contributors.
* All rights reserved.
* See the LICENSE file for details.
* SPDX-License-Identifier: (BSD-3-Clause)
*/
/**
* @file StackBuffer.hpp
* @brief Contains the implementation of LvArray:StackBuffer.
*/
#pragma once
// Source includes
#include "LvArrayConfig.hpp"
#include "Macros.hpp"
#include "bufferManipulation.hpp"
// System includes
#include <stddef.h>
#include <type_traits>
namespace LvArray
{
/**
* @class StackBuffer
* @brief This class implements the Buffer interface using a c-array.
* @tparam T type of data that is contained in the buffer. T must be both
* trivially copyable and trivially destructable.
* @tparam LENGTH the length of the buffer.
* @note Unlike the standard Buffer classes the StackBuffer does not permit
* making shallow copies.
* @note The parent class provides the default execution space related methods.
*/
template< typename T, int LENGTH >
class StackBuffer : public bufferManipulation::VoidBuffer
{
public:
static_assert( std::is_trivially_destructible< T >::value, "The StackBuffer can only hold trivially copyable and destructable types." );
/// Alias used in the bufferManipulation functions.
using value_type = T;
/// Signifies that the StackBuffer's copy semantics are deep.
constexpr static bool hasShallowCopy = false;
/**
* @brief Constructor for creating an empty/uninitialized buffer.
* @note For the StackBuffer an uninitialized buffer is equivalent to an empty buffer.
*/
LVARRAY_HOST_DEVICE inline constexpr
StackBuffer( bool=true )
{}
/**
* @brief Sized copy constructor, creates a deep copy.
* @param src The buffer to be coppied.
*/
LVARRAY_HOST_DEVICE inline constexpr
StackBuffer( StackBuffer const & src, std::ptrdiff_t ):
StackBuffer( src )
{}
/**
* @brief Create a copy of @p src with const T.
* @tparam _T A dummy parameter to allow enable_if, do not specify.
* @param src The buffer to copy.
*/
template< typename _T=T, typename=std::enable_if_t< std::is_const< _T >::value > >
LVARRAY_HOST_DEVICE inline constexpr
StackBuffer( StackBuffer< std::remove_const_t< T >, LENGTH > const & src ):
StackBuffer( reinterpret_cast< StackBuffer const & >( src ) )
{}
/**
* @brief Notionally this method reallocates the buffer, but since the StackBuffer is sized at
* compile time all this does is check that newCapacity doesn't exceed LENGTH.
* @param size The current size of the buffer, not used.
* @param space The space to perform the reallocation in, not used.
* @param newCapacity the new capacity of the buffer.
*/
LVARRAY_HOST_DEVICE inline
void reallocate( std::ptrdiff_t const size, MemorySpace const space, std::ptrdiff_t const newCapacity )
{
LVARRAY_UNUSED_VARIABLE( size );
LVARRAY_UNUSED_VARIABLE( space );
LVARRAY_ERROR_IF_GT( newCapacity, LENGTH );
}
/**
* @brief Free the data in the buffer but does not destroy any values.
* @note For this class this is a no-op since T must be trivially destructable.
*/
LVARRAY_HOST_DEVICE inline constexpr
void free()
{}
/**
* @return Return the capacity of the buffer.
*/
LVARRAY_HOST_DEVICE inline constexpr
std::ptrdiff_t capacity() const
{ return LENGTH; }
/**
* @return Return a pointer to the beginning of the buffer.
*/
LVARRAY_HOST_DEVICE inline constexpr
T * data() const
{ return const_cast< T * >( m_data ); }
/**
* @tparam INDEX_TYPE the type used to index into the values.
* @return The value at position @p i .
* @param i The position of the value to access.
* @note No bounds checks are performed.
*/
template< typename INDEX_TYPE >
LVARRAY_HOST_DEVICE inline constexpr
T & operator[]( INDEX_TYPE const i ) const
{ return const_cast< T * >( m_data )[ i ]; }
private:
/// The c-array containing the values.
T m_data[ LENGTH ];
};
} // namespace LvArray