HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_SmallArray.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_SmallArray.h (UT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #pragma once
12 
13 #ifndef __UT_SMALLARRAY_H_INCLUDED__
14 #define __UT_SMALLARRAY_H_INCLUDED__
15 
16 #include "UT_API.h"
17 #include "UT_Array.h"
18 #include "UT_Assert.h"
19 #include <SYS/SYS_Compiler.h>
20 #include <SYS/SYS_StaticAssert.h>
21 #include <utility>
22 #include <stddef.h>
23 
24 /// An array class with the small buffer optimization, making it ideal for
25 /// cases when you know it will only contain a few elements at the expense of
26 /// increasing the object size by MAX_BYTES (subject to alignment).
27 template <typename T, size_t MAX_BYTES = 64>
29 {
30  // As many elements that fit into MAX_BYTES with 1 item minimum
31  enum { MAX_ELEMS = MAX_BYTES/sizeof(T) < 1 ? 1 : MAX_BYTES/sizeof(T) };
32 
33 public:
34 
35 // gcc falsely warns about our use of offsetof() on non-POD types. We can't
36 // easily suppress this because it has to be done in the caller at
37 // instantiation time. Instead, punt to a runtime check instead.
38 #if !SYS_IS_GCC_GE(4, 8) || defined(__clang__) || defined(_MSC_VER)
39  #define UT_SMALL_ARRAY_SIZE_ASSERT() \
40  using ThisT = UT_SmallArray<T,MAX_BYTES>; \
41  SYS_STATIC_ASSERT(offsetof(ThisT, myBuffer) == sizeof(UT_Array<T>))
42 #else
43  #define UT_SMALL_ARRAY_SIZE_ASSERT() \
44  UT_ASSERT_P(!UT_Array<T>::isHeapBuffer());
45 #endif
46 
47  /// Default construction
49  : UT_Array<T>(/*capacity*/0)
50  {
51  UT_Array<T>::unsafeShareData((T*)myBuffer, 0, MAX_ELEMS);
53  }
54 
55  /// Copy constructor
56  /// @{
58  : UT_Array<T>(/*capacity*/0)
59  {
60  UT_Array<T>::unsafeShareData((T*)myBuffer, 0, MAX_ELEMS);
63  }
65  : UT_Array<T>(/*capacity*/0)
66  {
67  UT_Array<T>::unsafeShareData((T*)myBuffer, 0, MAX_ELEMS);
70  }
71  /// @}
72 
73  /// Move constructor
74  /// @{
76  {
77  UT_Array<T>::unsafeShareData((T*)myBuffer, 0, MAX_ELEMS);
79  UT_Array<T>::operator=(std::move(movable));
80  }
82  {
83  UT_Array<T>::unsafeShareData((T*)myBuffer, 0, MAX_ELEMS);
85  UT_Array<T>::operator=(std::move(movable));
86  }
87  /// @}
88 #undef UT_SMALL_ARRAY_SIZE_ASSERT
89 
90  /// Assignment operator
93  {
95  return *this;
96  }
97 
98  /// Move operator
101  {
102  UT_Array<T>::operator=(std::move(movable));
103  return *this;
104  }
105 
106 private:
107  alignas(T) char myBuffer[MAX_ELEMS*sizeof(T)];
108 };
109 
110 #endif // __UT_SMALLARRAY_H_INCLUDED__
#define UT_SMALL_ARRAY_SIZE_ASSERT()
Definition: UT_SmallArray.h:39
UT_Array< T > & operator=(const UT_Array< T > &a)
Definition: UT_ArrayImpl.h:824
UT_SmallArray< T, MAX_BYTES > & operator=(UT_SmallArray< T, MAX_BYTES > &&movable)
Move operator.
UT_SmallArray(UT_Array< T > &&movable) SYS_NOEXCEPT
Definition: UT_SmallArray.h:75
UT_SmallArray(const UT_Array< T > &copy)
Definition: UT_SmallArray.h:57
void unsafeShareData(UT_Array< T > &src)
Definition: UT_Array.h:839
UT_SmallArray< T, MAX_BYTES > & operator=(const UT_SmallArray< T, MAX_BYTES > &copy)
Assignment operator.
Definition: UT_SmallArray.h:92
UT_SmallArray()
Default construction.
Definition: UT_SmallArray.h:48
#define SYS_NOEXCEPT
Definition: SYS_Compiler.h:49
#define UT_API_TMPL
Definition: UT_API.h:13
UT_SmallArray(const UT_SmallArray< T, MAX_BYTES > &copy)
Definition: UT_SmallArray.h:64
UT_SmallArray(UT_SmallArray< T, MAX_BYTES > &&movable) SYS_NOEXCEPT
Definition: UT_SmallArray.h:81