LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
formatting.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008, 2009, 2010, 2011 LSST Corporation.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <http://www.lsstcorp.org/LegalNotices/>.
22  */
23 #ifndef NDARRAY_formatting_h_INCLUDED
24 #define NDARRAY_formatting_h_INCLUDED
25 
32 #include "ndarray/ExpressionBase.h"
33 
34 #include <iostream>
35 
36 namespace ndarray {
37 namespace detail {
38 template <typename Derived, int N = Derived::ND::value> class Formatter;
39 } // namespace detail
40 
47  int _width;
49  std::ios_base::fmtflags _flags;
50  std::string _delimiter;
51  std::string _open;
52  std::string _close;
53 public:
54 
56  explicit FormatOptions(
57  int width = 8,
58  int precision = 6,
59  std::ios_base::fmtflags flags = std::ios_base::fmtflags(0),
60  std::string const & delimiter = ", ",
61  std::string const & open = "[",
62  std::string const & close = "]"
63  ) :
64  _width(width),
65  _precision(precision),
66  _flags(flags),
67  _delimiter(delimiter),
68  _open(open),
69  _close(close)
70  {}
71 
73  template <typename Derived>
74  void apply(std::ostream & os, ExpressionBase<Derived> const & expr) {
75  detail::Formatter<Derived>::apply(*this,os,expr,0);
76  }
77 
78  template <typename Derived, int N> friend class detail::Formatter;
79 };
80 
82 template <typename Derived>
83 std::ostream & operator<<(std::ostream & os, ExpressionBase<Derived> const & expr) {
85  options.apply(os,expr);
86  return os;
87 }
88 
89 namespace detail {
90 
95 template <typename Derived, int N>
96 class Formatter {
97 public:
98  static void apply(
99  FormatOptions const & options,
100  std::ostream & os,
101  ExpressionBase<Derived> const & expr,
102  int level
103  ) {
104  os << options._open;
105  if (!expr.empty()) {
106  typename ExpressionBase<Derived>::Iterator const end = expr.end();
107  typename ExpressionBase<Derived>::Iterator iter = expr.begin();
109  for (++iter; iter != end; ++iter) {
110  os << options._delimiter;
111  os << std::endl << std::string(level,' ');
113  }
114  }
115  os << options._close;
116  }
117 };
118 
123 template <typename Derived>
124 class Formatter<Derived,1> {
125 public:
126  static void apply(
127  FormatOptions const & options,
128  std::ostream & os,
129  ExpressionBase<Derived> const & expr,
130  int level
131  ) {
132  os << options._open;
133  if (!expr.empty()) {
134  typename ExpressionBase<Derived>::Iterator const end = expr.end();
135  typename ExpressionBase<Derived>::Iterator iter = expr.begin();
136  int precision = os.precision(options._precision);
137  int width = os.width(options._width);
138  std::ios_base::fmtflags flags = os.setf(options._flags,std::ios_base::floatfield);
139  os << (*iter);
140  for (++iter; iter != end; ++iter) {
141  os << options._delimiter << (*iter);
142  }
143  os.precision(precision);
144  os.width(width);
145  os.setf(flags);
146  }
147  os << options._close;
148  }
149 };
150 
151 } // namespace detail
152 } // namespace ndarray
153 
154 #endif // !NDARRAY_formatting_h_INCLUDED
int iter
std::ios_base::fmtflags _flags
Definition: formatting.h:49
std::string _delimiter
Definition: formatting.h:50
void apply(std::ostream &os, ExpressionBase< Derived > const &expr)
Format the given expression into the given output stream.
Definition: formatting.h:74
Options for controlling stream output of ExpressionBase.
Definition: formatting.h:46
tuple options
Definition: lsstimport.py:45
std::string _close
Definition: formatting.h:52
Definitions for ExpressionBase.
static void apply(FormatOptions const &options, std::ostream &os, ExpressionBase< Derived > const &expr, int level)
Definition: formatting.h:126
Iterator begin() const
Return an Iterator to the beginning of the expression.
bool empty() const
Return true if the first dimension has no elements.
Iterator end() const
Return an Iterator to one past the end of the expression.
FormatOptions(int width=8, int precision=6, std::ios_base::fmtflags flags=std::ios_base::fmtflags(0), std::string const &delimiter=", ", std::string const &open="[", std::string const &close="]")
Standard constructor.
Definition: formatting.h:56
ExpressionTraits< Derived >::Iterator Iterator
Nested expression or element iterator.
static void apply(FormatOptions const &options, std::ostream &os, ExpressionBase< Derived > const &expr, int level)
Definition: formatting.h:98
CRTP base class for all multidimensional expressions.