suanPan
sync_ostream.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright (C) 2017-2023 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 
23 class sync_ostream {
24  std::unique_lock<std::mutex> print_lock;
25  std::ostream* ostream;
26 
27 public:
28  explicit sync_ostream(std::ostream&);
29  sync_ostream(const sync_ostream&) = delete;
30  sync_ostream(sync_ostream&&) noexcept;
31  sync_ostream& operator=(const sync_ostream&) = delete;
32  sync_ostream& operator=(sync_ostream&&) noexcept;
33  ~sync_ostream() = default;
34 
35  template<typename T> sync_ostream& operator<<(T&& item) {
36  *ostream << std::forward<T>(item);
37  return *this;
38  }
39 };
40 
41 #endif // SYNC_OSTREAM_H
Definition: sync_ostream.h:23
sync_ostream(std::ostream &)
Definition: sync_ostream.cpp:25
sync_ostream(const sync_ostream &)=delete