From 5c862400a4ee8d2a70325c4c2fd425f36969a5d0 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 7 Jul 2014 15:18:27 +1000 Subject: [PATCH] types: add first, returns first true argument --- types.hpp | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/types.hpp b/types.hpp index 6e74ec45..fcfb027c 100644 --- a/types.hpp +++ b/types.hpp @@ -14,18 +14,18 @@ * You should have received a copy of the GNU General Public License * along with libgim. If not, see . * - * Copyright 2011 Danny Robson + * Copyright 2011-2014 Danny Robson */ #ifndef __UTIL_TYPES_HPP #define __UTIL_TYPES_HPP -#include "platform.hpp" - #include #include #include +#include +//----------------------------------------------------------------------------- /// Returns the number of elements of a statically allocated array template constexpr size_t elems(const T (&)[N]) @@ -38,4 +38,24 @@ make_unique(Args&&... args) { return std::unique_ptr (new T(std::forward(args)...)); } + +//----------------------------------------------------------------------------- +template +T +first (T a) { + if (a) + return a; + + throw std::logic_error ("no valid object"); +} + +template +T +first (T a, Args&& ...b) { + if (a) + return a; + + return first (std::forward(b)...); +} + #endif