staticlib_io  1.2.11
 All Classes Namespaces Files Functions Variables
unique_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: unique_sink.hpp
19  * Author: alex
20  *
21  * Created on October 9, 2015, 1:02 PM
22  */
23 
24 #ifndef STATICLIB_IO_UNIQUE_SINK_HPP
25 #define STATICLIB_IO_UNIQUE_SINK_HPP
26 
27 #include <ios>
28 #include <memory>
29 
30 #include "staticlib/config.hpp"
31 
32 #include "staticlib/io/span.hpp"
33 
34 namespace staticlib {
35 namespace io {
36 
40 template <typename Sink>
41 class unique_sink {
45  std::unique_ptr<Sink> sink;
46 
47 public:
53  explicit unique_sink(Sink* sink) :
54  sink(sink) { }
55 
61  unique_sink(const unique_sink&) = delete;
62 
69  unique_sink& operator=(const unique_sink&) = delete;
70 
76  unique_sink(unique_sink&& other) STATICLIB_NOEXCEPT :
77  sink(std::move(other.sink)) { }
78 
85  unique_sink& operator=(unique_sink&& other) STATICLIB_NOEXCEPT {
86  sink = std::move(other.sink);
87  return *this;
88  }
89 
96  std::streamsize write(span<const char> span) {
97  return sink->write(span);
98  }
99 
105  std::streamsize flush() {
106  return sink->flush();
107  }
108 
114  Sink& get_sink() {
115  return *sink;
116  }
117 
118 };
119 
126 template <typename Sink>
128  return unique_sink<Sink>(sink);
129 }
130 
131 } // namespace
132 }
133 
134 #endif /* STATICLIB_IO_UNIQUE_SINK_HPP */
135