OSVR-Core  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
IndentingStream.h
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_IndentingStream_h_GUID_05296193_1397_4991_ACE4_D6FA08655E0B
26 #define INCLUDED_IndentingStream_h_GUID_05296193_1397_4991_ACE4_D6FA08655E0B
27 
28 // Internal Includes
29 #include <osvr/Util/Flag.h>
30 
31 // Library/third-party includes
32 #include <boost/iostreams/concepts.hpp>
33 #include <boost/iostreams/filtering_stream.hpp>
34 
35 // Standard includes
36 #include <stddef.h>
37 #include <vector>
38 #include <iosfwd>
39 
40 namespace osvr {
41 namespace util {
42  namespace detail {
46  class IndentFilter : public boost::iostreams::output_filter {
47  public:
48  IndentFilter(size_t spaces)
49  : m_theSpaces(spaces, ' '), m_lastWasNewline(true) {}
50 
51  static const char NEWLINE = '\n';
52  static const char CR = '\r';
53  void setLastWasNewline() { m_lastWasNewline = true; }
54  template <typename Sink> bool put(Sink &snk, char c) {
55 
56  namespace io = boost::iostreams;
57  osvr::util::Flag failure;
58  if (c == CR) {
59  // silently drop these, they can confuse us
60  return true;
61  }
62  if (c == NEWLINE) {
63  m_lastWasNewline = true;
64  } else if (m_lastWasNewline) {
65  failure +=
66  !io::write(snk, m_theSpaces.data(), m_theSpaces.size());
67  m_lastWasNewline = false;
68  }
69  failure += !io::put(snk, c);
70  return !failure;
71  }
72 
73  private:
74  std::vector<char> m_theSpaces;
75  bool m_lastWasNewline;
76  };
77  } // namespace detail
78 
82  class IndentingStream : public boost::iostreams::filtering_ostream {
83  public:
84  IndentingStream(size_t spaces, std::ostream &stream)
85  : m_filter(spaces), m_os(stream) {
86  push(boost::ref(m_filter));
87  push(m_os);
88  }
89 
90  void reset() { m_filter.setLastWasNewline(); }
91 
92  private:
93  detail::IndentFilter m_filter;
94  std::ostream &m_os;
95  };
96 } // namespace util
97 } // namespace osvr
98 
99 #endif // INCLUDED_IndentingStream_h_GUID_05296193_1397_4991_ACE4_D6FA08655E0B
The main namespace for all C++ elements of the framework, internal and external.
Definition: ClientKit.h:31
A boost::iostreams::output_filter that inserts the given number of spaces before the first character ...
Header.
A boost::iostreams::filtering_ostream with a constructor that automatically sets it up to indent the ...
A class that lightly wraps a bool, in order to provide easier maintenance of a "dirty" flag...
Definition: Flag.h:43