KTL
Loading...
Searching...
No Matches
debug.h
Go to the documentation of this file.
1#pragma once
2
3#include "../utility/assert.h"
4#include "../utility/empty_base.h"
5#include "../utility/meta.h"
6#include "../utility/source_location.h"
7#include "debug_fwd.h"
8#include "type_allocator.h"
9
10#include <cstring>
11#include <memory>
12#include <ostream>
13#include <type_traits>
14
15namespace ktl
16{
17 template<typename Alloc, typename Container>
18 class debug
19 {
20 private:
21 static_assert(detail::has_no_value_type_v<Alloc>, "Building on top of typed allocators is not allowed. Use allocators without a type");
22
23 public:
25
26 public:
31 explicit debug(Container& container)
32 noexcept(std::is_nothrow_default_constructible_v<Alloc>) :
33 m_Container(container),
34 m_Alloc() {}
35
39 template<typename... Args,
40 typename = std::enable_if_t<
41 std::is_constructible_v<Alloc, Args...>>>
42 explicit debug(Container& container, Args&&... args)
43 noexcept(std::is_nothrow_constructible_v<Alloc, Args...>) :
44 m_Container(container),
45 m_Alloc(std::forward<Args>(args)...) {}
46
47 debug(const debug&) = default;
48
49 debug(debug&&) = default;
50
51 debug& operator=(const debug&) = default;
52
53 debug& operator=(debug&&) = default;
54
55 bool operator==(const debug& rhs) const
57 {
58 return m_Alloc == rhs.m_Alloc;
59 }
60
61 bool operator!=(const debug& rhs) const
63 {
64 return m_Alloc != rhs.m_Alloc;
65 }
66
67#pragma region Allocation
73 void* allocate(size_type n, const source_location& source = KTL_SOURCE())
74 noexcept(detail::has_nothrow_allocate_v<Alloc>)
75 {
76#ifdef KTL_SOURCE_LOCATION
77 m_Container.push_back({ source.file_name(), source.line(), n });
78#endif
79
80 return detail::allocate(m_Alloc, n, source);
81 }
82
88 void deallocate(void* p, size_type n)
90 {
91 m_Alloc.deallocate(p, n);
92 }
93#pragma endregion
94
95#pragma region Construction
103 template<typename T, typename... Args>
104 typename std::enable_if<detail::has_construct_v<Alloc, T*, Args...>, void>::type
105 construct(T* p, Args&&... args)
106 noexcept(detail::has_nothrow_construct_v<Alloc, T*, Args...>)
107 {
108 m_Alloc.construct(p, std::forward<Args>(args)...);
109 }
110
116 template<typename T>
117 typename std::enable_if<detail::has_destroy_v<Alloc, T*>, void>::type
120 {
121 m_Alloc.destroy(p);
122 }
123#pragma endregion
124
125#pragma region Utility
131 template<typename A = Alloc>
132 typename std::enable_if<detail::has_max_size_v<A>, size_type>::type
133 max_size() const
134 noexcept(detail::has_nothrow_max_size_v<A>)
135 {
136 return m_Alloc.max_size();
137 }
138
145 template<typename A = Alloc>
146 typename std::enable_if<detail::has_owns_v<A>, bool>::type
147 owns(void* p) const
149 {
150 return m_Alloc.owns(p);
151 }
152#pragma endregion
153
158 Alloc& get_allocator() noexcept
159 {
160 return m_Alloc;
161 }
162
167 const Alloc& get_allocator() const noexcept
168 {
169 return m_Alloc;
170 }
171
176 Container& get_container() noexcept
177 {
178 return m_Container;
179 }
180
185 const Container& get_container() const noexcept
186 {
187 return m_Container;
188 }
189
190 private:
191 Container& m_Container;
192 KTL_EMPTY_BASE Alloc m_Alloc;
193 };
194}
Definition debug.h:19
Container & get_container() noexcept
Return a reference to the container that will be used to accumulate statistics.
Definition debug.h:176
bool operator==(const debug &rhs) const noexcept(detail::has_nothrow_equal_v< Alloc >)
Definition debug.h:55
const Alloc & get_allocator() const noexcept
Returns a const reference to the underlying allocator.
Definition debug.h:167
debug(debug &&)=default
debug(Container &container) noexcept(std::is_nothrow_default_constructible_v< Alloc >)
Construct the allocator with a reference to a container object.
Definition debug.h:31
debug & operator=(const debug &)=default
void deallocate(void *p, size_type n) noexcept(detail::has_nothrow_deallocate_v< Alloc >)
Attempts to deallocate the memory at location p.
Definition debug.h:88
debug(const debug &)=default
detail::get_size_type_t< Alloc > size_type
Definition debug.h:24
Alloc & get_allocator() noexcept
Returns a reference to the underlying allocator.
Definition debug.h:158
std::enable_if< detail::has_destroy_v< Alloc, T * >, void >::type destroy(T *p) noexcept(detail::has_nothrow_destroy_v< Alloc, T * >)
Destructs an object of T at the given location.
Definition debug.h:118
std::enable_if< detail::has_owns_v< A >, bool >::type owns(void *p) const noexcept(detail::has_nothrow_owns_v< A >)
Returns whether or not the allocator owns the given location in memory.
Definition debug.h:147
bool operator!=(const debug &rhs) const noexcept(detail::has_nothrow_not_equal_v< Alloc >)
Definition debug.h:61
std::enable_if< detail::has_max_size_v< A >, size_type >::type max_size() const noexcept(detail::has_nothrow_max_size_v< A >)
Returns the maximum size that an allocation can be.
Definition debug.h:133
void * allocate(size_type n, const source_location &source=KTL_SOURCE()) noexcept(detail::has_nothrow_allocate_v< Alloc >)
Attempts to allocate a chunk of memory defined by n.
Definition debug.h:73
debug(Container &container, Args &&... args) noexcept(std::is_nothrow_constructible_v< Alloc, Args... >)
Constructor for forwarding any arguments to the underlying allocator.
Definition debug.h:42
std::enable_if< detail::has_construct_v< Alloc, T *, Args... >, void >::type construct(T *p, Args &&... args) noexcept(detail::has_nothrow_construct_v< Alloc, T *, Args... >)
Constructs an object of T with the given ...args at the given location.
Definition debug.h:105
const Container & get_container() const noexcept
Return a const reference to the container that will be used to accumulate statistics.
Definition debug.h:185
debug & operator=(debug &&)=default
#define KTL_EMPTY_BASE
Definition empty_base.h:6
typename get_size_type< Alloc, void >::type get_size_type_t
Definition meta.h:33
constexpr bool has_nothrow_construct_v
Definition meta.h:123
constexpr bool has_construct_v
Definition meta.h:53
void * allocate(Alloc &alloc, size_t n, const source_location source) noexcept(false)
Definition meta.h:161
constexpr bool has_no_value_type_v
Definition meta.h:17
Definition cascading.h:16
#define KTL_SOURCE()
Definition source_location.h:11
Definition source_location.h:19