use defer.hpp instead builtin impl

This commit is contained in:
BlackMATov
2020-10-29 04:48:58 +07:00
parent 390e236a57
commit 5dfea8b512
29 changed files with 67 additions and 149 deletions

View File

@@ -112,7 +112,7 @@ namespace e2d::ecsex
Opts&&... opts)
{
static thread_local vector<ecs::entity> to_remove_components;
E2D_DEFER([](){ to_remove_components.clear(); });
DEFER([](){ to_remove_components.clear(); });
owner.for_each_component<T>([](const ecs::entity& e, const T&){
to_remove_components.push_back(e);
@@ -167,7 +167,7 @@ namespace e2d::ecsex
std::tuple<ecs::entity, Ts...>> components;
const std::size_t begin_index = components.size();
E2D_DEFER([begin_index](){
DEFER([begin_index](){
components.erase(
components.begin() + begin_index,
components.end());
@@ -191,7 +191,7 @@ namespace e2d::ecsex
std::tuple<ecs::const_entity, Ts...>> components;
const std::size_t begin_index = components.size();
E2D_DEFER([begin_index](){
DEFER([begin_index](){
components.erase(
components.begin() + begin_index,
components.end());
@@ -218,7 +218,7 @@ namespace e2d::ecsex
std::tuple<ecs::entity, Ts...>> components;
const std::size_t begin_index = components.size();
E2D_DEFER([begin_index](){
DEFER([begin_index](){
components.erase(
components.begin() + begin_index,
components.end());
@@ -247,7 +247,7 @@ namespace e2d::ecsex
std::tuple<ecs::const_entity, Ts...>> components;
const std::size_t begin_index = components.size();
E2D_DEFER([begin_index](){
DEFER([begin_index](){
components.erase(
components.begin() + begin_index,
components.end());

View File

@@ -82,7 +82,7 @@ namespace e2d::impl
}
ImGui::PushID(co.find());
E2D_DEFER([](){ ImGui::PopID(); });
DEFER([](){ ImGui::PopID(); });
const bool inspector_opened = ImGui::CollapsingHeader(
component_inspector<Component>::title,
@@ -90,7 +90,7 @@ namespace e2d::impl
const char* component_popup_context_str_id = "e2d_component_popup_context";
if ( ImGui::BeginPopupContextItem(component_popup_context_str_id) ) {
E2D_DEFER([](){ ImGui::EndPopup(); });
DEFER([](){ ImGui::EndPopup(); });
auto disabled_co = co.owner().template component<disabled<Component>>();
if ( bool enabled = !disabled_co; ImGui::Checkbox("Enabled", &enabled) ) {
@@ -137,7 +137,7 @@ namespace e2d::impl
}
ImGui::PushID(co.find());
E2D_DEFER([](){ ImGui::PopID(); });
DEFER([](){ ImGui::PopID(); });
if ( !co.owner().template component<disabled<Component>>() ) {
inspector_(co, ctx);

View File

@@ -153,7 +153,7 @@ namespace e2d::nodes
static thread_local vector<intrusive_ptr<Node>> parents;
const std::size_t begin_index = parents.size();
E2D_DEFER([begin_index](){
DEFER([begin_index](){
parents.erase(
parents.begin() + begin_index,
parents.end());
@@ -184,7 +184,7 @@ namespace e2d::nodes
static thread_local vector<intrusive_ptr<Node>> children;
const std::size_t begin_index = children.size();
E2D_DEFER([begin_index](){
DEFER([begin_index](){
children.erase(
children.begin() + begin_index,
children.end());
@@ -253,7 +253,7 @@ namespace e2d::nodes
static thread_local vector<gcomponent<Component>> components;
const std::size_t begin_index = components.size();
E2D_DEFER([begin_index](){
DEFER([begin_index](){
components.erase(
components.begin() + begin_index,
components.end());
@@ -284,7 +284,7 @@ namespace e2d::nodes
static thread_local vector<gcomponent<Component>> components;
const std::size_t begin_index = components.size();
E2D_DEFER([begin_index](){
DEFER([begin_index](){
components.erase(
components.begin() + begin_index,
components.end());

View File

@@ -12,7 +12,6 @@
#include "buffer_view.hpp"
#include "color.hpp"
#include "color32.hpp"
#include "defer.hpp"
#include "filesystem.hpp"
#include "filesystem.inl"
#include "font.hpp"

View File

@@ -9,6 +9,7 @@
#include "../base/_all.hpp"
#include "../math/_all.hpp"
#include <defer.hpp/defer.hpp>
#include <enum.hpp/enum.hpp>
namespace e2d

View File

@@ -1,94 +0,0 @@
/*******************************************************************************
* This file is part of the "Enduro2D"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2018-2020, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#pragma once
#include "_utils.hpp"
namespace e2d
{
namespace impl
{
template < typename F >
class defer_impl : noncopyable {
public:
explicit defer_impl(F f)
: f_(std::move(f)) {}
virtual ~defer_impl() noexcept {
if ( !dismissed_ ) {
f_();
}
}
void dismiss() noexcept {
dismissed_ = true;
}
private:
F f_;
bool dismissed_{};
};
template < typename F >
class error_defer_impl final : public defer_impl<F> {
public:
explicit error_defer_impl(F f)
: defer_impl<F>(std::move(f))
, exceptions_(std::uncaught_exceptions()) {}
~error_defer_impl() noexcept final {
if ( exceptions_ == std::uncaught_exceptions() ) {
this->dismiss();
}
}
private:
int exceptions_{};
};
template < typename F >
class return_defer_impl final : public defer_impl<F> {
public:
explicit return_defer_impl(F f)
: defer_impl<F>(std::move(f))
, exceptions_(std::uncaught_exceptions()) {}
~return_defer_impl() noexcept final {
if ( exceptions_ != std::uncaught_exceptions() ) {
this->dismiss();
}
}
private:
int exceptions_{};
};
}
template < typename F >
impl::defer_impl<std::decay_t<F>> make_defer(F&& f) {
return impl::defer_impl<std::decay_t<F>>(std::forward<F>(f));
}
template < typename F >
impl::error_defer_impl<std::decay_t<F>> make_error_defer(F&& f) {
return impl::error_defer_impl<std::decay_t<F>>(std::forward<F>(f));
}
template < typename F >
impl::return_defer_impl<std::decay_t<F>> make_return_defer(F&& f) {
return impl::return_defer_impl<std::decay_t<F>>(std::forward<F>(f));
}
}
#define E2D_DEFER(...)\
auto E2D_PP_CAT(e2d_generated_defer_, __LINE__) =\
::e2d::make_defer(__VA_ARGS__)
#define E2D_ERROR_DEFER(...)\
auto E2D_PP_CAT(e2d_generated_error_defer_, __LINE__) =\
::e2d::make_error_defer(__VA_ARGS__)
#define E2D_RETURN_DEFER(...)\
auto E2D_PP_CAT(e2d_generated_return_defer_, __LINE__) =\
::e2d::make_return_defer(__VA_ARGS__)

View File

@@ -37,7 +37,7 @@ namespace e2d::imgui_utils
template < typename... Args >
void show_colored_text(const color& color, str_view fmt, Args&&... args) {
ImGui::PushStyleColor(ImGuiCol_Text, make_vec4(color));
E2D_DEFER([](){ ImGui::PopStyleColor(); });
DEFER([](){ ImGui::PopStyleColor(); });
show_formatted_text(fmt, std::forward<Args>(args)...);
}
@@ -54,7 +54,7 @@ namespace e2d::imgui_utils
}
if ( ImGui::BeginCombo(label_cstr, preview_cstr) ) {
E2D_DEFER([](){ ImGui::EndCombo(); });
DEFER([](){ ImGui::EndCombo(); });
for ( std::size_t i = 0; i < enum_hpp::size<E>(); ++i ) {
str_view item_name = enum_hpp::names<E>()[i];
@@ -80,10 +80,10 @@ namespace e2d::imgui_utils
template < typename F, typename... Args >
void with_disabled_flag(F&& f, Args&&... args) {
ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
E2D_DEFER([](){ ImGui::PopItemFlag(); });
DEFER([](){ ImGui::PopItemFlag(); });
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, ImGui::GetStyle().Alpha * 0.5f);
E2D_DEFER([](){ ImGui::PopStyleVar(); });
DEFER([](){ ImGui::PopStyleVar(); });
std::invoke(std::forward<F>(f), std::forward<Args>(args)...);
}