tlx
allocator_base.hpp
Go to the documentation of this file.
1/*******************************************************************************
2 * tlx/allocator_base.hpp
3 *
4 * Part of tlx - http://panthema.net/tlx
5 *
6 * Copyright (C) 2015 Timo Bingmann <tb@panthema.net>
7 *
8 * All rights reserved. Published under the Boost Software License, Version 1.0
9 ******************************************************************************/
10
11#ifndef TLX_ALLOCATOR_BASE_HEADER
12#define TLX_ALLOCATOR_BASE_HEADER
13
14#include <cstddef>
15#include <memory>
16#include <type_traits>
17
18namespace tlx {
19
20template <typename Type>
22{
23 static constexpr bool debug = true;
24
25public:
26 using value_type = Type;
27 using pointer = Type *;
28 using const_pointer = const Type *;
29 using reference = Type&;
30 using const_reference = const Type&;
31 using size_type = std::size_t;
32 using difference_type = std::ptrdiff_t;
33
34 //! C++11 type flag
35 using is_always_equal = std::true_type;
36 //! C++11 type flag
38
39 //! Returns the address of x.
40 pointer address(reference x) const noexcept {
41 return std::addressof(x);
42 }
43
44 //! Returns the address of x.
46 return std::addressof(x);
47 }
48
49 //! Maximum size possible to allocate
50 size_type max_size() const noexcept {
51 return size_t(-1) / sizeof(Type);
52 }
53
54 //! Constructs an element object on the location pointed by p.
56 ::new (static_cast<void*>(p))Type(value); // NOLINT
57 }
58
59#if defined(_MSC_VER)
60// disable false-positive warning C4100: 'p': unreferenced formal parameter
61#pragma warning(push)
62#pragma warning(disable:4100)
63#endif
64 //! Destroys in-place the object pointed by p.
65 void destroy(pointer p) const noexcept {
66 p->~Type();
67 }
68#if defined(_MSC_VER)
69#pragma warning(push)
70#endif
71
72 //! Constructs an element object on the location pointed by p.
73 template <typename SubType, typename... Args>
74 void construct(SubType* p, Args&& ... args) {
75 ::new (static_cast<void*>(p))SubType(std::forward<Args>(args) ...); // NOLINT
76 }
77
78 //! Destroys in-place the object pointed by p.
79 template <typename SubType>
80 void destroy(SubType* p) const noexcept {
81 p->~SubType();
82 }
83};
84
85} // namespace tlx
86
87#endif // !TLX_ALLOCATOR_BASE_HEADER
88
89/******************************************************************************/
std::true_type propagate_on_container_move_assignment
C++11 type flag.
void destroy(pointer p) const noexcept
Destroys in-place the object pointed by p.
std::true_type is_always_equal
C++11 type flag.
const Type * const_pointer
static constexpr bool debug
const_pointer address(const_reference x) const noexcept
Returns the address of x.
void destroy(SubType *p) const noexcept
Destroys in-place the object pointed by p.
const Type & const_reference
void construct(SubType *p, Args &&... args)
Constructs an element object on the location pointed by p.
size_type max_size() const noexcept
Maximum size possible to allocate.
pointer address(reference x) const noexcept
Returns the address of x.
void construct(pointer p, const_reference value)
Constructs an element object on the location pointed by p.
std::ptrdiff_t difference_type