HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
defaultInitAllocator.h
Go to the documentation of this file.
1 //
2 // Copyright 2025 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_EXEC_VDF_DEFAULT_INIT_ALLOCATOR
8 #define PXR_EXEC_VDF_DEFAULT_INIT_ALLOCATOR
9 
10 #include "pxr/pxr.h"
11 
12 #include <memory>
13 #include <type_traits>
14 
16 
17 /// Vdf_DefaultInitAllocator
18 ///
19 /// This allocator intercepts value initialization and turns it into
20 /// default initialization. It's primary purpose is for use on containers,
21 /// such as `std::vector`, that are first resized and then immediately
22 /// filled with elements. Without the use of this allocator, the resize would
23 /// value-initialize every element before immediately overwriting the element
24 /// when it's filled in.
25 ///
26 template <typename T, typename AllocatorBase = std::allocator<T>>
27 class Vdf_DefaultInitAllocator : public AllocatorBase
28 {
29 public:
30  template <typename U>
31  struct rebind
32  {
34  U,
35  typename std::allocator_traits<AllocatorBase>::template
36  rebind_alloc<U>>;
37  };
38 
39  // Make all base class constructors available from this derived class.
40  using AllocatorBase::AllocatorBase;
41 
42  /// Value initializing construction. This method instead default
43  /// initializes instances of \p U.
44  ///
45  template <typename U>
46  void construct(U* ptr) noexcept(
47  std::is_nothrow_default_constructible_v<U>) {
48  // Default initialize U
49  ::new(static_cast<void*>(ptr)) U;
50  }
51 
52  /// Construction with custom constructor arguments. This method simply
53  /// forwards any arguments to the \p U constructor.
54  ///
55  template <typename U, typename...Args>
56  void construct(U* ptr, Args&&... args) {
57  std::allocator_traits<AllocatorBase>::construct(
58  static_cast<AllocatorBase&>(*this),
59  ptr,
60  std::forward<Args>(args)...);
61  }
62 };
63 
65 
66 #endif
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
auto ptr(T p) -> const void *
Definition: format.h:4331
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
**If you just want to fire and args
Definition: thread.h:618
void construct(U *ptr, Args &&...args)
void construct(U *ptr) noexcept(std::is_nothrow_default_constructible_v< U >)