iterator: use infix_iterator for coord ostream

This commit is contained in:
Danny Robson 2016-03-11 12:48:19 +11:00
parent cd15371fcf
commit 7f4cf49931
15 changed files with 124 additions and 78 deletions

View File

@ -47,6 +47,7 @@ UTIL_FILES = \
coord/base.hpp \ coord/base.hpp \
coord.hpp \ coord.hpp \
coord/init.hpp \ coord/init.hpp \
coord/iostream.hpp \
coord/names.hpp \ coord/names.hpp \
coord/ops.hpp \ coord/ops.hpp \
coord/store.hpp \ coord/store.hpp \

View File

@ -11,13 +11,15 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* Copyright 2015 Danny Robson <danny@nerdcruft.net> * Copyright 2015-2016 Danny Robson <danny@nerdcruft.net>
*/ */
#include "bezier.hpp" #include "./bezier.hpp"
#include "debug.hpp" #include "./debug.hpp"
#include "polynomial.hpp" #include "./polynomial.hpp"
#include "./stream.hpp"
#include "./coord/iostream.hpp"
#include <algorithm> #include <algorithm>
#include <iterator> #include <iterator>
@ -433,9 +435,14 @@ template <size_t S>
std::ostream& std::ostream&
util::operator<< (std::ostream &os, const bezier<S> &b) util::operator<< (std::ostream &os, const bezier<S> &b)
{ {
os << b[0]; using value_type = decltype(*b.cbegin());
for (size_t i = 1; i < S+1; ++i)
os << ", " << b[i]; os << "[";
std::transform (std::cbegin (b),
std::cend (b),
infix_iterator<stream::numeric<value_type>> (os, ", "),
stream::to_numeric<value_type>);
os << "]";
return os; return os;
} }

View File

