diff --git a/projects/kiwano/kiwano.vcxproj b/projects/kiwano/kiwano.vcxproj index a0ca2a9c..f7d520dd 100644 --- a/projects/kiwano/kiwano.vcxproj +++ b/projects/kiwano/kiwano.vcxproj @@ -23,6 +23,7 @@ + diff --git a/projects/kiwano/kiwano.vcxproj.filters b/projects/kiwano/kiwano.vcxproj.filters index 94ab0a18..7e28e439 100644 --- a/projects/kiwano/kiwano.vcxproj.filters +++ b/projects/kiwano/kiwano.vcxproj.filters @@ -354,6 +354,9 @@ base + + core + diff --git a/src/kiwano/core/Defer.h b/src/kiwano/core/Defer.h new file mode 100644 index 00000000..961c6c52 --- /dev/null +++ b/src/kiwano/core/Defer.h @@ -0,0 +1,69 @@ +// Copyright (c) 2016-2018 Kiwano - Nomango +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#pragma once +#include + +namespace kiwano +{ + +class Defer +{ +public: + Defer() = default; + + Defer(const Function& func) + : func_(func) + { + } + + Defer(Defer&& other) noexcept + : func_(other.func_) + { + other.func_ = nullptr; + } + + ~Defer() + { + if (func_) + func_(); + } + +private: + Defer(const Defer&) = delete; + Defer& operator=(const Defer&) = delete; + + Function func_; +}; + +class __DeferHelper +{ +public: + Defer operator-(const Function& func) const + { + return Defer{ func }; + } +}; + +#define KGE_DEFER auto __KGE_DEFER_VAR(__defer_line_, __LINE__, __) = ::kiwano::__DeferHelper() - +#define __KGE_DEFER_VAR(a, b, c) __KGE_DEFER_TOKEN_CONNECT(a, b, c) +#define __KGE_DEFER_TOKEN_CONNECT(a, b, c) a##b##c + +} // namespace kiwano diff --git a/src/kiwano/kiwano.h b/src/kiwano/kiwano.h index 1bd37f0f..d4630520 100644 --- a/src/kiwano/kiwano.h +++ b/src/kiwano/kiwano.h @@ -44,6 +44,7 @@ // #include +#include #include #include #include diff --git a/src/kiwano/platform/Application.cpp b/src/kiwano/platform/Application.cpp index 98921da9..a117aa31 100644 --- a/src/kiwano/platform/Application.cpp +++ b/src/kiwano/platform/Application.cpp @@ -19,11 +19,12 @@ // THE SOFTWARE. #include -#include +#include #include #include #include #include +#include namespace kiwano { @@ -62,24 +63,11 @@ void Application::Run(RunnerPtr runner) } // Ensure resources are destroyed before exiting - class DestroyHelper + KGE_DEFER[=]() { - Function f; - - public: - DestroyHelper(Function f) - : f(f) - { - } - - ~DestroyHelper() - { - f(); - } + this->Destroy(); }; - DestroyHelper helper([=]() { this->Destroy(); }); - // Everything is ready runner->OnReady();