From f87110f3d83e0d899af6f44d03fdf7ceb7da6afd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Can=20B=C3=B6l=C3=BCk?= Date: Sun, 28 Jun 2020 01:18:45 +0200 Subject: [PATCH] Implementing tuple enumeration/visiting. --- VTIL-Common/CMakeLists.txt | 1 + VTIL-Common/VTIL-Common.vcxproj | 1 + VTIL-Common/VTIL-Common.vcxproj.filters | 3 ++ VTIL-Common/includes/vtil/utility | 3 +- VTIL-Common/util/tuple_visit.hpp | 46 +++++++++++++++++++++++++ 5 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 VTIL-Common/util/tuple_visit.hpp diff --git a/VTIL-Common/CMakeLists.txt b/VTIL-Common/CMakeLists.txt index ed8b2a7..28b48cd 100644 --- a/VTIL-Common/CMakeLists.txt +++ b/VTIL-Common/CMakeLists.txt @@ -56,6 +56,7 @@ add_library(${PROJECT_NAME} STATIC util/zip.hpp util/colony.hpp util/object_pool.hpp + util/tuple_visit.hpp ) target_include_directories(${PROJECT_NAME} PUBLIC includes) diff --git a/VTIL-Common/VTIL-Common.vcxproj b/VTIL-Common/VTIL-Common.vcxproj index aaba6ab..95a6a6b 100644 --- a/VTIL-Common/VTIL-Common.vcxproj +++ b/VTIL-Common/VTIL-Common.vcxproj @@ -165,6 +165,7 @@ + diff --git a/VTIL-Common/VTIL-Common.vcxproj.filters b/VTIL-Common/VTIL-Common.vcxproj.filters index 654dcb5..db07c99 100644 --- a/VTIL-Common/VTIL-Common.vcxproj.filters +++ b/VTIL-Common/VTIL-Common.vcxproj.filters @@ -184,6 +184,9 @@ Utility + + Utility + diff --git a/VTIL-Common/includes/vtil/utility b/VTIL-Common/includes/vtil/utility index 71c81d5..2c47831 100644 --- a/VTIL-Common/includes/vtil/utility +++ b/VTIL-Common/includes/vtil/utility @@ -19,4 +19,5 @@ #include "../../util/reverse_iterator.hpp" #include "../../util/conditional_lock.hpp" #include "../../util/numeric_iterator.hpp" -#include "../../util/multivariate.hpp" \ No newline at end of file +#include "../../util/multivariate.hpp" +#include "../../util/tuple_visit.hpp" \ No newline at end of file diff --git a/VTIL-Common/util/tuple_visit.hpp b/VTIL-Common/util/tuple_visit.hpp new file mode 100644 index 0000000..0e33943 --- /dev/null +++ b/VTIL-Common/util/tuple_visit.hpp @@ -0,0 +1,46 @@ +// Copyright (c) 2020 Can Boluk and contributors of the VTIL Project +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// 3. Neither the name of mosquitto nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +#pragma once +#include +#include + +namespace vtil +{ + template + static void tuple_visit( const T& obj, Fn callable, std::index_sequence ) + { + static constexpr auto ignore = [ ] ( ... ) {}; + ignore( ( callable( std::get( obj ) ), 0 )... ); + } + + template + static void tuple_visit( const std::tuple& obj, Fn&& callable ) + { + return tuple_visit( obj, std::forward( callable ), std::index_sequence_for{} ); + } +}; \ No newline at end of file