Skip to content

Commit

Permalink
move RedirectCout to base/utilities.h
Browse files Browse the repository at this point in the history
  • Loading branch information
varunagrawal committed Jul 11, 2021
1 parent 5406393 commit 86c47d5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
29 changes: 29 additions & 0 deletions gtsam/base/utilities.h
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_;
};

}
25 changes: 0 additions & 25 deletions gtsam/nonlinear/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,30 +260,5 @@ Values localToWorld(const Values& local, const Pose2& base,

} // namespace utilities

/**
* 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_;
};

}

0 comments on commit 86c47d5

Please sign in to comment.