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.hpp \
coord/init.hpp \
coord/iostream.hpp \
coord/names.hpp \
coord/ops.hpp \
coord/store.hpp \

View File

@ -11,13 +11,15 @@
* See the License for the specific language governing permissions and
* 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 "polynomial.hpp"
#include "./debug.hpp"
#include "./polynomial.hpp"
#include "./stream.hpp"
#include "./coord/iostream.hpp"
#include <algorithm>
#include <iterator>
@ -433,9 +435,14 @@ template <size_t S>
std::ostream&
util::operator<< (std::ostream &os, const bezier<S> &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<stream::numeric<value_type>> (os, ", "),
stream::to_numeric<value_type>);
os << "]";
return os;
}

View File

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

View File

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

View File

@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and
* 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
@ -34,7 +34,7 @@ namespace util { namespace coord {
struct base : public init <S,T,tags...> {
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;

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
* limitations under the License.
*
* Copyright 2012-2015 Danny Robson <danny@nerdcruft.net>
* Copyright 2012-2016 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_COORDS_OPS
@ -241,6 +241,8 @@ namespace util {
UNARY_OP(!)
UNARY_OP(~)
UNARY_OP(+)
UNARY_OP(-)
#undef UNARY_OP

View File

@ -11,13 +11,14 @@
* See the License for the specific language governing permissions and
* 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 "maths.hpp"
#include "./debug.hpp"
#include "./maths.hpp"
#include "./stream.hpp"
#include <cmath>
@ -237,9 +238,10 @@ std::ostream&
util::operator<< (std::ostream &os, extent<S,T> e)
{
os << "[";
std::copy (std::begin (e.data),
std::end (e.data),
std::ostream_iterator<T> (os, ", "));
std::transform (std::cbegin (e.data),
std::cend (e.data),
infix_iterator<stream::numeric<T>> (os, ", "),
stream::to_numeric<T>);
os << "]";
return os;
}

View File

@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and
* 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 "../extent.hpp"
#include "../coord/iostream.hpp"
#include <cstdint>

View File

@ -16,7 +16,8 @@
#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
* limitations under the License.
*
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
* Copyright 2015-2016 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_GEOM_IOSTREAM_HPP

View File

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

View File

@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2011-2015 Danny Robson <danny@nerdcruft.net>
* Copyright 2011-2016 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_POINT_HPP
@ -23,7 +23,6 @@
#include <array>
#include <initializer_list>
#include <iostream>
#include <type_traits>
namespace util {
@ -59,10 +58,6 @@ namespace util {
template <size_t S, typename T, typename 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
template <typename T> using point1 = point<1,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
* 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 "cast.hpp"
#include "./debug.hpp"
#include "./cast.hpp"
#include "./coord/iostream.hpp"
#include <cmath>
#include <type_traits>

View File

@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2011 Danny Robson <danny@nerdcruft.net>
* Copyright 2011-2016 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_STREAM_HPP
@ -22,7 +22,7 @@
namespace util {
namespace stream {
//---------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////
template <typename T>
struct numeric
{
@ -30,26 +30,25 @@ namespace util {
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 {
public:
std::ostream & put (char c);