diff --git a/Makefile.am b/Makefile.am index d653d77e..51ea9d47 100644 --- a/Makefile.am +++ b/Makefile.am @@ -47,6 +47,7 @@ UTIL_FILES = \ coord/base.hpp \ coord.hpp \ coord/init.hpp \ + coord/iostream.hpp \ coord/names.hpp \ coord/ops.hpp \ coord/store.hpp \ diff --git a/bezier.cpp b/bezier.cpp index 58a49770..2a60ec24 100644 --- a/bezier.cpp +++ b/bezier.cpp @@ -11,13 +11,15 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2015 Danny Robson + * Copyright 2015-2016 Danny Robson */ -#include "bezier.hpp" +#include "./bezier.hpp" -#include "debug.hpp" -#include "polynomial.hpp" +#include "./debug.hpp" +#include "./polynomial.hpp" +#include "./stream.hpp" +#include "./coord/iostream.hpp" #include #include @@ -433,9 +435,14 @@ template std::ostream& util::operator<< (std::ostream &os, const bezier &b) { - os << b[0]; - for (size_t i = 1; i < S+1; ++i) - os << ", " << b[i]; + using value_type = decltype(*b.cbegin()); + + os << "["; + std::transform (std::cbegin (b), + std::cend (b), + infix_iterator> (os, ", "), + stream::to_numeric); + os << "]"; return os; } diff --git a/bezier.hpp b/bezier.hpp index f1cac7bc..bafd612c 100644 --- a/bezier.hpp +++ b/bezier.hpp @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2015 Danny Robson + * Copyright 2015-2016 Danny Robson */ #ifndef __UTIL_BEZIER_HPP @@ -26,6 +26,8 @@ namespace util { template class bezier { public: + using value_type = point2f::value_type; + bezier (const util::point2f (&)[S+1]); point2f eval (float t) const; diff --git a/colour.cpp b/colour.cpp index c0eed86b..1c5a368e 100644 --- a/colour.cpp +++ b/colour.cpp @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2010-2013 Danny Robson + * Copyright 2010-2016 Danny Robson */ #include "colour.hpp" @@ -383,9 +383,12 @@ template std::ostream& util::operator<< (std::ostream &os, util::colour c) { os << "colour("; - for (size_t i = 0; i < S - 1; ++i) - os << stream::numeric (c[i]) << ", "; - os << stream::numeric (c[S-1]) << ")"; + std::transform (std::cbegin (c), + std::cend (c), + infix_iterator> (os, ", "), + stream::to_numeric); + os << ")"; + return os; } diff --git a/coord/base.hpp b/coord/base.hpp index db396fc0..9300076b 100644 --- a/coord/base.hpp +++ b/coord/base.hpp @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2012-2015 Danny Robson + * Copyright 2012-2016 Danny Robson */ #ifndef __UTIL_COORD_BASE_HPP @@ -34,7 +34,7 @@ namespace util { namespace coord { struct base : public init { static_assert (S > 0, "coord dimensions must be strictly positive"); - typedef T value_type; + using value_type = T; static constexpr size_t dimension = S; static constexpr size_t elements = S; diff --git a/coord/iostream.hpp b/coord/iostream.hpp new file mode 100644 index 00000000..d5254a3e --- /dev/null +++ b/coord/iostream.hpp @@ -0,0 +1,48 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright 2016 Danny Robson + */ + +#ifndef __UTIL_IOSTREAM +#define __UTIL_IOSTREAM + +#include "../iterator.hpp" +#include "../stream.hpp" + +#include + +namespace util { + template < + template class K, + size_t S, + typename T + > + std::ostream& + operator<< (std::ostream &os, const K &k) + { + os << "["; + std::transform (std::cbegin (k), + std::cend (k), + infix_iterator< + stream::numeric + > (os, ", "), + stream::to_numeric); + os << "]"; + + return os; + } + +} + +#endif diff --git a/coord/ops.hpp b/coord/ops.hpp index 7494a2f3..4f035360 100644 --- a/coord/ops.hpp +++ b/coord/ops.hpp @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2012-2015 Danny Robson + * Copyright 2012-2016 Danny Robson */ #ifndef __UTIL_COORDS_OPS @@ -241,6 +241,8 @@ namespace util { UNARY_OP(!) UNARY_OP(~) + UNARY_OP(+) + UNARY_OP(-) #undef UNARY_OP diff --git a/extent.cpp b/extent.cpp index 446974ab..3047dddc 100644 --- a/extent.cpp +++ b/extent.cpp @@ -11,13 +11,14 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2010-2015 Danny Robson + * Copyright 2010-2016 Danny Robson */ -#include "extent.hpp" +#include "./extent.hpp" -#include "debug.hpp" -#include "maths.hpp" +#include "./debug.hpp" +#include "./maths.hpp" +#include "./stream.hpp" #include @@ -237,9 +238,10 @@ std::ostream& util::operator<< (std::ostream &os, extent e) { os << "["; - std::copy (std::begin (e.data), - std::end (e.data), - std::ostream_iterator (os, ", ")); + std::transform (std::cbegin (e.data), + std::cend (e.data), + infix_iterator> (os, ", "), + stream::to_numeric); os << "]"; return os; } diff --git a/geom/aabb.hpp b/geom/aabb.hpp index 20c3e292..83eb3c4c 100644 --- a/geom/aabb.hpp +++ b/geom/aabb.hpp @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2015 Danny Robson + * Copyright 2015-2016 Danny Robson */ @@ -20,6 +20,7 @@ #include "../point.hpp" #include "../extent.hpp" +#include "../coord/iostream.hpp" #include diff --git a/geom/iostream.cpp b/geom/iostream.cpp index 2ef4b33b..bc5add01 100644 --- a/geom/iostream.cpp +++ b/geom/iostream.cpp @@ -16,7 +16,8 @@ #include "./iostream.hpp" -#include "../geom/sphere.hpp" +#include "./sphere.hpp" +#include "../coord/iostream.hpp" /////////////////////////////////////////////////////////////////////////////// diff --git a/geom/iostream.hpp b/geom/iostream.hpp index 2390ecd0..cf533099 100644 --- a/geom/iostream.hpp +++ b/geom/iostream.hpp @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2015 Danny Robson + * Copyright 2015-2016 Danny Robson */ #ifndef __UTIL_GEOM_IOSTREAM_HPP diff --git a/point.cpp b/point.cpp index 61b96898..3c772a4d 100644 --- a/point.cpp +++ b/point.cpp @@ -11,13 +11,13 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2011 Danny Robson + * Copyright 2011-2016 Danny Robson */ -#include "point.hpp" +#include "./point.hpp" -#include "debug.hpp" -#include "maths.hpp" +#include "./debug.hpp" +#include "./maths.hpp" #include #include @@ -64,26 +64,9 @@ util::point::sanity (void) const template const util::point util::point::ORIGIN (T {0}); - //----------------------------------------------------------------------------- -template -std::ostream& -util::operator<< (std::ostream &os, util::point p) { - os << "point" << S << "("; - os << p.data[0]; - - for (size_t i = 1; i < S; ++i) - os << ", " << p.data[i]; - - os << ")"; - return os; -} - - -//----------------------------------------------------------------------------- -#define INSTANTIATE_S_T(S,T) \ -template struct util::point; \ -template std::ostream& util::operator<< (std::ostream &os, util::point); \ +#define INSTANTIATE_S_T(S,T) \ +template struct util::point; #define INSTANTIATE(T) \ INSTANTIATE_S_T(1,T) \ diff --git a/point.hpp b/point.hpp index 5b21aba6..b0bd5f32 100644 --- a/point.hpp +++ b/point.hpp @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2011-2015 Danny Robson + * Copyright 2011-2016 Danny Robson */ #ifndef __UTIL_POINT_HPP @@ -23,7 +23,6 @@ #include #include -#include #include namespace util { @@ -59,10 +58,6 @@ namespace util { template constexpr typename std::common_type::type chebyshev (point, point); - // iostream operators - template - std::ostream& operator<< (std::ostream&, point); - // Convenience typedefs template using point1 = point<1,T>; template using point2 = point<2,T>; diff --git a/region.cpp b/region.cpp index 5323c174..26c6404b 100644 --- a/region.cpp +++ b/region.cpp @@ -11,14 +11,16 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2010-2015 Danny Robson + * Copyright 2010-2016 Danny Robson */ -#include "region.hpp" +#include "./region.hpp" -#include "debug.hpp" -#include "cast.hpp" +#include "./debug.hpp" +#include "./cast.hpp" + +#include "./coord/iostream.hpp" #include #include diff --git a/stream.hpp b/stream.hpp index af2345ba..b7866530 100644 --- a/stream.hpp +++ b/stream.hpp @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2011 Danny Robson + * Copyright 2011-2016 Danny Robson */ #ifndef __UTIL_STREAM_HPP @@ -22,7 +22,7 @@ namespace util { namespace stream { - //--------------------------------------------------------------------- + /////////////////////////////////////////////////////////////////////// template struct numeric { @@ -30,26 +30,25 @@ namespace util { T val; }; - template - std::ostream& operator<< (std::ostream &os, numeric n) - { - static_assert (std::is_fundamental::value, - "numeric streamer is intended for chars"); - - using integral_t = typename std::conditional< - std::is_floating_point::value, - T, - typename std::conditional< - std::is_signed::value, - sized_type< intmax_t>::sint, - sized_type::uint - >::type - >::type; - - return os << (integral_t)n.val; - } //--------------------------------------------------------------------- + template + numeric + to_numeric (const T &t) + { + return numeric (t); + } + + + //--------------------------------------------------------------------- + template + std::ostream& + operator<< (std::ostream &os, numeric n) + { + return os << +n.val; + } + + /////////////////////////////////////////////////////////////////////// class null : public std::ostream { public: std::ostream & put (char c);