@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* Copyright 2015 Danny Robson <danny@nerdcruft.net> * Copyright 2015-2016 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef __UTIL_BEZIER_HPP #ifndef __UTIL_BEZIER_HPP
@ -26,6 +26,8 @@ namespace util {
template <size_t S> template <size_t S>
class bezier { class bezier {
public: public:
using value_type = point2f::value_type;
bezier (const util::point2f (&)[S+1]); bezier (const util::point2f (&)[S+1]);
point2f eval (float t) const; point2f eval (float t) const;

View File

@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* Copyright 2010-2013 Danny Robson <danny@nerdcruft.net> * Copyright 2010-2016 Danny Robson <danny@nerdcruft.net>
*/ */
#include "colour.hpp" #include "colour.hpp"
@ -383,9 +383,12 @@ template <size_t S, typename T>
std::ostream& std::ostream&
util::operator<< (std::ostream &os, util::colour<S,T> c) { util::operator<< (std::ostream &os, util::colour<S,T> c) {
os << "colour("; os << "colour(";
for (size_t i = 0; i < S - 1; ++i) std::transform (std::cbegin (c),
os << stream::numeric<T> (c[i]) << ", "; std::cend (c),
os << stream::numeric<T> (c[S-1]) << ")"; infix_iterator<stream::numeric<T>> (os, ", "),
stream::to_numeric<T>);
os << ")";
return os; return os;
} }

View File

@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* Copyright 2012-2015 Danny Robson <danny@nerdcruft.net> * Copyright 2012-2016 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef __UTIL_COORD_BASE_HPP #ifndef __UTIL_COORD_BASE_HPP
@ -34,7 +34,7 @@ namespace util { namespace coord {
struct base : public init <S,T,tags...> { struct base : public init <S,T,tags...> {
static_assert (S > 0, "coord dimensions must be strictly positive"); 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 dimension = S;
static constexpr size_t elements = S; static constexpr size_t elements = S;

48
coord/iostream.hpp Normal file
View File

@ -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 <danny@nerdcruft.net>
*/
#ifndef __UTIL_IOSTREAM
#define __UTIL_IOSTREAM
#include "../iterator.hpp"
#include "../stream.hpp"
#include <ostream>
namespace util {
template <
template <size_t,typename> class K,
size_t S,
typename T
>
std::ostream&
operator<< (std::ostream &os, const K<S,T> &k)
{
os << "[";
std::transform (std::cbegin (k),
std::cend (k),
infix_iterator<
stream::numeric<T>
> (os, ", "),
stream::to_numeric<T>);
os << "]";
return os;
}
}
#endif

View File

@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* Copyright 2012-2015 Danny Robson <danny@nerdcruft.net> * Copyright 2012-2016 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef __UTIL_COORDS_OPS #ifndef __UTIL_COORDS_OPS
@ -241,6 +241,8 @@ namespace util {
UNARY_OP(!) UNARY_OP(!)
UNARY_OP(~) UNARY_OP(~)
UNARY_OP(+)
UNARY_OP(-)
#undef UNARY_OP #undef UNARY_OP

View File

@ -11,13 +11,14 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* Copyright 2010-2015 Danny Robson <danny@nerdcruft.net> * Copyright 2010-2016 Danny Robson <danny@nerdcruft.net>
*/ */
#include "extent.hpp" #include "./extent.hpp"
#include "debug.hpp" #include "./debug.hpp"
#include "maths.hpp" #include "./maths.hpp"
#include "./stream.hpp"
#include <cmath> #include <cmath>
@ -237,9 +238,10 @@ std::ostream&
util::operator<< (std::ostream &os, extent<S,T> e) util::operator<< (std::ostream &os, extent<S,T> e)
{ {
os << "["; os << "[";
std::copy (std::begin (e.data), std::transform (std::cbegin (e.data),
std::end (e.data), std::cend (e.data),
std::ostream_iterator<T> (os, ", ")); infix_iterator<stream::numeric<T>> (os, ", "),
stream::to_numeric<T>);
os << "]"; os << "]";
return os; return os;
} }

View File

@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* Copyright 2015 Danny Robson <danny@nerdcruft.net> * Copyright 2015-2016 Danny Robson <danny@nerdcruft.net>
*/ */
@ -20,6 +20,7 @@
#include "../point.hpp" #include "../point.hpp"
#include "../extent.hpp" #include "../extent.hpp"
#include "../coord/iostream.hpp"
#include <cstdint> #include <cstdint>

View File

@ -16,7 +16,8 @@
#include "./iostream.hpp" #include "./iostream.hpp"
#include "../geom/sphere.hpp" #include "./sphere.hpp"
#include "../coord/iostream.hpp"
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

View File

@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* Copyright 2015 Danny Robson <danny@nerdcruft.net> * Copyright 2015-2016 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef __UTIL_GEOM_IOSTREAM_HPP #ifndef __UTIL_GEOM_IOSTREAM_HPP

View File

@ -11,13 +11,13 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* Copyright 2011 Danny Robson <danny@nerdcruft.net> * Copyright 2011-2016 Danny Robson <danny@nerdcruft.net>
*/ */
#include "point.hpp" #include "./point.hpp"
#include "debug.hpp" #include "./debug.hpp"
#include "maths.hpp" #include "./maths.hpp"
#include <cmath> #include <cmath>
#include <cstdlib> #include <cstdlib>
@ -64,26 +64,9 @@ util::point<S,T>::sanity (void) const
template <size_t S, typename T> template <size_t S, typename T>
const util::point<S,T> util::point<S,T>::ORIGIN (T {0}); const util::point<S,T> util::point<S,T>::ORIGIN (T {0});
//-----------------------------------------------------------------------------
template <size_t S, typename T>
std::ostream&
util::operator<< (std::ostream &os, util::point<S,T> 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) \ #define INSTANTIATE_S_T(S,T) \
template struct util::point<S,T>; \ template struct util::point<S,T>;
template std::ostream& util::operator<< (std::ostream &os, util::point<S,T>); \
#define INSTANTIATE(T) \ #define INSTANTIATE(T) \
INSTANTIATE_S_T(1,T) \ INSTANTIATE_S_T(1,T) \

View File

@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* Copyright 2011-2015 Danny Robson <danny@nerdcruft.net> * Copyright 2011-2016 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef __UTIL_POINT_HPP #ifndef __UTIL_POINT_HPP
@ -23,7 +23,6 @@
#include <array> #include <array>
#include <initializer_list> #include <initializer_list>
#include <iostream>
#include <type_traits> #include <type_traits>
namespace util { namespace util {
@ -59,10 +58,6 @@ namespace util {
template <size_t S, typename T, typename U> template <size_t S, typename T, typename U>
constexpr typename std::common_type<T,U>::type chebyshev (point<S,T>, point<S,U>); constexpr typename std::common_type<T,U>::type chebyshev (point<S,T>, point<S,U>);
// iostream operators
template <size_t S, typename T>
std::ostream& operator<< (std::ostream&, point<S,T>);
// Convenience typedefs // Convenience typedefs
template <typename T> using point1 = point<1,T>; template <typename T> using point1 = point<1,T>;
template <typename T> using point2 = point<2,T>; template <typename T> using point2 = point<2,T>;

View File

@ -11,14 +11,16 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* Copyright 2010-2015 Danny Robson <danny@nerdcruft.net> * Copyright 2010-2016 Danny Robson <danny@nerdcruft.net>
*/ */
#include "region.hpp" #include "./region.hpp"
#include "debug.hpp" #include "./debug.hpp"
#include "cast.hpp" #include "./cast.hpp"
#include "./coord/iostream.hpp"
#include <cmath> #include <cmath>
#include <type_traits> #include <type_traits>

View File

@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* Copyright 2011 Danny Robson <danny@nerdcruft.net> * Copyright 2011-2016 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef __UTIL_STREAM_HPP #ifndef __UTIL_STREAM_HPP
@ -22,7 +22,7 @@
namespace util { namespace util {
namespace stream { namespace stream {
//--------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////
template <typename T> template <typename T>
struct numeric struct numeric
{ {
@ -30,26 +30,25 @@ namespace util {
T val; T val;
}; };
template <typename T>
std::ostream& operator<< (std::ostream &os, numeric<T> n)
{
static_assert (std::is_fundamental<T>::value,
"numeric streamer is intended for chars");
using integral_t = typename std::conditional<
std::is_floating_point<T>::value,
T,
typename std::conditional<
std::is_signed<T>::value,
sized_type< intmax_t>::sint,
sized_type<uintmax_t>::uint
>::type
>::type;
return os << (integral_t)n.val;
}
//--------------------------------------------------------------------- //---------------------------------------------------------------------
template <typename T>
numeric<T>
to_numeric (const T &t)
{
return numeric<T> (t);
}
//---------------------------------------------------------------------
template <typename T>
std::ostream&
operator<< (std::ostream &os, numeric<T> n)
{
return os << +n.val;
}
///////////////////////////////////////////////////////////////////////
class null : public std::ostream { class null : public std::ostream {
public: public:
std::ostream & put (char c); std::ostream & put (char c);