OSVR-Core
OptionalStream.h
Go to the documentation of this file.
1 
11 // Copyright 2016 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_OptionalStream_h_GUID_2C133DE4_22D9_4195_B36E_0663DF0A6378
26 #define INCLUDED_OptionalStream_h_GUID_2C133DE4_22D9_4195_B36E_0663DF0A6378
27 
28 // Internal Includes
29 // - none
30 
31 // Library/third-party includes
32 // - none
33 
34 // Standard includes
35 #include <iostream>
36 
37 namespace osvr {
38 namespace vbtracker {
39  namespace detail {
40 
42  public:
45 
47  explicit OptionalStream(std::ostream &os) : m_os(&os) {}
48 
49  OptionalStream(OptionalStream const &) = delete;
50 
51  OptionalStream operator=(OptionalStream const &) = delete;
52 
53  OptionalStream(OptionalStream &&other) : m_os(other.m_os) {
55  other.m_os = nullptr;
56  }
57 
60  if (m_os) {
61  (*m_os) << std::endl;
62  }
63  }
64 
68  template <typename T> OptionalStream &operator<<(T &&rhs) {
69  if (m_os) {
70  // checking first to save a forward in no-op cases, which
71  // are expected to be most common.
72  (*m_os) << std::forward<T>(rhs);
73  }
74  return *this;
75  }
77  template <typename T> OptionalStream &operator<<(T const &rhs) {
78  if (m_os) {
79  // checking first to save a forward in no-op cases, which
80  // are expected to be most common.
81  (*m_os) << rhs;
82  }
83  return *this;
84  }
85 
86  private:
87  std::ostream *m_os = nullptr;
88  };
89  } // namespace detail
90  inline detail::OptionalStream outputIf(std::ostream &os, bool condition) {
91  return condition ? detail::OptionalStream{os}
93  }
94  inline detail::OptionalStream outputUnless(std::ostream &os,
95  bool condition) {
96  return !condition ? detail::OptionalStream{os}
98  }
99 } // namespace vbtracker
100 } // namespace osvr
101 #endif // INCLUDED_OptionalStream_h_GUID_2C133DE4_22D9_4195_B36E_0663DF0A6378
OptionalStream & operator<<(T &&rhs)
Stream me anything! (I might do nothing with it, but...)
Definition: OptionalStream.h:68
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
OptionalStream(std::ostream &os)
Construct with a stream for use.
Definition: OptionalStream.h:47
Definition: newuoa.h:1888
Definition: OptionalStream.h:41
OptionalStream & operator<<(T const &rhs)
Stream me anything! (I might do nothing with it, but...)
Definition: OptionalStream.h:77
OptionalStream()
Nothing to stream to.
Definition: OptionalStream.h:44
OptionalStream(OptionalStream &&other)
Definition: OptionalStream.h:53
~OptionalStream()
Definition: OptionalStream.h:58