Loading...
Searching...
No Matches
defaultInitAllocator.h
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
15PXR_NAMESPACE_OPEN_SCOPE
16
26template <typename T, typename AllocatorBase = std::allocator<T>>
27class Vdf_DefaultInitAllocator : public AllocatorBase
28{
29public:
30 template <typename U>
31 struct rebind
32 {
33 using other = Vdf_DefaultInitAllocator<
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
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
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
64PXR_NAMESPACE_CLOSE_SCOPE
65
66#endif
Vdf_DefaultInitAllocator.
void construct(U *ptr) noexcept(std::is_nothrow_default_constructible_v< U >)
Value initializing construction.
void construct(U *ptr, Args &&... args)
Construction with custom constructor arguments.