From 9d6be23dd51ce8b16ebad0edd0324f5c266b6aa6 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 17 Feb 2015 16:18:55 +1100 Subject: [PATCH] range: templatise normalise methods return type --- Makefile.am | 1 + range.cpp | 8 -------- range.hpp | 7 +++++-- range.ipp | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 range.ipp diff --git a/Makefile.am b/Makefile.am index 7ec7f5f5..5f564fea 100644 --- a/Makefile.am +++ b/Makefile.am @@ -135,6 +135,7 @@ UTIL_FILES = \ rational.hpp \ region.cpp \ region.hpp \ + region.ipp \ si.cpp \ signal.cpp \ signal.hpp \ diff --git a/range.cpp b/range.cpp index 3da2a58c..5bc10e4d 100644 --- a/range.cpp +++ b/range.cpp @@ -87,14 +87,6 @@ range::expand (T val) { } -template -double -range::normalise (T val) const { - return ((double)val - min) / - ((double)max - min); -} - - template range& range::operator*= (T val) { diff --git a/range.hpp b/range.hpp index e2ba47fa..62082698 100644 --- a/range.hpp +++ b/range.hpp @@ -14,7 +14,7 @@ * You should have received a copy of the GNU General Public License * along with libgim. If not, see . * - * Copyright 2010 Danny Robson + * Copyright 2010-2015 Danny Robson */ @@ -52,7 +52,8 @@ namespace util { /// Normalise a number to [0, 1] within the range. Does not check /// bounds, it is the caller's responsibility to clamp the result if /// needed. - double normalise (T val) const; + template + U normalise (T val) const; range& operator*= (T); range operator* (T) const; @@ -92,4 +93,6 @@ namespace util { } } +#include "range.ipp" + #endif diff --git a/range.ipp b/range.ipp new file mode 100644 index 00000000..840861ae --- /dev/null +++ b/range.ipp @@ -0,0 +1,41 @@ +/* + * This file is part of libgim. + * + * libgim is free software: you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * libgim is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with libgim. If not, see . + * + * Copyright 2010-2015 Danny Robson + */ + +#ifdef __UTIL_RANGE_IPP +#error +#else +#define __UTIL_RANGE_IPP +#endif + +#include + + +//----------------------------------------------------------------------------- +namespace util { + template + template + U + range::normalise (T val) const { + static_assert (std::is_floating_point::value, + "normalise isn't implemented for integer types"); + + return static_cast (val - min) / + static_cast (max - min); + } +}