'in place' example and new memory buffer helper class

This commit is contained in:
BlackMATov
2023-01-03 07:17:02 +07:00
parent 04846cc18d
commit 953d721511
8 changed files with 514 additions and 79 deletions

View File

@@ -13,6 +13,7 @@
#include "meta_base/fixed_function.hpp"
#include "meta_base/hash_combiner.hpp"
#include "meta_base/is_in_place_type.hpp"
#include "meta_base/memory_buffer.hpp"
#include "meta_base/noncopyable.hpp"
#include "meta_base/overloaded.hpp"
#include "meta_base/select_overload.hpp"
@@ -23,6 +24,8 @@
namespace meta_hpp
{
using detail::memory_buffer;
using detail::select_const;
using detail::select_non_const;
using detail::select_overload;

View File

@@ -84,7 +84,7 @@ namespace meta_hpp::detail
};
template < typename Function >
inline void swap(fixed_function<Function>& l, fixed_function<Function>& r) noexcept {
void swap(fixed_function<Function>& l, fixed_function<Function>& r) noexcept {
l.swap(r);
}
}

View File

@@ -0,0 +1,92 @@
/*******************************************************************************
* This file is part of the "https://github.com/blackmatov/meta.hpp"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2021-2022, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#pragma once
#include "base.hpp"
namespace meta_hpp::detail
{
class memory_buffer final {
public:
memory_buffer() = default;
memory_buffer(const memory_buffer&) = delete;
memory_buffer& operator=(const memory_buffer&) = delete;
memory_buffer(memory_buffer&& other) noexcept
: memory_{other.memory_}
, size_{other.size_}
, align_{other.align_} {
other.memory_ = nullptr;
other.size_ = 0;
other.align_ = std::align_val_t{};
}
memory_buffer& operator=(memory_buffer&& other) noexcept {
if ( this != &other ) {
memory_buffer{std::move(other)}.swap(*this);
}
return *this;
}
explicit memory_buffer(std::size_t size, std::align_val_t align)
: memory_{::operator new(size, align)}
, size_{size}
, align_{align} {}
~memory_buffer() noexcept {
reset();
}
[[nodiscard]] bool is_valid() const noexcept {
return memory_ != nullptr;
}
[[nodiscard]] explicit operator bool() const noexcept {
return is_valid();
}
void reset() noexcept {
if ( memory_ != nullptr ) {
::operator delete(memory_, align_);
memory_ = nullptr;
size_ = 0;
align_ = std::align_val_t{};
}
}
void swap(memory_buffer& other) noexcept {
std::swap(memory_, other.memory_);
std::swap(size_, other.size_);
std::swap(align_, other.align_);
}
[[nodiscard]] void* get_memory() noexcept {
return memory_;
}
[[nodiscard]] const void* get_memory() const noexcept {
return memory_;
}
[[nodiscard]] std::size_t get_size() const noexcept {
return size_;
}
[[nodiscard]] std::align_val_t get_align() const noexcept {
return align_;
}
private:
void* memory_{};
std::size_t size_{};
std::align_val_t align_{};
};
inline void swap(memory_buffer& l, memory_buffer& r) noexcept {
l.swap(r);
}
}

View File

@@ -17,12 +17,12 @@ namespace meta_hpp::detail
template < typename Class, typename... Args >
concept class_bind_constructor_kind =
class_kind<Class> &&
requires(Args&&... args) { { Class{std::forward<Args>(args)...} }; };
std::is_constructible_v<Class, Args...>;
template < typename Class >
concept class_bind_destructor_kind =
class_kind<Class> &&
requires(Class&& inst) { { inst.~Class() }; };
std::is_destructible_v<Class>;
template < typename Class, typename Base >
concept class_bind_base_kind =