suanPan
sync_ostream.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright (C) 2017-2024 Theodore Chang
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  ******************************************************************************/
17 
18 #ifndef SYNC_OSTREAM_H
19 #define SYNC_OSTREAM_H
20 
21 #include <mutex>
22 #include <ostream>
23 
24 class sync_ostream {
25  std::unique_lock<std::mutex> print_lock;
26  std::ostream* ostream;
27 
28 public:
29  explicit sync_ostream(std::ostream&);
30  sync_ostream(const sync_ostream&) = delete;
31  sync_ostream(sync_ostream&&) noexcept;
32  sync_ostream& operator=(const sync_ostream&) = delete;
33  sync_ostream& operator=(sync_ostream&&) noexcept;
34  ~sync_ostream() = default;
35 
36  template<typename T> sync_ostream& operator<<(T&& item) {
37  *ostream << std::forward<T>(item);
38  return *this;
39  }
40 };
41 
42 #endif // SYNC_OSTREAM_H
Definition: sync_ostream.h:24
sync_ostream(std::ostream &)
Definition: sync_ostream.cpp:25
sync_ostream(const sync_ostream &)=delete