TrioCFD 1.9.8
TrioCFD documentation
Loading...
Searching...
No Matches
TVAlloc.h
1/****************************************************************************
2* Copyright (c) 2024, CEA
3* All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9*
10* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
11* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
12* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13*
14*****************************************************************************/
15
16#ifndef TVAlloc_included
17#define TVAlloc_included
18
19/*! Allocator adaptor that intercepts the 'construct' calls to convert value initialization into default initialization
20 * Written by Casey Carter (@codercasey)
21 * Taken from https://hackingcpp.com/cpp/recipe/uninitialized_numeric_array.html
22 *
23 * This allows "std::vector<T>(30)" to only allocate, not initialize, the data.
24 */
25template< typename T, typename Alloc = std::allocator<T> >
26class TVAlloc : public Alloc
27{
28 using a_t = std::allocator_traits<Alloc>;
29public:
30 // Obtain alloc<U> where U != T
31 template<typename U>
36
37 // Make inherited ctors visible
38 using Alloc::Alloc;
39
40 // Ddefault-construct objects - WITHOUT initialisation!
41 template<typename U>
42 void construct (U* ptr) noexcept( std::is_nothrow_default_constructible< U>::value)
43 {
44 ::new(static_cast<void*>(ptr)) U; // 'placement new':
45 }
46
47 // Construct with ctor arguments
48 template<typename U, typename... Args>
49 void construct (U* ptr, Args&& ... args)
50 {
51 a_t::construct(
52 static_cast<Alloc&>(*this),
53 ptr, std::forward<Args>(args)...);
54 }
55
56};
57
58#endif /* TVAlloc_included */
void construct(U *ptr, Args &&... args)
Definition TVAlloc.h:49
void construct(U *ptr) noexcept(std::is_nothrow_default_constructible< U >::value)
Definition TVAlloc.h:42
TVAlloc< U, typename a_t::template rebind_alloc< U > > other
Definition TVAlloc.h:34