osmanip
Library with useful output stream tools like: color and style manipulators, progress bars and terminal graphics.
sstream.hpp
Go to the documentation of this file.
1 //====================================================
2 // File data
3 //====================================================
12 //====================================================
13 // Preprocessor settings
14 //====================================================
15 #pragma once
16 #ifndef OSMANIP_UTILITY_SSTREAM_HPP
17 #define OSMANIP_UTILITY_SSTREAM_HPP
18 
19 //====================================================
20 // Headers
21 //====================================================
22 
23 // STD headers
24 #include <stdint.h>
25 
26 #include <mutex>
27 #include <sstream>
28 
29 namespace osm {
30 
31  //====================================================
32  // Stringbuf
33  //====================================================
40  class Stringbuf : public std::stringbuf {
41  public:
42 
43  // Constructors and destructor
45  ~Stringbuf() override;
46 
47  // Getters
48  std::mutex &getMutex();
49 
50  // Methods
51  int32_t sync() override;
52 
53  private:
54 
55  // Attributes
56  std::mutex mutex_;
57  };
58 
59  //====================================================
60  // Ostreambuf
61  //====================================================
69  class Ostreambuf : public Stringbuf {
70  public:
71 
72  // Constructors and destructor
73  Ostreambuf();
74  explicit Ostreambuf(std::ostream *ostream);
75  ~Ostreambuf() override;
76 
77  // Setters
78  void setOstream(std::ostream *ostream);
79 
80  // Getters
81  std::ostream *getOstream();
82 
83  // Methods
84  int32_t sync() override;
85 
86  private:
87 
88  // Methods
89  void sync_output();
90  void sync_redirection();
91 
92  // Members
93  std::ostream *ostream_;
94  };
95 
96 } // namespace osm
97 
98 #endif // OSMANIP_MAIN_INCLUDE_UTILITY_SSTREAM_HPP
This class inherits Stringbuf and adds the ability to send output to a specific std::ostream buffer a...
Definition: sstream.hpp:69
int32_t sync() override
Synchronizes the buffer with the specified object.
Definition: sstream.cpp:154
Ostreambuf()
Construct a new Ostreambuf object. Default constructor will set the main attributes to default values...
Definition: sstream.cpp:85
void setOstream(std::ostream *ostream)
Sets the std::ostream* object to route output. If there is already an std::ostream* present,...
Definition: sstream.cpp:118
~Ostreambuf() override
Destructs OutputRedirector object. Calls this->pubsync() before being destroyed.
Definition: sstream.cpp:101
std::ostream * getOstream()
Returns the current std::ostream* object.
Definition: sstream.cpp:138
This class inherits std::stringbuf and adds additional functionality and thread safety.
Definition: sstream.hpp:40
std::mutex & getMutex()
Get the mutex for the object.
Definition: sstream.cpp:61
~Stringbuf() override
Destructs OutputRedirector object. Calls this->pubsync() before being destroyed.
Definition: sstream.cpp:45
Stringbuf()
Construct a new Stringbuf object. Default constructor will set the main attributes to default values.
int32_t sync() override
Calls the base class sync() function.
Definition: sstream.cpp:74
Definition: canvas.cpp:30