staticlib_io  1.2.11
 All Classes Namespaces Files Functions Variables
flushable_sink.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015, alex at staticlibs.net
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * File: flushable_sink.hpp
19  * Author: alex
20  *
21  * Created on October 13, 2015, 7:53 AM
22  */
23 
24 #ifndef STATICLIB_IO_FLUSHABLE_SINK_HPP
25 #define STATICLIB_IO_FLUSHABLE_SINK_HPP
26 
27 #include <ios>
28 
29 #include "staticlib/config.hpp"
30 
32 #include "staticlib/io/span.hpp"
33 
34 namespace staticlib {
35 namespace io {
36 
40 template<typename NonFlushableSink>
45  NonFlushableSink sink;
46 
47 public:
54  explicit flushable_sink(NonFlushableSink&& sink) :
55  sink(std::move(sink)) { }
56 
62  flushable_sink(const flushable_sink&) = delete;
63 
70  flushable_sink& operator=(const flushable_sink&) = delete;
71 
77  flushable_sink(flushable_sink&& other) STATICLIB_NOEXCEPT :
78  sink(std::move(other.sink)) { }
79 
86  flushable_sink& operator=(flushable_sink&& other) STATICLIB_NOEXCEPT {
87  sink = std::move(other.sink);
88  return *this;
89  }
90 
97  std::streamsize write(span<const char> span) {
98  return sink.write(span);
99  }
100 
106  std::streamsize flush() {
107  return 0;
108  }
109 
115  NonFlushableSink& get_sink() {
116  return sink;
117  }
118 
119 };
120 
128 template <typename NonFlushableSink,
129  class = typename std::enable_if<!std::is_lvalue_reference<NonFlushableSink>::value>::type>
131  return flushable_sink<NonFlushableSink>(std::move(sink));
132 }
133 
141 template <typename NonFlushableSink>
144 }
145 
146 } // namespace
147 }
148 
149 #endif /* STATICLIB_IO_FLUSHABLE_SINK_HPP */
150