Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions source/tinystl/memory/allocate_at_least.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include "tinystl/memory/allocator_traits.h"

namespace tinystl {

template <class Pointer>
struct allocation_result {
Pointer ptr;
size_t count;
};

template <class Alloc>
constexpr allocation_result<typename allocator_traits<Alloc>::pointer>
allocate_at_least(Alloc &alloc, size_t n) {
return {alloc.allocate(n), n};
}

} // namespace tinystl
31 changes: 31 additions & 0 deletions source/tinystl/memory/temp_value.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include "tinystl/memory/addressof.h"
#include "tinystl/memory/allocator_traits.h"

namespace tinystl {
template <class T, class Alloc>
class temp_value {
public:
template <class... Args>
temp_value(Alloc &alloc, Args &&...args) : m_alloc(alloc) {
tinystl::allocator_traits<Alloc>::construct(
m_alloc, addr(), std::forward<Args>(args)...
);
}

~temp_value() { tinystl::allocator_traits<Alloc>::destroy(m_alloc, addr()); }

T &get() noexcept { return *addr(); }

private:
T *addr() { return addressof(m_value); }

private:
union {
T m_value;
};

Alloc m_alloc;
};
} // namespace tinystl
20 changes: 20 additions & 0 deletions source/tinystl/memory/to_address.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include "tinystl/memory/pointer_traits.h"
#include "tinystl/type_traits/is_function.h"

namespace tinystl {
template <class T>
constexpr T *to_address(T *p) noexcept {
static_assert(!is_function_v<T>);
return p;
}

template <class T>
constexpr auto to_address(const T &p) noexcept {
if constexpr (requires { pointer_traits<T>::to_address(p); })
return pointer_traits<T>::to_address(p);
else
return to_address(p.operator->());
}
} // namespace tinystl
15 changes: 15 additions & 0 deletions source/tinystl/type_traits/is_const.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include "tinystl/type_traits/integral_constant.h"

namespace tinystl {

template <class T>
struct is_const : false_type {};

template <class T>
struct is_const<const T> : true_type {};

template <class T>
inline constexpr bool is_const_v = is_const<T>::value;
} // namespace tinystl
19 changes: 19 additions & 0 deletions source/tinystl/type_traits/is_function.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include "tinystl/type_traits/integral_constant.h"
#include "tinystl/type_traits/is_const.h"
#include "tinystl/type_traits/is_reference.h"

namespace tinystl {

// primary template
// template <class T>
// struct is_function : bool_constant<__is_function(T)> {};

template <class T>
struct is_function
: bool_constant<!is_const_v<const T> && !is_reference_v<T>> {};

template <class T>
inline constexpr bool is_function_v = is_function<T>::value;
} // namespace tinystl
76 changes: 76 additions & 0 deletions source/tinystl/utility/exception_guard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#pragma once

#include <cassert>
#include <utility>

namespace tinystl {

template <class Roolback>
class exception_guard_exceptions {
public:
exception_guard_exceptions() = delete;

explicit exception_guard_exceptions(Roolback roolbaclk)
: m_roolback(std::move(roolbaclk)), m_complete(false) {}

exception_guard_exceptions(exception_guard_exceptions &&other) noexcept
: m_roolback(std::move(other.m_roolback)), m_complete(other.m_complete) {
other.m_complete = true;
}
exception_guard_exceptions(const exception_guard_exceptions &) = delete;
exception_guard_exceptions &
operator=(const exception_guard_exceptions &) = delete;
exception_guard_exceptions &
operator=(exception_guard_exceptions &&other) = delete;

~exception_guard_exceptions() noexcept(false) {
if (!m_complete) {
m_roolback();
}
}

void complete() noexcept { m_complete = true; }

private:
Roolback m_roolback;
bool m_complete;
};

template <class Roolback>
class exception_guard_nonexceptions {
public:
exception_guard_nonexceptions() = delete;

explicit exception_guard_nonexceptions(Roolback)
: m_complete(false) {}

exception_guard_nonexceptions(exception_guard_nonexceptions &&other) noexcept
: m_complete(other.m_complete) {
other.m_complete = true;
}

exception_guard_nonexceptions(const exception_guard_nonexceptions &) = delete;
exception_guard_nonexceptions &
operator=(const exception_guard_nonexceptions &) = delete;
exception_guard_nonexceptions &
operator=(exception_guard_nonexceptions &&other) = delete;

~exception_guard_nonexceptions() noexcept {
assert(!m_complete && "exception_guard not complete with exceptions disabled");
}

void complete() noexcept { m_complete = true; }

private:
bool m_complete;
};

template <class Roolback>
using exception_guard = exception_guard_exceptions<Roolback>;

template <class Roolback>
exception_guard<Roolback> make_exception_guard(Roolback roolback) {
return exception_guard<Roolback>(std::move(roolback));
}

} // namespace tinystl
Loading