-
Notifications
You must be signed in to change notification settings - Fork 767
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move RedirectCout to base/utilities.h
- Loading branch information
1 parent
5406393
commit 86c47d5
Showing
2 changed files
with
29 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#pragma once | ||
|
||
namespace gtsam { | ||
/** | ||
* For Python __str__(). | ||
* Redirect std cout to a string stream so we can return a string representation | ||
* of an object when it prints to cout. | ||
* https://stackoverflow.com/questions/5419356/redirect-stdout-stderr-to-a-string | ||
*/ | ||
struct RedirectCout { | ||
/// constructor -- redirect stdout buffer to a stringstream buffer | ||
RedirectCout() : ssBuffer_(), coutBuffer_(std::cout.rdbuf(ssBuffer_.rdbuf())) {} | ||
|
||
/// return the string | ||
std::string str() const { | ||
return ssBuffer_.str(); | ||
} | ||
|
||
/// destructor -- redirect stdout buffer to its original buffer | ||
~RedirectCout() { | ||
std::cout.rdbuf(coutBuffer_); | ||
} | ||
|
||
private: | ||
std::stringstream ssBuffer_; | ||
std::streambuf* coutBuffer_; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters