tlx
Loading...
Searching...
No Matches
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#if __cplusplus >= 201103L
55 //! Constructs an element object on the location pointed by p.
56 template <typename SubType, typename... Args>
57 void construct(SubType* p, Args&& ... args)
58 noexcept(std::is_nothrow_constructible<SubType, Args...>::value) {
59 ::new (static_cast<void*>(p))SubType(std::forward<Args>(args) ...); // NOLINT
60 }
61
62 //! Destroys in-place the object pointed by p.
63 template <typename SubType>
64 void destroy(SubType* p) const noexcept(std::is_nothrow_destructible<SubType>::value) {
65 p->~SubType();
66 }
67#else
68 //! Constructs an element object on the location pointed by p.
70 ::new (static_cast<void*>(p))Type(value); // NOLINT
71 }
72
73#if defined(_MSC_VER)
74// disable false-positive warning C4100: 'p': unreferenced formal parameter
75#pragma warning(push)
76#pragma warning(disable:4100)
77#endif
78 //! Destroys in-place the object pointed by p.
79 void destroy(pointer p) const noexcept {
80 p->~Type();
81 }
82#if defined(_MSC_VER)
83#pragma warning(push)
84#endif
85#endif
86};
87
88} // namespace tlx
89
90#endif // !TLX_ALLOCATOR_BASE_HEADER
91
92/******************************************************************************/
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.
static constexpr bool debug
const_pointer address(const_reference x) const noexcept
Returns the address of x.
const Type & const_reference
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
const Type * const_pointer