From 2dcb315ce6bf156af889beb1cffb9c0c8cb7850f Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 12 Dec 2016 17:07:53 +1100 Subject: [PATCH] region: prefer constexpr functions over static variables --- region.cpp | 27 +++------------------------ region.hpp | 4 ++-- region.ipp | 26 ++++++++++++++++++++++++++ test/region.cpp | 10 +++++----- 4 files changed, 36 insertions(+), 31 deletions(-) diff --git a/region.cpp b/region.cpp index 0203010d..5ee285d6 100644 --- a/region.cpp +++ b/region.cpp @@ -232,8 +232,8 @@ util::region::intersection (region rhs) const point_t a, b; for (size_t i = 0; i < S; ++i) { - a[i] = max (p[i], rhs.p[i]); - b[i] = min (p[i] + e[i], rhs.p[i] + rhs.e[i]); + a[i] = util::max (p[i], rhs.p[i]); + b[i] = util::min (p[i] + e[i], rhs.p[i] + rhs.e[i]); if (b[i] < a[i]) throw std::logic_error ("no overlap"); @@ -336,28 +336,7 @@ util::region::operator== (region rhs) const } -///---------------------------------------------------------------------------- -/// The largest specifiable finite region. -/// -/// Starts at half the minimum value to allow the width to cover some positive -/// range rather than just cancelling out the lowest value for signed types. -/// -/// Specifically does not allow infinities. Use/define INFINITE when required. - -template -const util::region -util::region::MAX ( - util::point {std::numeric_limits::lowest () / 2}, - util::extent {std::numeric_limits::max ()} -); - - -template -const util::region -util::region::UNIT (util::point{0}, util::extent{1}); - - -//----------------------------------------------------------------------------- +/////////////////////////////////////////////////////////////////////////////// template std::ostream& util::operator<< (std::ostream &os, const util::region &rhs) { diff --git a/region.hpp b/region.hpp index 44c2764a..b707a2bd 100644 --- a/region.hpp +++ b/region.hpp @@ -105,8 +105,8 @@ namespace util { { return !(*this == rhs); } // Utility constants - static const region MAX; - static const region UNIT; + static constexpr region max (void); + static constexpr region unit (void); void sanity (void) const; }; diff --git a/region.ipp b/region.ipp index e0e9f062..2209f445 100644 --- a/region.ipp +++ b/region.ipp @@ -31,3 +31,29 @@ util::region::cast (void) const e.template cast () }; } + + +/////////////////////////////////////////////////////////////////////////////// +template +constexpr +util::region +util::region::max (void) +{ + return { + util::point {std::numeric_limits::lowest () / 2}, + util::extent {std::numeric_limits::max ()} + }; +} + + +//----------------------------------------------------------------------------- +template +constexpr +util::region +util::region::unit (void) +{ + return { + point_t::origin (), + extent_t {1} + }; +} diff --git a/test/region.cpp b/test/region.cpp index 66efb720..9f4a26d0 100644 --- a/test/region.cpp +++ b/test/region.cpp @@ -23,13 +23,13 @@ main (int, char **) tap.expect (!a.intersects (b), "simple 2d intersection"); } - tap.expect (util::region2d::MAX.intersects (util::region2d::UNIT), "maximal region2d intersection"); - tap.expect (util::region2f::MAX.intersects (util::region2f::UNIT), "maximal region2f intersection"); + tap.expect (util::region2d::max ().intersects (util::region2d::unit ()), "maximal region2d intersection"); + tap.expect (util::region2f::max ().intersects (util::region2f::unit ()), "maximal region2f intersection"); - tap.expect_eq (util::region2d::UNIT.area (), 1.0, "unit region2d area"); - tap.expect_eq (util::region2f::UNIT.area (), 1.0f, "unit region2f area"); + tap.expect_eq (util::region2d::unit ().area (), 1.0, "unit region2d area"); + tap.expect_eq (util::region2f::unit ().area (), 1.0f, "unit region2f area"); - util::point2u p0 { 0 }; + util::point2u p0 { 0 }; util::extent2u e0 { 2 }; tap.expect (util::region2u (p0, e0).includes (util::point2u {1, 1}), "unsigned region centre inclusion");